Kubernetes on Ampere - Deploy WordPress and MySQL
Tutorial
WordPress is a very popular blogging and content management tool that uses a relational database to store blog articles, related data, metadata, pictures etc. This tutorial shows how to deploy a WordPress Site and MySQL database on a Kubernetes cluster hosted on Ampere servers or virtual machines. Each component runs as a container. The WordPress container is the front-end tier and the MySQL container will be the backend/database for WordPress.
We use the official arm64 Docker images for WordPress and MySQL from Docker hub.
If deploying on the cloud, you can use your cloud provider’s Kubernetes engine. Ampere instances are available on OCI, GCP and Azure clouds. When deploying on bare-metal servers, create a Kubernetes cluster using Kubeadm or Minikube.
The following manifest describes a single instance deployment of MySQL. The MySQL container uses a PersistentVolume (PV) for storage using the host path /tmp/mysql/data and mounted at /var/lib/mysql. The password for the MySQL database is stored in a Kubernetes secret which is generated from password.txt.
NOTE: For production deployments, we recommend PVs backed by persistent disks to store data outside of containers.
mysql-deployment.yaml
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/tmp/mysql/data"
persistentVolumeReclaimPolicy: Recycle
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
---
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress-mysql
spec:
ports:
- port: 3306
selector:
app: wordpress-mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress-mysql
spec:
selector:
matchLabels:
app: wordpress-mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress-mysql
spec:
containers:
- image: mysql:oracle
name: mysql
env:
- name: MYSQL_DATABASE
value: wordpress
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password.txt
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
This manifest describes a WordPress deployment. The WordPress container also uses a PersistentVolume for storing the website files using the host path /tmp/wp/data and mounted at /var/www/html. The environment variable WORDPRESS_DB_HOST sets the name of the MySQL service and WORDPRESS_DB_PASSWORD defines the password to access the MySQL database from the Kubernetes secret generated from the password.txt file.
If using a cloud deployment, you can set the service type to LoadBalancer and use the IP address of the cloud load balancing service to access the WordPress front-end. If using Kubernetes on bare metal, you can set the type to NodePort and use the Kubernetes node IP address to access WordPress front-end. Alternatively, you can use type as LoadBalancer and specify the external IP address of your server.
wordpress-deployment.yaml
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: wp-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/tmp/wp/data"
persistentVolumeReclaimPolicy: Recycle
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: wp-pv-claim
labels:
app: wordpress
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
tier: frontend
spec:
ports:
- port: 80
selector:
app: wordpress
tier: frontend
type: LoadBalancer
externalIPs:
- 10.76.87.233
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
tier: frontend
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
tier: frontend
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: frontend
spec:
containers:
- image: wordpress:6.0.1-php7.4
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql
- name: WORDPRESS_DB_NAME
value: wordpress
- name: WORDPRESS_DB_USER
value: root
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password.txt
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wp-pv-claim
kubectl create secret generic mysql-pass --from-file=password.txt kubectl create -f mysql-deployment.yaml kubectl create -f wordpress-deployment.yaml
Verify that services are running using the following command:
# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 51m
wordpress LoadBalancer 10.100.16.187 10.76.87.233 80:32160/TCP 40m
wordpress-mysql ClusterIP 10.105.136.201 <none> 3306/TCP 40m
# kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE mysql-pv-volume 20Gi RWO Recycle Bound default/wp-pv-claim manual 13h wp-pv-volume 20Gi RWO Recycle Bound default/mysql-pv-claim manual 13h
Copy the external IP address of the WordPress service and open the link http://<"ip-address"> in a browser. You should see the WordPress setup page.
Fill in the details and set up the WordPress site. Your WordPress site is now up and running on Ampere !!