AWS TASK-6 Completed successfully
Deploying WordPress on Kubernetes with RDS using Terraform
Aditya Jain
Task Objective
- Write an Infrastructure as Code using Terraform, which automatically deploys WordPress.
- For the database use, RDS ( Relational Database Service ) provided by AWS
- Deploy WordPress as a container either on top of Minikube or EKS or Fargate service on AWS.
- The WordPress application must be accessible from the public world if deployed on AWS or through workstation if deployed on Minikube.
Terraform
It is a tool that can use the services of any cloud ( also called provider ) with standardized commands. Terraform makes this easier because it is built on a plugin-based architecture. We just have to initialize the plugins of the cloud we want to use. It uses a declarative language called Hashicorp Configuration Language ( HCL ).
Kubernetes
Kubernetes is an open-source container-orchestration system for automating computer application deployment, scaling, and management. It is an ideal platform for hosting cloud-native applications that require rapid scaling, like real-time data streaming, and many more.
Amazon Relational Database Service (RDS)
It 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. Amazon RDS is available on several database instance types — optimized for memory, performance, or I/O.
Database Engines provided RDS :
- Amazon Aurora
- PostgreSQL
- MySQL
- MariaDB
- Oracle
- SQL Server
Now let’s jump right into the solution !!!
Requirements :
- Minikube
- kubectl
- Terraform ( ^0.12.26 )
- AWS account
→ Create a deployment using terraform and expose the port of
WordPress using service
To provision anything using Terraform we need their respective providers. Since we have to create a deployment we need a provider called “kubernetes”
provider "kubernetes" {
config_context_cluster = "minikube"
}
“config_context_cluster” is an argument wherein we have to specify the path of the config file.
Then run “terraform init” to install the plugin.
The output of “terraform init”
resource "kubernetes_deployment" "wordpressdp" {
metadata {
name = "wordpress"
}
spec {
replicas = 3
selector {
match_labels = {
env = "dev"
region = "IN"
App = "wordpress"
}
}
template {
metadata {
labels = {
env = "dev"
region = "IN"
App = "wordpress"
}
} spec {
container {
image = "wordpress:4.8-apache"
name = "mywp"
}
}
}
}
}
This code will create a deployment with the desired state of 3.
resource "kubernetes_service" "wplb" {
metadata {
name = "wordpress"
}
spec {
selector = {
App = "wordpress"
}
port {
node_port = 30300
port = 80
target_port = 80
}
type = "NodePort"
}
}
Service is a way using which we can expose an application running on a set of pods as a network service. NodePort is a type of service which exposes the service on each Node’s IP at a static port called “node_port”.
It usually uses selectors in order to monitor/ find the pods in a deployment. This is why selectors play an important role in management.
Now we have to execute “ terraform apply ” to provision our resources.
After its execution, we can verify that all our required resources have been successfully provisioned by the following commands.
By using the obtained endpoint we can access WordPress.
→ Launch a MySQL database using terraform with the required
configuration
Since we have to provision resources in AWS we need a provider called “aws”.
provider "aws"{
region = "ap-southeast-1"
}
If you have a profile created then specify it in the following way:
provider "aws" {
region = "ap-southeast-1"
profile = "profile_name"
}
I am using the “ default ” profile.
Then run “terraform init” to install the plugin.
The output of “terraform init”
resource "aws_db_instance" "mysqldb" {
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
allocated_storage = 10
storage_type = "gp2"
name = "prodDB"
username = "aditya"
password = "adityaj"
port = "3306"
publicly_accessible = true
skip_final_snapshot = trueparameter_group_name = "default.mysql5.7"
tags = {
Name = "prodDB"
}
}
This will create a DB instance with MySQL. It is important to make this data publicly accessible in order to connect it with WordPress.
Now we have to execute “ terraform apply ” to provision our resources.
→ Connect the RDS instance with WordPress
We can connect the WordPress application to the RDS database by providing the username, password, database name, and database host.
We can now see that WordPress is connected with the RDS instance.
Kubernetes enables us to easily create multiple pods, then make sure that that number of pods always exists. If a pod does crash it automatically launches new ones with zero downtime. It also has the ability to scale the number of pods and to update or delete multiple pods with a single command.
On the other hand, RDS manages the MySQL database by performing tasks such as hardware provisioning, patching, and backups (automated backup is done which enables point-in-time recovery for any database instance ).