Using Terraform to Create a Kubernetes Deployment

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

02:00:00

Difficulty

Advanced

We have just landed a big client for our blog hosting business. However, they have several requirements that we must meet. They want their blog hosted on Kubernetes and they want to use Wordpress instead of Ghost blog. Because their sites have high traffic, the solution needs to support scalability. This is a huge opportunity for us and it can lead to hosting several of more their blogs. To fulfill their requirements, we will be using Terraform to create a Kubernetes deployment for Wordpress and MySQL. Both deployments will be using services to route traffic to the correct set of pods.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Using Terraform to Create a Kubernetes Deployment

The Scenario

We have just landed a big client for our blog hosting business. However, they have several requirements that we must meet. They want their blog hosted on Kubernetes and they want to use Wordpress instead of Ghost blog. Because their sites have high traffic, the solution needs to support scalability. This is a huge opportunity for us and it can lead to hosting several of more their blogs. To fulfill their requirements, we will be using Terraform to create a Kubernetes deployment for Wordpress and MySQL. Both deployments will be using services to route traffic to the correct set of pods.

Logging In

Use the IP address and credentials provided on the hands-on lab overview page, and log in with SSH to the server.

All of the examples here involve using Vim, but use whichever text editor is most comfortable.

Instructions

We need to create two files, variables.tf and main.tf, and deploy them.

Create the Variables File

Create variable.tf:

vi variable.tf

variable.tf contents:

variable "app_label" {
  default = "wordpress"
}

variable "mysql_tier" {
  default = "mysql"
}

variable "wordpress_tier" {
  default = "frontend"
}

variable "wordpress_version" {
  default = "4.7.3"
}

variable "mysql_password" {
  default = "P4sSw0rd0!"
}

Create the Main Terraform File

Create main.tf:

vi main.tf

main.tf contents:

resource "kubernetes_service" "mysql_service" {
  metadata {
    name = "wordpress-mysql"
    labels = {
      app = "${var.app_label}"
    }
  }
  spec {
    selector {
      app  = "${var.app_label}"
      tier = "${var.mysql_tier}"
    }
    port {
      port = "3306"
    }

    type = "NodePort"
  }
}

resource "kubernetes_deployment" "mysql_deployment" {
  metadata {
    name   = "wordpress-mysql"
    labels = {
      app = "${var.app_label}"
    }
  }

  spec {
    replicas = "1"

    selector {
      match_labels {
        app  = "${var.app_label}"
        tier = "${var.mysql_tier}"
      }
    }

    template {
      metadata {
        labels {
          app  = "${var.app_label}"
          tier = "${var.mysql_tier}"
        }
      }

      spec {
        container {
          name  = "mysql"
          image = "mysql:5.7"

          env {
            name  = "MYSQL_ROOT_PASSWORD"
            value = "${var.mysql_password}"
          }

          port {
            container_port = "3306"
            name           = "mysql"
          }
        }
      }
    }
  }
}

resource "kubernetes_service" "wordpress_service" {
  metadata {
    name   = "wordpress"
    labels = {
      app = "${var.app_label}"
    }
  }
  spec {
    selector {
      app  = "${var.app_label}"
      tier = "${var.wordpress_tier}"
    }

    port {
      port        = "80"
      target_port = "80"
      node_port   = "8080"
    }

    type = "NodePort"
  }
}

resource "kubernetes_deployment" "wordpress_deployment" {
  metadata {
    name = "wordpress"
  }

  spec {
    replicas = "1"

    selector {
      match_labels {
        app  = "${var.app_label}"
        tier = "${var.wordpress_tier}"
      }
    }

    template {
      metadata {
        labels {
          app  = "${var.app_label}"
          tier = "${var.wordpress_tier}"
        }
      }

      spec {
        container {
          name  = "wordpress"
          image = "wordpress:${var.wordpress_version}-apache"

          env {
            name = "WORDPRESS_DB_HOST"
            value = "wordpress-mysql"
          }

          env {
            name  = "WORDPRESS_DB_PASSWORD"
            value = "${var.mysql_password}"
          }

          port {
            container_port = "80"
            name           = "wordpress"
          }
        }
      }
    }
  }
}

Deploy the infrastructure

Initialize Terraform:

terraform init

Validate the files:

terraform validate

Deploy the pod:

terraform apply -auto-approve

Conclusion

Well, we've done it. We can wait a few minutes, then just navigate to the lab server's public IP (on port 8080) in a web browser. We'll land at a Wordpress install that's been waiting for us. Congratulations!