Updated on 2024-09-29 GMT+08:00

Step 1: Deploying MySQL

WordPress must be used together with MySQL. WordPress runs the content management program while MySQL serves as a database to store data.

Prerequisites

You have created a CCE cluster that contains a node with 4 vCPUs and 8 GiB memory. For details on how to create a cluster, see Creating a Kubernetes Cluster.

Operations on the Console

  1. Log in to the CCE console.
  2. Click the name of the target cluster to access the cluster console.
  3. In the navigation pane, choose Workloads. Then, click Create Workload in the upper right corner.
  4. Configure the basic information about the workload.

    • Workload Type: Select StatefulSet.
    • Workload Name: Set it to mysql.
    • Namespace: Select default.
    • Pods: In this example, change the quantity to 1, which means, there is only one pod running in the mysql workload.
    Figure 1 Creating a MySQL workload

  5. Configure the basic information about the container.

    In the Container Settings area, click Basic Info and click Select Image next to Image Name. In the dialog box displayed, select Open Source Images, search for mysql, select the mysql image, and select 5.7 from the drop-down list for Image Tag.

    Figure 2 Selecting an image tag

  6. Click Environment Variables and add four environment variables. You can check MySQL to view the environment variables that can be configured.

    • MYSQL_ROOT_PASSWORD: password of the root user of the MySQL database, which can be customized.
    • MYSQL_DATABASE: name of the database to be created when the image is started, which can be customized.
    • MYSQL_USER: database user name, which can be customized.
    • MYSQL_PASSWORD: database user password, which can be customized.
    Figure 3 Configuring environment variables

  7. Click Lifecycle and configure Startup Command, as shown in Figure 4.

    • Command:
      /bin/bash
    • Running parameters:
      -c
      rm -rf /var/lib/mysql/lost+found;docker-entrypoint.sh mysqld;
    Figure 4 Configuring startup commands

  8. Click Data Storage, click Add Volume, select VolumeClaimTemplate (VTC) from the drop-down list, and add an EVS disk for MySQL.

    Click Create PVC and configure the following parameters (Keep default for other parameters):

    • PVC Type: Select EVS.
    • PVC Name: Enter a name, for example, mysql.
    • Creation Mode: Only Dynamically provision is supported.
    • Storage Classes: The default value is csi-disk.
    • AZ: Select an AZ. The EVS disk can only be attached to nodes in the same AZ. After an EVS disk is created, the AZ where the disk locates cannot be changed.
    • Disk Type: Select a proper type as required.
    • Capacity (GiB): Enter the capacity as required. The default value is 10 GiB.

    Click Create and enter the path for mounting the storage volume to the container. The default path used by MySQL is /var/lib/mysql.

    Figure 5 Mounting a storage volume for MySQL

  9. In the Headless Service Parameters area, configure a headless Service.

    A headless Service needs to be configured for the StatefulSet networking. The headless Service generates DNS name for each pod for accessing a specific StatefulSet pod. For a replicated MySQL database, the headless Service needs to be used to read and write the MySQL primary server, and copies existing data from other running replicas. In this example, there is only one pod running in the MySQL workload. Therefore, the headless Service is not used. In this case, enter 3306 for both the Service port and container port. For details about the replicated MySQL examples, see Run a Replicated Stateful Application.
    Figure 6 Headless Service

  10. In the Service Settings area, click the plus sign (+) and create a Service for accessing MySQL from WordPress.

    Select ClusterIP for Service Type, enter mysqlin the Service Name text box, set both the Container Port and Service Port to 3306, and click OK.

    The default access port in the MySQL image is 3306. In this example, both the container port and Service port are set to 3306 for convenience. The access port can be changed to another port.

    In this way, the MySQL workload can be accessed through {Service name}:{Access port} (for example, mysql:3306) from within the cluster.

    Figure 7 Creating a Service

  11. Click Create Workload.

    Wait until the workload is created.

    The created workload will be displayed on the StatefulSets tab.

    Figure 8 Workload created successfully

