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

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.

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 .

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

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

Then,

Now for checking our plan:

kubectl get all

Thank You…….