Deploy WordPress on Kubernetes & AWS RDS using Terraform

WordPress on Kubernetes & MYSQL on AWS RDS

Bonjour and welcome to all.Today we will be working on a use case,Every another guy working on cloud will have to face it. So before moving on further let us just focus on today's agenda :-

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.

Before working on these technology, let us know about them from their creators. What they are and why and where they are used ?

What Is Kubernetes?

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.

The name Kubernetes originates from Greek, meaning helmsman or pilot. Google open-sourced the Kubernetes project in 2014. Kubernetes combines over 15 years of Google’s experience running production workloads at scale with best-of-breed ideas and practices from the community.

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

Amazon RDS is available on several database instance types — optimized for memory, performance or I/O — and provides you with six familiar database engines to choose from, including Amazon Aurora, PostgreSQL, MySQL, MariaDB, Oracle Database, and SQL Server. You can use the AWS Database Migration Service to easily migrate or replicate your existing databases to Amazon RDS.

What Is Terraform?

Terraform (https://www.terraform.io/) is an open source project by Hashicorp written in golang (https://golang.org/). It lets you define cloud resources (servers, s3 buckets, lambda functions, IAM policies, etc.) in code and check them into a source control. You can then “execute” the configuration and create/modify/delete all the cloud resources with a single command.

If you have any resources in AWS/Google Cloud/Azure, etc. its a high likelihood that terraform can improve your workflow and make management of your cloud resources a breeze! I have used it with AWS, so, most of this post will discuss terraform in context of AWS. But, it works fine with Google Cloud, Azure, Alibaba cloud, etc.

Using Terraform:

  • terraform init : Creates a .terraform directory and initialises with all the plugins required.
  • terraform plan : Outputs how terraform interprets the main.tf file and what resources it will create/modify/delete. Its a dry-run. Which is very critical because you would like know exactly what changes it will do your cloud resources. Surprises are bad!
  • terraform apply : Reads the main.tf and makes all the changes to the cloud. This step outputs a .tfstate file that contains identifiers of cloud resources. This generated file is very important and you should never edit this manually(Recommended).

A best practice is to set up a terraform role in IAM on AWS, use that to manage resource access to terraform and then execute it on the machine with that role.

Requirements Setup

To make this project, we need to have some small setup ready. There are some pre-requisites one should take care of :-

  1. Minikube(in case of local cluster) & Terraform installed
  2. AWS Activated account with IAM role privileged to use AWS RDS services.
  3. AWS profile configured on local machine.

Steps toward our work -

Let us first configure profile in the AWS CLI with the IAM role.Before proceeding ahead with terraform we have to use our Command Prompt to configure IAM role as Profile in your AWS CLI.

AWS profile configuration
  1. Plugins Initialization :-Now Create a file with Extension .tf in separate folder and run the following command to initialize with Terraform Environment and downloading and Installing Terraform Plugins.

For AWS plugin initialization use —

Here if we leave the provider block completely empty, then it will automatically configure the minikube by creating/generating a config in a default location (~/.kube/config).

2. Creating AWS RDS instance :- An AWS-RDS MySQL 5.7.30 instance would be created use this snippet, with an allocated storage of 10 Gi ,and several other properties kept in mind for further use.

3. Getting RDS Endpoint URL :- In the last step we created AWS RDS instance,now its time to take the endpoint URL to be used in our Wordpress application.And this snippet will print the endpoint URL of the AWS RDS.

4. Deploying Wordpress on Kubernetes :- In this step we will deploy wordpress on kubernetes by the use of terraform. In kubernetes we have created kubernetes deployment and service with the Wordpress image. This will work as frontend for our application which will use the RDS SQL instance as backend database. Here is the gist for that —

After successful completion of code setup, its time to run. And to run all this-

  • First we need to initialise the terraform code for the plugins it requires. Use this code for initialise —

terraform init

Use the following command to get the blueprint of what the code is going to do to the machine.

terraform plan

And finally use this command to execute the code.

terraform apply

The process will begin once you accept all the resource addition. This is the same process you have to do for the AWS folder also in order to create an RDS instance using the terraform code.

Now connect WordPress to MySQL database

Here in the above Attached picture , We will use Database endpoint , that we got while deploying RDS , as out database host .

After doing all the basic one time configuration,we will finally see the homepage of the Wordpres.

Hence the Wordpress launched over kubernetes is now connected with the database which was over AWS RDS.Now After Completion of Work , Destroy both the Deployments

terraform destroy --auto-approve

To delete setup of Kubernetes use the following command.

kubectl delete all --all

And here we call it a wrap. All the codes discussed earlier can be found on my Github Repo & connect with me on Linkedin !!

Thank you Everyone for reading .!! Bella Ciao