Creating a replicaset using Terraform

Anurag Mittal
3 min readJun 21, 2020

--

There is as such no resource is available in Terraform for creating replicaset. So we have to use deployment.

Deployment is a way to manage the pods.

Environment Variables (ENV)

we have to set environment variables inside the container. To make them permanent for a container , we have to put these EV in file → /root/.bashrc
But is the Container got delete then we lost the EV we have set . So when we create a replica set for creating a pod than at that time put these EV inside the container in the RS . yml file to make them permanent.
From 1st RS we launch a database(Mysql) for the website running on wordpress container and from 2nd RS we launch a container for wordpress having php website. Expose the RS having php website so that client can connect to it.
These two containers can ping to each other with their ip .

Step 1. — Here I am creating one replicaset for launching MYSql pod . For creating MYSql pod i am using mysql.yml file. ENV variables are part of the container It is required to put environment variables inside the container.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: mysqlrs1

spec:
replicas: 1
selector:
matchLabels:
env: dev
template:
metadata:
name: mysqlpod1
labels:
env: dev
spec:
containers:
— name: “mysqlcon1”
image: “mysql:5.6”
env:
— name: MYSQL_ROOT_PASSWORD
value: redhat
— name: MYSQL_DATABASE
value: mylwdb
— name: MYSQL_USER
value: anurag
— name: MYSQL_PASSWORD
value: anuragpassword

Step 2. — Here I am creating one replicaset for launching Wordpress pod . For creating Wordpress pod i am using wp.yml file.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: wprs1

spec:
replicas: 1
selector:
matchLabels:
env: dev
app: wp
template:
metadata:
name: wppod1
labels:
env: dev
app: wp
spec:
containers:
— name: “wpcon1”
image: “wordpress:4.8-apache”

Step 3. — Now creating one terraform file for launching the deployment having Mysql pod

provider “kubernetes” {

config_context_cluster = “minikube”
}

resource “kubernetes_deployment” “mysql” {
metadata {
name = “mysql”

}

spec {
replicas = 1

selector {
match_labels = {
env = “dev”
dc = “IN”
app = “webserver”

}

match_expressions {

key = “dc”
operator = “In”
values = [“IN”,”US”]

}

match_expressions {

key = “env”
operator = “In”
values = [“dev” , “prod”]

}

match_expressions {

key = “app”
operator = “In”
values = [“webserver”]

}

}

template {
metadata {
name = “mysqlpod”
labels = {
env = “dev”
dc = “IN”
app = “webserver”
}
}

spec {
container {
image = “mysql:5.6”
name = “mysql”

env {
name = “MYSQL_ROOT_PASSWORD”
value = “redhat”
}
env {
name = “MYSQL_DATABASE”
value = “mylwdb”
}
env {
name = “MYSQL_USER”
value = “anurag”
}

env {
name = “MYSQL_PASSWORD”
value = “anuragpassword”
}

}
}

}
}
}

Step 4. — Now creating one terraform file for launching the deployment having Wordpress pod

provider “kubernetes” {

config_context_cluster = “minikube”
}

resource “kubernetes_deployment” “wordpress” {
metadata {
name = “mywp”

}

spec {
replicas = 1

selector {
match_labels = {
env = “dev”
dc = “IN”
app = “webserver”


}
match_expressions {

key = “dc”
operator = “In”
values = [“IN”,”US”]

}

match_expressions {

key = “env”
operator = “In”
values = [“dev” , “prod”]

}

match_expressions {

key = “app”
operator = “In”
values = [“webserver”]

}

}

template {
metadata {
name = “mywp”
labels = {
env = “dev”
dc = “IN”
app = “webserver”
}
}

spec {
container {
image = “wordpress:4.8-apache”
name = “wp”

}

}
}
}
}

Step 5. — Now from these two terraform files we can create a deployment for the wordpress and mysql .

we have to first initalize the terraform file — terraform init

After this apply terraform file that we have created for launching the setup- terraform apply.

Step 6. Expose the deployment for the wordpresss so that client can connect to the website.

Here is my github link — https://github.com/anurag08-git/rsbyterraform

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Anurag Mittal
Anurag Mittal

Written by Anurag Mittal

Learning the new technologies like mlops, devops, hybrid multi cloud to enhance my skills.

No responses yet

Write a response