Deploying the WordPress application on Kubernetes and AWS using Terraform and AWS-RDS.

Deploying the WordPress application on Kubernetes and AWS using Terraform and AWS-RDS.

What is 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.

No alt text provided for this image


What is Kubernetes ?

Kubernetes is an open-source container-orchestration system(COS) which basically monitors the Operating System launched using the containerization technology like Docker or Podaman and if the O.S fails due to any reason the Kubernetes relaunches the same O.S with zero latency time.

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.

No alt text provided for this image


What is AWS ?

Amazon Web Services (AWS) is a subsidiary of Amazon that provides on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis. In aggregate, these cloud computing web services provide a set of primitive abstract technical infrastructure and distributed computing building blocks and tools. One of these services is Amazon Elastic Compute Cloud (EC2), which allows users to have at their disposal a virtual cluster of computers, available all the time, through the Internet.

Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform, offering over 175 fully featured services from data centers globally. Millions of customers—including the fastest-growing startups, largest enterprises, and leading government agencies—are using AWS to lower costs, become more agile, and innovate faster.

No alt text provided for this image


What is AWS-RDS ?

Amazon Relational Database Service (Amazon RDS) is a web service that makes it easier to set up, operate, and scale a relational database in the AWS Cloud. It provides cost-efficient, resizable capacity for an industry-standard relational database and manages common database administration tasks. It frees you to focus on your applications so you can give them the fast performance, high availability, security and compatibility they need.

Amazon RDS is available on several database instance type - optimized for memory, performance or I/O - and provides you with six familiar database engines to choose from, including Amazon Aurora , PostgreSQL,  MySQL , MariaDB, Oracle Database, and SQL Server. You can use the AWS Database Migration Service to easily migrate or replicate your existing databases to Amazon RDS.

No alt text provided for this image

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

5). In this setup, the WordPress will be running on the Kubernetes in the local system and connected with the database running on the cloud i.e AWS.

Procedure

1) Providing service provider information to terraform.

No alt text provided for this image

2) Creating Security Group for relational database service.

Creating the Security group allowing the port number 3306 which will allow incoming traffic to MySQL.

No alt text provided for this image

When the above code is executed using the Terraform apply command, it will result in the creation of the security group allowing port number 3306 for MySQL.

No alt text provided for this image

3) Launching the Database service for wordpress.

Now launching the RDS(Relational database service) in AWS this database will act as a storage unit for WordPress.

No alt text provided for this image

When the above code is executed using the Terraform apply command it will result in the creation of the database in the AWS.

No alt text provided for this image

4) Launching Kubernetes

Launching the Kubernetes service in the local system using the Terraform code and creating the separate namespace for the Terraform.

No alt text provided for this image

The above code will result in the creation of a separate namespace for the Kubernetes.

No alt text provided for this image

5) Creating the Persistent Volume Claim (PVC)

Persistent volume claim will request the storage from the users local system, with the help of PV we can achieve the data availability i.e due to any reason if the WordPress goes down the data associated with the WordPress will not be lost and new WordPress O.S will be launched with zero latency time.

No alt text provided for this image

When the above code is executed using the Terraform apply command it will result in the creation of 2GB local storage connected to the WordPress database instance.

No alt text provided for this image

6) Launching the Wordpress pod using deployment over kubernetes.

Launching the Kubernetes pod using deployment because deployment is the one who keeps on monitoring the pod(O.S) launched and if the pod goes down, the deployment is the one who launches the new pod with the same configuration with zero latency time.

resource "kubernetes_deployment" "wordpress" {
  depends_on = [kubernetes_persistent_volume_claim.wordpress_pvc]
  metadata {
    name = "wordpress"
    namespace = kubernetes_namespace.NS.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.my_db.address
          }
          env{
            name = "WORDPRESS_DB_USER"
            value = aws_db_instance.my_db.username
          }
          env{
            name = "WORDPRESS_DB_PASSWORD"
            value = aws_db_instance.my_db.password
          }
          env{
          name = "WORDPRESS_DB_NAME"
          value = aws_db_instance.my_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.wordpress_pvc.metadata[0].name
          }
        }
      }
    }
  }

}



When the above code is executed using the Terraform Apply command it will result in the creation of the WordPress pod using deployment.

No alt text provided for this image

Now we have a WordPress pod ready which is running in our local system and it connected with the Relational database service(RDS) in AWS.

7) Exposing the pod.

The WordPress pod is ready but the outside world i.e internet will not have connectivity with it so we need to expose the pod so that the outside world can have connectivity with it.

No alt text provided for this image

When the above code is executed using Terraform apply command the pod will be exposed to the internet on port number 80 i.e HTTP.

No alt text provided for this image

8) Wordpress will be launched.

No alt text provided for this image

Thus we can see that using the minikube IP of the system and port number provided, the WordPress is launched with the database connected to it in the backend which is running on servers of AWS.

Destroying the complete infrastructure after solving use case.

terraform destroy


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

Thank You for Viewing this article.

No alt text provided for this image









To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics