Deploying Wordpress on Minikube(K8s) & Integrating with RDS(AWS) using Terraform

Kunal Jaiswal
5 min readSep 4, 2020

--

Task which we are going to perform

1.) Write an Infrastructure as a code using terraform, which automatically deploy the Wordpress application on Minikube.

2.) On AWS, use RDS service for the relational database for Wordpress application.

3.) The Wordpress application should be accessible from the public world.

Introduction:

What is AWS?

AWS stands for Amazon Web Services which is the subsidiary of Amazon providing on-demand cloud computing platforms.

What is 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 re-sizable capacity while automating time-consuming administration tasks such as hardware provisioning, database setup, patching and backups.

What is Kubernates?

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services. On K8S (short for Kubernetes) we create containers and launch our application on top of these containers. Containers provide isolation and everything is managed by K8S.

What is Terraform?

Terraform is an open-source infrastructure as a code software tool created by HashiCorp. It enables users to define and provision a datacenter infrastructure using a high-level configuration language known as Hashicorp Configuration Language (HCL).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.

Let’s do some practical

Before do anything, we have to give the credential and password.

Step 1: Write an Infrastructure as a code using terraform, which automatically deploy the Wordpress application on Minikube.

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 = "wordpress-container"
}
}
}
}
}
resource "kubernetes_service" "service" {
metadata {
name = "service"
}
spec {
selector = {
App = kubernetes_deployment.wordpress.spec.0.template.0.metadata[0].labels.App
}
port {
node_port = 30000
port = 80
target_port = 80
}
type = "NodePort"
}
}

Now , we have to run the terraform code .Before running the code we have to first Initialize the code and download the necessary plugins of terraform for Kubernetes using below command.

terraform init
Successfully initialize the code

Now we run our terraform code by below command.

terraform apply --auto-approve
Successfully apply the terraform code

For checking whether wordpress is deploying or not on the minikube, then run the below command.

kubectl get all
Successfully deploy wordpress on the minikube

Step 2: On AWS, use RDS service for the relational database for Wordpress application.

provider "aws" {
region = "ap-south-1"
profile = "kunal1"
}
resource "aws_db_instance" "default" {
allocated_storage = 10
storage_type = "gp2"
identifier = "sqldb"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
name = "RDS"
username = "kunal"
password = "kkjaiswal"
publicly_accessible = true
port = 3306
parameter_group_name = "default.mysql5.7"
skip_final_snapshot = true
tags = {
Name = "database"
}
}
output "dns" {
value = aws_db_instance.default.address
}

Now , we have to run the terraform code .Before running the code we have to first Initialize the code and download the necessary plugins of terraform for Kubernetes using below command.

terraform init
Successfully initialize the code

Now we run our terraform code by below command.

terraform apply --auto-approve
Successfully apply the code

Now check it on the aws console.

Successfully create RDS

Step 3: After the infrastructure is created now we need to visit the WordPress website.Next we have to fill all the information of the database and after clicking the next button everything will be installed and configured.

Database connectivity is done.

So, our task is successfully done✌✌

For complete solution📝 of this problem,https://github.com/KunalKumarJaiswal/task-6

Thanks for learning. If you have any suggestion or any query then feel free for suggest and ask.

--

--