Application on Kubernetes and AWS(using Terraform,AWS RDS)

Sandeshjain
5 min readSep 14, 2020

Application on Kubernetes and AWS using terraform :

1.Write an Infrastructure as code using terraform, which automatically deploy the Wordpress application

2. On AWS, use RDS service for the relational database for Wordpress application.

3. Deploy the Wordpress as a container either on top of Minikube or EKS or Fargate service on AWS

4. The Wordpress application should be accessible from the public world if deployed on AWS or through workstation if deployed on Minikube.

Prerequisites:

AWS Account(& IAM)

AWS CLI

Minikube-Minikube is a tool that makes it easy to run Kubernetes locally.

Kubectl(This is a client program to contact to Kubernetes),Here we have setup our minikube vm on top of Virtual Box

minikube start --driver=<virtualbox>

Here we are using virualbox as our driver to communicate b/w the OS and the device:

Providing the profile:

VPC(Virtual private cloud in AWS,where we want to launch our RDS.

data "aws_vpc" "default" {
default= true
}

data "aws_subnet_ids" "all" {
vpc_id= data.aws_vpc.default.id
}

A relational database is a type of database that stores and provides access to data points that are related to one another.

RDS service for the relational database for Wordpress application.

★AWS RDS is a managed relational Database web service provided by AWS.It provided the 11–9’s guarantee that our data will be safe in its different availability zones.If any AZ fails,then it has recovery and backup option which will automaitically backup the data,also we have a option to make copies by creating the snapshots.

★We can use the database which we are familiar with:MySQL, MariaDB, PostgreSQL, Oracle, Microsoft SQL Server.

So here we are using MySQL Service provided by AWS RDS.

✔Set the user,password,name of the database,this is very important otherwise we won’t we able to access the wordpress.

Deploying the wordpress container on the top of Minikube:

We want to launch the wordpress on the top of Minikube

Provider is the Kubernetes:

provider "kubernetes" {
config_context_cluster = "minikube"
}

So we will use the Kubernetes deployment is a resource in Kubernetes that provides the regular updates of the pods.We defined in the code replicas=2, which means that our desired no. of pods that should running on a particular time should be 2.So if any of the pods fails due to More load then replica set will keep on trying (behind the scene)to launch a new pod without any downtime.

resource "kubernetes_deployment" "wordpress" {
metadata {
name = "mywp"
}
spec {
replicas = 2
selector {
match_labels = {
env="production"
region="IN"
app="wordpress"
}
match_expressions{
key="env"
operator="In"
values= ["production"]
}
}
template {
metadata {
labels = {
env="production"
region="IN"
app="wordpress"
}
}
spec {
container {
image = "wordpress"
name = "mywp"
}
}
}
}
}

So image that we are using is the wordpress as we want to launch the wordpress app.

selectors let you select Kubernetes resources based on the the values we have defined,as we have mentioned the region here IN->India(we can give any,for our understanding)This is useful in the future,as when we have more no. of pods then using the selectors we can sort them to know about them or edit.

Making the wordpress publicly accessible through the workstation i.e. Minikube

resource "kubernetes_service" "wordpress" {
metadata {
name = "mywordpress"
}
spec {
selector = {
app = kubernetes_deployment.wordpress.spec.0.template.0.metadata[0].labels.app
}
port {
node_port = 30321
port = 80
target_port = 80
}
type = "NodePort"
}
}

Kubernetes service defines a logical set of pods and a policy for accessing the pod set.

So here we are giving the port as 30321 and port->80 to make it publicly accessible.

So our code is ready for the deployment of Wordpress application on top of Kubernetes cluster with the database provided by the AWS RDS service!

terraform init-To initialize or download all the plugins from the terraform registry

terraform validate-Terraform command to check if there is any error in the code.

terraform apply — auto-approve: This command will deploy everything accordingly as we have defined in the code.

Now we can expose our pods to make them publicly accessibly using:

kubectl expose deployment mywordpress — type=LoadBalancer — port=80

Enter the details of database as we defined in the code and use the database endpoint from the aws.

terraform destroy — auto-approve: This command will destroy everything that is created or deployed in the aws.

Feel free to ask any doubt:

Linkedin

𝙏𝙃𝘼𝙉𝙆 𝙔𝙊𝙐 𝙑𝙀𝙍𝙔 𝙈𝙐𝘾𝙃 !!

--

--