Deploy the Wordpress application on Kubernetes and AWS using terraform

Deploy the Wordpress application on Kubernetes and AWS using terraform

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 .

4. The Wordpress application should be accessible from the workstation deployed on Minikube.

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.

What is Minikube?

Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a Virtual Machine (VM) on your laptop for users looking to try out Kubernetes or develop with it day-to-day.

Prerequisites:

  1. Terraform must be installed in the system.
  2. kubectl(client program for Kubernetes cluster)
  3. Installed Minikube: You have to must install minikube before doing this task.
  4. AWS Account for launching the RDS database by using terraform.

Practical Implementation:

CONFIGURING AWS:

aws configure --profile <profile_name>

STARTING MINIKUBE:

minikube start
No alt text provided for this image

Configuring Kubernetes Cluster:

provider "kubernetes" {
config_context_cluster  = "minikube"
}
resource "kubernetes_deployment" "wordpress" {
metadata {
name = "wordpress"
}
spec {
replicas = 1
strategy {
type = "RollingUpdate"
}
selector {
match_labels = {
type = "cms"
}
}
template {
metadata {
labels ={
type = "cms"
}
}
spec {
container {
image = "wordpress"
name  = "wordpress"
port{
container_port = 80
}
}
}
}
}
}
resource "kubernetes_service" "Nodeport" {
depends_on=[kubernetes_deployment.wordpress]
metadata {
name = "terraform-example"
}
spec {
type = "NodePort"
selector = {
type = "cms"
}
port {
port = 80
target_port = 80
protocol = "TCP"
}
}
}

Creating Security Group for RDS:


resource "aws_security_group" "rds_sg" {

  name        = "rds-sg"

  description = "SG for RDS"

  vpc_id      = var.vpc_id

 

  ingress {

    description = "Database Rule"

    from_port   = 3306

    to_port     = 3306

    protocol    = "tcp"

    cidr_blocks = ["0.0.0.0/0"]

  }

}

Launching AWS Database i.e RDS:

resource "aws_db_instance" "wp_db" {

  engine            = "mysql"

  engine_version    = "5.7.21"

  identifier        = "mysql-db"

  instance_class    = "db.t2.micro"

  storage_type      = "gp2"

  allocated_storage = 20

  publicly_accessible = true

  vpc_security_group_ids = [aws_security_group.rds_sg.id]

  port = 3306

  name = var.db_name

  skip_final_snapshot = true

  final_snapshot_identifier = "mysqlfinalsnp"

 

  auto_minor_version_upgrade = false

    depends_on = [

    aws_security_group.rds_sg,  ]

}

 

// Database Host address

output "db_host" {

    value = aws_db_instance.wp_db.address


}

Creating Deployment for wordpress:

resource "kubernetes_deployment" "wp-dep" {
metadata {
name   = "wp-dep"
labels = {
Country = "India"
}
}
depends_on = [
kubernetes_persistent_volume_claim.wp-pvc
]
spec {
replicas = 1
selector {
match_labels = {
pod     = "wp"
env     = "Production"
Country = "India"        
}
}

template {
 metadata {
 labels = {
 pod     = "wp"
 env     = "Production"
 Country = "India" 
 }
 }
spec {
 volume {
name = "wp-vol"
 persistent_volume_claim {
  claim_name = kubernetes_persistent_volume_claim.wp-pvc.metadata.0.name
}
 }
 container {

  image = "wordpress"

   name  = "wp-container"

    env {

 name  = "WORDPRESS_DB_HOST"

 value = var.db_host


   env {

     name  = "WORDPRESS_DB_USER"
     value = var.db_user
  }
 env {
 name  = "WORDPRESS_DB_PASSWORD"
  value = var.db_pass
 }
  env{
  name  = "WORDPRESS_DB_NAME"
  value = var.db_name
  }

 env{

     name  = "WORDPRESS_TABLE_PREFIX"
  value = "wp_"
  }

  volume_mount {
 name       = "wp-vol"
  mount_path = "/var/www/html/"
 }
 port {
  container_port = 80
  }
 }
}
}
}
}

Creating Load balancer Service for WordPress Pods:

resource "kubernetes_service" "wpService" {
metadata {
name   = "wp-svc"
 labels = {
env     = "Production"
Country = "India"
}
 } 
depends_on = [
kubernetes_deployment.wp-dep
]
spec {
 type     = "NodePort"
selector = {
pod = "wp"
}
port {
 name = "wp-port"
 port = 80
 }
 }
}

//Output LoadBalancer IP

output "wp_node_port" {

  value = kubernetes_service.wpService.spec.0.port.0.node_port



Now run the following commands :

  • terraform init
  • terraform approve
  • terraform plan
  • terraform apply
No alt text provided for this image

Here we see the output Wordpress is launched Successfully .

No alt text provided for this image

So, this was the whole setup. Finally completed the task - 6 of Hybrid Multi Cloud Training under the mentorship of Mr. Vimal Daga Sir .

THANKS FOR READING !!


To view or add a comment, sign in

More articles by Rahul Rastogi

  • Private subnet using NAT gateways

    Description of task: 1. Write an Infrastructure as code using terraform, which automatically create a VPC.

    2 Comments
  • Launch AWS EFS Application using Terraform

    TASK DESCRIPTION : Setup Elastic File System using IAAS (Infrastructure as a Service) i.e Terraform.

    2 Comments
  • Launching VPC on AWS through Terraform

    We have to launch following resources through Terraform: 1. Create a VPC using terraform code.

    2 Comments
  • Multi Node Kubernetes Cluster

    In this blog I will try to show "How to setup Multi Node Kubernetes Cluster " in a very detailed manner. We have to…

  • Face Recognition using Transfer Learning

    Task-4 Let us first understand the concept of Transfer Learning from the basis and then we will see how it works. WHAT…

  • Task-1 "Launching Application using Terraform"

    This is my first task of Hybrid Multi Cloud training cum internship under the mentorship of VIMAL DAGA SIR. Task…

    4 Comments
  • What is MACHINE LEARNING?

    "Machine learning is closely related to computational statistics, which focuses on making predictions using computers."…

    2 Comments
  • Project Description

    MY FIRST DEVOPS PROJECT TASK The task is to make integration between different tools(Git Bash,Github,Jenkins,Docker)…

    6 Comments

Others also viewed

Explore content categories