Kubernetes on Google Cloud Platform

This is the second part in a series of posts about on getting started with Kubernetes. In my previous post, I covered how to setup single node of Kubernetes Cluster locally using Minikube and discussed a few issues which I came across during its setup. Since Kubernetes comes with its own toolset, we can pretty much configure and run the software on multiple cloud providers, on-premise servers and even our local machine. This post is a step by step guide on setting up a highly available multi-zone Kubernetes cluster using Google Cloud Platform(GCP). Google Cloud Platform has excellent support for Kubernetes through the Google Container Engine(GKE) that provides a Container Engine service built on top of Kubernetes. This makes managing a Kubernetes cluster a breeze. In this post, I am going to detail on how to spin up multi-node Kubernetes cluster on GKE (Google Container Engine).

Kubernetes is a container orchestration software that started at Google. A cluster has a set of nodes (think computers) on which we have pods, where each pod is a unit that contains several containers. In simple terms, it’s about managing a cluster (a set of machines) on which we have containers running. To follow this post, you should have a minimal understanding of Kubernetes and Google Container Engine.


Setup And Configure Google Cloud Platform

In this section, I will be talking about pre-requisites to configure Google Cloud Platform and different ways to communicate to Google Cloud platform. Note:-Users of Google Cloud Platform are eligible for a $300 free trial.

Prerequisites – To follow this post,

  1. You should have a Google Cloud account
  2. Create a Google Cloud Project
      i) Go to Google Cloud Platform console
      ii) Create a project. My project is “kubernetes-project”. Every project has an id that you’re mostly working with. My PROJECT ID is “kubernetes-project-184510”. This is a unique name across all Google Cloud projects

