Setting up your project on GCP fast using Terraform and Kubernetes

Automated setup with Terraform

Kubernetes on Google Cloud Platform

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

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

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Downgrade MongoDB replica set to a standalone node

Enigma — Development Log #1

Achieving Compile-Time Localization Validation

How to effectively build and manage a QA team?

Band Protocol August 2020 Community Update

Split-Apply-Combine Strategy for Data Mining.

An idea of how to use the Pair class in Kotlin

Deploy Kotlin Apps to Kubernetes without Dockerfiles

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

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

More from Medium

How To Set up a Simple Kubernetes Cluster on Google Cloud Platform

GKE Ingress SSL with Google Managed Certificates

AWS open5gs K8S High Availability [part 3]

Never experiment and use Google Cloud Platform