Deploying wordpress on kubernetes using terraform with RDS

Task overview:- In this task, we have to deploy a WordPress application on Kubernetes and use rds for the database using terraform.

Image for post

Services used:

Kubernetes:-Kubernetes is an open-source container orchestration system for automating computer application deployment, scaling, and management. It aims to provide a "platform for automating deployment, scaling, and operations of application containers across clusters of hosts". It works with a range of container tools, including Docker.

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

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

Step 1:write infrastructure as code using terraform which automatically deploy WordPress application on Kubernetes

code will launch a deployment having two pods each having a WordPress site running also deployment will watch the pods and ig due to any reason pod will go down it will launch another pod

provider "kubernetes" {}



resource "kubernetes_deployment" "word_launch" {
  metadata {
    name = "my-word"
    labels = {
      test = "myword"
    }
  }


  spec {
    replicas = 2


    selector {
      match_labels = {
        test = "myword"
      }
    }


    template {
      metadata {
        labels = {
          test = "myword"
        }
      }


      spec {
        container {
          image = "wordpress"
          name  = "myword"
        }
      }
    }
  }
}

Step2: now we expose the deployment for outside connectivity and also will only allow connecting to 80 port which is port for the WordPress site

resource "kubernetes_service" "wordlb" {
  metadata {
    name = "wordlb"
  }
  spec {
    selector = {
      test = "${kubernetes_deployment.word_launch.metadata.0.labels.test}"
    }
    port {
      port = 80
      target_port = 80
    }


    type = "NodePort"
  }}

No alt text provided for this image

Now we write terraform code which will help to get URL by which we can access the WordPress site

resource "null_resource" "url" {
 provisioner "local-exec" {
  command = "minikube service list"
 }
 
depends_on = [ kubernetes_service.wordlb ]


}

No alt text provided for this image

Step3: Now we use the RDS service of aws for relational database for WordPress application

Now the below code will launch the RDS service of aws which has MySQL engine type, we have to provide DB name, username, password so that we can connect also we have to make MySQL publically accessible so that WordPress running locally on minikube can connect to MySQL running on aws ..also before all these, we have to create a security group that allows port 3306 for MySQL and gives this sg to RDS.

provider "aws" {
  region     = "ap-south-1"
  profile    = "vishnu1"
}
resource "aws_security_group" "allow_sql" {


  name        = "allow_sql"
  description = "Allow sql inbound traffic"
  vpc_id      = "vpc-f348579b"


  ingress {
    description = "sql from VPC"
    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"]
  }


  tags = {
    Name = "allow_sql"
  }
}


resource "aws_db_instance" "mysql" {
  identifier = "database-sql"
  allocated_storage    = 20
  storage_type         = "gp2"
  engine               = "mysql"
  engine_version       = "5.7.30"
  instance_class       = "db.t2.micro"
  name                 = "mydb"
  username             = "vishnu1"
  password             = "******"(//type yours)
  parameter_group_name = "default.mysql5.7"
  iam_database_authentication_enabled = true
  publicly_accessible = true
  skip_final_snapshot = true
  vpc_security_group_ids = [ "${aws_security_group.allow_sql.id}"]}

No alt text provided for this image

No alt text provided for this image

Now we write terraform code that will give us the DNS name of MySQL which we will use to connect the database host with the WordPress site

output "ip" {
value = "${aws_db_instance.mysql.address}"
}

output dns name:database-sql.cyeo3hg5qkhr.ap-south-1.rds.amazonaws.com

No alt text provided for this image

Now using the URL we go to the WordPress site and give database name, username, password, and hostname to launch the site.



After doing everything correctly we get our WordPress site launched in conclusion we have successfully deployed a WordPress site on Kubernetes and use aws RDS as a database

Thank You So Much!!!



Comments

Popular posts from this blog

Task - 4 DevOps Assembly Lines

🎴Layout, ⚡ Hot Reload in Flutter!!!