Setting up your project on GCP fast using Terraform and Kubernetes

Automated setup with Terraform

When we need to set up a cloud infrastructure for the project, we want to make it quickly. That’s when automation tools come to help. Terraform is one of the instruments that serve for automated setup, and it is compatible with more than 70 providers, which is beyond handy. Besides, Terraform is pretty straightforward to get along with thanks to its declarative language. You don’t need to get used to a completely different CLI, like in cases when you switch to another cloud provider.

Kubernetes on Google Cloud Platform

Being one of the native Google products, Kubernetes is integrated into GCP quite as is, with no alterations. So you can conveniently set it up and manage it through the GCP CLI or with the help of external instruments like Terraform.

Before you begin

  1. Create a Google Cloud project
  2. Create a service account key and download it in JSON format

Terraform and GCP setup step-by-step

Create a new directory for the project and create a main.tf file for the Terraform config, and populate it with the following content:

provider "google" {
credentials = file("CREDENTIALS_FILE.json")
project = "your-project"
region = "us-west1"
}
variable "credentials" {
type = string
}
variable "project" {
type = string
}
variable "region" {
type = string
}
credentials = "CREDENTIALS_FILE.json"
project = "your-project"
region = "us-west1"
provider  "google" {
credentials = file(var.credentials)
project = var.project
region = var.region
}
terraform init
Terraform has been initialised!
variable "cluster_name" {
type = string
}
variable "cluster_zone" {
type = string
}
variable "app_name" {
type = string
}
cluster_name = “cluster-1”
cluster_zone = “us-west1-a”
app_name = “test”
resource "google_container_cluster" "cluster-1" {
name = var.cluster_name
location = var.cluster_zone
initial_node_count = 3
node_config {
labels = {
app = var.app_name
}
tags = ["app", var.app_name]
}

timeouts {
create = "30m"
update = "40m"
}
}
output "cluster" {  value = google_container_cluster.cluster-1.name}
resource "kubernetes_deployment" "example" {
metadata {
name = "terraform-example"
labels = {
app = var.app_name
}
}
spec {
replicas = 3
selector {
match_labels = {
app = var.app_name
}
}
template {
metadata {
labels = {
app = var.app_name
}
}
spec {
container {
image = "nginx:1.7.8"
name = "example"
resources {
limits = {
cpu = "0.5"
memory = "512Mi"
}
requests = {
cpu = "250m"
memory = "50Mi"
}
}
liveness_probe {
http_get {
path = "/"
port = 80
http_header {
name = "X-Custom-Header"
value = "Awesome"
}
}
initial_delay_seconds = 3
period_seconds = 3
}
}
}
}
}
}
terraform plan -var-file=secrets.tfvars
terraform apply -var-file=secrets.tfvars

Good job!

You’re almost done with your journey, the cluster is up and running, and you can be (ugh) proud of yourself!

Useful links

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Valor Software

Valor Software

21 Followers

Useful articles from experienced Valor specialists in various spheres of digital development.