Video Thumbnail for Lesson
3.3: Remote GKE Cluster

Provisioning a Google Kubernetes Engine Cluster

In this section, you will set up a Kubernetes cluster using Google Cloud Platform (GCP).

1. Authenticating and Configuring the GCP CLI

Before creating the cluster, you need to authenticate and configure the GCP CLI. Follow these steps to initialize the gcloud CLI.

# task gcp:01-init-cli
#  - Authenticate and configure the gcloud CLI.
gcloud init

2. Enabling Necessary APIs

GCP does not enable many of its APIs by default. In order to use the various services required, we must first enable those APIs.

# task gcp:02-enable-apis
#  - Enable necessary APIs
gcloud services enable \
  compute.googleapis.com \
  container.googleapis.com \
  cloudresourcemanager.googleapis.com \
  iam.googleapis.com \
  secretmanager.googleapis.com \
  servicemanagement.googleapis.com \
  serviceusage.googleapis.com

3. Setting Default Region and Zone

We then need to set the default region and zone to avoid needing to pass them into each command.

# task gcp:03-set-region-and-zone
# - Set default region and zone
gcloud config set compute/region ${GCP_REGION}
gcloud config set compute/zone ${GCP_ZONE}

4. Creating the VPC

Again, we could deploy a cluster into the default Virtual Private Cloud (VPC) but it is better practice to avoid using the default VPC and create a new one.

# task gcp:04-create-vpc
#  - Create VPC.
gcloud compute networks create ${CLUSTER_NAME} --subnet-mode=custom

5. Creating the Subnet

Before we can deploy compute resources into the VPC, we must create a subnet.

# task gcp:05-create-subnet
#  - Create subnet
gcloud compute networks subnets create subnet-1 \
  --network=${CLUSTER_NAME} \
  --region=${GCP_REGION} \
  --range=10.0.0.0/20

6. Creating the GKE Cluster

With the network and subnet created, you can now create the GKE cluster. This will take a few minutes.

🚨 Note: You will need to look up GCP project ID to use in the --workload-pool option. This will allow us to use the GKE Workload Identity feature to authenticate to other GCP services dynamically.

# task gcp:06-create-cluster
#  - Create GKE cluster
gcloud container clusters create ${CLUSTER_NAME} \
  --zone ${GCP_ZONE} \
  --network ${CLUSTER_NAME} \
  --subnetwork subnet-1 \
  --machine-type e2-standard-2 \
  --num-nodes 2 \
  --gateway-api=standard \
  --workload-pool={{.GCP_PROJECT_ID}}.svc.id.goog

7. Connecting to the GKE Cluster

After the cluster finishes creating, you can retrieve the kubeconfig file for your cluster to connect with kubectl.

# task gcp:08-connect-to-cluster
#  - Connect to the GKE cluster
gcloud container clusters get-credentials ${CLUSTER_NAME} --zone ${GCP_ZONE}

You should now have a fully functioning GCP Kubernetes cluster. Verify by running:

kubectl get nodes
  1. Cleaning Up the Cluster

After completing your work with the cluster, you can clean up by deleting the cluster and associated resources.

# task gcp:09-clean-up
#  - Clean up the GCP Kubernetes cluster and associated resources.
gcloud container clusters delete ${CLUSTER_NAME} --zone ${GCP_ZONE} --quiet
gcloud compute networks subnets delete subnet-1 --region=${GCP_REGION} --quiet
gcloud compute networks delete ${CLUSTER_NAME} --quiet