Deploy the WordPress application on Kubernetes and connecting it with Amazon RDS Database
Task Description:
- Write an Infrastructure as code using terraform, which automatically deploy the WordPress application.
- On AWS, use RDS service for the relational database for WordPress application.
- Deploy the WordPress as a container either on top of Minikube or EKS or Fargate service on AWS.
- The WordPress application should be accessible from the public world if deployed on AWS or through workstation if deployed on Minikube.
What is Amazon RDS?
Amazon Relational Database Service (Amazon RDS) makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while automating time-consuming administration tasks such as hardware provisioning, database setup, patching and backups. It frees you to focus on your applications so you can give them the fast performance, high availability, security and compatibility they need.
What is Kubernetes?
Kubernetes or k8s is a popular open source platform for container orchestration — that is, for the management of applications built out of multiple, largely self-contained runtimes called containers.
Containers have become increasingly popular since the Docker containerization project launched in 2013, but large, distributed containerized applications can become increasingly difficult to coordinate. By making containerized applications dramatically easier to manage at scale, Kubernetes has become a key part of the container revolution.
Before beginning with the task there are some pre-requisites:
a] You should have any hyperviser installed (in my case it’s oracle virtual box).
b] “minikube” is a local k8s which I will be using for this task has to be installed along with “kubectl” which is a CLI tool for using Kubernetes.
c] AWS account
The whole infrastructure will be setup using terraform code in just one click, as it provides IAAC (Infrastructure as a code) facility.
STEP1:
I am going to write code for deploying WordPress on Kubernetes that is running in my local virtual machine. Before that we will run “terraform init” command in terminal to installed all the required plugins.
First, we will set providers of Kubernetes and AWS. Almost any infrastructure type can be represented as a resource in Terraform. A provider is responsible for understanding API interactions and exposing resources.
Next we will launch a Kubernetes Deployment. We can just launch POD normally but if we launch Pod via Deployment it will be better because Deployment will continuously monitor our Pod/s, if any Pod fails automatically a new one will be launched. We can define number of replicas as well. The code is as follows:
We have to expose our Deployment for that we will use Kubernetes service. Service is an abstract way to expose an application running on a set of Pods as a network service.
I have have use Service type “Nodeport”.
Nodeport: This type of service exposes the service on each node’s IP at a static port. A ClusterIP service is created automatically, and the NodePort service will route to it. From outside the cluster, you can contact the NodePort service by using “<NodeIP>:<NodePort>”.
The code is as follows:
We can verify if our code is right or not using “terraform validate” command. Then finally we can run “terraform apply — auto-approve” command which will setup the infrastructure.
After running the above code, one part of the setup that is Deployment of WordPress application will be done.
STEP2:
Here I will write code for launching an instance on AWS cloud which will be a database for our application. I will use AWS RDS service for this purpose. The code is as follows:
As you can see, above I have given name of our database, along with username and password for authentication, port number and more things.
This will print the Endpoint that RDS will providing us which we are going to use as Database Host later.
After running the above code, our database instance will be launched and we can confirm it via AWS console as well.
STEP3:
Now, I will start my minikube which is local Kubernetes running on my virtual machine.
To check if WordPress successfully or not we can use the following client-side commands i.e kubectl.
We can see many information like port number associated with WordPress, number of replicas, etc.
To get the IP of minikube we can use:
STEP4: FINAL OUTPUT
Let’s access the WordPress Application from our web browser.
This step is important as all the credentials have to be entered correctly. In case error occurs check inbound rules of security group attached to database instance.
Click on it and WordPress will be installed. Further few details have to be entered then final website will appear.
After login, finally this will appear:
This means we have successfully deployed WordPress on Kubernetes and connected it with AWS RDS Database.
The terraform code that I have written can be executed in one go as well, but I have done in parts. Also after the complete infrastructure is tested we can also destroy it in just one click by using “terraform destroy” command.
GitHub Link: https://github.com/ther1chie/WordPress_on_Kubernetes_connected_to_AmazonRDSDatabase