Problem Statement :

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 WordPress as a container either on top of Minikube.

4. The WordPress application should be accessible from the public world.


RDS-

Amazon RDS is a service which provides database connectivity through the Internet. RDS makes it very simple and easy to set-up a relational database in the cloud.

Instead of concentrating on database features, you can concentrate more on the application to provide high availability, security, and compatibility. RDS is a fully managed RDBMS service.


Kubernetes-

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation.

It allows you to run multiple Virtual Machines (VMs) on a single physical server's CPU. Virtualization allows applications to be isolated between VMs and provides a level of security as the information of one application cannot be freely accessed by another application.

Virtualization allows better utilization of resources in a physical server and allows better scalability because an application can be added or updated easily, reduces hardware costs, and much more. With virtualization you can present a set of physical resources as a cluster of disposable virtual machines.

Each VM is a full machine running all the components, including its own operating system, on top of the virtualized hardware.


Terraform - 

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.

The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.


Prerequisites:

  1. Account on AWS 
  2. AWS CLI should be installed 
  3. Create aim user and profile must me configured in terminal.
  4. kubectl (client program for kubernetes cluster)
  5. Terraform should be installed in our system
  6. minikube should me installed.

Let's begin-


Step 1

Create a .tf file and write code for Service Provider name, profile name and Security Group for RDS.


// Service Provider

provider "aws" {

  region  = "ap-south-1"

  profile = "ankit"

}



// Security Group For RDS

resource "aws_security_group" "rds" {

  name        = "rds"

  description = "Allow mysql inbound traffic"

  ingress {

    from_port   = 3306

    to_port     = 3306

    protocol    = "tcp"

    cidr_blocks = ["0.0.0.0/0"]

  }

  egress {

    from_port   = 0

    to_port     = 0

    protocol    = "-1"

    cidr_blocks = ["0.0.0.0/0"]

  }

}


Step 2

Create DataBase Instance to store Data of Webpage.


// DataBase Instance TO store Data of Website

resource "aws_db_instance" "wp_DB" {

  depends_on             = [aws_security_group.rds]

  allocated_storage      = 20

  storage_type           = "gp2"

  engine                 = "mysql"

  engine_version         = "5.7"

  instance_class         = "db.t2.micro"

  name                   = "wp_DB"

  username               = "ankit"

  password               = "myredhat"

  parameter_group_name   = "default.mysql5.7"

  publicly_accessible    = true

  skip_final_snapshot    = true

  vpc_security_group_ids = [aws_security_group.rds.id]

  tags = {

    name = "wp-mysql"

  }

}


Step 3

Kubernetes service provider, create name space for k8 and Create PVC.


// Kubernetes Service Provider

provider "kubernetes" {

  config_context = "minikube"

}


// NameSpace for K8

resource "kubernetes_namespace" "wpk8" {

  metadata {

    name = "wpk8"

  }

}


// Creating PVC

resource "kubernetes_persistent_volume_claim" "wp_pvc" {

  depends_on = [aws_db_instance.wp_DB]

  metadata {

    name      = "newwordpressclaim"

    namespace = kubernetes_namespace.wpk8.id

  }

  spec {

    access_modes = ["ReadWriteOnce"]

    resources {

      requests = {

        storage = "20Gi"

      }

    }

  }

}


Step 4

Deploy WordPress.


// Launching Wordpress Deploment

resource "kubernetes_deployment" "wordpress" {

  depends_on = [kubernetes_persistent_volume_claim.wp_pvc]

  metadata {

    name      = "wordpress"

    namespace = kubernetes_namespace.wpk8.id

    labels = {

      Env = "wordpress"

    }

  }

  spec {

    replicas = 1

    selector {

      match_labels = {

        Env = "wordpress"

      }

    }

    template {

      metadata {

        labels = {

          Env = "wordpress"

        }

      }

      spec {

        container {

          name  = "wordpress"

          image = "wordpress:4.8-apache"

          env {

            name  = "WORDPRESS_DB_HOST"

            value = aws_db_instance.wp_DB.address

          }

          env {

            name  = "WORDPRESS_DB_USER"

            value = aws_db_instance.wp_DB.username

          }

          env {

            name  = "WORDPRESS_DB_PASSWORD"

            value = aws_db_instance.wp_DB.password

          }

          env {

            name  = "WORDPRESS_DB_NAME"

            value = aws_db_instance.wp_DB.name

          }

          port {

            container_port = 80

          }

          volume_mount {

            name       = "pv-wordpress"

            mount_path = "/var/lib/pam"

          }

        }

        volume {

          name = "pv-wordpress"

          persistent_volume_claim {

            claim_name = kubernetes_persistent_volume_claim.wp_pvc.metadata[0].name

          }

        }

      }

    }

  }

}


Step 5

Now Expose Pod For Public World So that everyone in the world can access our websit.


// Expose Pod for public to access wordpress

resource "kubernetes_service" "expose" {

  depends_on = [kubernetes_deployment.wordpress]

  metadata {

    name      = "exposewp"

    namespace = kubernetes_namespace.wpk8.id

  }

  spec {

    selector = {

      Env = "${kubernetes_deployment.wordpress.metadata.0.labels.Env}"

    }

    port {

      node_port   = 32123

      port        = 80

      target_port = 80

    }

    type = "NodePort"

  }

}


Now Save the file and run this command ( terraform init ) this command will download all plugins of your code.

The run ( terraform apply ) this command will apply the code.

After some time your infrastructure will be created.

pastedGraphic_6.png


Now to know url run this command in ur terminal ( minikube service list ). This command will give you url to setup your wordpress. In my case url is http://192.168.99.101:32123 Now copy this url and paste it into any browser.


pastedGraphic_7.png


You will get wordpress setup screen. Chose your language and click next.


pastedGraphic_8.png

Now here fill your details.

pastedGraphic_9.png


Now Login

pastedGraphic_10.png

pastedGraphic_11.png


You can also check DataBase from AWS GUI.

pastedGraphic_12.png


Now to destroy whole infrastructure in one command just use this command and hit enter.

( terraform destroy -auto-approve ) In some time whole infrastructure will be destroyed.

pastedGraphic_13.png


Thanks For reading this article Hope my article helped you a loat.

Give Your Feedback please.