When deploying WordPress on Kubernetes, you’ll need to create several YAML files to configure the various components needed for a WordPress site. Kubernetes uses these YAML files to define, configure, and manage the deployment of containerized applications. For WordPress, the essential components include a web server (WordPress itself), a database (MySQL or MariaDB), and persistent storage for both. Here’s a step-by-step guide to writing the necessary YAML files to get WordPress up and running on Kubernetes.
1. PersistentVolume (PV) and PersistentVolumeClaim (PVC)
Persistent volumes are used to store persistent data like the WordPress content and the database files. PVCs are requests for storage by a user.
WordPress PV and PVC
kind: PersistentVolume
apiVersion: v1
metadata:
name: wordpress-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data/wordpress"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: wordpress-pvc
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Database PV and PVC
kind: PersistentVolume
apiVersion: v1
metadata:
name: mysql-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data/mysql"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pvc
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
2. MySQL Deployment
You need to deploy MySQL as the database for WordPress. This involves creating a deployment and a service.
MySQL Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: wordpress
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
spec:
containers:
- image: mysql:5.7
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: yourpassword
- name: MYSQL_DATABASE
value: wordpress
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
MySQL Service
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: wordpress
clusterIP: None
3. WordPress Deployment and Service
Finally, deploy WordPress and expose it through a service.
WordPress Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
spec:
selector:
matchLabels:
app: wordpress
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
spec:
containers:
- image: wordpress:latest
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: mysql
- name: WORDPRESS_DB_PASSWORD
value: yourpassword
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wordpress-pvc
WordPress Service
apiVersion: v1
kind: Service
metadata:
name: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress
type: LoadBalancer
Deployment Steps
- Create the Persistent Volumes and Claims: Apply the PV and PVC configurations first to ensure the storage is available for the deployments.
- Deploy MySQL: Next, apply the MySQL deployment and service YAML files. This step sets up the database WordPress will use.
- Deploy WordPress: Finally, apply the WordPress deployment and service YAML files. This step will set up WordPress and connect it to the previously deployed MySQL database.
Use the kubectl apply -f <filename>.yaml
command to apply each of these
Leave a Reply