Updated on 2023-08-18 GMT+08:00

Creating a MySQL Workload

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. Choose the target cluster.
  3. In the navigation pane, choose Workloads. Then, click Create Workload.
  4. Set workload parameters.

    Basic Info
    • Workload Type: Select Deployment.
    • Workload Name: Set it to mysql.
    • Namespace: Select default.
    • Pods: Change the value to 1 in this example.
    Figure 1 Basic information about the MySQL workload

    Container Settings

    In the Basic Info area, click Select Image. In the dialog box displayed, select Open Source Images, search for mysql, select the mysql image, and set the image tag to 5.7.

    Figure 2 Selecting an image tag

    Add the following four environment variables (details available in MySQL):

    • MYSQL_ROOT_PASSWORD: password of the root user of MySQL.
    • MYSQL_DATABASE: name of the database created during image startup.
    • MYSQL_USER: name of the database user.
    • MYSQL_PASSWORD: password of the database user.
    Figure 3 Setting environment variables

    Service Settings

    Click the plus sign (+) to create a Service for accessing MySQL from WordPress.

    Select ClusterIP for Access Type, set Service Name to mysql, 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 (mysql:3306 in this example) from within the cluster.

    Figure 4 Creating a Service

  5. Click Create Workload.

    Wait until the workload is created.

    The created Deployment will be displayed on the Deployments tab.

    Figure 5 Workload created successfully

Operations Through kubectl

This section describes how to use kubectl to create a Deployment and expose the Deployment 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-deployment.yaml. mysql-deployment.yaml is an example file name. You can rename it as required.

    vi mysql-deployment.yaml

    apiVersion: apps/v1
    kind: Deployment
    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
              env:
                - name: MYSQL_ROOT_PASSWORD
                  value: password@123
                - name: MYSQL_DATABASE
                  value: database
                - name: MYSQL_USER
                  value: db_user
                - name: MYSQL_PASSWORD
                  value: password@123
              resources:
                requests:
                  cpu: 250m
                  memory: 512Mi
                limits:
                  cpu: 250m
                  memory: 512Mi
          imagePullSecrets:
            - name: default-secret

  3. Create a MySQL workload.

    kubectl apply -f mysql-deployment.yaml

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

    deployment "mysql" created

    Check the Deployment.

    kubectl get deployment

    If the following information is displayed, the Deployment 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