Hybrid Cloud setup over AWS using Minikube and RDS Database service

Published by Anubhav Singh on

  1. This is the sixth task of my journey of Hybrid Multi-Cloud. In this task, I am going to set up my own infrastructure and AWS and integrating that we will set our whole setup.

In case you don’t know about the AWS or Cloud computing follow the below blog so that you get some basic knowledge about these technologies.

Task Description

  • Write an Infrastructure as code using Terraform, which automatically deploys the WordPress application.
  • Use the RDS service of AWS to launch a relation database for WordPress.
  • Deploy WordPress as a container either on top of minikube or EKS or Fargat service on AWS.
  • The WordPress application should be accessible from the public world if deployed on AWS or through workstation if deployed on Minikube.

Prerequisite

  • AWS account
  • Minikube (as I am going to perform over minikube)
  • Terraform tool

Note: In case you don’t know how to setup minikube for the window do let me know in the comment I will prepare a different post for that as well.

Launch of WordPress over minikube

In order to write Terraform code for launching WordPress, it is important that we provide a provider in the code so that terraform can able to install the required plugin for that and we can be able to perform the further task.

provider "kubernetes" {
  config_context_cluster   = "minikube"
}

For launching the WordPress I am using kubernetes_deployment

resource "kubernetes_deployment" "wp" {
  metadata {
    name = "wp"
    labels = {
     myapp = "MyWpSite"
    }
  }

  spec {
    replicas = 1
    selector {
      match_labels = {
        myapp = "MyWpSite"
      }
    }

    template {
      metadata {
        labels = {
          myapp = "MyWpSite"
        }
      }

      spec {
        container {
          image = "wordpress:5.4"
          name  = "wordpress-app"
        }
      }
    }
  }
}

To connect it with the outside world and to provide our system more reliability we have to create a load balancer typically known as service. Here is the code

resource "kubernetes_service" "wp_service"{
  metadata {
    name = "wp-service"
  }
  spec {
    selector = {
      myapp = "${kubernetes_deployment.wp.metadata.0.labels.myapp}"
    }
    session_affinity = "ClientIP"
    port {
      port        = 80
      target_port = 80
    }
    type = "NodePort"
  }
}

Copy the whole code in a .tf extension file and run the below commands

We are working on minikube so we have to start that first

minikube start

 

Once the minikube is successfully started install the terraform plugin to perform the task

<pre class="wp-block-syntaxhighlighter-code">terraform init<br /><br /><img class="wp-image-918" style="font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;" src="https://brighterbees.com/blog/wp-content/uploads/2020/09/Screenshot-277-1024x576.png" alt="" /></pre>

This will install the required plugin. Now to run the services, use the below command.

terraform apply

terraform apply wp

This will successfully set the minikube cluster. You can also check that by the following command

kubectl get all

kubectl get all

Once the setup is done our next work to set RDS on AWS.

RDS on AWS

To connect with AWS also we are going to use CLI and to use that you must have AWS CLI tool. Install the CLI tool in your system and add the required path in your window env.

To configure the AWS account, use

aws configure

Now prepare another terraform file.

In terraform, before starting any service we have to tell the provider name and by this code, we are defining our provider like AWS.

provider "aws" {
  region   = "ap-south-1"
  profile  = "anubhav"
}

For best security purposes we require a firewall and in AWS we have security groups to secure our environment.

resource "aws_security_group" "allow_mysql" {
  name        = "allow_mysql"
  description = "Allow MySQL Port"
  ingress {
    description = "allow_sql_port"
    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 = "task6-sg"
  }
}

This will create a security group that will only allow to 3306 port of MySQL.

resource "aws_db_instance" "wp_db" {
  allocated_storage    = 20
  storage_type         = "gp2"
  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t2.micro"
  name                 = "wpdb"
  username             = "anubhav"
  password             = "mypass12345"
  vpc_security_group_ids = [ aws_security_group.allow_mysql.id ]
  publicly_accessible  = true
  iam_database_authentication_enabled = true
  skip_final_snapshot  = true
}

 The above code is used to launch the RDS Database instance. Again copy the whole code in a .tf extension file and run

terraform init 

and then

terraform apply

terraform apply rds

rds

Once the whole setup is done. Now it’s time to check our work. To open our WordPress we have to know the minikube IP by the following command

minikube ip

Use URL: http://minikube_ip:32391

32391 is the port of wp-service you can check that by “kubectl get all” command we have used that at the beginning of this post.

wordpress setup

Fill the details whatever you have defined while creating RDS service and for the databasehost follow the endpoint of the RDS instance.

 

wp setup

Hurray! We have successfully set up a Hybrid Cloud structure. Hope you liked my effort.

In case if you want to connect with me over the over social media you can

That’s it

Thank you


0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

STAY CONNECT WITH US