How to run WordPress on Kubernetes
WordPress is a popular open-source content management system (CMS) for creating websites, blogs, and e-commerce platforms. Running WordPress on Kubernetes can be a powerful way to manage our WordPress deployment, ensuring scalability, resilience, and ease of management.
In this Answer, we’ll discuss the steps required to run WordPress on Kubernetes.
Deploying WordPress
Firstly, we will talk about the main step: deploying WordPress. To do this, we will define the specifications of the WordPress resource in the YAML file, such as the apiVersion, the kind of deployment, and the desired state of deployment:
# wordpress.yamlapiVersion: apps/v1kind: Deploymentmetadata:name: wordpressspec:replicas: 1selector:matchLabels:app: wordpresstemplate:metadata:labels:app: wordpressspec:containers:- name: wordpressimage: wordpress:latestports:- containerPort: 80env:- name: WORDPRESS_DB_HOSTvalue: mysql- name: WORDPRESS_DB_NAMEvalue: wordpress- name: WORDPRESS_DB_USERvalue: root- name: WORDPRESS_DB_PASSWORDvalue: password
Explanation
The line-by-line explanation of the code section above is as follows:
Lines 2–5: The
apiVersionbeing equal tov1is defined here, along with the kind of resource being created, which isDeploymentand the name of the resourcewordpressis defined inside themetadatasection.Lines 6–10: The desired number of replicas for deployment is set to one, as well as the labels that the pods must have to be selected during deployment under the
selectorsection.Lines 11–14: The pod template is used to create new pods with its metadata and labels.
Lines 15–29: The specification for the containers running in the pod is defined, consisting of its
name,imagelabel, port numbers and environment variables for the container including database connection details.
Defining the WordPress service
Next, we will define the WordPress service as shown below:
apiVersion: v1kind: Servicemetadata:name: wordpressspec:selector:app: wordpressports:- protocol: TCPport: 80targetPort: 80type: LoadBalancer
We define the apiVersion , kind, and metadata with the only difference being in the type of deployment, which is Service.
Here, we are defining a Kubernetes service to expose the WordPress deployment externally by describing the desire state of the service. This is done by the selector (this specifies how the service selects which pods to route traffic to) ports, and type of service, which is LoadBalancer in this case.
Deploying MySQL
We move to the steps where we need a database to store WordPress' content, configuration and other essential data. Here, we choose MySQL to be that database:
apiVersion: apps/v1kind: Deploymentmetadata:name: mysqlspec:replicas: 1selector:matchLabels:app: mysqltemplate:metadata:labels:app: mysqlspec:containers:- name: mysqlimage: mysql:latestenv:- name: MYSQL_ROOT_PASSWORDvalue: password- name: MYSQL_DATABASEvalue: wordpressports:- containerPort: 3306
Similar to deploying WordPress, we start off by defining the apiVersion, kind of deployment and the metadata containing the name of the resource (mysql in this case). A similar structure to the WordPress deployment is followed, including the environment variables for the MySQL root password and name of the WordPress database (lines 18–22).
Defining the MySQL service
Finally, we define the MySQL service to expose the MySQL deployment internally. This service allows other components in the Kubernetes cluster to access the MySQL database:
apiVersion: v1kind: Servicemetadata:name: mysqlspec:selector:app: mysqlports:- protocol: TCPport: 3306targetPort: 3306
Similar to defining the WordPress service, we define a Kubernetes service to expose the WordPress deployment via the selector and ports having port and targetPort numbers equal to 3306 and the TCP protocol.
Code
By combining all of the steps explained above, we can test our WordPress deployment in Kubernetes below. Run the code to see it in action.
# wordpress.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress:latest
ports:
- containerPort: 80
env:
- name: WORDPRESS_DB_HOST
value: mysql
- name: WORDPRESS_DB_NAME
value: wordpress
- name: WORDPRESS_DB_USER
value: root
- name: WORDPRESS_DB_PASSWORD
value: password
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
spec:
selector:
app: wordpress
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:latest
env:
- name: MYSQL_ROOT_PASSWORD
value: password
- name: MYSQL_DATABASE
value: wordpress
ports:
- containerPort: 3306
---
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
selector:
app: mysql
ports:
- protocol: TCP
port: 3306
targetPort: 3306Conclusion
Its important to remember that deploying WordPress on Kubernetes can be complex, especially if one is new to Kubernetes. It's a good idea to start small, gradually adding complexity as one becomes more comfortable with the technologies involved. Tools such as Helm charts are available to help simplify the deployment process.
Unlock your potential: Kubernetes Deployment and Advanced Operations series, all in one place!
If you've missed any part of the series, you can always go back and check out the previous Answers:
How to create a Kubernetes cluster locally with Minikube
Learn how to set up a Kubernetes cluster on your local machine using Minikube for development and testing.How to create deployment on a Kubernetes cluster via YAML files
Master the process of defining and managing deployments in Kubernetes using YAML configuration files.What is an Ingress controller in Kubernetes?
Understand the role of an Ingress controller in managing external access and routing traffic within a Kubernetes cluster.How to implement namespace quotas in Kubernetes
Discover how to apply resource limits to specific namespaces to optimize resource allocation and prevent overuse.How to implement Kubernetes resource quotas
Learn how to enforce limits on CPU, memory, and storage consumption across your Kubernetes cluster.Add users using certificates in a Kubernetes cluster
Explore the process of adding and authenticating users in Kubernetes by issuing and managing certificates.What is service discovery in Kubernetes?
Understand how Kubernetes enables seamless service discovery, allowing applications to communicate efficiently.How to put a database in Kubernetes
Learn the best practices for deploying and managing databases within a Kubernetes environment.How to run WordPress on Kubernetes
Follow a step-by-step guide to deploying a scalable and resilient WordPress site on Kubernetes.
Free Resources