Deployment of Wordpress on Kubernetes and linking it with AWS Relational Database Servive all through Terraform.

Deployment of Wordpress on Kubernetes and linking it with AWS Relational Database Servive all through Terraform.

Hello guys back with the another article focussing on AWS Relational Database Service i.e setting up of Wordpress application on Kubernetes and linking it with AWS Relational Database Service deployed on AWS.

So lets start with my project.Steps involved in it are-:

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.

Lets start step by step-:

Step 1-: Create a deployment using terraform and expose the port of the wordpress using service

provider "kubernetes" {
  config_context_cluster = "minikube"
}

resource "kubernetes_deployment" "wordpress" {
  metadata {
    name = "wordpress"
    
  }

  spec {
    replicas = 1

    selector {
      match_labels = {
        env = "production"
        region = "IN"
        App = "wordpress"
      }
       match_expressions {
              key      = "env"
              operator = "In"
              values   = ["production", "webserver"]
            }
    }
    

    template {
      metadata {
        labels = {
          env = "production"
          region = "IN"
          App = "wordpress"
        }
      }

         
      spec {
       container {
      image = "wordpress"
      name  = "mywordpress-cont"
     

          
        }
      }
    }
  }
}

resource "kubernetes_service" "wordpress" {
  metadata {
    name = "wordpress"
  }
   spec {
        selector = {
      App = kubernetes_deployment.wordpress.spec.0.template.0.metadata[0].labels.App
    }
    port {
      node_port   = 30201
      port        = 80
      target_port = 80
    }

    type = "NodePort"
  }
}


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

Step 2: Launch a RDS mysql database using terraform with the required configuration.

provider "aws" {
  region = "ap-south-1"
  profile = "shivam1"
}
data "aws_vpc" "default" {
  default = true
}

data "aws_subnet_ids" "all" {
  vpc_id = data.aws_vpc.default.id
}

data "aws_security_group" "default" {
  vpc_id = data.aws_vpc.default.id
  name   = "launch-wizard-1"
}

module "db" {
  source  = "terraform-aws-modules/rds/aws"
  version = "~> 2.0"

  identifier = "database-1"

  engine            = "mysql"
  engine_version    = "5.7.19"
  instance_class    = "db.t2.micro"
  storage_type = "gp2"
  allocated_storage = 20
  storage_encrypted = false

  username = "root"
  password = "rocksshivam"
  port     = "3306"

  vpc_security_group_ids = [data.aws_security_group.default.id]
  subnet_ids = data.aws_subnet_ids.all.ids
  publicly_accessible = true

  availability_zone = "ap-south-1a"

  maintenance_window = "Mon:00:00-Mon:03:00"
  backup_window      = "03:00-06:00"

  multi_az = false

   backup_retention_period = 0

  tags = {
    Owner       = "user"
    Environment = "dev"
  }

  enabled_cloudwatch_logs_exports = ["audit", "general"]
 
  iam_database_authentication_enabled = false
 

  # DB parameter group
  family = "mysql5.7"

  # DB option group
  major_engine_version = "5.7"

  # Snapshot name upon DB deletion
  final_snapshot_identifier = "demodb"

  # Database Deletion Protection
  deletion_protection = false

  parameters = [
    {
      name  = "character_set_client"
      value = "utf8"
    },
    {
      name  = "character_set_server"
      value = "utf8"
    }
  ]

  options = [
    {
      option_name = "MARIADB_AUDIT_PLUGIN"

      option_settings = [
        {
          name  = "SERVER_AUDIT_EVENTS"
          value = "CONNECT"
        },
        {
          name  = "SERVER_AUDIT_FILE_ROTATIONS"
          value = "37"
        },
      ]
    },
  ]
}


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

Step 3: Download the SQL Client.

Once the database instance creation is complete and the status changes to available, you can connect to a database on the DB instance using any standard SQL client. In this step, we will download MySQL Workbench, which is a popular SQL client. 

a. Go to the Download MySQL Workbench page to download and install MySQL Workbench. For more information on using MySQL, see the MySQL Documentation.

Note:  Remember to run MySQL Workbench from the same device from which you created the DB Instance. The security group your database is placed in is configured to allow connection only from the device from which you created the DB instance.

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

You are now connected to the database! On the MySQL Workbench, you will see various schema objects available in the database. Now you can start creating tables, insert data, and run queries.

Now lets gonna see the output for all the steps we have done-:

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

Lets Verify in our Database:-

No alt text provided for this image

And thats all guys we have completed what we want to do Hope you liked it you can get my code on my github profile provided under-:

Thank You guys for reading the article.

























































































































































































































































































































































































































































To view or add a comment, sign in

More articles by Shivam Seth

Insights from the community

Others also viewed

Explore topics