Ways to communicate to Google Cloud platform.

  • Once you are done with the basic setup, now we can create a cluster. One way of doing it is you can initialize and configure from a console window on the local window using Google Cloud SDK(the Google command line interface for Google Cloud platform). If you don’t have Google Cloud SDK installed, you can follow this tutorial for the setup Google Cloud SDK. It explains how to install the Google Cloud SDK, initialize it and run core gcloud commands from the command-line. Once you have installed the Google Cloud SDK, initialize and configure the gcloud CLI. Once this set up is done your local system is communicating with your Google Cloud Platform account and project. After completing this step you will be able to create a Kubernetes cluster.
  • The second way of communicating with Google Cloud Platform is by using Google Cloud Shell. Google Cloud interactive shell, is a browser-based command line environment running in the cloud. This Debian-based Docker container is loaded with all the development tools like docker, gcloud, kubectl. Google Cloud Shell comes pre-installed with the Google Cloud SDK. You can easily manage your projects and resources without having to install the Google Cloud SDK or other tools on your system. I have used Google Cloud interactive shell for executing the Kubernetes cluster creation commands in the below sections.

  • Creating Kubernetes Cluster On GKE

    GKE Cluster can be created in two ways: via the gcloud Interactive Shell or via the Cloud Platform Console. Both the methods use Google Container Engine (GKE) for creating clusters. In below sections, I will provide you with the steps for setting up a single and multi-zone Kubernetes cluster using both the methods.

    Cluster creation via Google Cloud Shell

    You can create GKE cluster by following simple commands on Google Shell.

  • Enable the Container and Compute Engine APIs Container Engine API and Compute Engine API.
  • Activate Google Cloud Shell – Activate Google Cloud Shell by selecting the project(in
    my case ‘kubernetes-project’) created from the Google Cloud Platform dashboard. Then select the Activate Google Cloud Shell button in the top nav. Once connected, you are already authenticated and the PROJECT_ID environment variable is already set by default. Note: As mentioned earlier, Google Cloud Shell comes pre-installed with the Google Cloud SDK and built-in authorization for access to projects and resources hosted on Google Cloud Platform.
  • Google Cloud Interactive Shell
  • Create Cluster using Gcloud shell – Below are the set of commands used to create one zone Kubernetes cluster using gcloud interactive shell.
    $ CLUSTER_NAME=kubernetes-lab1
     
    $ gcloud config set container/cluster ${CLUSTER_NAME} 
     
    $ MACHINE_TYPE=n1-standard-2   
     
    # By default this spins up a 3-node cluster. You can change the default with `--num-nodes VAL`
    $ gcloud container clusters create ${CLUSTER_NAME} --zone europe-west1-d --scopes storage-rw --machine-type ${MACHINE_TYPE}

    By default, if we do not specify the number of nodes and their types, then Container Engine will use three n1-standard-1 (1 vCPU, 3.75 GB memory) Compute Engine VMs for the cluster. In this example, I am creating a Kubernetes cluster in ‘europe-west1-d’ zone with three instances (nodes) per zone using machine type ‘n1-standard-2’. The actual command would look like the following.

    $ gcloud container clusters create kubernetes-lab1 \
             --zone europe-west1-d \
             --scopes storage-rw \
             --machine-type n1-standard-2

    You can also create a multi-zone cluster using below command

    gcloud container clusters create kubernetes-lab1 \
          --zone europe-west1-d \
          --additional-zones europe-west1-a,europe-west1-b \
          --scopes storage-rw 
          --machine-type n1-standard-2

    Note that –scopes specifies scopes for the node instances. Multiple comma-delimited scopes can be specified. The scopes necessary for the cluster to function properly (compute-rw, storage-ro), are always added, even if not explicitly specified. I initially created the Kubernetes cluster without defining a permission scope. Hence I was getting “googleapi: Error 403: Insufficient Permission” error when I tried to access the node instances. If no scope is set when the cluster is created, the default scope is assigned and this only provides read permission for Cloud Storage. If you haven’t assigned your cluster with the correct scope, the nodes in the cluster would not have the required authorization/permission to write to Google Cloud Storage. Later I managed to create a cluster with ‘storage-rw’ scope and everything worked as expected.

  • We can also check the status on the GCP Console. As a sanity check, to make sure the cluster is up and running via kubectl, use below command.

    $ kubectl get all
    NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
    svc/kubernetes   10.0.0.1             443/TCP   22s

    Since the cluster is fully setup with the Kubernetes platform we can start interacting with it by using the kubectl command as shown below

    $ kubectl cluster-info
    Kubectl Cluster Information

    As you can see, the details contain the Kubernetes Master URL and other URLs for various Kubernetes services, one of which is the Kubenetes-Dashboard which is the Web UI running on the Kubernetes Master.

    The new cluster will also appear in the Container Engine -> Kubernetes cluster section within Google Cloud Platform:

    Container Engine – Kubernetes cluster

    Cluster creation via Cloud Platform Console

    Creating a cluster using Google Cloud Console is pretty easy and straight forward.

  • Go to Container Engine page, which can be found via the hamburger menu in the top left. Next, go to Container clusters and select Create a container cluster as shown in the below image

    Cluster Creation using Google Cloud Console
  • In this example, I am creating a Kubernetes cluster in ‘us-central1-a’ zone with three instances (nodes) per zone using machine type ‘n1-standard-1’
  • Select Create once you are done with the configuration. This will take a few minutes to complete. Once the cluster is created you should see something like this:

    Cluster created in Google Cloud Console
  • The nodes are managed by Compute Engine VMs and Kubernetes master is managed by Container Engine. Since the nodes are managed by Compute Engine VMs, we can also SSH into them. However, you cannot SSH into Kubernetes Master. Now you have a Kubernetes Cluster ready to use! Make sure that you turn off the VM’s after you finish your work.

    Cluster Node created
  • Once the cluster is up and running (this took few minutes to start up), we need to point kubectl to this cluster. Since the cluster is fully setup with the Kubernetes platform we can start interacting with it by using the kubectl command
    $ gcloud container clusters get-credentials kubernetes-lab2

    Note: If you are using an existing Kubernetes Engine cluster or if you have created a cluster through Google Cloud Platform Console, you need to run the above command to retrieve cluster credentials and configure kubectl command-line tool with them. If you have already created a cluster with the gcloud container clusters create command listed above, this step is not necessary.


    In this post, we learned how to
    1) Setup and configure Google Cloud Platform
    2) Set up a kubernetes cluster using Google Cloud Shell and Google Cloud Console.

    If you liked what you read, know more about Qxf2.


    References

    1) Get Started With Kubernetes
    2) Spinning Up Your First Kubernetes Cluster on GKE
    3) googleapi: Error 403: Insufficient Permission


  • Leave a Reply

    Your email address will not be published. Required fields are marked *