Deploying WordPress Application On Kubernetes With AWS RDS Using Terraform

Nischal Vooda
6 min readSep 3, 2020

--

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?

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.

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
minikube started

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 = 1strategy {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 = 80target_port = 80protocol = "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)

terraform plan(to confirm that the code is perfect)

terraform apply(to execute the code)

Now first confirm whether the deployment is created or not by

kubectl get deployment or kubectl get deploy as shown below.

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

kubectl get pods

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.

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.

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

after saving this run these commands

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

finally you will get like this

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

to get the URL use this command

past it in your favourite browser

you will find like this

chouse your favourite language and click on continue

you will find this

give tour credentials

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

Click on submit

give your credentials

congratulations 🎊

you have created database for your WordPress

We can destroy the complete infrastructure in one-click.

terraform destroy --auto-approve

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

Thanks a lot !! Hope you learned and enjoyed.

Connect me on my LinkedIn as well.

--

--

No responses yet