Example: Deploying WordPress and MySQL with Persistent Volumes

This tutorial shows you how to deploy a WordPress site and a MySQL database using Minikube. Both applications use PersistentVolumes and PersistentVolumeClaims to store data.

A PersistentVolume (PV) is a piece of storage in the cluster that has been manually provisioned by an administrator, or dynamically provisioned by Kubernetes using a StorageClass. A PersistentVolumeClaim (PVC) is a request for storage by a user that can be fulfilled by a PV. PersistentVolumes and PersistentVolumeClaims are independent from Pod lifecycles and preserve data through restarting, rescheduling, and even deleting Pods.

Warning: This deployment is not suitable for production use cases, as it uses single instance WordPress and MySQL Pods. Consider using WordPress Helm Chart to deploy WordPress in production.

Note: The files provided in this tutorial are using GA Deployment APIs and are specific to kubernetes version 1.9 and later. If you wish to use this tutorial with an earlier version of Kubernetes, please update the API version appropriately, or reference earlier versions of this tutorial.

Objectives

  • Create PersistentVolumeClaims and PersistentVolumes
  • Create a kustomization.yaml with
    • a Secret generator
    • MySQL resource configs
    • WordPress resource configs
  • Apply the kustomization directory by kubectl apply -k ./
  • Clean up

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version. The example shown on this page works with kubectl 1.14 and above.

Download the following configuration files:

  1. mysql-deployment.yaml

  2. wordpress-deployment.yaml

Create PersistentVolumeClaims and PersistentVolumes

MySQL and Wordpress each require a PersistentVolume to store data. Their PersistentVolumeClaims will be created at the deployment step.

Many cluster environments have a default StorageClass installed. When a StorageClass is not specified in the PersistentVolumeClaim, the cluster’s default StorageClass is used instead.

When a PersistentVolumeClaim is created, a PersistentVolume is dynamically provisioned based on the StorageClass configuration.

Warning: In local clusters, the default StorageClass uses the hostPath provisioner. hostPath volumes are only suitable for development and testing. With hostPath volumes, your data lives in /tmp on the node the Pod is scheduled onto and does not move between nodes. If a Pod dies and gets scheduled to another node in the cluster, or the node is rebooted, the data is lost.

Note: If you are bringing up a cluster that needs to use the hostPath provisioner, the --enable-hostpath-provisioner flag must be set in the controller-manager component.

Note: If you have a Kubernetes cluster running on Google Kubernetes Engine, please follow this guide.

Create a kustomization.yaml

Add a Secret generator

A Secret is an object that stores a piece of sensitive data like a password or key. Since 1.14, kubectl supports the management of Kubernetes objects using a kustomization file. You can create a Secret by generators in kustomization.yaml.

Add a Secret generator in kustomization.yaml from the following command. You will need to replace YOUR_PASSWORD with the password you want to use.

  1. cat <<EOF >./kustomization.yaml
  2. secretGenerator:
  3. - name: mysql-pass
  4. literals:
  5. - password=YOUR_PASSWORD
  6. EOF

Add resource configs for MySQL and WordPress

The following manifest describes a single-instance MySQL Deployment. The MySQL container mounts the PersistentVolume at /var/lib/mysql. The MYSQL_ROOT_PASSWORD environment variable sets the database password from the Secret.

application/wordpress/mysql-deployment.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: wordpress-mysql
  5. labels:
  6. app: wordpress
  7. spec:
  8. ports:
  9. - port: 3306
  10. selector:
  11. app: wordpress
  12. tier: mysql
  13. clusterIP: None
  14. ---
  15. apiVersion: v1
  16. kind: PersistentVolumeClaim
  17. metadata:
  18. name: mysql-pv-claim
  19. labels:
  20. app: wordpress
  21. spec:
  22. accessModes:
  23. - ReadWriteOnce
  24. resources:
  25. requests:
  26. storage: 20Gi
  27. ---
  28. apiVersion: apps/v1
  29. kind: Deployment
  30. metadata:
  31. name: wordpress-mysql
  32. labels:
  33. app: wordpress
  34. spec:
  35. selector:
  36. matchLabels:
  37. app: wordpress
  38. tier: mysql
  39. strategy:
  40. type: Recreate
  41. template:
  42. metadata:
  43. labels:
  44. app: wordpress
  45. tier: mysql
  46. spec:
  47. containers:
  48. - image: mysql:5.6
  49. name: mysql
  50. env:
  51. - name: MYSQL_ROOT_PASSWORD
  52. valueFrom:
  53. secretKeyRef:
  54. name: mysql-pass
  55. key: password
  56. ports:
  57. - containerPort: 3306
  58. name: mysql
  59. volumeMounts:
  60. - name: mysql-persistent-storage
  61. mountPath: /var/lib/mysql
  62. volumes:
  63. - name: mysql-persistent-storage
  64. persistentVolumeClaim:
  65. claimName: mysql-pv-claim

