Deploy WordPress and MySQL with Portworx
You can deploy WordPress, an open-source content management system, with Portworx on Kubernetes. Portworx enables reliable and persistent storage to ensure WordPress runs with HA and has Shared4 volumes for file uploads. You can horizontally scale the WordPress container. The cluster automatically repairs itself in the event of a node failure.
The following collection of tasks describe how to deploy Wordpress and MySQL with Portworx:
- Dynamically provision a volume for MySQL
- Dynamically provision a volume for WordPress
- Create a Kubernetes secret for storing your MySQL password
- Deploy MySQL
- Deploy WordPress
- Validate the cluster functionality
Dynamically provision a volume for MySQL
-
Create a file called
mysql-sc.yaml, specifying the following fields and values:- apiVersion: as
storage.k8s.io/v1 - kind: as
StorageClass - metadata.name: with the name of your
StorageClassobject (this example usesmysql-sc) - provisioner: as
pxd.portworx.com. For details about the Portworx-specific parameters, refer to the Portworx Volume section of the Kubernetes website. - parameters.repl: with the number of replicas Portworx should create (this example creates two replicas)
- parameters.priority_io: with the type of the storage pool (this example uses a high-priority storage pool)
apiVersion: storage.k8s.io/v1kind: StorageClassmetadata:name: mysql-scprovisioner: pxd.portworx.comparameters:repl: "3"priority_io: "high"
For more information on how to configure a storage class, see StorageClass.
- apiVersion: as
-
Apply the spec:
kubectl apply -f mysql-sc.yaml -
Create a file called
mysql-pvc.yamlwith the following content:apiVersion: v1kind: PersistentVolumeClaimmetadata:name: mysql-pvcannotations:volume.beta.kubernetes.io/storage-class: mysql-scspec:accessModes:- ReadWriteOnceresources:requests:storage: 2Gi
This PVC references the mysql-sc storage class. As a result, Kubernetes will automatically create a new PVC for each replica.
-
Apply the spec:
kubectl apply -f mysql-pvc.yaml
Dynamically provision a volume for WordPress
- Create a file called
wordpress-sc.yamlwith the following content:apiVersion: storage.k8s.io/v1kind: StorageClassmetadata:name: wordpress-scprovisioner: pxd.portworx.comparameters:repl: "3"priority_io: "high"shared: "true"
The sharedv4: "true" flag creates a globally shared namespace volume which can be used by multiple Pods.
-
Apply the spec:
kubectl apply -f wordpress-sc.yaml -
Create a file called
wordpress-pvc.yamlwith the following content:apiVersion: v1kind: PersistentVolumeClaimmetadata:name: wp-pvclabels:app: wordpressannotations:volume.beta.kubernetes.io/storage-class: wordpress-scspec:accessModes:- ReadWriteManyresources:requests:storage: 1Gi
This PVC references the wordpress-sc storage class. As a result, Kubernetes will automatically create a new PVC for each replica.
-
Apply the spec:
kubectl apply -f wordpress-pvc.yaml
Create a Kubernetes secret for storing your MySQL password
A secret is an object that contains sensitive data. To create a secret, you can use the kubectl create secret command. Note that, to protect your sensitive data, the kubectl get and kubectl describe commands do not display the content of a secret.
-
Use the
echocommand to save your MySQL password to a file called./password.txt, replacing<YOUR-PASSWORD>with your actual password:echo -n '<YOUR-PASSWORD' > ./password.txt -
To create a new secret, enter the
kubectl create secretcommand, specifying:
-
The
genericparameter. This instructs Kubernetes to create a secret based on a file, directory, or specified literal value. -
The name of your secret (this example uses
mysql-pass) -
The
--from-fileflag with the name of the file in which you stored your passwordkubectl create secret generic mysql-pass --from-file=./password.txt
-
Use the
kubectl get secretscommand to verify that Kubernetes created the secret:kubectl get secrets
Deploy MySQL
-
Create a file named
mysql-service.yamlwith the following content:apiVersion: v1kind: Servicemetadata:name: wordpress-mysqllabels:app: wordpressspec:ports:- port: 3306selector:app: wordpresstier: mysqlclusterIP: None -
Apply the spec:
kubectl apply -f mysql-service.yaml -
Create a file named
mysql-deployment.yamlwith the following content:apiVersion: apps/v1kind: Deploymentmetadata:name: wordpress-mysqllabels:app: wordpressspec:selector:matchLabels:app: wordpressstrategy:type: Recreatetemplate:metadata:labels:app: wordpresstier: mysqlspec:# Use the Stork scheduler to enable more efficient placement of the podsschedulerName: storkcontainers:- image: mysql:5.6imagePullPolicy:name: mysqlenv:# $ kubectl create secret generic mysql-pass --from-file=password.txt# make sure password.txt does not have a trailing newline- name: MYSQL_ROOT_PASSWORDvalueFrom:secretKeyRef:name: mysql-passkey: password.txtports:- containerPort: 3306name: mysqlvolumeMounts:- name: mysql-persistent-storagemountPath: /var/lib/mysqlvolumes:- name: mysql-persistent-storagepersistentVolumeClaim:claimName: mysql-pvcNote the following about this
Deployment:- Kubernetes creates a single-instance MySQL database
- Kubernetes creates an environment variable called
MYSQL_ROOT_PASSWORDthat contains your MySQL password - Portworx mounts the Portworx persistent volume in the
/var/lib/mysqldirectory - The Stork scheduler will place your Pods closer to where their data is located
-
Apply the spec:
kubectl apply -f mysql-deployment.yaml
Deploy WordPress
-
Create a file called
wordpress-service.yamlwith the following content:apiVersion: v1kind: Servicemetadata:name: wordpresslabels:app: wordpressspec:ports:- port: 80nodePort: 30303selector:app: wordpresstier: frontendtype: NodePortNote that the
spec.typefield is set toNodePort. Kubernetes exposes the service on each node and makes it accessible from outside the cluster. For more details, see the Type NodePort section of the Kubernetes documentation. -
Apply the spec:
kubectl apply -f wordpress-service.yaml -
Create a file called
wordpress-deployment.yamlwith the following content:apiVersion: apps/v1kind: Deploymentmetadata:name: wordpresslabels:app: wordpressspec:selector:matchLabels:app: wordpressreplicas: 3strategy:type: Recreatetemplate:metadata:labels:app: wordpresstier: frontendspec:# Use the Stork scheduler to enable more efficient placement of the podsschedulerName: storkcontainers:- image: wordpress:4.8-apachename: wordpressimagePullPolicy:env:- name: WORDPRESS_DB_HOSTvalue: wordpress-mysql- name: WORDPRESS_DB_PASSWORDvalueFrom:secretKeyRef:name: mysql-passkey: password.txtports:- containerPort: 80name: wordpressvolumeMounts:- name: wordpress-persistent-storagemountPath: /var/www/htmlvolumes:- name: wordpress-persistent-storagepersistentVolumeClaim:claimName: wp-pvcNote the following about this
Deployment- Portworx will create three replicas of each volume
- The Stork scheduler will place your Pods closer to where their data is located
- Kubernetes will create an environment variable called
WORDPRESS_DB_PASSWORDthat contains your MySQL password
-
Apply the spec:
kubectl apply -f wordpress-deployment.yaml
Validate the cluster functionality
-
List your Pods:
kubectl get pods -
Display your services:
kubectl get services
Clean up
-
Enter the following command to delete the Kubernetes secret:
kubectl delete secret mysql-pass -
Use the
kubectl deleteto delete your WordPress deployment and PVC:kubectl delete -f wordpress-deployment.yamlkubectl delete -f wordpress-pvc.yaml -
Use the
kubectl deleteto delete your MySQL deployment and PVC:kubectl delete -f mysql-deployment.yamlkubectl delete -f mysql-pvc.yaml