PART 2

 

In Part 1, of this tutorial the details of how the WordPress pods are deployed, the deployment files and the infrastructure that is required to deploy a WordPress application on Kubernetes cluster. 

In Part 2 of this tutorial, we will actually deploy all the files and test the WordPress application using a web browser. 

 

 

Step 2: On the Master node

 

 

Login as the “jenkins” user on the Master Node. 

    • $ cd ~

    • $ mkdir wp-config-files                  (you can give any directory name you like)

 

Next, mount the EFS created for the config files on this directory. The command to mount the directory is mentioned in the AWS EFS console and has been explained in the blog.

    • $ sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-011a40c8f24e5b94d.efs.ap-south-1.amazonaws.com:/ /home/jenkins/wp-config-files

/home/jenkins/wp-config-files” is the complete path of the directory we have created for the WordPress config files.

    • $ df -k                    (should show the wp-config-files mounted on the EFS)

    • $ cd wp-config-files

    • $ sudo yum install -y git

 

Download the WordPress yaml files from github location.

    • $ git clone https://github.com/AvangelsTech/simple-wordpress-files.git .

This will download the required  “yaml” files for deploying the WordPress application on the Kubernetes cluster in the current “wp-config-files” directory

 

Now let’s set up the Worker node for the WordPress data files.

 

 

Step 3: On the Worker node

 

 

Login to the Worker Node as “jenkins” user. 

    • $ cd ~

    • $ mkdir wp-data-files           (you can give any directory name you like)

 

Now mount the EFS created for the WordPress data files on this directory. The command to mount the directory is mentioned in the AWS EFS console and has been explained in this article.

    • $ sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-09b70377fe373b9f6.efs.ap-south-1.amazonaws.com:/ /home/jenkins/wp-data-files   

/home/jenkins/wp-data-files” is the complete path of the directory we have created for the WordPress data files.

    • $ df -k                    (should show the wp-data-files mounted on the EFS)

 

We will create 2 more directories “html” and “mysql“. All datafiles (configuration files) of WordPress are stored in the “html” directory, while all the datafiles (configuration files) of MySQL are stored in “mysql” directory. 

    • $ cd /home/jenkins/wp-data-files   

    • $ mkdir html mysql

    • $ sudo su

    • # chown jenkins:jenkins html

    • # chown jenkins:jenkins mysql

    • # exit 

    • $ exit

 
The directory setup on both the Master node and Worker node is complete.

 

 

 

Step 4: On the Master node

 

 

Login to the Master Node as “jenkins” user for the next steps. 

    • $ cd ~/wp-config-files

    • $ cd ~

    • $ kubectl create -f wp-config-files/

This command will deploy the WordPress and MySQL pods and create the necessary services 

You should see an output like below:

 

 

 

Step 5: Access WordPress

 

 

Get the public IP of the Worker node from the EC2 console.

In the service “yaml” file, we have used 30210 is the nodePort for the WordPress service. 

Open any web browser and paste the “public IP address of the Worker Node” EC2 instance with the port no.

http://13.235.23.116:32010/

Syntax for accessing WordPress: HTTP://<public-ip-address-worker-node>:32010 

Port “32010”  is the node port of the WordPress

 

However, this may show a “Database Connection Error” on the web browser.

 
 
How to remove the Database Connection Error

 

On the Master Node, you need to log in to the MySQL pod and create a new database. The database is not created by default. We will create a database called “wordpress” by logging into the MySQL pod. 

    • $ kubectl exec -ti wordpress-mysql-0 /bin/bash

    • # mysql -u root -p

Enter password: 

    • mysql> create database wordpress;

    • mysql> exit

    • Bye

    • bash-4.2# exit

    • exit

This creates the new “wordpress” database in MySQL pod. 

 

Next delete the deployment, as we need to make changes in the datafiles of WordPress. 

    • $ kubectl delete -f wp-config-files

 

 

Step 6: On the worker node

 

 

Login as “jenkins” user on the Worker node, as this is where the WordPress and MySQL datafile reside. 

 
IMPORTANT: All important configurations of WordPress are defined in a configuration file called “wp-config.php”. We define the DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, in this file. So keep this file carefully and highly secure. 

 

    • $ cd ~

    • $ cd wp-data-files

    • $ cd html

    • $ sudo su

    • # vi wp-config.php

Once you login to the “wp-config.php” file make the change to the below-mentioned parameters. This parameters are what we have defined in our Kubernetes deployment “yaml” file. 

// ** Database settings – You can get this info from your web host ** //

/** The name of the database for WordPress */

define( ‘DB_NAME’, getenv_docker(‘WORDPRESS_DB_NAME’, ‘wordpress’) );

/** Database username */

define( ‘DB_USER’, getenv_docker(‘WORDPRESS_DB_USER’, ‘root‘) );

/** Database password */

define( ‘DB_PASSWORD’, getenv_docker(‘WORDPRESS_DB_PASSWORD’, ‘admin‘) );

 

NOTE:

WORDPRESS_DB_NAME=wordpress    ## Name of the database. You can provide any database name.

WORDPRESS_DB_USER=root         ## Name of the database user. You can provide any name.

WORDPRESS_DB_PASSWORD=admin    ## Password of the database user. You can provide any password.

 
IMPORTANT: The WORDPRESS_DB_PASSWORD, should match the password provided in the “secret.yaml” file. The “secret.yaml” file password in base64, so you just ensure that password the change is made accordingly.

 

 

Step 7: On the Master Node

 

 

Login as the “jenkins” user and recreate the WordPress deployment. 

    • $ cd ~                       ## (Our deployment “yaml” files are in wp-config-file” located under “/home/jenkins”)

    • $ kubectl create -f wp-config-file

This will re-create the deployment and take the database, database_user and database_password values we defined in Step 5, in the “wp-config.php” file. 

 

Open any web browser and paste the “public IP address of the Worker Node” EC2 instance with the port no.

http://13.235.23.116:32010/

Syntax for accessing WordPress: HTTP://<public-ip-address-worker-node>:32010 

Port “32010”  is the node port of WordPress service.

NOTE: Your EC2 instance’s public IP will be different from what is mentioned here. So insert the correct public IP. 

 

If everything is OK, then you will see the below screens, and input the data as mentioned.  (You can select the language of your choice)

 

  • English (United States) –> Continue

 

 

Enter the details. The below-entered values are just an illustration, so you can enter appropriate values. (Keep the password of root carefully)

 

 

If everything is OK, you should see the below screen.

 

 

Click –> “Log in” to login to the WordPress Dashboard

 

 
CONGRATULATIONS you have successfully deployed a WordPress website on your Kubernetes cluster. 

 

You can now build a website using this WordPress deployment. 

 

Part 1 –> How to deploy WordPress on Kubernetes