The following manifest describes a single-instance WordPress Deployment. The WordPress container mounts the PersistentVolume at /var/www/html for website data files. The WORDPRESS_DB_HOST environment variable sets the name of the MySQL Service defined above, and WordPress will access the database by Service. The WORDPRESS_DB_PASSWORD environment variable sets the database password from the Secret kustomize generated.

application/wordpress/wordpress-deployment.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: wordpress
  5. labels:
  6. app: wordpress
  7. spec:
  8. ports:
  9. - port: 80
  10. selector:
  11. app: wordpress
  12. tier: frontend
  13. type: LoadBalancer
  14. ---
  15. apiVersion: v1
  16. kind: PersistentVolumeClaim
  17. metadata:
  18. name: wp-pv-claim
  19. labels:
  20. app: wordpress
  21. spec:
  22. accessModes:
  23. - ReadWriteOnce
  24. resources:
  25. requests:
  26. storage: 20Gi
  27. ---
  28. apiVersion: apps/v1
  29. kind: Deployment
  30. metadata:
  31. name: wordpress
  32. labels:
  33. app: wordpress
  34. spec:
  35. selector:
  36. matchLabels:
  37. app: wordpress
  38. tier: frontend
  39. strategy:
  40. type: Recreate
  41. template:
  42. metadata:
  43. labels:
  44. app: wordpress
  45. tier: frontend
  46. spec:
  47. containers:
  48. - image: wordpress:4.8-apache
  49. name: wordpress
  50. env:
  51. - name: WORDPRESS_DB_HOST
  52. value: wordpress-mysql
  53. - name: WORDPRESS_DB_PASSWORD
  54. valueFrom:
  55. secretKeyRef:
  56. name: mysql-pass
  57. key: password
  58. ports:
  59. - containerPort: 80
  60. name: wordpress
  61. volumeMounts:
  62. - name: wordpress-persistent-storage
  63. mountPath: /var/www/html
  64. volumes:
  65. - name: wordpress-persistent-storage
  66. persistentVolumeClaim:
  67. claimName: wp-pv-claim
  1. Download the MySQL deployment configuration file.

    1. curl -LO https://k8s.io/examples/application/wordpress/mysql-deployment.yaml
  2. Download the WordPress configuration file.

    1. curl -LO https://k8s.io/examples/application/wordpress/wordpress-deployment.yaml
  3. Add them to kustomization.yaml file.

  1. cat <<EOF >>./kustomization.yaml
  2. resources:
  3. - mysql-deployment.yaml
  4. - wordpress-deployment.yaml
  5. EOF

Apply and Verify

The kustomization.yaml contains all the resources for deploying a WordPress site and a MySQL database. You can apply the directory by

  1. kubectl apply -k ./

Now you can verify that all objects exist.

  1. Verify that the Secret exists by running the following command:

    1. kubectl get secrets

    The response should be like this:

    1. NAME TYPE DATA AGE
    2. mysql-pass-c57bb4t7mf Opaque 1 9s
  2. Verify that a PersistentVolume got dynamically provisioned.

    1. kubectl get pvc

    Note: It can take up to a few minutes for the PVs to be provisioned and bound.

    The response should be like this:

    1. NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
    2. mysql-pv-claim Bound pvc-8cbd7b2e-4044-11e9-b2bb-42010a800002 20Gi RWO standard 77s
    3. wp-pv-claim Bound pvc-8cd0df54-4044-11e9-b2bb-42010a800002 20Gi RWO standard 77s
  3. Verify that the Pod is running by running the following command:

    1. kubectl get pods

    Note: It can take up to a few minutes for the Pod’s Status to be RUNNING.

    The response should be like this:

    1. NAME READY STATUS RESTARTS AGE
    2. wordpress-mysql-1894417608-x5dzt 1/1 Running 0 40s
  4. Verify that the Service is running by running the following command:

    1. kubectl get services wordpress

    The response should be like this:

    1. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    2. wordpress LoadBalancer 10.0.0.89 <pending> 80:32406/TCP 4m

    Note: Minikube can only expose Services through NodePort. The EXTERNAL-IP is always pending.

  5. Run the following command to get the IP Address for the WordPress Service:

    1. minikube service wordpress --url

    The response should be like this:

    1. http://1.2.3.4:32406
  6. Copy the IP address, and load the page in your browser to view your site.

    You should see the WordPress set up page similar to the following screenshot.

    wordpress-init

Warning: Do not leave your WordPress installation on this page. If another user finds it, they can set up a website on your instance and use it to serve malicious content.

Either install WordPress by creating a username and password or delete your instance.

Cleaning up

  1. Run the following command to delete your Secret, Deployments, Services and PersistentVolumeClaims:

    1. kubectl delete -k ./

What’s next