Deploy wordpress on kubernetes using terraform with RDS

What we want to do…

Gauri Raskar
4 min readSep 2, 2020

--

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.

Let’s start….

First we will see what is 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.

First we will add providers

provider "kubernetes" {
config_context_cluster = "minikube"
}
provider "aws" {
region = "ap-south-1"
profile = "gauri"
}

Now we need to start minikube

to do that we will first open VirtualBox and start manually or fire “minikube start” minikube

Create RDS

Now let’s create RDS(It will be public facing RDS)along with security group allowing mysql port(3306)

While creating instance we will provide username and password for our database

RDS will be publicly accessible

/*
Create a security group for rds instance which will allow mysql port
*/
resource "aws_security_group" "mysql" {
name = "db"
description = "security group for webservers"
ingress {
description = "MYSQL"
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "rds sg"
}
}
/*
Create RDS with mysql engine
*/
resource "aws_db_instance" "rds-instance" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"vpc_security_group_ids = [aws_security_group.mysql.id]
engine_version = "5.7.30"
instance_class = "db.t2.micro"
name = "wordpressDB"
username = "gauri"
password = "gauri123"
parameter_group_name = "default.mysql5.7"

publicly_accessible = true
skip_final_snapshot = truetags = {
Name = "mywpdb"
}
depends_on = [aws_security_group.mysql]
}

Deploy and expose Wordpress

Now we will create deployment for our wordpress instance on kubernetes using terraform, to make our wordpress instance publicly accessible we will add nodeport using resource named “expose” this will allow 8080 port

We are using “output variable” called “myurl” ,using this we will get url for our wordpress instance as an output on console which can be directly used to hit our wordpress instance from browser

resource "kubernetes_deployment" "mywordpress" {metadata {
name ="mywordpress"
labels = {
app = "mywordpress"
}
}
spec {
replicas = 1
selector {
match_labels = {
env= "dev",
app= "mywordpress"
}
}
template {
metadata {
labels = {
env= "dev",
app= "mywordpress"
}
}
spec {
container {
image = "wordpress:4.8-apache"
name = "wordpress-ui"

env {
name = "WORDPRESS_DB_HOST"
value = aws_db_instance.rds-instance.address
}
env {
name = "WORDPRESS_DB_USER"
value = aws_db_instance.rds-instance.username
}
env {
name = "WORDPRESS_DB_PASSWORD"
value = aws_db_instance.rds-instance.password
}
env {
name = "WORDPRESS_DB_NAME"
value = aws_db_instance.rds-instance.name
}
env{
name="WORDPRESS_TABLE_PREFIX"
value="myblog"
}
}//container

}
}
}
}
resource "kubernetes_service" "expose" {
metadata {
name = "wordpress-expose"
}
spec {
selector = {
app = "${kubernetes_deployment.mywordpress.metadata.0.labels.app}"
}
port {
port = 8080
target_port = 80
}
type = "NodePort"
}
}
# Printing the Wordpress URL on the console
output "myurl" {
value = " Connect to the url : 192.168.99.100:${kubernetes_service.expose.spec[0].port[0].node_port}"
}

Now we can fire “terraform plan” command which will give us overall idea of the infrastructure we are going to create, also we can fire “terraform validate” command to see if we have any errors

Now finally lets create our infrastructure by using command “ terraform apply”

As mentioned above we will get output which will be an url for our wordpress instance

output

Outputs:myurl =  Connect to the url : 192.168.99.100:31389

Now lets hit our wordpress site

Fill in admin details and now you are good to go and you will see following output

You can see the url provided by mycurl output , now lets log in and add one simple post for testing purpose

We have successfully launched wordpress and created our first blog.

Conclusion

We created infrastructure as a code for wordpress with RDS and kubernetes using terraform

i would like to thank Mr. Vimal daga sir for such a great guidance and helping me enhance my technical skills in right way….

--

--