Deployment of Web Application On Local Kubernetes Cluster by Integrating with AWS RDS using Terraform.

Abhishek Sharma
5 min readSep 5, 2020

What is kubernetes?

Kubernetes is open source software that allows you to deploy and manage containerized applications at scale. Kubernetes manages clusters of Amazon EC2 compute instances and runs containers on those instances with processes for deployment, maintenance, and scaling. Using Kubernetes, you can run any type of containerized applications using the same toolset on-premises and in the cloud.

About AWS RDS Amazon Relational Database Service (Amazon RDS)

It is a web service that makes it easier to set up, operate, and scale a relational database in the AWS Cloud. It provides cost-efficient, resizable capacity for an industry-standard relational database and manages common database administration tasks.

🔰 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.

⏩ In these task we are going to automate the deployment of Web Application by Infrastructure As A Code using terraform code which integrates the local kubernetes cluster with AWS RDS .

Task Begins:

As we have to write Infrastructure As A Code using terraform which automatically deploy WordPress Application on Kubernetes Cluster . In my case I go for local Kubernetes Cluster as Workstation i.e Minikube .

This the code for launching the Wordpress Application.

resource "null_resource" "minikubestart" {
provisioner "local-exec" {
command = "minikube start"
}
}
provider "kubernetes" {
config_context_cluster = "minikube"
}
resource "kubernetes_deployment" "wordpress" {
metadata {
name = "wp"
}
spec {
replicas = 3
selector {
match_labels = {
env = "production"
region = "IN"
App = "wordpress"
}
match_expressions {
key = "env"
operator = "In"
values = ["production" , "webserver"]
}
}
template {
metadata {
labels = {
env = "production"
region = "IN"
App = "wordpress"
}
}
spec {
container {
image = "wordpress:4.8-apache"
name = "wp"
}
}
}
}
}
resource "kubernetes_service" "wordpresslb" {
metadata {
name = "wplb"
}
spec {
selector = {
app = "wordpress"
}
port {
protocol = "TCP"
port = 80
target_port = 80
}
type = "NodePort"
}
}

In above terraform code use of Replica Set which defines with fields, including a selector that specifies how to identify Pods it can acquire, a number of replicas indicating how many Pods it should be maintaining, and a pod template specifying the data of new Pods it should create to meet the number of replicas criteria. A ReplicaSet then fulfills its purpose by creating and deleting Pods as needed to reach the desired number. When a ReplicaSet needs to create new Pods, it uses its Pod template.

Use of kubernetes service to acquire the facility of Load Balancer and to expose the deployment of Web Application to public world access . Here I use the service of kubernetes named as Node Port which works as a Load Balancer but is not external Load Balancer like Elastic Load Balancer in AWS .We can also make the use of ELB for these service .

Another file for creating the RDS on top of AWS.

Now we have to create back end for WordPress that is database . It is always critical to manage the database of any web application for that we move towards use of Database As A Service provided by AWS Cloud called as Relational Database Service (RDS).

provider "aws" {
region = "ap-south-1"
profile = "testing"
}
resource "aws_db_instance" "mydb" {
allocated_storage = 20
identifier = "dbinstance"
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7.30"
instance_class = "db.t2.micro"
name = "mydb"
username = "abhishek"
password = "abhi858585"
iam_database_authentication_enabled = true
parameter_group_name = "default.mysql5.7"
skip_final_snapshot = true
publicly_accessible = true
tags = {
Name = "sqldb"
}
}

Next file is for providing the IP to connect to Wordpress:

resource "null_resource" "minikubeservice" {
provisioner "local-exec" {
command = "minikube service list"

}
depends_on = [
kubernetes_deployment.wordpress,
kubernetes_service.wordpresslb,
aws_db_instance.mydb
]
}

For Deploying the whole setup in a single command we have to do this:

Step1: # terraform init

Then,

Step2: #terraform validate

Now for checking our plan:

Step3: #terraform plan

So , After knowing the Plan . Here , We can set-up the Complete Infrastructure in just a single-click .

Step4: #terraform apply — auto-approve

We can see in AWS GUI Console one MySQL database is created in RDS →

Now we can see the deployment of WordPress Application is created on local kubernetes cluster we use named as Minikube .

To see everything in Kubernetes cluster is properly deployed or not using command →

kubectl get all

Using the IP address launch the WordPress Application and enter the details of RDS .

Here the Wordpress is launched

Thank You…….

--

--