Deployment Of Wordpress On Kubernetes With AWS RDS Using Terraform
--
Problem Statement
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.
Let's get started...
In this task, I have written Infrastructure as code using Terraform, which automatically deploy the Wordpress application. On AWS, I have used RDS service for the relational database for Wordpress application. Then I deployed the Wordpress as a container on top of Minikube, the Wordpress application is accessible to the public world.
What is Kubernetes?
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 the need of Kubernetes?
Containers are a good way to bundle and run your applications. In a production environment, you need to manage the containers that run the applications and ensure that there is no downtime. For example, if a container goes down, another container needs to start. This is where Kubernetes helps us. Everything is managed by Kubernetes itself. We just need to specify the templates of the container and Kubernetes will do its job.
Minikube is an open-source tool that helps to run Kubernetes on a local computer. Before using minikube we need to start it so here I wrote terraform code to start minikube on my Local Computer.
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 resizable capacity while automating time-consuming administration tasks such as hardware provisioning, database setup, patching and backups.
Before that, I’m gonna tell you some basics about Terraform we make use of the following the commands
terraform init - To install the required plugins
terraform apply - To make the resources run
terraform plan - is used to create an execution plan
terraform validate - To check the code
terraform destroy - To destroy all the resources in single click
Let’s go by each step to understand the task in the simplest manner
Step1
Creating the separate folder for web page code and in that create terraform file with extension .tf and after initializing the terraform file so that it can download the required plugins for that particular folder. We have to log in to AWS CLI
Step2
We need to specify the region and profile name for setting up the provider in Terraform. Which logins to the AWS account to perform actions. And Also we need to start our Minikube VM.
provider "aws"{
region = "ap-south-1"
shared_credentials_file = "C:/Users/satvikakolisetty/Downloads/new_user_credentials.csv"
profile = "satvi"
}provider "kubernetes" {
config_context_cluster = "minikube"
}
Step3
Now we need to write the code for deploying Wordpress on Minikube. We will also be creating a Load Balancer service on Kubernetes which will expose our deployment.
wp.tf File
It will launch the Wordpress Application
resource "kubernetes_deployment" "wordpress" {
metadata {
name = "wp"
}
spec {
replicas = 3
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"
}
}
}
}
}resource "kubernetes_service" "wordpresslb" {
metadata {
name = "wplb"
}
spec {
selector = {
app = "wordpress"
}
port {
protocol = "TCP"
port = 80
target_port = 80
}
type = "NodePort"
}
}
Step 4
Now we need to create our Database where the WordPress application will store all its data. For this, we are using AWS-RDS.
db.tf file:-
It will create the RDS on top of AWS.
resource "aws_db_instance" "mydb" {
allocated_storage = 20
identifier = "dbinstance"
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7.30"
instance_class = "db.t2.micro"
name = "mydb"
username = "satvi"
password = "root22"
iam_database_authentication_enabled = true
parameter_group_name = "default.mysql5.7"
skip_final_snapshot = true
publicly_accessible = true
tags = {
Name = "sqldb"
}
}
main.tf File
It will provide the IP to connect to Wordpress.
resource "null_resource" "minikubeservice" {
provisioner "local-exec" {
command = "minikube service list"
}
depends_on = [
kubernetes_deployment.wordpress,
kubernetes_service.wordpresslb,
aws_db_instance.mydb
]
}terraform plan
terraform plan
terraform apply --auto-approve
kubectl get all
minikube service list
We will use the minikube IP