Launching A WordPress Application With MYSQL Database in K8S Cluster On AWS

Anuja Kumari
9 min readSep 23, 2021

Hello Everyone 🙋‍♀️🙋‍♀️

In this article i have written about how we can launch a wordpress application connected with MySql database inside kubernetes Cluster on AWS.

💥 Task description

  • Launch EC2 instances on AWS Cloud eg. for master and slave
  • Configure master and slave node separately.
  • Launch a Wordpress and mysql database connected to it in the respective slaves
  • Expose wordpress pod and client able to hit the wordpress ip with its respective port.

🔰 What is Kubernetes ?

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.

🔅 What is Kubernetes MultiNode Cluster ?

A Kubernetes cluster is a set of node machines for running containerized applications. A cluster contains a control plane i.e master and one or more compute machines, or nodes. The control plane is responsible for maintaining the desired state of the cluster. Nodes actually run the applications and workloads.

🔰AWS ( Amazon Web Services )

Amazon Web Service is a subsidiary of Amazon providing on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis.

🔰 MYSQL :

A relational database organizes data into one or more data tables in which data types may be related to each other; these relations help structure the data. SQL is a language programmers use to create, modify and extract data from the relational database, as well as control user access to the database. In addition to relational databases and SQL, an RDBMS like MySQL works with an operating system to implement a relational database in a computer’s storage system, manages users, allows for network access and facilitates testing database integrity and creation of backups.

🔰 WordPress :

WordPress is a free and open-source content management system (CMS) written in PHP and paired with a MySQL or MariaDB database.

Let’s begin😄

💥 Launch the ec2-instances on AWS.

Here, I am launching 3 Instances , one for Master node, and other two for worker/slave nodes…by using Amazon Linux.

INSTANCES

Now after launching the ec2-instances , configure all the instances for the kubernetes multinode cluster.

So lets configure master and slaves which we have launched above.

🔆Now, Configure MASTER NODE

Give ROOT power :

For running different services, Master node needs Container…

✅Now, make the docker enable….so that when we start the OS it will be already in started state.

✅To install the software required for K8s setup i.e, kubeadm…we have to configure yum before installing this…for that go to the file /etc/yum.repos.d/kubernetes.repo and write the required conditions

Now, install kubeadm

kubeadm is a tool using which we can create a minimum viable K8s cluster and it provides kubeadm init and kubeadm join as best practice fast path.

Install kubelet.

kubelet is the node agent that runs on each node. It register the node with apiserver.

✅Now, enable kubelet

As we can see above, kubelet is trying to start but gets failed.

Now, we have to pull the image of different types of resources using kubeadm . we can see images by using command docker images .

✅Now, initialize the MASTER

But, it fails to initialize the master…Because by default docker uses cgroupfs driver as we can see in above picture👆…so we have to change the driver in the docker to systemd drive.

✅For this, Go to /etc/docker and create a file with daemon.json

✅Now, restart docker .Using command docker info | grep Driver for checking the driver is now updated from cgroupfs to systemd..

✅Now, for setting ROUTING PATH….

✅ Now we need to set the bridge routing to 1 using…..

echo "1" > /proc/sys/net/bridge/bridge-nf-call-iptables

✅ Now, we can initialize the master

As we are using free-tier account of aws i.e, we r using t2.micro having 1GB ram and 1CPU so we have to ignore the error of mem and CPU by using the below command….. .

✅After initializing, it give us some commands that we need to run for making the nodes as slave/worker.

AS we can see we don’t have any resources in the default namespace👆

🔅CONFIGURE WORKER NODE

In Worker node also we have to do the same process….

First give Root power to user by using command

✅Now install docker by using command..

✅Now we have to enable docker

✅To install the software required for K8s setup i.e, kubeadm…we have to configure yum before installing this…

✅Now, install kubeadm

✅Now, we need to pull images using kubeadm

By default docker uses cgroupfs driver …so we have to change the driver in the docker to systemd drive.

✅For this, Go to /etc/docker and create a file with daemon.json

✅Now, restart docker .Using command docker info | grep Driver for checking the driver is now updated from cgroupfs to systemd..

✅Now, for setting ROUTING PATH….

✅Now, we need to change the iptable…

✅Now, start the system

🔅 At Master node

HERE, we have to create a token so that worker nodes can join the master node.

🔅At Worker node

Run the above join command given by master node in the worker node…

Configure the 2nd worker node similarly….

If we have many worker nodes then pod will be launched in any of the nodes using Schedular program..

🔅AT Master node..

✅Now if we run kubectl get nodes , then we will find nodes are not ready….so we need to make overlay connection between master and worker. Now, we use Flannel plugin .

“kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"

✅ Now run the command….

💥 Successfully setup the Multi node K8s cluster on AWS✌🤩

💥 Now, we have to launch the wordpress with mysql data base. So i am launching wordpress and mysql inside a pod…

To launch the wordpress use this command

👉 kubectl run mywp — image=wordpress:5.1.1-php7.3-apache

To launch mysql pod

👉 kubectl run mydb — image=mysql:5.7 — env=MYSQL_ROOT_PASSWORD=redhat — env=MYSQL_DATABASE=wpdb — env=MYSQL_USER=anuja — env=MYSQL_PASSWORD=redhat

💥Exposing Wordpress pod:

👉 “kubectl expose pods mywp1 — type=NodePort — port=80”

To access the wordpress in public world we need to expose. Here, i am exposing on NodePort as the the client is in the public world.

👉 “kubectl get svc”

💥 Now you can take the public of any node weather master or slave with the exposed port and type on browser ….. you will landed to the wordpress login page and then enter password and username of the mysql database and hit the run installation button. your wordpress application is ready !!

click on Run the installation

💥 Finally our Task is completed successfully !!!!😄✌🏻

Keep Learning !! Keep Sharing!!!

--

--

Anuja Kumari

Learner @ Linuxworld Informative Pvt Ltd || DevOps(Docker,K8S, Jenkins, Terraform, Git and GitHub) || AWS || ( Python | Java )