This topic describes how to deploy a WordPress site with a MySQL database on Kubernetes.

Prerequisites

Background information

This example shows how to customize orchestration templates to set up a WordPress site with a MySQL database.

The main components are:

  • WordPress
  • MySQL

The required resources are:

  • PVs
  • Secrets
  • Services

Procedure

  1. Log on to the Container Service console.
  2. In the wordpress and wordpress-mysql YAML file, define a PVC named wordpress-pvc and a PVC named wordpress-mysql-pvc respectively and bind the PVCs to corresponding PVs.
  3. In the left-side navigation pane, choose Configuration > Secrets, select the target cluster and namespace, and click Create in the upper-right corner to create a secret. For more information, see Create a Secret.

    The secret is used to manage the usernames and passwords that are required to create and access the MySQL database.

    In this example, a secret named mysql-pass is created to manage the MySQL root password. The secret type is set to Opaque. The secret is needed in the wordpress and wordpress-mysql YAML files.

  4. In the left-side navigation pane, choose Applications > Deployments. In the upper-right corner, click Create from Template.

    Select the target cluster and namespace. The complete wordpress YAML file is as follows:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: wordpress
      labels:
        app: wordpress
    spec:
      selector:
        matchLabels:
          app: wordpress
          tier: frontend
      strategy:
        type: Recreate
      template:
        metadata:
          labels:
            app: wordpress
            tier: frontend
        spec:
          containers:
          - image: wordpress:4
            name: wordpress
            env:
            - name: WORDPRESS_DB_HOST
              value: wordpress-mysql  # specify the MySQL database that is accessed by this WordPress service. The value must be the same as the MySQL service name.
            - name: WORDPRESS_DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: password-mysql
            ports:
            - containerPort: 80
              name: wordpress
            volumeMounts:
            - name: wordpress-pvc
              mountPath: /var/www/html
          volumes:
          - name: wordpress-pvc
            persistentVolumeClaim:
              claimName: wordpress-pv-claim
    The complete wordpress-mysql YAML file is as follows:
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: wordpress-mysql
      labels:
        app: wordpress
    spec:
      selector:
        matchLabels:
          app: wordpress
          tier: mysql
      strategy:
        type: Recreate
      template:
        metadata:
          labels:
            app: wordpress
            tier: mysql
        spec:
          containers:
          - image: mysql:5.6
            name: mysql
            env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: password-mysql
            ports:
            - containerPort: 3306
              name: mysql
            volumeMounts:
            - name: wordpress-mysql-pvc
              mountPath: /var/lib/mysql
          volumes:
          - name: wordpress-mysql-pvc
            persistentVolumeClaim:
              claimName: wordpress-mysql-pv-claim
  5. To enable external access to WordPress, create a LoadBalancer service. Container Service will automatically create an SLB instance to manage external access.

    To enable WordPress to access the MySQL database internally, create a service of the Cluster IP type. The MySQL database does not require external access, which makes a LoadBalancer service unnecessary.

    For more information about services, see Create a service.

    The YAML files that define services for WordPress and MySQL are as follows:
    apiVersion: v1
    kind: Service
    metadata:
      name: wordpress
      labels:
        app: wordpress
    spec:
      ports:
        - port: 80
      selector:
        app: wordpress
        tier: frontend
      type: LoadBalancer
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: wordpress-mysql
      labels:
        app: wordpress
    spec:
      ports:
        - port: 3306
      selector:
        app: wordpress
        tier: mysql
      clusterIP: None
  6. After the services are deployed, choose Ingresses and Load Balancing > Services in the left-side navigation pane. Find the WordPress service and its external endpoint.
  7. Enter XX.XX.XX.XX/wp-admin/install.php in a browser to access WordPress.
    Replace XX.XX.XX.XX with the external endpoint from the preceding step, which is the IP address of the associated SLB instance.

What to do next

When you configure WordPress, you can use the credential in the previously created secret to log on to WordPress. Data generated in the container that runs WordPress is stored in the corresponding PV.