Creating Replica Controller for Kubernetes using Terraform
Kubernetes is an open-source container-orchestration system for automating application deployment, scaling, and management.It aims to provide a “platform for automating deployment, scaling, and operations of application containers across clusters of hosts”. It works with a range of container tools, including Docker
Whenever we create a pod,most of the time they give a random names to pods.So it hard to monitor these pods using there names.Hence we give unique labels to them.
Kubernetes has a program called Replica Controller that has capability to keep on eye /monitor these pods.If in case any pod fails,it relaunch that pod.
Terraform is an open-source infrastructure as code software tool created by HashiCorp. It enables users to define and provision a datacenter infrastructure using a high-level configuration language known as Hashicorp Configuration Language, or optionally JSON.
JOB:- create a replica controller for wordpress and mysql and wordpress store its data in mysql.
provider “kubernetes”{
config_context_cluster = “minikube”
}
resource “kubernetes_replication_controller” “mysql_rc” {
metadata {
name = “mysql-rc”
labels = {
dc = “IN”
}
}spec {
replicas = 1
selector = {
dc = “IN”
}
template {
metadata {
labels = {
dc = “IN”
env = “dev”
}
}spec {
container {
image = “mysql:5.6”
name = “mysqlcon1”
env{
name = “MYSQL_ROOT_PASSWORD”
value = “redhat”
}
env{
name = “MYSQL_DATABASE”
value = “mylwdb”
}
env{
name = “MYSQL_USER”
value = “himanshu”
}
env{
name = “MYSQL_PASSWORD”
value = “himanshu”
}}
}
}
}
}resource “kubernetes_replication_controller” “wp_rc” {
metadata {
name = “wp-rc”
labels = {
dc = “US”
}
}spec {
replicas = 1
selector = {
dc = “US”
app = “wp”
}
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”
}
}
}
}
}
}
After exposing the pod we give the pod ip to the client.Client directly cannot access the container.
Now we can this in this screenshot with following commands:-
# terraform init
# terraform apply -auto-approve
# kubectl get all
Thank you so much for giving your time to my article..