Deploying WordPress Application On Kubernetes With AWS RDS Using Terraform

Deploying WordPress Application On Kubernetes With AWS RDS Using Terraform

Things we do here:-

  • We will do everything here using terraform automation tool:

Deploy the WordPress application on Kubernetes and AWS using terraform

1. Writing an Infrastructure as code using Terraform, which automatically deploys the WordPress application

2. On AWS, use RDS service for the relational database for WordPress application.

3. Deploying WordPress as a container either on top of Minikube.

4. The WordPress application should be accessible from the public world if deployed on Minikube.

Prerequisites:

  1. Terraform must be installed in the system.
  2. AWS CLI must be there.
  3. kubectl(client program for Kubernetes cluster)
  4. Installed Minikube: You have to must install minikube before doing this task.
  5. AWS Account for launching the RDS database by using terraform.

introduction:

What is Kubernetes?

K

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

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.

RDS(Relational Database Service)

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. It frees you to focus on your applications so you can give them the fast performance, high availability, security and compatibility they need.

DATABASE

A database is an organized collection of data, generally stored and accessed electronically from a computer system. Where databases are more complex they are often developed using formal design and modelling techniques. The database management system (DBMS) is the software that interacts with end-users, applications, and the database itself to capture and analyze the data.

Terraform

It is an open-source infrastructure as code software tool created by HashiCorp. Users define and provision data centre infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. HashiCorp maintains an extensive list of official providers, and can also integrate with community-developed providers. Users can interact with Terraform providers by declaring resources or by calling data sources.

let’s get started

CONFIGURING AWS:

aws configure --profile <profile_name>


STARTING MINIKUBE:

minikube start

No alt text provided for this image
No alt text provided for this image

We have to create the WordPress through Terraform and integrate it with the Kubernetes as shown below.

CODE FOR KUBERNETES CLUSTER :

provider "aws" {
region = "ap-south-1"
access_key = "AKIAYHBFDWOZ4RGENPHT"
secret_key = "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"
}
provider "kubernetes" {
config_context_cluster  = "minikube"
}
resource "kubernetes_deployment" "wordpress" {
metadata {
name = "wordpress"
labels = {
test = "MyExampleApp"
}
}
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" "Nodeport" {
depends_on=[kubernetes_deployment.wordpress]
metadata {
name = "terraform-example"
}
spec {
type = "NodePort"
selector = {
type = "cms"
}
port {
port = 80
target_port = 80
protocol = "TCP"
}
}
}

you can give profile name also but here I am providing access_key and secret_key directly

after saving this file run this command’

terraform init (to initialise and download the packages)

No alt text provided for this image

terraform plan(to confirm that the code is perfect)

No alt text provided for this image

terraform apply(to execute the code)

No alt text provided for this image

Now first confirm whether the deployment is created or not by

kubectl get deployment or kubectl get deploy as shown below.

No alt text provided for this image

You can also check for the pods as shown below by the command

kubectl get pods

No alt text provided for this image

To confirm it whether it is created or not you can check it by taking the IP of your Minikube and the public port that was generated.

For getting Minikibe IP :

You can use the command minikube ip as shown below.

No alt text provided for this image

When found it now get the public port at which our WordPress site is created.

For getting this use the command

kubectl get services or you can use kubectl get all

as shown below.

No alt text provided for this image

now we have to create a code for MySQL RDS which will create one MySQL database using RDS.

No alt text provided for this image

after saving this run these commands

  • terraform init
  • terraform approve
  • terraform plan
  • terraform apply

finally you will get like this

No alt text provided for this image

here you can see that RDS is created in my was account

No alt text provided for this image

to get the URL use this command

No alt text provided for this image

past it in your favourite browser

you will find like this

No alt text provided for this image

chouse your favourite language and click on continue

you will find this

No alt text provided for this image

give tour credentials

Once it is done in the Database Host write the Endpoint of the RDS Database made in the cloud.

No alt text provided for this image
No alt text provided for this image

Click on submit

give your credentials

No alt text provided for this image

congratulations 🎊

you have created database for your WordPress

We can destroy the complete infrastructure in one-click.

terraform destroy --auto-approve
No alt text provided for this image

So, this was the whole setup. So easy and short.

Thanks a lot !! Hope you learned and enjoyed.

THANKS FOR READING...


To view or add a comment, sign in

Others also viewed

Explore topics