Integration of AWS-RDS and Wordpress running on Kubernetes pods.

Akhilesh Jain
11 min readSep 9, 2020

In this article, I have created an Integration which Includes AWS service RDS used for Database and Wordpress site running on Kubernetes pod. This whole Integration is created using Terraform, which will automatic deploy this integration, according to the instructions given by us according to our Used Cases.

Technologies Used :

→AWS RDS

→ Kubernetes Pods(Wordpress)

What is Kubernetes?

Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications.

It groups containers that make up an application into logical units for easy management and discovery. Kubernetes builds upon 15 years of experience of running production workloads at Google, combined with best-of-breed ideas and practices from the community.

→ Terraform (I have used module Approach of terraform)

What is Terraform ?

Use Infrastructure as Code to provision and manage any cloud, infrastructure, or service. Terraform users define infrastructure in a simple, human-readable configuration language called HCL (HashiCorp Configuration Language). Users can write unique HCL configuration files or borrow existing templates from the public module registry.

Why Terraform? While you could use kubectl or similar CLI-based tools mapped to API calls to manage all Kubernetes resources described in YAML files, orchestration with Terraform presents a few benefits. Use the same configuration language to provision the Kubernetes infrastructure and to deploy applications into it.

→ For creating RDS Database, we need a VPC and a Subnet, here I am using the default VPC.

→ But for Security of ports of our database, we need a Security Group, either we can use default Security Group and add rules to it, but here I am creating a New Security Group with the desired rules.

So, lets get started creating this infrastructure.

This is the Structure of my Folder Task6.

  • In this, I have created 2 folder(modules) : “awsrds” and “k8swp”. Both contains terraform files, which are reponsible for working of modules. This foolder task6 has 2 files: “main.tf” and “variables.tf”.

main.tf → This is our main file which will call the modules to apply the infrastructure and is also used to assign values to variables used in modules. This file is the only way to route between different modules, use global variables.

variables.tf → This is file which is used to store global variables, which can be used in main file, and then used in modules.

vat.tf file

→ In this file, we have created 3 variables, which are used to give username for rds resource, password for database and port for WordPress.

In this Integration, I am using AWS Iam Role named as “akhil”. You can refer to the below link to create an Iam Role.

Now, we are ready to go to create our Infrastructure, Firstly I am taking “awsrds” folder for explanation. Below is the code for this module:

→ In this, Firstly we have to tell about provider which in our case is “aws”,

→ Then, I have given VPC, which is default in our case.

→ Then is creation of Security Group:- In this, I have given Security Rule which allows 3306 port in ingress block, ingress block defines that whatever traffic comes to this firewall/security group, can only access the ports specified. Egress block defines, from where traffic can come, here, I have given from anywhere, traffic can come.

security group

→ Now, we can attach this security group to our RDS Resource, this resource is basically called as aws_db_instance :

DB Instance

In this, I have given several attributes, like

  • identifier — It is the name of our DB Instance
  • engine and engine_version can be used according to used cases, but in my case, I have used “Mysql”.
  • name — This is the name of our database, on which wordpress will act as frontend.
  • username — This is the name of user which can use our database. In my case, I have given this username through a variable in variables.tf and value given will be “admin”.
  • password — This is the password for user “admin”.
  • instance_class — This is the type of Instance we want, as t2.micro is free, so I am using this.
  • publicly_accessible — This is given as “true”, beacuse we are accessing this Database from outside of given VPC, if we set it to false, it becomes privately accessible, and only instances from the same VPC can access this.
  • vpc_security_group_ids — Using this, I have attached my Security Group to this resource.

— I have given depends on keyword, as we want when Security Group is created, then only this database must create.

To use variables as I used in db_instance, we have to initialize variable in the file, I have initialized these 2 variables (rdsusername and rdspasswd) at top of our file.

— I have created 2 variables also in this file.

  • These are created as these will be used in my other module, which is kubernetes module for wordpress pod. We are exporting these value which means that, when our database will be created, we get many values which are exported, but we are taking only these 2 (endpoint and name).
  • endpoint — This is the final URL of our database , which will help our wordpress pod to connect to this database.
  • name — This is the name of our database.

Now, our AWS module is ready, and now we can come to Kubernetes module.

→ In this also, firstly we have to giveour provider, it can be anywhere in public cloud, but here it is on my Local System, and on top of VM “minikube”.

→ Similarly, as I have used variables in AWS module, we are using some variables here also.

→ These variables will fetch the following, as these will be given to our pod environmental variables, we can give this manually also, but our usedcase is for pure automation, that can be possible :

  • rdsusername” — username of our database, which is “admin”
  • rdspasswd” — Password for our user
  • rdsendpoint” — URL of our database(this is same as we can give address,but domain name has more advantages)
  • rdsdbname” — Name of our database
  • port” — This is the port on which our Wordpress Pod will be running.

