Setting up your project on GCP fast using Terraform and Kubernetes

Automated setup with Terraform

Kubernetes on Google Cloud Platform

Before you begin

Terraform and GCP setup step-by-step

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!

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