GCP : Create GKE Cluster and deploy an application

Reading Time: 5 mins

Overview

Google Kubernetes Engine (GKE) is the simplest and most common way of setting up a Kubernetes Cluster. You may be able to receive free credits for trying it out (though note that a free account comes with limitations). Either way, you will need to connect your credit card or other payment method to your google cloud account.

Note: Consider setting a cloud budget for your Google Cloud account in order to make sure you don’t accidentally spend more than you wish to.

Install gcloud CLI, kubectl

In this section, Install the required CLI’s in your local machine which will be used to connect to GKE clusters once deployed.

  • Download and install the gcloud command line tool at its install page. It will help you create and communicate with a Kubernetes cluster.
  • Install kubectl (reads kube control), it is a tool for controlling Kubernetes clusters in general or using command:
gcloud components install kubectl
# Login to google cloud:

gcloud auth login

# A new browser opens to authenticate, click Allow

# Use below command to list the config

gcloud config list

# To switch the project, Syntax:

gcloud config set project <PROJECT_ID>
  • Go to console.cloud.google.com and log in.
  • Navigate Kubernetes Engine API and enable the same
  • GKE clusters can be created from cloud console or via terminal. In this demo, I will cover the steps to deploy the cluster through cloud console and terminal (computer terminal)

Create GKE cluster through console

  • In Google cloud console, navigate to Compute > Kubernetes Engine > Clusters

  • Click on CREATE
  • You will be asked to select between GKE Standard and GKE Autopilot, for this demo I will go with GKE Standard – Configure
  • Cluster basics:
      • Name: Give a Name
      • Location type: select Zonal or Regional, # I have selected Zonal in order to specify exact zone
      • Control Plane version: Leave to default

  • Node Pools
      • Name: Give node pool a name
      • Size: Num of nodes # These are total num of vm instances to be deployed

      • Nodes: You can change the Series, Machine type, Boot disk type, Boot disk size, Max Pods per node
      • Security: You can leave to default service account or select from drop down
      • Metadata:  Leave to default
  • Cluster: You can leave to default, unless something to be changed
  • click on CREATE
  • Once the cluster is created and status turns green, click on the cluster > connect > command-line access

  • Copy the command and execute in your local machine
# Get the contexts

gcloud container clusters get-credentials captain-cluster-1 --zone asia-south1-a --project eknath-se
Fetching cluster endpoint and auth data.
kubeconfig entry generated for captain-cluster-1.

# Get the nodes
kubectl get nodes
NAME STATUS ROLES AGE VERSION
gke-captain-cluster--captain-first-po-c3c72618-4rk2 Ready <none> 110m v1.21.5-gke.1302

# Get contexts

kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* gke_eknath-se_asia-south1-a_captain-cluster-1 gke_eknath-se_asia-south1-a_captain-cluster-1 gke_eknath-se_asia-south1-a_captain-cluster-1

Create GKE cluster through terminal

Note: This section is optional if you also want to deploy GKE cluster using terminal. If you have already deployed a cluster through console then jump to next section to deploy a test application.

# Syntax to create a GKE cluster

gcloud container clusters create \ --machine-type n1-standard-2 \ --num-nodes 2 \ --zone <compute zone from the list linked below> \ --cluster-version latest \ <CLUSTERNAME>


# Example:

gcloud container clusters create --machine-type e2-medium --num-nodes 1 --zone asia-south1-a --cluster-version latest captain-cluster-fromcli

# Get context:

kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE

* gke_eknath-se_asia-south1-a_captain-cluster-fromcli gke_eknath-se_asia-south1-a_captain-cluster-fromcli gke_eknath-se_asia-south1-a_captain-cluster-fromcli

# Get the nodes from newly created cluster

kubectl get nodes
NAME STATUS ROLES AGE VERSION
gke-captain-cluster-from-default-pool-f793ad5a-fmbs Ready <none> 15m v1.21.6-gke.1500

# Get the pods from cluster

kubectl get pods -A
  • Replace <CLUSTERNAME> with a name that can be used to refer to this cluster in the future.
  • --machine-type specifies the amount of CPU and RAM in each node within this default node pool. There is a variety of types to choose from.
  • --num-nodes specifies how many nodes to spin up. You can change this later through the cloud console or using the gcloud command line tool.
  • --zone specifies the data center zone where your cluster will be created. You can pick something from this list that is not too far away from your users.
  • A region in GCP is a geographical region with at least three zones, where each zone is representing a datacenter with servers etc.
      • A regional cluster creates pods across zones in a region(three by default), distributing Kubernetes resources across multiple zones in the region. This is different from the default cluster, which has all its resources within a single zone(as shown above).
      • A regional cluster has Highly Available (HA) kubernetes api-servers, this allows jupyterhub which uses them to have no downtime during upgrades of kubernetes itself.
      • They also increase control plane uptime to 99.95%.
      • To avoid tripling the number of nodes while still having HA kubernetes, the --node-locations flag can be used to specify a single zone to use.

Deploy an application

  • In this demo, I will be using the images stored in gcr.io (google container registry).
#Login to gcr.io with docker login command using downloaded json key. Steps to create a service account and generating a key is documented in http://captainvirtualization.com/tap-prepare/#Image_Repo_GCR_-_Optional

docker login -u _json_key -p "$(cat eknath-se-ccc86.json)" https://gcr.io
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /home/ubuntu/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

# Deploy an application. In below example, spring-deploy is the name of deployment, image used is gcr.io/eknath-se/test-repo/tbs-spring-image:latest

# Create a deployment

kubectl create deployment spring-deploy --port=8080 --image=gcr.io/eknath-se/test-repo/tbs-spring-image:latest --replicas=2
deployment.apps/spring-deploy created

# Expose the deployment

kubectl expose deployment spring-deploy --port=8080 --type=LoadBalancer
service/spring-deploy exposed

# Collect the External IP of service spring-deploy:

kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.116.0.1 <none> 443/TCP 44m
spring-deploy LoadBalancer 10.116.7.130 35.200.175.199 8080:32106/TCP 50s
  • Access the external ip from browser: