Introduction
This guide provides step-by-step instructions on deploying a WordPress application on Kubernetes using MySQL as the database backend. We’ll use Minikube as our Kubernetes environment.
Prerequisites
Minikube installed and running.
kubectl
command-line tool installed and configured.Basic knowledge of Kubernetes concepts like Pods, Services, and Persistent Volumes.
Step 1: Start Minikube
First, start Minikube. This will initialize a single-node Kubernetes cluster on your local machine.
minikube start
Step 2: Create the MySQL Deployment and Service
We’ll set up a MySQL deployment and expose it through a service so that WordPress can connect to it.
Create the MySQL YAML File (mysql.yml
)
Create a file named mysql.yml
with the following content:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.6
env:
- name: MYSQL_ROOT_PASSWORD
value: "password"
- name: MYSQL_DATABASE
value: "wordpress"
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
---
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
Explanation:
PersistentVolumeClaim: Requests storage to persist MySQL data, preventing data loss if the Pod restarts.
Deployment: Defines a MySQL Pod using the official MySQL 5.6 image.
Service: Exposes MySQL on port 3306 within the cluster, making it accessible to WordPress.
Apply the MySQL Configuration
Deploy MySQL by applying the mysql.yml
configuration:
kubectl apply -f mysql.yml
Verify that the MySQL service and deployment are running:
kubectl get pods
kubectl get svc mysql
Step 3: Create the WordPress Deployment and Service
Now we’ll deploy WordPress and link it to the MySQL service.
Create the WordPress YAML File (wordpress.yml
)
Create a file named wordpress.yml
with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
spec:
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress:4.8-apache
env:
- name: WORDPRESS_DB_HOST
value: mysql:3306
- name: WORDPRESS_DB_PASSWORD
value: "password"
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress
type: LoadBalancer
Explanation:
Deployment: Configures WordPress using the Apache WordPress 4.8 image. Environment variables configure the MySQL host and password.
Service: Exposes WordPress on port 80. Setting the
type
toLoadBalancer
provides external access to WordPress when using Minikube’s tunnel feature.
Apply the WordPress Configuration
Deploy WordPress by applying the wordpress.yml
configuration:
kubectl apply -f wordpress.yml
Verify the WordPress deployment and service:
kubectl get pods
kubectl get svc wordpress
Step 4: Access the WordPress Site
In Minikube, services with a LoadBalancer
type require the use of the minikube tunnel
command to create an external IP.
Start Minikube Tunnel
Run minikube tunnel
in a separate terminal to create the external IP for accessing WordPress:
minikube tunnel
Get the WordPress Service URL
To get the external URL of the WordPress service, run:
minikube service wordpress --url
This command outputs a URL (e.g., http://<external-ip>:80
). Open this URL in your browser to access the WordPress setup page.
Step 5: Complete the WordPress Setup
In the browser, follow the WordPress setup instructions to complete the configuration. Once finished, your WordPress site will be ready for use.
Summary
You have successfully deployed a multi-tier WordPress application on Kubernetes using Minikube. This setup can serve as a base for deploying more complex, multi-tier applications.