WORDPRESS AND MYSQL integration USING AWS RDS AND KUBERNETES
Mohamed Afrid
DevOps Engineer | 3x RedHat | 1x AWS | 3x Azure | CKA | CKS | Terraform Certified
Amazon RDS is the Relational Database Service offered as a web service by Amazon. It makes it easy to set-up and operate a relational database in the cloud. It provides a very cost-effective way to use industry’s leading RDBMS software as a managed service. Because of this web service from AWS, You do not have to buy any server or install any database software in it.
You just need to have an AWS Account and start using the RDS features. A cloud administrator uses Amazon RDS to set up, manage and scale a relational database instance in the cloud. The service also automatically backs up RDS database instances, captures a daily snapshot of data and retains transaction logs to enable point-in-time recovery.
OUTLINES
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.
CONFIGURING TERRAFORM AND AWS
provider "aws" { region = "us-east-2" profile = "cloudafrid" }
VPC AND SUBNET
Amazon Virtual Private Cloud (Amazon VPC) lets you provision a logically isolated section of the AWS Cloud where you can launch AWS resources in a virtual network that you define. You have complete control over your virtual networking environment, including selection of your own IP address range, creation of subnets, and configuration of route tables and network gateways.
Subnet is “part of the network” , in other words, part of a Virtual Private Cloud (VPC). Each subnet must reside entirely within one Availability Zone and cannot span zones.
data "aws_vpc" "default_vpc" { default = true } data "aws_subnet_ids" "default_subnet" { vpc_id = data.aws_vpc.default_vpc.id }
CREATING SECURITY GROUP FOR RDS
A security group is also needed to be attached to the instance for the security of the OS, so that anyone cannot enter , only some specific ports are allowed to enter. The code for the same can be written as
resource "aws_security_group" "mysg" { name = "rds_sg" ingress { description = "SSH" from_port = 3306 to_port = 3306 protocol = "tcp" cidr_blocks = [ "0.0.0.0/0" ] } }
After running the terraform code the security group created is as
CREATING RDS FOR MYSQL
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.
resource "aws_db_instance" "default" { allocated_storage = 10 identifier = "wordpress-db" storage_type = "gp2" engine = "mysql" engine_version = "5.7.30" instance_class = "db.t2.micro" name = "mydb" username = "root" password = "redhatdbpass" port = 3306 parameter_group_name = "default.mysql5.7" iam_database_authentication_enabled = true vpc_security_group_ids = [ "${aws_security_group.mysg.id}" ] skip_final_snapshot = true deletion_protection = false publicly_accessible = true }
After running the terraform code the RDS created is as
CREATING KUBERNETES DEPLOYMENT FOR WORDPRESS
A Kubernetes deployment is a resource object in Kubernetes that provides declarative updates to applications. A deployment allows you to describe an application’s life cycle, such as which images to use for the app, the number of pods there should be, and the way in which they should be updated.
Lets create a provider for kubernetes, so that our code can contact the specific cluster
provider "kubernetes"{ config_context_cluster = "minikube" }
resource "kubernetes_replication_controller" "wp_rc" { metadata { name = "wp-rc" labels = { dc = "US" } } spec { replicas = 1 selector = { dc = "US" app = "wp" } # port { # node_port = 30200 # port = 80 # target_port = 80 # } # type = "NodePort" template { metadata { labels = { dc = "US" app = "wp" } } spec { container { image = "wordpress:4.8-apache" name = "wpcon1" env{ name = "WORDPRESS_DB_HOST" value = kubernetes_replication_controller.mysql_rc.spec[0].template[0].spec[0].hostname } env{ name = "WORDPRESS_DB_USER" value = "himanshu" } env{ name = "WORDPRESS_DB_PASSWORD" value = "himanshu" } env{ name = "WORDPRESS_DB_NAME" value = "mywpdb" } } } } } }
CREATING KUBERNETES SERVICE TO EXPOSE THE DEPLOYMENT
In Kubernetes, a Service is an abstraction which defines a logical set of Pods and a policy by which to access them. It is an abstract way to expose an application running on a set of Pods as a network service.
resource "kubernetes_service" "wp_service" { depends_on = [ kubernetes_deployment.wp_rc, ] metadata { name = "wp-service" } spec { selector = { app = "wp" } port { port = 80 target_port = 80 node_port = 30050 } type = "NodePort" } }
After running the terraform code the Kubernetes Deployment and Service is as
OPENING THE DEPLOYED APPLICATION USING CHROME AUTOMATICALLY
resource "null_resource" "ChromeOpen" { depends_on = [ kubernetes_service.wp_service, ] provisioner "local-exec" { command = "minikube service ${kubernetes_service.wp_service.metadata[0].name}" } }
The Chrome browser opens the Deployment URL with the exposed Port and the WebPage looks like
Select the Language accordingly and Click "Continue".
Give the relevant information about the Website title , Username and Password and Click on "Install WordPress".
You can Login by using your created UserName and Password. The Website created looks like
CREATING THE INFRASTRUCTURE
You need to go inside the folder where the .tf file has been made and run the command as
to download all the plugins required to run the code.
terraform init
Run the following command to check if there is some mistake in the code.
terraform validate
If the output of the above command is "Success" then deploy the infrastructure by using the command as
terraform apply
The complete architecture can be destroyed by using a single command as
terraform destroy
CONCLUSION
You can deploy your own Application or any Application on Kubernetes or AWS EKS or Fargate Cluster etc. The DataBase used for these Applications can be deployed on AWS RDS. AWS RDS makes it easy to set up, operate, and scale a relational database in the cloud i.e, you do not need to have to buy extra resources and set up security for your Database. AWS RDS provides all this within low costs,