Operations Through kubectl

This section describes how to use kubectl to create a StatefulSet and expose the StatefulSet through a ClusterIP Service to allow access from within the cluster.

  1. Use kubectl to connect to the cluster. For details, see Connecting to a Cluster Using kubectl.
  2. Create a description file named mysql.yaml. mysql.yaml is an example file name. You can rename it as required.

    vi mysql.yaml

    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: mysql
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: mysql
          version: v1
      template:
        metadata:
          labels:
            app: mysql
            version: v1
        spec:
          containers:
            - name: container-1
              image: mysql:5.7
              command:
                - /bin/bash
              args:
                - '-c'
                - rm -rf /var/lib/mysql/lost+found;docker-entrypoint.sh mysqld;
              env:
                - name: MYSQL_ROOT_PASSWORD  # Password of the root user of MySQL, which can be customized
                  value: password@123
                - name: MYSQL_DATABASE       # Name of the database to be created when the image is started, which can be customized
                  value: database
                - name: MYSQL_USER           # Database user name, which can be customized
                  value: db_user
                - name: MYSQL_PASSWORD       # Database user password, which can be customized
                  value: password@123
              resources:
                requests:
                  cpu: 250m
                  memory: 512Mi
                limits:
                  cpu: 250m
                  memory: 512Mi
              volumeMounts:
                - name: mysql
                  mountPath: /var/lib/mysql
          imagePullSecrets:
            - name: default-secret
      serviceName: headless-mysql
      volumeClaimTemplates:  #Dynamically attach the EVS disk to the workload.
        - apiVersion: v1
          kind: PersistentVolumeClaim
          metadata:
            name: mysql
            namespace: default
            annotations:
              everest.io/disk-volume-type: SAS  # EVS disk type
            labels:
              failure-domain.beta.kubernetes.io/region: ap-southeast-1  #Region where the EVS disk is created
              failure-domain.beta.kubernetes.io/zone:    #AZ where the EVS disk is created. It must be the same as the AZ of the node.
          spec:
            accessModes:
              - ReadWriteOnce  # The EVS disk must be ReadWriteOnce.
            resources:
              requests:
                storage: 10Gi
            storageClassName: csi-disk  # Storage class name. The value is csi-disk for an EVS disk.
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: headless-mysql
      namespace: default
      labels:
        app: mysql
        version: v1
    spec:
      selector:
        app: mysql
        version: v1
      clusterIP: None
      ports:
        - name: mysql
          protocol: TCP
          port: 3306
          targetPort: 3306
      type: ClusterIP

  3. Create a MySQL workload.

    kubectl apply -f mysql.yaml

    If the following information is displayed, the StatefulSet is being created.

    statefulset "mysql" created

    View the workload.

    kubectl get statefulset

    If the following information is displayed, the StatefulSet is running.

    NAME           READY     UP-TO-DATE   AVAILABLE   AGE 
    mysql          1/1       1            1           4m5s

  4. Create a description file named mysql-service.yaml. mysql-service.yaml is an example file name. You can rename it as required.

    vi mysql-service.yaml

    apiVersion: v1
    kind: Service
    metadata:
      name: mysql
      namespace: default
    spec:
      selector:
        app: mysql
        version: v1
      ports:
        - name: cce-service-0
          targetPort: 3306
          nodePort: 0
          port: 3306
          protocol: TCP
      type: ClusterIP

  5. Create a Service.

    kubectl create -f mysql-service.yaml

    If information similar to the following is displayed, the Service has been created.

    service/mysql created

    kubectl get svc

    If information similar to the following is displayed, the access type has been configured, and the workload is accessible.

    NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
    kubernetes   ClusterIP      10.247.0.1       <none>        443/TCP          3d
    mysql        ClusterIP    10.247.202.20     <none>        3306/TCP        51s