Deploying WordPress application on Kubernetes with AWS RDS using terraform

Deploying WordPress application on Kubernetes with AWS RDS using terraform

>>>>The Objective of the task was to<<<<<

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.

Amazon Relational Database Service (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. It frees you to focus on your applications so you can give them the fast performance, high availability, security, and compatibility they need. Amazon RDS is available on several database instance types - optimized for memory, performance, or I/O - and provides you with six familiar database engines to choose from, including Amazon Aurora, MySQL, Oracle Database, etc..

Now's Let jump towards the practical

The easiest way to configure the provider is by creating/generating a config in a default location (~/.kube/config). That allows you to leave the provider block completely empty.

provider "kubernetes" {}

 

resource "kubernetes_service" "example" {

  metadata {

    name = "wp-service"

    labels = {

      app = "wordpress"

    }

  }

  spec {

    selector = {

      app = "wordpress"

      tier = "frontend"

    }

  port {

    node_port   = 30000

    port        = 80

    target_port = 80

  }

  type = "NodePort"

 }

}

 

 

resource "kubernetes_persistent_volume_claim" "pvc" {

  metadata {

    name = "wp-pvc"

    labels = {

      app = "wordpress"

      tier = "frontend"

    }

  }

 

 

  spec {

    access_modes = ["ReadWriteMany"]

    resources {

      requests = {

        storage = "5Gi"

      }

    }

  }

}

resource "kubernetes_deployment" "wp-dep" {

  metadata {

    name = "wp-dep"

    labels = {

      app = "wordpress"

      tier = "frontend"

    }

  }

  spec {

    replicas = 2

    selector {

      match_labels = {

        app = "wordpress"

        tier = "frontend"

      }

    }

   

    template {

      metadata {

        labels = {

          app = "wordpress"

          tier = "frontend"

        }

      }

      spec {

        volume {

          name = "wordpress-persistent-storage"

          persistent_volume_claim {

            claim_name = kubernetes_persistent_volume_claim.pvc.metadata.0.name

          }

        }

        container {

           image = "wordpress"

           name  = "wordpress-container"

           port {

             container_port = 80

           }

           volume_mount {

             name = "wordpress-persistent-storage"

             mount_path = "/var/www/html"

           }

        }

      }

    }

  }

}

The above file will deploy the WordPress application.

and now we will run the above file using terraform, the commands for the same is

terraform init, terraform plan, terraform apply

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

We can see that terraform file is executed properly without any error now will check if the pods are running by kubectl get pods and kubectl get all -o wide commands

We can see that the container is running properly and exposed to port 30000 so if any user from the outside world wants to access the website he can use my minikube ip:30000 to access the website. Now we will deploy the RDS database for our WordPress application in AWS and collect all the information like database name password and username to login to WordPress.

provider "aws" { 

region  = "ap-south-1" 

profile = "Shailly"

}

 resource "aws_db_instance" "default" {

 

  allocated_storage    = 20

  storage_type         = "gp2"

  engine               = "mysql"

  engine_version       = "5.7"

  instance_class       = "db.t2.micro"

  name                 = "RDS"

  username             = "shailly"

  password             = "shailly1123"

  parameter_group_name = "default.mysql5.7"

 

 skip_final_snapshot = true
tags = {

Name = "shah"

}

 }

output "dns" {

 

value = aws_db_instance.default.address

 }



The above terraform code for RDS will create RDS in AWS I have provided the database name username and password which we will need while login to WordPress.

Now we will run the RDS file by using terraform

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

As we can see our terraform code has been executed successfully so now we will go to AWS and check our Amazon RDS database.

No alt text provided for this image

Connecting RDS to Word Press

finally, we can connect the WordPress application to our RDS database by providing the username, password, database name, and database host. Now go to google chrome and type the minikube ip:30000 to launch the WordPress application.

No alt text provided for this image


No alt text provided for this image

We can see that WordPress is connected with our RDS database in AWS and our website is working fine.

Thank you !!!

Srishti Jain

DevOps Engineer || Azure 1x || CI/CD || AKS || Jenkins || Kubernetes || Git

4y

Well done!

Ambesh Sharma

Senior Experience Engineer @ Publicis Sapient | Ex - Infosys | Front-end Engineer | Javascript | Typescript | React JS | Node JS

4y

Well done 👍

Aman Pandey

Software Developer at Evince Development | PHP | WordPress | Shopify Developer

4y

Congratulation

Vishal Bhati

ServiceNow Certified Implementer | CSA| CAD |5x-Micro Cert's

4y

Well done..Shaily

To view or add a comment, sign in

More articles by Shailly Shah

  • Configuring Docker With Ansible(RH294)

    Configuring Docker With Ansible(RH294)

    Ansible(RH294) Task 1: Write an Ansible PlayBook that does the following operations in the managed nodes: 🔹 Configure…

Insights from the community

Others also viewed

Explore topics