Setup WordPress and MySQL on Kubernetes Cluster
This setup will help us to understand how to configure a multi-tier web application on Kubernetes. In this example, we will launch a WordPress container and MySQL container. But first, you need the Kubernetes cluster ready with you, If you don’t have it then follow this article:
After having the Kubernetes cluster ready go to the control plan/master node for creating this structure. Now, just follow these simple steps:
Step 1: PVC and PV
Here, PV stands for Persistent Volume. PV is nothing but just a storage that comes from different locations like clouds or networks or the control plan itself. PVC stands for Persistent Volume Claim. PVC helps to claim the PV from the Kubernetes dynamically. If you wanted to know more about it then just go to this site:
We need PV for MySQL image for creating the database. First, a folder to share and then create a PV, here is a code for it.
mkdir /root/data
Create a file called persistent-volume.yaml with the following code, you can give any name to the file, it depends on you.
apiVersion: v1
kind: PersistentVolume
metadata:
name: wp-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/root/data"
kubectl apply -f persistent-volume.yaml
The above-given command will create a PV with wp-pv name and 1 GiB storage. For checking the details about run the following command:
kubectl get pv wp-pv
Now, let’s create PVC. Create the file called persistent-volume-claim.yaml and paste the following code.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wp-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Run the following command:
kubectl apply -f persistent-volume-claim.yaml
This PVC claims the storage from the PV that we created before in this article.
kubectl get pvc wp-pv-claim
Step 2: Create a MySQL deployment
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mysql
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: <password>
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: wp-pv-claim
Create a file with mysql-deployment.yaml name. In this deployment, I have used mysql:5.6 MySQL image. MySQL image has some environmental variables that we have to mention like MYSQL_ROOT_PASSWORD for the database password, also we have to create the service for it and attach PV with it.
kubectl apply -f mysql-deployment.yaml
Step 3: Create a WordPress deployment
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: frontend
template:
metadata:
labels:
app: wordpress
tier: frontend
spec:
containers:
- image: wordpress:4.8-apache
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql
- name: WORDPRESS_DB_PASSWORD
value: <wordpress_db_password>
ports:
- containerPort: 80
name: wordpress
Create a file called wordpress-deployment.yaml. This wordpress:4.8-apache image also has some env like WORDPRESS_DB_HOST and WORDPRESS_DB_PASSWORD.
kubectl apply -f wordpress-deployment.yaml
But we can’t access it directly, we need a service that can expose it to the public world, so run this command to expose this deployment:
kubectl expose deployment Wordpress --port=80 --type=NodePort
kubectl get svc wordpress
Now, we can access the site with http://[public-ip-master]:30694.
That’s all. You can install WordPress as you can.
Thank you for reading the article.