Using Terraform for deploying WordPress Site on Kubernetes Pod while using MySQL from the AWS RDS as the backend for it

Using Terraform for deploying WordPress Site on Kubernetes Pod while using MySQL from the AWS RDS as the backend for it

In this article we will be creating an Infrastructure over the cloud which deploys the complete WordPress site auto-scaled with the help of Kubernetes while providing MySQL as the backend to it. The MySQL database is also created on cloud using one of the database management service of AWS cloud called as RDS. We will also automate the complete code with the help of Terraform.

Prerequisites about the technologies used-

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.

AWS 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.

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.

Let's begin-

STEP 1 - Creating Single-node Kubernetes cluster using minikube

Kubernetes clusters can be either Single-node or Multi-node.Here,we will be using minikube for automatically creating a single-node Kubernetes cluster. Here we will create just a Single-node cluster and start it with the help of running the following commands in the windows cmd.

minikube start --vm-driver virtualbox
minikube start

STEP 2 - Giving the AWS credentials and kubernetes cluster configuration context

In order to use any service of the Terraform we need to provide the details of the service providers so that it can run the commands respective to it and automate the process. Upon providing the provider details for AWS and Kubernetes it will download the respective plugins enabling us to use them.

provider "aws" {
  region  = "ap-south-1"
  profile = "mansi"
}
provider "kubernetes" {
  config_context_cluster   = "minikube"
}

STEP 3 - Creating a security group for RDS allowing MySQL access

In order to have accessibility to the AWS RDS we need to create a security group that allows port 3306 in the ingress rules of the security group.

 resource "aws_security_group" "sql_sg" {
  name        = "mysqlsg"
  description = "Allow inbound traffic to MySQL"
  vpc_id      = "vpc-5de8f535"
ingress {
    description = "Allow MySQL"
    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 = "rds_mysql"
  }
}

STEP 4 - Creating AWS RDS Instance

Now we will be creating an instance in the AWS RDS which has the engine type MySQL and allows public access so that we can the WordPress container running in the public network can easily access it.

resource "aws_db_instance" "mysql" {
  
  allocated_storage    = 20
  storage_type         = "gp2"
  engine               = "mysql"
  engine_version       = "5.7.30"
  instance_class       = "db.t2.micro"
  name                 = "mysql_wordpress"
  username             = "admin"
  password             = "mansi09879879"
  parameter_group_name = "default.mysql5.7"
  publicly_accessible = "true"
  port                = "3306"
  vpc_security_group_ids= ["${aws_security_group.sql_sg.id}",]
  final_snapshot_identifier = "false"
  skip_final_snapshot = "true"
}

STEP 5 - Creating Kubernetes Deployment with WordPress as container image

After successful deployment of MySQL on AWS RDS now we can deploy the WordPress container in the Kubernetes. The container will be created with the help of WordPress image in Kubernetes and the number of replicas which are specified will always be maintained by Kubernetes as it a monitoring tool it auto - scales the WordPress pods and keeps the desired number of pods running. We also provide some labels at the time of launching pods for distinction and easy management of pods.

resource "kubernetes_deployment" "WordPress_deploy" {
    depends_on = [aws_db_instance.mysql]
  metadata {
    name = "wordpress"
    
  }

spec {
    replicas = 1

    selector {
      match_labels = {
        app = "wordpress"
      }
    }

    template {
      metadata {
          name = "wp-pod"
          labels = {
             app = "wordpress"
            }
       }

      spec {
        container {
          image = "wordpress:4.8-apache"
          name  = "wp_cloud"
         
         
        }
      
    }

STEP 6 - Creating NodePort service for exposing WordPress server

NodePort is used for exposing the WordPress pod to the public network through the port 80 as by default the pods are isolated and cannot be accessed. This service automatically redirects the user to the WordPress pod. NodePort also uses labels for determing the target pods.

 resource "kubernetes_service" "service" {
    depends_on = [kubernetes_deployment.WordPress_Deploy]
  metadata {
    name = "wp-service"
  }
    spec {
    selector = {
      app = kubernetes_deployment.WordPress_deploy.metadata.0.labels.app
    }
    
    port {
      port        = 8080
      target_port = 80
    }

    type = "NodePort"
    } 
      
    }

STEP 7 - Terraform init and Terraform apply

Finally we will automatically launch the complete infrastructure using the Terraform aplly command. Before running the Terraform apply command we need to run the Terraform init command so that it get the desired plugins of the providers.

terraform init
terraform apply

Finally when the complete infrastructure is launched we get the installation page of the WordPress on the minikube node IP and port number provided by the NodePort.

No alt text provided for this image

We can also access the dashboard upon successful login.

No alt text provided for this image

We successfully created the complete infrastructure automatically using Terraform.


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

Mansi Gautam的更多文章

社区洞察

其他会员也浏览了