Deploying WordPress site using Kubernetes and integrating it with a database by using Amazon RDS Service

Deploying WordPress site using Kubernetes and integrating it with a database by using Amazon RDS Service

- What is Amazon RDS Service?

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.

got this content from AWS site. For more info visit: https://aws.amazon.com/rds/

- Task Description :

Deploy the WordPress application on Kubernetes and AWS using terraform including the following steps;

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.

- Pre-Requisites :

-> Should have a AWS account

->minikube and kubernetes must be installed and configured

-> Terraform should be installed and configured in your system

->> Task in Detail :

1. Create a AWS Profile for this task by using the command :

aws configure --profile profilename

2. Create a file terraform.tf file to launch WordPress on Kubernetes

3. In terraform.tf file,we use the kubernetes provider:

provider "kubernetes" {

 config_context_cluster = "minikube"
}

Start the minikube service by using a null resource:

resource "null_resource" "minikube" {

 provisioner "local-exec" {
        command = "minikube start"

   }
}

4. Use Kubernetes deployment resource to deploy pod for wordpress and properly configure the things that you need:

resource "kubernetes_deployment" "wordpress" {
 metadata {
  name = "wp"
 }
 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:4.8-apache"
    name = "wp" 
    }
   }
  }
 }
}


5. Use Kubernetes Service resource and add a nodeport so that we can access the wordPress,so that it can be accessed by everyone:

resource "kubernetes_service" "mywordpress" {
 metadata {
  name = "mywordpress"
 }
 spec {
  selector = {
   App = "wordpress"
  }
  port {
   node_port   = 31000
   port = 80
   target_port = 80
  }
  type = "NodePort"
 }
}

The range for Nodeport is 30000-32767,make sure you assign a port for nodeport within the range

6. Create another file db.tf to create a database for our wordpress site

we will use terraform for using AWS RDS service so we use aws provider:

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

then in the db.tf file use data to extract vpc so that we can use the security group of that vpc and configure it/set some rules for it to access our database:

data "aws_vpc" "myvpc" {
  default = true
}

data "aws_subnet_ids" "mysubnet" {
  vpc_id = data.aws_vpc.myvpc.id
}

then now we use it to change the rules for the security group present in that VPC by using aws_security_group resource and then assign the retreived subnet ID for database :

resource "aws_security_group" "default" {
  name        = "main_rds_sg"
  description = "Allow all inbound traffic"
  vpc_id      = data.aws_vpc.myvpc.id


  ingress {
    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 = "wordpress-db"
  }
}

resource "aws_db_subnet_group" "default" {
  name       = "main_subnet_group"
  subnet_ids = data.aws_subnet_ids.mysubnet.ids
}

Here I have used 3306 port for inbound rules so that it can easily connect to our database,Now we launch the databse using aws_db_instance resource

resource "aws_db_instance" "mydb" {
  allocated_storage      = "10"
  publicly_accessible    = true
  identifier             = "wordpress-db"
  engine                 = "mariadb"
  engine_version         = "10.4.8"
  instance_class         = "db.t2.micro"
  name                   = "mydatabase"
  username               = "Asish"
  password               = "password"
  vpc_security_group_ids = [aws_security_group.default.id]
  db_subnet_group_name   = aws_db_subnet_group.default.id
  skip_final_snapshot    = true
}

As you can see,here I have used mariadb engine for my database and provided the security group we have configured by using vpc_security_group_ids parameter and db_subnet_group_name parameter to specify the subnet

then I have used output to get the address of our database to be used as host in our wordpress site:

output "ip" {
  value = aws_db_instance.mydb.address
  
}

You can easily get details of all the resources I have used from the official hashicorp site

7. Now after we write both the files,now we initialise the terraform by using the command:

terraform init
No alt text provided for this image

it will initialise all our resources then we check the validity of our code to see it there is any error by using the command:

terraform validate
No alt text provided for this image

Now, we check how our resource would be actually created before creating it by using terraform plan command:

No alt text provided for this image

then we create the resource by using the command:

terraform apply -auto-approve
No alt text provided for this image

after the infrastructure is created use minikube service list command to get the IP for our worpress site:

No alt text provided for this image

Now use the IP to open the WordPress site:

No alt text provided for this image

Now use the correct credentials you have provided in your db.tf file

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

Now install the wordpress for your site:

No alt text provided for this image

After this now you can access your wordpress site:

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

Finally,our task is done. To see the code I have written for this task follow my repository link for the task. Github Link: https://github.com/Pheonix-reaper/Wordpress-and-AWS_RDS-Service

If you follow all the steps I have mentioned,you too can launch a Wordpress site on kubernetes with a database created by using AWS RDS Service
Chiranjeeb Nayak

Software Engineer - III @Walmart l Ex - Juspay(Google Pay) l Youtuber (20k+) | LLM Engineer I Gen AI I MERN Mentor | DSA Instructor | 4* codechef | 5* Hackerank | competitive programmer || BUILDING - CODEMAP

4 年

congrats??

回复
Debarshi Mondal

Serverless Developer at Serverless Guru ?? | AWS Community Builder ?? | AWS Certified

4 年

Bravo!

回复

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

Asish Patnaik的更多文章

社区洞察

其他会员也浏览了