Deploying WordPress on Kubernetes With AWS RDS using Terraform

Tanmay Sharma
5 min readOct 13, 2020

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

Amazon RDS is available on several database instance types — 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.

About Kubernetes and Minikube-

Kubernetes is a popular open source platform for container orchestration, that is for the management of applications built out of multiple, largely self-contained runtime called containers.

Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a Virtual Machine (VM) on your laptop for users looking to try out Kubernetes or develop with it day-to-day.

Problem Statement:-

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

1. Writing an Infrastructure as code using Terraform, which automatically deploy the WordPress application

2. On AWS, using RDS service for the relational database for WordPress application.

3. Deploying 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 a workstation if deployed on Minikube.

Problem Statement:-

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

1. Writing an Infrastructure as code using Terraform, which automatically deploy the WordPress application

2. On AWS, using RDS service for the relational database for WordPress application.

3. Deploying 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 a workstation if deployed on Minikube.

Solution:

Start the minikube-

Once minikube has started, write the Terraform code which will deploy the WordPress on the Minikube and Mysql on AWS RDS for Database.

Step 1:

Kubernetes deployment with 2 replicas is created. Deployment in Kubernetes uses labels as a selector for determining its target pod. Thus we have to provide labels to our pods.

NordPort service of Kubernetes exposes the pods to port 80 to the public so that whenever any client comes to the IP of the node on the NodePort specified, they will be redirected to the port 80 of WordPress.

provider "kubernetes" {
config_context_cluster = "minikube"
}
resource "kubernetes_service" "service" {
metadata {
name = "wordpress"
}
spec {
selector = {
app = "wordpress"
}
session_affinity = "ClientIP"
port {
port = 80
target_port = 80
node_port = 30017
}
type = "NodePort"
}
}
resource "kubernetes_deployment" "deployment" {
metadata {
name = "wordpress"
labels = {
app = "wordpress"
}
}
spec {
replicas = 2
selector {
match_labels = {
app = "wordpress"
}
}
template {
metadata {
labels = {
app = "wordpress"
}
}
spec {
container {
image = "wordpress"
name = "wordpress"
}
}
}
}
}

Step 2:

Create a security group for the RDS service exposing 3306 port.

resource "aws_security_group" "sg_rds" {
name = "sg"
description = "sg for RDS"
ingress {
description = "Database Rule"
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}

Step 3:

For creating AWS services first we have to tell the provider and profile for our AWS. Then use aws_db_instance resource of Terraform which will create an RDS which is publicly accessible so that it can be assessed by the WordPress server running in the Kubernetes.

provider "aws" {
region = "ap-south-1"
profile = "tanmay2"
}
resource "aws_db_instance" "mydb" {
allocated_storage = 10
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7.21"
instance_class = "db.t2.micro"
name = "mydb"
username = "Tanmay"
password = "tanmay786"
port = 3306
vpc_security_group_ids = [aws_security_group.sg_rds.id]
parameter_group_name = "default.mysql5.7"
publicly_accessible = true
skip_final_snapshot = true
auto_minor_version_upgrade = false
depends_on = [
aws_security_group.sg_rds,
]
}

After writing the code we can get the entire infrastructure by using terraform init command, which first initializes all the plugins required then using terraform apply command, it builds entire infrastructure.

Step 4:

Using kubectl get all the command we can see the resources we have created in the Kubernetes.

Step 5:

Everything thing is ready. Now using the minikube IP we can access the WordPress site can configure it.

Conclusion:

In this blog, we have learned how to deploy the WordPress site on the top on Minikube with AWS RDS Database service using the Terraform code to automate the infrastructure.

I hope you all enjoy reading this blog!

--

--