GKE & Ocean Creation

Importing of an existing Kubernetes cluster

GCP GKE


Ocean Kubernetes terraform modules


Terraform a GKE cluster powered by Ocean

The instructions below will walk you through the full creation of an Ocean powered GKE cluster. Copy all code snippets into a single terraform file and make sure to update all required variables as described below.

Step 1: Populate the variables

##populate variables 
variable "spotinst_account" {
  type = string
  default = "insert_spot_account_id format: act-00000000"
}

/* admin token */
variable "spotinst_token" {
  type = string
  default = "insert_toke_here"
}

variable "cluster_name" {
  type = string
  default = "my-gke-test-cluster"
  description = "(Required) The GKE cluster name."
}

variable "region" {
  type = string
  default = "us-east1"
  description = "(Required) GCP Region for provisioning regional resources."
}

variable "zone" {
  type = string
  default = "us-east1-b"
  description = "(Required) GCP Zone for provisioning zonal resources."
}

variable "project_id" {
  type = string
  default = "sales-labs"
  description = "(Required) Project text ID."
}


Step 2: Setup the required terraform providers

terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 4.0"
    }
    spotinst = {
      source  = "spotinst/spotinst"
      version = "~> 1.117.0" 
    }
  }
}

provider "spotinst" {
  account = var.spotinst_account
  token   = var.spotinst_token
}

provider "google" {
  project     = var.project_id
  region      = var.region
  credentials = file("sales-labs-4b5cedf6adb8.json") ## see workshop prerequisites if you need to create this file
}

provider "kubernetes" {
  host  = "https://${google_container_cluster.primary.endpoint}"
  token = data.google_client_config.default.access_token
  cluster_ca_certificate = base64decode(google_container_cluster.primary.master_auth[0].cluster_ca_certificate)
}

### data resources ###
data "google_client_config" "default" {}

data "google_compute_image" "COS" {
  family  = "cos-stable"
  project = "gke-node-images"
}

Step 3: Create a GCP VPC for the test cluster

##create a VPC 
resource "google_compute_network" "vpc" {
  name                    = "${var.cluster_name}-vpc"
  auto_create_subnetworks = "false"
  project                 = var.project_id
}

resource "google_compute_subnetwork" "subnet" {
  name          = "${var.cluster_name}-subnet"
  region        = var.region
  network       = google_compute_network.vpc.name
  ip_cidr_range = "10.10.0.0/24"
  
}

Step 4: Create the GKE test cluster using the GKE terraform resource

resource "google_container_cluster" "primary" {
  name     = var.cluster_name
  location = var.region
  project  = var.project_id
  remove_default_node_pool = false
  initial_node_count       = 1
  network    = google_compute_network.vpc.name
  subnetwork = google_compute_subnetwork.subnet.name
}

Step 5: Create the Ocean cluster

module "ocean-gcp-k8s" {
  source     = "spotinst/ocean-gcp-k8s/spotinst"

  cluster_name                      = var.cluster_name
  location                          = var.region
  use_as_template_only              = true
  
  depends_on = [google_container_cluster.primary]
}

Step 6: Install the Ocean controller in your GKE cluster

module "ocean-controller" {
  source     = "spotinst/ocean-controller/spotinst"
  

  # Credentials.
  spotinst_account = var.spotinst_account
  spotinst_token   = var.spotinst_token

  tolerations = []
  cluster_identifier = module.ocean-gcp-k8s.ocean_controller_id
  
  depends_on = [module.ocean-gcp-k8s]

}

Step 7: Run the newly created terraform file using the following commands

terraform init 
terraform apply