In the realm of distributed environments, platforms like Kubernetes are undoubtedly setting up a standard. This is applicable across dynamic builds and also production environments. Complex application ecosystems and the most sought-after Kubernetes have made services like MySQL quite important in the field of distributed environments.
The open-source Helm package manager for Kubernetes makes the process of deploying and upgrades on a Kubernetes cluster quite easy and also offers information on applications like Kubernetes Charts.
Here in this article, we will discuss how to use Helm to install WordPress on Kubernetes. From the point of scalability and accessibility aspects, Helm makes WordPress secure and offers simplified upgrade and rollback via it.
For the database component, the external MySQL server database component is used that enables Helm to be a part of a separate cluster. So, at the end of this article, you will learn how to install WordPress with MySQL on Kubernetes.
Requirements
To ensure a seamless installation, you need to meet the following requirements:
- You need a Kubernetes 1.10+ cluster with RBAC. This is role-based access control.
- Configure the kubectl for cluster connectivity. You can also go through Kubernetes resources for instructions.
- You also need to deploy Helm and Tiller.
- For the database, you need a MySQL server with SSH access. It also should have the root MySQL password. You can log into your MySQL server and check Kubernetes cluster connectivity. If there is more than one cluster on the kubectl config file, you need to check if you are connected to the correct cluster.
kubectl config get-contexts
Output:
Output
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* do-sfo2-wordpress-cluster do-sfo2-wordpress-cluster do-sfo2-wordpress-cluster-admin
minikube minikube minikube
This (*) sign is for the default cluster selection. Check the current context, by executing this command:
kubectl config use-context context-name
Step 1: Configuring MySQL
There should be a MySQL user and an associated database for WordPress. External hosts should be able to connect here. Once the WordPress installation is complete this will be on a server as a part of the Kubernetes cluster. This is not required for a single and dedicated MySQL server.
On the MySQL server, you can log into MySQL using:
mysql -u root -p
Enter the password you set up for the root MySQL account. This is during the installation. Log in to MySQL and you can create the database and the user details for WordPress.
database name: wordpress
User name: wordpress_user
password: password
To create the database, you can use the following command:
CREATE DATABASE wordpress;
Create MySQL user for this database:
CREATE USER wordpress_user IDENTIFIED BY ‘password’;
Add admin access for wordpress_user for both internal and external networks:
GRANT ALL PRIVILEGES ON wordpress.* TO wordpress_user@’%’;
To manage access to internal MySQL tables you can use the below command
FLUSH PRIVILEGES;
To exit MySQL client:
exit;
Check MySQL command-line client using wordpress_user :
mysql -u wordpress_user -p
Check database access:
show databases;
Output:
Output
+——————–+
| Database |
+——————–+
| information_schema |
| wordpress |
+——————–+
2 rows in set (0.03 sec)
You can exit once completed:
exit;
So, now the WordPress installation is deployed on a separate server, you can modify the MySQL configuration for external connections.
Select to open the file /etc/mysql/mysql.conf.d/mysqld.cnf
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
Configure the bind-address setting within this file. MySQL by default accepts 127.0.0.1 (localhost). Modify this to 0.0.0.0 so that it looks like below-
/etc/mysql/mysql.conf.d/mysqld.cnf
# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address = 0.0.0.0
Save the changes and restart MySQL:
sudo systemctl restart mysql
Check the remote connectivity:
mysql -h mysql_server_ip -u wordpress_user -p
Edit mysql_server_ip to your MySQL server IP address and move to the next step.
Step 2: Install WordPress
We can now use Helm to install WordPress.
It starts with the installation of MariaDB on a separate pod as the database. But for installation purposes, it needs to use a MySQL database. You can make this and other configuration edits like the default user credentials during this time via command-line parameters or YAML configuration.
In the following steps, we will use a configuration file.
Create a new directory and select the followings:
mkdir myblog-settings
cd myblog-settings
Create values.yaml, using your text editor:
nano values.yaml
Enter custom variables to help WordPress connect to the database and add some basic information and admin credentials after the WordPress installation is complete.
These are stored as default on values.yaml file from the WordPress Helm chart. Blog/Site Info has all the general features for the WordPress blog. It contains the name and the login details. The Database Settings helps to connect to the remote MySQL server. MariaDB is inactivated at the final phase.
Add the following to values.yaml file, and edit the highlighted values with the new values:
values.yaml
## Blog/Site Info
wordpressUsername: sammy
wordpressPassword: password
wordpressEmail: sammy@example.com
wordpressFirstName: Sammy
wordpressLastName: the Shark
wordpressBlogName: Sammy’s Blog!
## Database Settings
externalDatabase:
host: mysql_server_ip
user: wordpress_user
password: password
database: wordpress
## Disabling MariaDB
mariadb:
enabled: false
The following options are configured:
wordpressUsername: WordPress user’s login
wordpressPassword: WordPress user’s password
wordpressEmail: WordPress user’s email
wordpressFirstName: WordPress user’s first name
wordpressLastName: WordPress user’s last name
wordpressBlogName: Name of the Site or Blog
host: MySQL server IP address or hostname
user: MySQL user
password: MySQL password
database: MySQL database name
Exit from the editor once this task is finished.
Now you can use helm to install WordPress. Initiate helm for the latest WordPress chart with the name myblog, using values.yaml as the setup file:
helm install –name myblog -f values.yaml stable/wordpress
Output:
Output
NAME: myblog
LAST DEPLOYED: Fri Jan 25 20:24:10 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Deployment
NAME READY UP-TO-DATE AVAILABLE AGE
myblog-wordpress 0/1 1 0 1s
==> v1/PersistentVolumeClaim
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
myblog-wordpress Pending do-block-storage 1s
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
myblog-wordpress-5965f49485-8zfl7 0/1 Pending 0 1s
==> v1/Secret
NAME TYPE DATA AGE
myblog-externaldb Opaque 1 1s
myblog-wordpress Opaque 1 1s
==> v1/Service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myblog-wordpress LoadBalancer 10.245.144.79 <pending> 80:31403/TCP,443:30879/TCP 1s
(…)
Now, myblog-wordpress is created on Kubernetes. It will take some time before the container and the External-IP information are accessible. For the service status and external IP address execute the following command:
kubectl get services
Output
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 20h
myblog-wordpress LoadBalancer 10.245.144.79 203.0.113.110 80:31403/TCP,443:30879/TCP 3m40s
This gives you information about the services running on your cluster. It also shows that IP addresses like myblog-wordpress are available on the external IP address 203.0.113.110.
Note: If during the installation you have used the minikube, you have to execute a minikube service myblog-wordpress to check the container web server
WordPress installation is now active. You can use kubectl get services, followed by /wp-admin in your web browser:
http://203.0.113.110/wp-admin
Use the custom values from values.yaml file to enter and set up the WordPress site.
Step 3: Upgrading
WordPress needs to be constantly upgraded for smooth functioning. The helm upgrade ensures Helm upgrades.
Execute the following command to see the list of releases:
helm list
Output
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
myblog 1 Fri Jan 25 20:24:10 2019 DEPLOYED wordpress-5.1.2 5.0.3 default
If you want to upgrade to the latest version of a chart, you need to upgrade Helm repositories with the following command:
helm repo update
Output
Hang tight while we grab the latest from your chart repositories…
…Skip local chart repository
…Successfully got an update from the “stable” chart repository
Update Complete. ⎈ Happy Helming!⎈
The newer version of the WordPress chart is available with:
helm inspect chart stable/wordpress
Output
apiVersion: v1
appVersion: 5.1.1
description: Web publishing platform for building blogs and websites.
engine: gotpl
home: http://www.wordpress.com/
icon: https://bitnami.com/assets/stacks/wordpress/img/wordpress-stack-220×234.png
keywords:
– wordpress
– cms
– blog
– http
– web
– application
– php
maintainers:
– email: containers@bitnami.com
name: Bitnami
name: wordpress
sources:
– https://github.com/bitnami/bitnami-docker-wordpress
version: 5.9.0
The chart version also gets upgraded from version 5.9.0 with WordPress 5.1.1 . Execute the following command for the latest charts:
helm upgrade -f values.yaml myblog stable/wordpress
This command will give you results similar to the helm install. Use the same configuration file as you had used the first time while installing the WordPress for the custom database settings.
Execute helm list to get more updates on the release:
Output
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
myblog 2 Fri May 3 14:51:20 2019 DEPLOYED wordpress-5.9.0 5.1.1 default
WordPress is upgraded to WordPress chart.
RollBack
Every upgrade creates a new revision of release by Helm. This is a version history detail that you can revisit to know about the changes. To rollback you can always use the helm rollback command:
helm rollback release-name revision-number
If there are any edits or changes which you want to not go ahead with then you go back to the initial version by using
helm rollback myblog 1
If this rollback goes through, then the output will be:
Output
Rollback was a success! Happy Helming!
Execute the helm list to check if it was downgraded
Output
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
myblog 3 Mon Jan 28 22:02:42 2019 DEPLOYED wordpress-5.1.2 5.0.3 default
Notice that rolling back a release will create a new revision, based on the target revision of the roll-back. This makes myblog version 3 the latest file. So, we can see there is a change in the revision number.
Conclusion
In this article, we explored how to install WordPress on a Kubernetes cluster with an external MySQL server using Helm. The list of steps with code snippets and examples should help you to perform the complete process. If you face any issue while following the above-mentioned steps, drop us a comment through the below box.