Connecting WordPress with AWS RDS Database
Today we’ll learn about how to connect WordPress with the RDS Database of AWS.
What is RDS?
Amazon Relational Database Service (Amazon RDS) is a web service that makes it easier to set up, operate, and scale a relational database in the AWS Cloud. It provides cost-efficient, resizable capacity for an industry-standard relational database and manages common database administration tasks.
What is Minikube?
Minikube was developed to allow users to run Kubernetes locally. When you install and run Minikube on your computer, it runs a single-node Kubernetes cluster inside a virtual machine.
Pre-requisites
Minikube
Terraform
AWS CLI
Steps-
Running Minikube
In order to run Minikube in your machine, it should be pre-installed.
To start a local cluster —
minikube start
Link for installation -
https://kubernetes.io/docs/tasks/tools/install-minikube/
Code -
Mention the provider at the starting of Terraform code-
following is the code to let Terraform that we’ll be working on AWS, along with credentials like the version, region, and profile for the user.
provider "aws" {
version = "~> 2.0"
region = "ap-south-1"
profile = "default"
}
Mention provider Kubernetes-
provider "kubernetes" {
config_context_cluster = "minikube"
}
Create a PVC for WordPress with 5 GB of storage-
Create a PVC in K8S with 5 GI of storage and mount it to /var/www/html.
resource "kubernetes_persistent_volume_claim" "pvc" {
metadata {
name = "pvc"
labels = {
app = "wordpress"
}
}
spec {
access_modes = ["ReadWriteMany"]
resources {
requests = {
storage = "5Gi"
}
}
}
}
Create a deployment in Kubernetes-
Create a deployment in K8S means that it will run the pods and will also ensure that if a pod is down or gets terminated, the number of pods needed to be launched has to specified by the user. Also, create a container with WordPress.
resource "kubernetes_deployment" "wp-deploy" {
metadata {
name = "wp"
labels = {
app = "wordpress"
}
}
spec {
replicas = 2
selector {
match_labels = {
app = "wordpress"
}
}
template {
metadata {
labels = {
app = "wordpress"
}
}
spec {
volume {
name = "wp-pvc"
persistent_volume_claim {
claim_name = "pvc"
}
}
container {
image = "wordpress"
name = "wp-container"
port {
container_port = 80
}
volume_mount {
name = "wp-pvc"
mount_path = "/var/www/html"
}
}
}
}
}
}
Create a service in K8S-
By using NodePort we expose the current deployment to the outer world so that when RDS is launched it can be connected to the same.
resource "kubernetes_service" "expose" {
metadata {
name = "wpservice"
labels = {
app = "wordpress"
}
}
spec {
selector = {
app = "wordpress"
}
port {
node_port = 31000
port = 80
target_port = 80
}
type = "NodePort"
}
}
Creating an AWS RDS Instance -
Create an rds instance with 10 gigs of storage, and remember the name, username, and password of the instance so that the same can be used while connecting it to WordPress.
resource "aws_db_instance" "rds" {
allocated_storage = 10
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
identifier = "wordpress-db"
name = "task_6"
username = "root"
password = "redhat123"
port = 3306
skip_final_snapshot = true
publicly_accessible = true
apply_immediately = true
}
Print the endpoint of RDS as Output in Terraform -
output in Terraform can be generated by using output with a tag and the value.
output "endpoint" {
value = aws_db_instance.rds.endpoint
}
Outputs-
Github Link -
Note-
Make sure to destroy the currently created environment once the task is successful, to avoid unnecessary AWS billing.
terraform delete
Thank you for reading my blog, and hope it was helpful.
Link for installation -
https://kubernetes.io/docs/tasks/tools/install-minikube/