Deployment of WordPress site on Kubernetes with AWS RDS connection using AWS and Terraform

Binal Kagathara
6 min readSep 15, 2021

Data is the most crucial item in today’s world for any sector or professional.
In addition, the application must be available at all times with no downtime.
So we need a fully dependable database, such as AWS RDS, as well as a service, such as Kubernetes, to ensure that our application or service has zero downtime.

Aim of this blog is , We will write an Infrastructure as code using Terraform, which automatically deploy the WordPress application :

  1. Write an Infrastructure as code using Terraform, which automatically deploy the WordPress application
  2. Use AWS 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.

WordPress

WordPress is a content management system (CMS) that allows you to host and build websites. WordPress contains plugin architecture and a template system, so you can customize any website to fit your business, blog, portfolio, or online store.

Kubernetes

Kubernetes is an open-source container-orchestration system for automating computer application deployment, scaling, and management. It was originally designed by Google and is now maintained by the Cloud Native Computing Foundation.

Terraform

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

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

AWS CLI. Here link for installation.

Minikube Installed. Here link for installation.

Terraform Installed. Here link for installation.

Hope you get up to this and you are ready with the prerequisites. So let’s get started.

Write the code in to Terraform file.

Provider -AWS :

  • The Amazon Web Services (AWS) provider is used to interact with the many resources supported by AWS. The provider needs to be configured with the proper credentials before it can be used.

Write the provider code,

// Declaring  Providerprovider "aws" {access_key = "access_key"secret_key = "secret_Access_key"region     = "us-east-1"}

Provider — Kubernetes :

  • Here we can feel the power of Terraform we can control and manage cloud and Kubernetes also with terraform code. Kubernetes is also a provider for Terraform
// Kubernetes provider declaredprovider "kubernetes" {config_context_cluster   = "minikube"config_path = "~/.kube/config"}

Amazon RDS

  • Our requirement is to provision Database in AWS. Earlier we added the provider of AWS and we have connection with our AWS account
// Creating the RDS database on AWSresource "aws_db_instance" "rdsdb" {allocated_storage    = 20storage_type         = "gp2"engine               = "mysql"engine_version       = "5.7"instance_class       = "db.t2.micro"port                 = "3306"name                 = "mydb"username             = "admin"password             = "password"parameter_group_name = "default.mysql5.7"publicly_accessible  = "true"skip_final_snapshot  = "true"iam_database_authentication_enabled = "true"vpc_security_group_ids = ["security_group_id"]}
  • Created resource of aws_db_instance with 20G allocated storage and There are Multiple different data bases engines provided by AWS. mysql 5.7 is one of the compatible version of MySQL with WordPress

Create the WordPress Pod

//Creates a Wordpress podresource "kubernetes_pod" "wp1" {metadata {name = "wp1"labels = {app = "wordpress"}}spec {container {image = "wordpress"name  = "wp1"}}}
  • We need to expose the pod to public world to make our clients connect to WordPress.In Kubernetes we have a concept of service to expose particular pod to public world with NodePort or LoadBalancer
//Creating a service nodeportresource "kubernetes_service" "wp_lb" {metadata {name = "wplb"}spec {selector = {app =  kubernetes_pod.wp1.metadata.0.labels.app}port {node_port   = 32123port        = 80target_port = 80}type = "NodePort"}}

If you need whole code then the here is GitHub link for you!

Build this Infrastructure

To run the Terraform code, we need some plugin.

terraform init  //command to download plugins needed for provider

To validate Terraform code :

terraform validate

To run the Terraform code :

  • We must be in the directory where our tf file present and — auto-approve is to run immediately without asking confirmation.
  • In “terraaform apply” they will ask for the confirmation while in “terraform apply — auto approve” they won’t ask any confirmation.
terraform apply

With this command, AWS RDS and pod will created on Kubernetes.here we can use kubectl get all to see the running pod. And then use “kubectl describe all” command and see the minikube running IP and node number. Here the IP address is 192.168.99.100 and node number is 32123.

AWS RDS is created

To view the site, type the IP:Node number in the browser and configure the database details and run the WordPress.

Provide the information related to your database host, password etc. here and submit it to proceed. you will get database host endpoint here :

AWS => login => service => RDS => Databases => Select database => Endpoint

This endpoint is your database host endpoint.

Now, click here on run the installation to install the app and connect to your database.

Last but not least,

Destroying the whole setup we have done :

If some misbehaving happens or some other problem happens, you can destroy the whole setup just by using one command “terraform destroy”. It can destroy each and every thing created by using Terraform code only.

terraform destroy --auto-approve

In this blog we have used minikube from the local machine. In next blog we will see how to run the minikube in AWS EC2 instance.

Thank you!!

--

--

Binal Kagathara

DevOps Engineer| AWS Certified Solution Architect - Associate | IT student