Deploy WordPress Application On Kubernetes(Minikube) with AWS RDS Using Terraform

AmanGoyal
6 min readApr 27, 2021

Task Overview:

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:

  1. You must have an AWS account and One IAM user WITH Administration power in it . Also you should have User’s Credential file .
  2. AWS CLI , Terraform associated with Command line .
  3. You must Configure Your IAM profile to Command line .
  4. You must have kubectl (client program for Kubernetes cluster) and Minikube configured in your system.

Some New and important terms Related to this Task:-

  1. Kubernetes:- Kubernetes is open source software that allows you to deploy and manage containerized applications at scale. Kubernetes manages clusters of Amazon EC2 compute instances and runs containers on those instances with processes for deployment, maintenance, and scaling. Using Kubernetes, you can run any type of containerized applications using the same toolset on-premises and in the cloud.

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

So Let’s start our Task with writing code.

Step 1:- Write an Infrastructure as code using terraform which automatically deploy the WordPress Application on top of Kubernetes (Minikube) .

First run Minikube start command to start minikube

minikube start

provider "kubernetes" {
config_context_cluster = "minikube"
}
resource "kubernetes_service" "loadbalancer" {
metadata {
name = "loadbalancer"
}
spec {
selector = {
app = "wp"
}
port {
port = 80
target_port = 80
node_port = 32123
}
type = "NodePort"
}
}
resource "kubernetes_deployment" "wp-deploy" {
metadata {
name = "wp-deploy-aman"
}
spec {
replicas = 1
selector {
match_labels = {
app = "wp"
}
}
template {
metadata {
labels = {
app = "wp"
}
}
spec {
container {
image = "wordpress:4.8-apache"
name = "wp-con"
}
}
}
}
}

As we have written the code for WordPress . So now initialize and download plugins Using Following Command

terraform init

Now check the validity of the code and also run plan command so that we can check what changes are going to happen

terraform validate 
terraform plan

Now run apply command to run the code and deploy the things that are needed.

terraform apply --auto-approve

Now as we need to see whether our Cluster is set up or not ,Run following command .

kubectl get all

we can see our launched cluster here

As we have deployed WordPress successfully So now We are going to write a code for Deploying Database using AWS-RDS

provider "aws" {
region = "ap-south-1"
profile = "aman"
}
resource "aws_db_instance" "mysql-db" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
vpc_security_group_ids = [aws_security_group.sg.id]
identifier = "mysqldb"
name = "mysqldb"
username = "aman"
password = "Amaner123"
parameter_group_name = "default.mysql5.7"
publicly_accessible = true
skip_final_snapshot = true
}data "http" "myip"{
url = "http://ipv4.icanhazip.com"
}resource "aws_security_group" "sg" {
name = "mysql-sg"ingress {
description = "only 3306 port for MySQL"
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["${chomp(data.http.myip.body)}/32"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "mysql-sg"
}
}output "dns"{
value = aws_db_instance.mysql-db.address
}

Now again initialize and download plugins Using Following Command

terraform init

Now check the validity of the code and also run plan command so that we can check what changes are going to happen

terraform validate 
terraform plan

Now run apply command to run the code and deploy the things that are needed.

terraform apply --auto-approve

Here is the output of Deployment

Now connect wordpress to mysql database

Here in the above Attached picture , We will use Database endpoint , that we got while deploying RDS , as out database host .

Installed and login on WordPress

Sample website that I Launched.

After Completion of Work , Destroy both the Deployments.

kubectl delete all --all
terraform destroy --auto-approve

And that’s all with this task .

Special Thanks to Mr. Vimal Daga sir for enlighten us with this much knowledge .

You can check out my LinkedIn profile.

Also you can check out my GitHub profile.

That’s all with this article . Hope you Enjoy reading it.
Thanks for giving your precious time to this article✌ :-)

#aws #righteducation #rds #keeplearningkeepsharing

--

--