How to deploy the Wordpress application on Kubernetes and AWS using Terraform and RDS?

Ishita Mittal
4 min readSep 17, 2020

--

Kubernetes is a powerful open-source system, initially developed by Google, for managing containerized applications in a clustered environment. It aims to provide better ways of managing related, distributed components and services across varied infrastructure.

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.

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

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.

Prerequisite:

  • We need AWS user (AWS IAM user for administrative access)
  • For deploying wordpress on the top of minikube, we need to have minikube installed.
  • We should have Kubectl configured in your system.

Step-by-Step Walkthrough:

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.

In order to run Minikube in your machine, it should be pre-installed.

To start a local cluster —

minikube start

Mention the providers at the starting of Terraform code-

provider "aws" {
version = "~> 2.0"
region = "ap-south-1"
profile = "Ishita"
}
provider "kubernetes" {
config_context_cluster = "minikube"
}

Create a deployment in Kubernetes-

Create a deployment in K8S means that it will run the pods and will also ensure that if a pod is down or gets terminated, the number of pods needed to be launched has to specified by the user. Also, create a container with WordPress.

Create a deployment in Kubernetes-

resource "kubernetes_deployment" "WpApp" {
metadata {
name = "mypod1"
}


spec {
replicas = 3


selector {
match_labels = {
App = "wordpress"
}


}


template {
metadata {
labels = {
App = "wordpress"
}
}
spec {
container {
image = "wordpress:4.8-apache"
name = "wppod"
}
}
}
}
}

Create a service in Kubernetes-

resource "kubernetes_service" "example" {
metadata {
name = "myservice1"
}
spec {
selector = {
App = "wordpress"
}
port {
port = 80
target_port = 80
}
type = "NodePort"
}

}

Creating an AWS RDS Instance -

resource "aws_db_instance" "mysqldb" {
allocated_storage = 10
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
storage_type = "gp2"
name = "mydb"
username = "aki"
password = "redhat123"
port = "3306"
publicly_accessible = true
skip_final_snapshot = true
parameter_group_name = "default.mysql5.7"


tags = {
Name = "database-1"
}
}

For execution, use:

terraform init
terraform apply --auto-approve

The RDS setup is now completed successfully!!

Now, we have to import both of these files into our main.tf file

Use below code in the main file to do this

module "kube" {
source = "./kubernetes"
}
module "rds" {
source = "./rds"
}

After doing this we have to again run terraform init command to load the module files

Now, run the below command to deploy all the services

terraform apply -auto-approve

That’s how we can deploy the wordpress application on Kubernetes and AWS using Terraform and RDS.

Thank you for reading!!

A special thanks to World Record Holder, Vimal Daga sir for his extraordinary teaching skills and to provide such a platform where we can develop ourselves and learn new technologies, their integration, etc. from his years of hard work and research. I consider myself really lucky to be a part of his trainings where I get to improve myself and learn new & exciting things every day.

--

--

No responses yet