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

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

!! ?????????? ?????????????????????? !!

??Welcome you all to my article based on TASK-6 of Hybrid Multi Cloud Computing ??

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

?? Prerequisites:

  1. Terraform must be installed.
  2. AWS cli
  3. kubectl(client program for kubernetes cluster)

??WHY TERRAFORM ??

Terraform provides full lifecycle management of Kubernetes resources including creation and deletion of pods, replication controllers, and services.

Unlike the kubectl CLI, Terraform will wait for services to become ready before creating dependent resources. This is useful when you want to guarantee state following the command's completion. As a concrete example of this behavior, Terraform will wait until a service is provisioned so it can add the service's IP to a load balancer. No manual processes necessary!...

Let's Begin....

Step 1: Configure AWS via CLI

No alt text provided for this image

Here you have to provide your AWS Access key and Secret Access Key...

Step 2: Start the minikube

No alt text provided for this image

What's Minikube????

Minikube is an open source tool that enables you to run Kubernetes on your laptop or other local machine. It can work with Linux, Mac, and Windows operating systems. It runs a single-node cluster inside a virtual machine on your local machine.

Step 3: Lets start write Infrastructure As A Code using Terraform for deployment of WordPress Application on Minikube Kubernetes Cluster .

provider "kubernetes" {
    config_context_cluster   = "minikube"
}
resource "kubernetes_deployment" "webapp" {
  metadata {
    name = "wordpress"
  }
  spec {


    replicas = 3
    
    selector{
    match_labels = {
      env = "Development"
      dc = "IN"
      App = "wordpress"
    }
    match_expressions {
      key = "env"
      operator = "In"
      values = ["Development" , "wordpress"]
    }
  }
   template {
        metadata {
         labels = {
      env = "Development"
      dc = "IN"
      App = "wordpress"
    }
        }


      spec {
        container {
          image = "wordpress:4.8-apache"
          name  = "cont"


        }
      }
    }
}
}
resource "kubernetes_service" "service" {
  metadata {
    name = "loadbalancer"
  }
  spec {
    selector = {
      App = kubernetes_deployment.webapp.spec.0.template.0.metadata[0].labels.App
    }
    port {
      node_port   = 30000 
      port        = 80
      target_port = 80
    }
    type = "NodePort"
} 
}

Save the above code in a file with ".tf" extension, i.e. terraform file.

Here I have used Kubernetes Deployment as a resource because we know the main power of deployment if one of the pod goes down it automatically create another one with same specification just in second...

I have used 3 replicas to control the traffic. According to our deployment 3 pods are always running..if anyone of goes down it automatically launches..Labels are used to tag the pods ...To launch wordpress in containers I have provided wordpress:4.8-apache image....

To expose the pods to end users, provision a service. A service is capable of provisioning a load-balancer in some cloud providers. It can manage the relationship between pods and the load balancer while new pods are launched.

Step 4: Now , we have to run the terraform code

Use this command, before running the code initialize the code and download the necessary plugins of terraform for Kubernetes .

terraform init 

No alt text provided for this image

It's always good to know the general workflow of our code before running the code .Since we have to first see how our works, for this we can use this command??

terraform plan

No alt text provided for this image
No alt text provided for this image

Now, finally to run our terraform code we can use this command??

terraform apply

No alt text provided for this image

We can see whether everything in our Kubernetes cluster is properly deployed or not, using this command??

kubectl get all 

No alt text provided for this image

Step 5: Now , we have to create the Database using Relational Database Service (RDS) using AWS

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

For WordPress we are using MySQL RDS by AWS . Now ,we have to write another terraform code for MySQL RDS which will create one MySQL database using RDS .

Following terraform code is used for creating mysql database....

provider "aws" {
    region = "ap-south-1"
}
resource "aws_db_subnet_group" "default" {
  name       = "main"
  subnet_ids = ["subnet-11b3b479", "subnet-63942a18"]


  tags = {
    Name = "My DB subnet group"
  }
}
resource "aws_db_instance" "sqldb" {
  allocated_storage    = 10
  storage_type         = "gp2"
  engine               = "mysql"
  engine_version       = "8.0.20"
  instance_class       = "db.t2.micro"
  name                 = "sqldb"
  username             = "vishal"
  password             = "mydbPassword123"
  parameter_group_name = "default.mysql5.7"
  db_subnet_group_name = "${aws_db_subnet_group.default.name}"
  publicly_accessible = true
  iam_database_authentication_enabled = true


tags = {
    Name  = "wordpress_mysql_db"
}
}
output "ip" {
  value = aws_db_instance.sqldb.address

  }

Save the above code in a file with ".tf" extension, i.e. terraform file.

Now we need to run the code, let's quickly initialize the code and download the necessary plugins of terraform for Kubernetes .

terraform init

No alt text provided for this image

It's always good to know the general workflow of our code before running the code .Since we have to first see how our works, for this we can use this command??

terraform plan

No alt text provided for this image

Finally, lets run our code using the command below...

terraform apply

No alt text provided for this image
No alt text provided for this image

Let's check if database created Successfully or not in AWS GUI Console...??

No alt text provided for this image
No alt text provided for this image

Step 6: Finally, we can launch the WordPress Application using service URL provided by Node Port of kubernetes cluster

To get the service URL of minikube use the command given below??

minikube service list

No alt text provided for this image

Finally!! Everything is been done. Let's launch the application on browser....

Get started with you blog or site..

No alt text provided for this image

To connect MySQL database created by RDS to WordPress Application enter the appropriate details of database we use for back end as below

No alt text provided for this image

?? Finally I successfully completed the TASK-6 of Hybrid Multi Cloud Computing .

? I would like to thanks Mr.Vimal Daga for giving such challenging task which helps to solve real use cases of Hybrid Multi Cloud Computing .

??To see the complete terraform code, check the GitHub Link - Task-6

!! Thanking you all for visiting my article !!

?? Keep Sharing Keep Learning ??











Rahul Chougule

Product Developer I at ABS | Frontend | React | Redux | JavaScript | Immediate Joiner

4 å¹´

Excellent ?

Deepak Saini

DevOps/SRE Engineer & Cloud Enthusiast | RHCSA | RHCE | SRE | Linux | Ansible | Terraform | Jenkins | AWS | GCP | AZURE | Kubernetes | Docker | Troubleshooting

4 å¹´

Great work Vishal Dhole

Anushka Visapure

Solution-Oriented DevOps Engineer || Skilled in Kubernetes | Terraform | Ansible | Docker | Git and GitHub | GitHub Action || Expanding Capabilities in AWS | GCP | Linux.

4 å¹´

Great ??

Priyanka Dabade

ServiceNow Developer | CAD Certified | German A1 certified ????

4 å¹´

Amazing!!

要查看或添加评论,请登录

Vishal Dhole的更多文ç«