Deploy a WordPress blog on Minikube with persistent data
Learn how to deploy a WordPress blog on Minikube quickly with persistent data, readiness/liveness probes & secrets.
Introduction
If you want to learn more about Kubernetes, deploying a WordPress blog on it is a great way to start. Not only does it take very little time, but the overall process is also pretty simple & straightforward.
You’ll also learn about 3 key concepts of Kubernetes in the process — Readiness/Liveness probes, secrets & persistent data.
To keep things simple, I am using Minikube here as it is the quickest way to spin up a single-node Kubernetes cluster with minimum maintenance.
This tutorial is based on macOS & the steps would be applicable for Linux based systems as well.
Let’s get started now.
Requirements
- Minikube — v1.9.2
- Kubectl — v1.18.2
- Your favorite text editor — I use VS Code.
Steps to perform
1. Create a Kubernetes secret.
Since WordPress stores it’s data on a MySQL database, we’ll need a secret in Kubernetes for the password. Instead of putting the password in clear-text in MySQL pod definition, we’ll use a secret.
For this, first, create a base64 representation of your password. The below command will do it for you.
admin@shashank-mbp ~/wordpress-minikube> echo -n '1f2d1e2e67df' | base64
Note that 1f2d1e2e67df
is your password. Feel free to use any password of your choice. The output of the above command will be your base64 encoded password. Copy it.
Now, create a file secrets.yml
& paste your base64 encoded password in the last line.
apiVersion: v1
kind: Secret
metadata:
name: mysql-pass
type: Opaque
data:
password:
Execute this command to create the secret.
admin@shashank-mbp ~/wordpress-minikube> kubectl apply -f secrets.yml
secret/mysql-pass created
You can also open the Kubernetes dashboard to view the newly created secret (if you don’t want to use CLI). The dashboard can be opened by typing minikube dashboard
command in a new terminal.
2. Download the resource files from my GitHub repository. You can clone it as well.
We will use Kubernetes resource files to create 2 deployments — 1 for WordPress UI & 1 for MySQL database.
There are 2 files for this.
wordpress-deployment.yaml
mysql-deployment.yaml
If you observe the files carefully, you’ll find how I am using PersistentVolumeClaim
to make data persistent and readinessProbe
& livenessProbe
to ensure that MySQL pod will be considered healthy only if it is ready to accept connections on port 3306.
The same holds true for WordPress static files, like HTML, CSS as well. It has been defined separately in
wordpress-deployment.yaml
file.
Persistent data.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: wordpress
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
Readiness & Liveness probes.
readinessProbe:
tcpSocket:
port: 3306
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 3306
initialDelaySeconds: 15
periodSeconds: 20
You can see that I am specifying 20 GB disk space to store MySQL data. You can change it as per your preference.
To create the deployments & other Kubernetes resources, run the following commands one by one.
For MySQL.
admin@shashank-mbp ~/wordpress-minikube> kubectl apply -f mysql-deployment.yaml
service/wordpress-mysql created
persistentvolumeclaim/mysql-pv-claim created
deployment.apps/wordpress-mysql created
For WordPress.
admin@shashank-mbp ~/wordpress-minikube> kubectl apply -f wordpress-deployment.yaml
service/wordpress created
persistentvolumeclaim/wp-pv-claim created
deployment.apps/wordpress created
You should now be able to see your deployments in Kubernetes. You can open the dashboard to check the status or you can also execute the below commands.
Please note that Kubernetes will pull the MySQL & WordPress images from Docker Hub, so this will take some time for the pods to be in a running state. You might notice errors in Kubernetes dashboard as well but they will disappear in a few minutes.
admin@shashank-mbp ~/wordpress-minikube> kubectl get pods
NAME READY STATUS RESTARTS AGE
wordpress-644fd9d44c-w5hnj 0/1 ContainerCreating 0 2m40s
wordpress-mysql-b8cd4c66c-mtd9t 0/1 ContainerCreating 0 2m44s
This will show you the pod status.
admin@shashank-mbp ~/wordpress-minikube> kubectl describe pod wordpress-644fd9d44c-w5hnj
Name: wordpress-644fd9d44c-w5hnj
Namespace: default
Priority: 0
Node: minikube/192.168.99.102
Start Time: Sat, 09 May 2020 10:19:29 +0530
Labels: app=wordpress
pod-template-hash=644fd9d44c
tier=frontend
Annotations: <none>
Status: Pending
IP:
IPs: <none>
Controlled By: ReplicaSet/wordpress-644fd9d44c
Containers:
wordpress:
Container ID:
Image: wordpress:5.4.1-apache
Image ID:
Port: 80/TCP
Host Port: 0/TCP
State: Waiting
Reason: ContainerCreating
Ready: False
Restart Count: 0
Environment:
WORDPRESS_DB_HOST: wordpress-mysql
WORDPRESS_DB_PASSWORD: <set to the key 'password' in secret 'mysql-pass'> Optional: false
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-8hg6t (ro)
/var/www/html from wordpress-persistent-storage (rw)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
wordpress-persistent-storage:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: wp-pv-claim
ReadOnly: false
default-token-8hg6t:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-8hg6t
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling <unknown> default-scheduler running "VolumeBinding" filter plugin for pod "wordpress-644fd9d44c-w5hnj": pod has unbound immediate PersistentVolumeClaims
Warning FailedScheduling <unknown> default-scheduler running "VolumeBinding" filter plugin for pod "wordpress-644fd9d44c-w5hnj": pod has unbound immediate PersistentVolumeClaims
Normal Scheduled <unknown> default-scheduler Successfully assigned default/wordpress-644fd9d44c-w5hnj to minikube
Normal Pulling 2m50s kubelet, minikube Pulling image "wordpress:5.4.1-apache"
3. Get the URL of WordPress service to install it & access the UI.
Once your deployments are in green (both wordpress & wordpress-mysql, type this command to get the IP address & port where your WordPress dashboard is available.
admin@shashank-mbp ~/wordpress-minikube> minikube service wordpress --url
http://192.168.99.102:32749
Open this URL in a browser tab & finish the installation.
And that’s it! How easy it was to deploy a WordPress blog on Minikube, complete with persistent data, MySQL readiness/liveness probe & secrets for MySQL password.
Till then, take care & stay safe!