→ As I have to now create Pod on minikube, so we need to start the VM (Minikube), so we can do it with our code, as shown below :

  • Provisioner is used for this, and as we are doing command locally, so “local-exec” provisioner is used, and then in command, we can write, what we want, here command is “minikube start”.

→ Now, setup is ready to create our pod.

Kubernetes service — NodePort

— This is the Kubernetes Service “Nodeport”, which will expose port 80 and add our given port to the pod, through which we can use our pod.

→ Now, we just want to create our pod.

replication controller (a)
replication controller (b)

— This is the code of our Replication Controller, which will create wordpress pod.

— Reason for using Replication controller is that, it helps us in AutoScaling pods, if 1 pod fails, it will automatically create a new pod for us with same desired configurations. We can set number of desired pods, which are known as “replicas”.
- MetaData block contains the name and labels, which are used by replication controller to undertake the pods.
- Desired Configurations are written in the Template Block, which contains specifications like container details (such as container name, image of container which will be pulled either locally or from docker hub, port of container), environmental variables(these are given in image description on docker hub as used in Fig. Replication Controller(b), Each variable must be written in env{} block).
Here, I have given image as “wordpress:latest”, which will pull the latest version of image from docker hub.

— For more detailed explanation, you can refer to my ReplicaSet Article.

→ Now, last step is left, which is to call the modules and then associate the values of variables.

main file(a)

This is the Syntax of a module to define and call using the source attribute.
In Source, we have to tell about the location of the folder/module. Within the module block only, we have to assign the values of variable which the module will use, and some of these variables are defined in variables.tf file, and some of the variables are exported by a resource.
eg:-
-rdsusername, rdspasswd are variables written in variables.tf and will be associated to the module’s variable, that’s what I have done. Here, in my case, I have bychance given the same name of module’s variables and local variables.
- rdsendpoint, rdsdbname are exported variables which are defined in kubernetes module block

main file (b)

This is the null resource which will depend on kubernetes module and will run the following the command written which will open a tab and run this IP:Port. The IP Address given in this command is of my Minikube VM.

So, Our Code is ready now for deployment.

→ Use #terraform init command in all 3 folders : “task6”, “awsrds” and “k8swp”, to install plugins and call the modules.

Terraform Init output

To deploy the code, Use command from location of the main file (autoapprove will not ask for confirmation):

# terraform apply — auto-approve

output of terraform apply command
Creation Complete

NOTE : DB_Instance will take around 4 minutes to create.

Outputs from AWS Console :

Default VPC
Security Group created
DB Instance creating
DB Instance Created

Outputs of Kubernetes Deployments:

Nodeport service created
Replication Controller Created
Wordpress Pod Created

Outputs from Wordpress WebpagePage :

Automatically opened by Nullresource
This form has to be filled by us and then “begin installation”.
User has successfully created by us and now we can login.
We can login now, and we will be redirected to our desired page
Dashboard

→ We will be creating a post, from this wordpress page and check in the database, whether the page is saved in our database or not.

  • For this, see left side menu, click on posts and then on new posts.
  • Empty Page will come, and we can write anything in that, so i have written the following content :
New Post
  • Click on “Publish” on top right corner, as then only, our data will be saved on DB Instance.
Post is Published :)

Now, Lets check that if our data is stored there or not :

For this, you must have installed “mysql” on your system and must add to Environmental Variables of System by adding it to the path.

Environment Variables (Edit in Path)
mysql path in my case

→ So, now lets connect to our database :

We have to use domain name URL/Endpoint and with this username and passwd as shown in command
our desired database is “poddb”
As we want that our post is saved or not, so desired table is “wp_posts”
to see data of complete table, data is too much in this, but we can find the lines that we have written in post
As, you can see that both the lines are there which we write in our post
Exited mysql

→ Now we can destroy our infrastructure also with 1 command only:

# terraform destroy — auto-approve

→ We can verify that all the resources which were created have been destroyed.

RDS Database is being deleted
Nothing is there

Now, this post ends here, to see the code, or use it, you can refer to the Github URL mentioned in below section.

“I have practiced and gained all knowledge of this project(task) under the mentorship of Mr. VIMAL DAGA Sir during the Hybrid Multi Cloud Training by Linux World India.”

I hope this article is Informative and Explanatory. Hope you like it !!!

For any suggestions or if any reader find any flaw in this article, please email me to “akhileshjain9221@gmail.com”

Thank You Readers, for viewing this !!!

--

--

Akhilesh Jain

I am a student and persuing under graduation in computer science and engineering.