Deployment Of Wordpress On Kubernetes With AWS RDS Using Terraform

Problem Statement

Deploy the Wordpress application on Kubernetes and AWS using terraform including the following steps;

Let's get started...

In this task, I have written Infrastructure as code using Terraform, which automatically deploy the Wordpress application. On AWS, I have used RDS service for the relational database for Wordpress application. Then I deployed the Wordpress as a container on top of Minikube, the Wordpress application is accessible to the public world.

What is Kubernetes?

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services. On K8S (short for Kubernetes) we create containers and launch our application on top of these containers. Containers provide isolation and everything is managed by K8S.

What is the need of Kubernetes?

Containers are a good way to bundle and run your applications. In a production environment, you need to manage the containers that run the applications and ensure that there is no downtime. For example, if a container goes down, another container needs to start. This is where Kubernetes helps us. Everything is managed by Kubernetes itself. We just need to specify the templates of the container and Kubernetes will do its job.

What is AWS-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.

terraform init - To install the required plugins 
terraform apply - To make the resources run
terraform plan - is used to create an execution plan
terraform validate - To check the code
terraform destroy - To destroy all the resources in single click

Step1

Creating the separate folder for web page code and in that create terraform file with extension .tf and after initializing the terraform file so that it can download the required plugins for that particular folder. We have to log in to AWS CLI

Step2

We need to specify the region and profile name for setting up the provider in Terraform. Which logins to the AWS account to perform actions. And Also we need to start our Minikube VM.

provider "aws"{
region = "ap-south-1"
shared_credentials_file = "C:/Users/satvikakolisetty/Downloads/new_user_credentials.csv"
profile = "satvi"
}
provider "kubernetes" {
config_context_cluster = "minikube"
}

Step3

Now we need to write the code for deploying Wordpress on Minikube. We will also be creating a Load Balancer service on Kubernetes which will expose our deployment.


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"
}
}

Step 4

Now we need to create our Database where the WordPress application will store all its data. For this, we are using AWS-RDS.

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 = "satvi"
password = "root22"
iam_database_authentication_enabled = true
parameter_group_name = "default.mysql5.7"
skip_final_snapshot = true
publicly_accessible = true
tags = {
Name = "sqldb"
}
}
resource "null_resource" "minikubeservice" {
provisioner "local-exec" {
command = "minikube service list"

}
depends_on = [
kubernetes_deployment.wordpress,
kubernetes_service.wordpresslb,
aws_db_instance.mydb
]
}
terraform plan
terraform plan
terraform apply --auto-approve
kubectl get all
minikube service list