Deploy the Wordpress application on Kubernetes and AWS using terraform

Deploy the Wordpress application on Kubernetes and AWS using terraform

This is my task-6 of Hybrid Multi Cloud.

Task-Description:-

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.

Now, Let's start with practical part.

Prerequisite:

  1. Create aws profile and configure.
  2. Terraform install
  3. Minikube install

It will launch the Wordpress Application.

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

It will create the RDS on top of AWS.

provider "aws" {
  region = "ap-south-1"
  profile = "mannu-IAM"
}
resource "aws_db_instance" "mydb" {
  allocated_storage    = 20
  identifier           = "db-instance"
  storage_type         = "gp2"
  engine               = "mysql"
  engine_version       = "5.7.30"
  instance_class       = "db.t2.micro"
  name                 = "mydb"
  username             = "mannu"
  password             = "root12345678"
  iam_database_authentication_enabled = true
  parameter_group_name = "default.mysql5.7"
  skip_final_snapshot  = true
  publicly_accessible = true
  tags = {
    Name = "sqldb"
  }
}

It will provide 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

]

}

When we run this RDS will be created on top of AWS.

No alt text provided for this image

In Order to Build the Complete Infrastructure, At first we have to initialize Terraform

Run command terraform init.

No alt text provided for this image

now run terraform validate.

No alt text provided for this image

Now to deploy the whole infrastructure run terraform apply.

No alt text provided for this image

now -minikube service list

No alt text provided for this image

To check if the deployment is running fine or not- kubectl get all

Using the IP address launch the WordPress Application and enter the details of RDS .

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

Thank you for reading and giving your time.







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

Mansi Bhandari的更多文章

社区洞察

其他会员也浏览了