Deploy the Wordpress application on Kubernetes and AWS using terraform and RDS Task 6
Harsh Rajotya
Technical Blogger & DevOps Engineer @ Medium & DevOpsFarm Inc | Writing, Automation, Cloud
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 are Containers?
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.
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.
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.
Prerequisite:
- AWS account
- Minikube (kubernetes) installed:
- https://kubernetes.io/docs/tasks/tools/install-minikube/
- terraform installed
- https://learn.hashicorp.com/tutorials/terraform/install-cli
What is the Task?
In this task we will deploy the WordPress application on top of Kubernetes which will be connected to the AWS-RDS database where it will store all the critical information. We will create this infrastructure using Terraform. Steps to the task include the following:
- Write an infrastructure as code using terraform which automatically deploy the WordPress application.
- On AWS use RDS service for the relational database for WordPress application.
- Deploy the Wordpress as a container either on top of minikube or EKS or Fargate service on AWS.
- The WordPress application should be accessible from the public world if deployed on AWS or through workstation if deployed on Minikube.
How to Accomplish this Task?
Follow the steps below to accomplish the task:
Step 1 : Log in to AWS using the CLI. Also we need to start our Minikube VM.
Step 2 : In our code we need to initialize the provider that we want to use in our code. So we need to initialized Kubernetes and AWS.
provider "aws"{ region = "ap-south-1" shared_credentials_file = "C:/Users/admin/Downloads/new_user_credentials" profile = "harsh" } provider "kubernetes" { config_context_cluster = "minikube" }
In our CMD we need to run the Terraform init command.
Step 3 : 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.
resource "kubernetes_deployment" "wpdeployement" { metadata { name = "wordpress-deployment" labels = { type = "frontend_deployment" } } spec { replicas = 1 strategy { type = "RollingUpdate" } selector{ match_labels = { type = "cms" env = "prod" } } template { metadata { labels = { type = "cms" env = "prod" } } spec { container { image = "wordpress" name = "wordpress" port { container_port = 80 } } } } } } resource "kubernetes_service" "loadbalancer" { depends_on = [kubernetes_deployment.wpdeployement] metadata { name = "wordpress-service" } spec { type = "NodePort" selector = { type = "cms" } port { port = 80 target_port = 80 protocol = "TCP" } } }
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.
resource "aws_db_instance" "wpdb" { allocated_storage = 20 storage_type = "gp2" engine = "mysql" engine_version = "5.7" instance_class = "db.t2.micro" name = "RDS" username = "vinod" password = "wordsqlpass" skip_final_snapshot = true publicly_accessible = true port = 3306
}
Step 5 : Next step is to run the infrastructure using the Terraform apply command.
Step 6 : 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.
Thus now we have accomplished the task of automating the deployment of WordPress on top of Kubernetes with Database in AWS using Terraform.
Thank You!!!