Introduction to the Kubernetes Deployment of WordPress tutorial
How Do I Deploy a WordPress Instance for Kubernetes? As the world’s most popular content management system, WordPress runs websites of all sizes, both in terms of content volume and web traffic. Deploying WordPress on Kubernetes is an effective way to scale your website horizontally and successfully handle spikes in website traffic.
This tutorial covers two ways to deploy WordPress on Kubernetes – using Helm charts and creating a deployment from scratch.
Prerequisite
- A Kubernetes cluster kubectl
- Helm 3
- System administrative privileges
Use Helm Chart to deploy WordPress on Kubernetes
How do I deploy a WordPress instance on Kubernetes? Helm charts come with pre-configured application installation that can be deployed with a few simple commands.
- Add a repository containing the WordPress Helm charts you want to deploy:
helm repo add [repo-name] [repo-address]
The system confirms that the repository has been added successfully. The example uses a Bitnami chart.
2. Update your local Helm repository:
helm repo update
3. Kubernetes Deployment WordPress Tutorial: Use the helm install command to install the chart.
helm install [release-name] [repo-address]
Wait for the chart to be deployed.
4. WordPress services use LoadBalancer as a way to expose the service. If you’re using minikube, open another terminal window and type the following command to emulate LoadBalancer:
minikube tunnel
5. When minikube displays the Running status, minimize the window and return to the previous window.
6. Check the readiness of the deployment by typing the following:
kubectl get all
The command lists and displays the status of pods, services, and deployments.
Kubernetes Deployment WordPress Tutorial
7. Once the pods and deployment are ready, use the following command to export SERVICE_IP
environment variables:
export SERVICE_IP=$(kubectl get svc --namespace default wp-test-wordpress --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")
8. Now use the echo command to display the IP address of the service:
echo "WordPress URL: http://$SERVICE_IP/"
9. Enter the address in your browser’s address bar. WordPress installation begins.
Use persistent volumes to deploy WordPress on Kubernetes
How Do I Deploy a WordPress Instance for Kubernetes? When you deploy WordPress with a custom configuration, you’ll need to create a series of YAML files for the database that WordPress and the application will use. The example below uses MySQL, but you can also choose MariaDB.
1. Use a text editor to create a YAML file to configure storage for your MySQL database.
nano mysql-storage.yaml
The file defines a persistent storage volume (PV) and claims that storage using PersistentVolumeClaim (PVC). The example uses the following configuration:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
labels:
app: wordpress
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
Save the file and exit.
2. Create a MySQL deployment configuration YAML.
nano mysql-deployment.yaml
How do I deploy a WordPress instance on Kubernetes? The deployment file contains data related to container images and storage. The claim at the bottom of claimName
should correspond to the name of the PVC you created in step 1.
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.7
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
Save the file and exit.
Note: Learn how to deploy a MySQL DB instance on Kubernetes using persistent volumes.
3. Kubernetes Deployment WordPress Tutorial: Configuring YAML for Database Creation Services.
nano mysql-service.yaml
This file specifies the port that WordPress uses to connect to the service:
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
Save the file and exit.
4. Now create the same YAML file for WordPress itself. Start with storage allocation:
nano wordpress-storage.yaml
The example uses the following configuration:
apiVersion: v1
kind: PersistentVolume
metadata:
name: wp-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wp-pv-claim
labels:
app: wordpress
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
Save the file and exit.
5. Create a deployment file:
nano wordpress-deployment.yaml
How Do I Deploy a WordPress Instance for Kubernetes? The file provides a Docker image and connects the WordPress deployment with the PVC:
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:5.8-apache
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wp-pv-claim
Save the file and exit.
6. Create a service YAML:
nano wordpress-service.yaml
The example uses LoadBalancer to expose the service:
apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress
tier: frontend
type: LoadBalancer
Save the file and exit.
7. Create a kustomization.yaml file that will be used to easily apply the configuration:
nano kustomization.yaml
The file contains two parts:
secretGenerator
generates a Kubernetes key that passes login information to the container.The resources
section lists all the files that will be involved in the configuration. Make a list of the files you created in the previous steps.
secretGenerator:
- name: mysql-pass
literals:
- password=test123
resources:
- mysql-storage.yaml
- mysql-deployment.yaml
- mysql-service.yaml
- wordpress-storage.yaml
- wordpress-deployment.yaml
- wordpress-service.yaml
Save the file and exit.
8. Apply the files listed in kustomization.yaml using the following command:
kubectl apply -k ./
The system confirms that secrets, services, PVs, PVCs, and deployments have been successfully created:
Kubernetes Deployment WordPress Tutorial
9. Check that the pods and deployments are ready:
kubectl get all
10. If you are using minikube, open another terminal window and start a minikube tunnel to provide a load balancing simulation:
minikube tunnel
After the simulation runs, minimize the window and return to the previous window.
11. How do I deploy a WordPress instance on Kubernetes? Type the following command to expose the service URL that you will use to access your deployed WordPress instance:
minikube service wordpress-service --url
The URL is shown as command output:
Note: If you don’t use minikube, use the external IP address and the port of the WordPress service in the table in step 9.
12. Copy the URL and paste it into your web browser. WordPress installation begins.
Kubernetes Deployment WordPress Tutorial Conclusion
How Do I Deploy a WordPress Instance for Kubernetes? After reading this tutorial, you’ll learn two ways to deploy a WordPress instance on Kubernetes. If you want to learn more about managing WordPress, read 25 performance and optimization tips to speed up your WordPress.