Building a GKE with Cloud SDK

Provision Google Kubernetes Engine with Google Cloud SDK

Joaquín Menchaca (智裕)
The Startup
Published in
4 min readJun 19, 2020

--

I have a friend that wants to learn Kubernetes, and so I thought: why don’t I kick of a zero-to-hero series on Kubernetes on Google Cloud with GKE (Google Kubernetes Engine).

This series will start with provisioning GKE cluster, deploying a stateless web application, and adding integration with DNS and TLS certificates.

For this first article, the focus will be how to provision a GKE cluster using Google Cloud SDK command line tool for interacting with Google Cloud. I will do a follow up article to show how to do something similar with Terraform.

For this exercise you will need to setup an account on Google Cloud and create an initial Google project that has billing enabled. For this tutorial we’ll use a fictional project name:

Requirements

You will need the following setup and configured:

  1. Google Cloud SDK: these tools are needed to manage Google Cloud
  2. Kubectl (pronounced koob-cuttle) is the Kubernetes client cli tool to interact with your newly created cluster.

Using Google Cloud SDK

You can create a new cluster using the gcloud container clusters create command.

Basic Cluster

You can provision a new cluster easily with the following command accepting the common defaults with the following:

After about 20 minutes, you cluster should be created. It will use intelligent defaults and create a 3 node cluster with 1 node in each zone.

Cluster with Added CloudDNS Scope

In the future, we may want to allow deploy applications to update DNS records as part of the deployment, so we’ll want to create a cluster that has the ability to communicate with the CloudDNS service. This is done by adding an OAuth scope that permits this activity.

Below is an example bash script that uses gcloud container clusters create to create a GKE cluster with the added scope:

You can download and run the above bash script like this:

Testing the Cluster

After you run this command, gcloud will add a new context to your KUBECONFIG. You can see this addition with:

With this context set to test-cluster, you can see the components in your GKE cluster with:

Deploying an Application

Now that we have a GKE cluster provisioned, we can deploy web application hello-kubernetes.

Deploy the Deployment

The first resource we will deploy is a deployment controller. This describes a set of three pods that will automatically recover should one of the pods fail.

Create a file named hello-k8s-deploy.yaml with the following contents:

Now deploy this resource with the following:

You can check the status with:

Deploy the Service

For some high availability, we will want to talk to any one of three pods. We can do this with a service resource, where the service will route to one of three pods.

Create a file hello-k8s-svc.yaml with the following contents:

Now deploy this resource with the following:

You can check the status with:

Testing the Deployment

You can run this command to view the web application locally.

After this, the hello-kubernetes can be viewed from a web browser at http://localhost:8080, and should like something similar to this:

http://localhost:8080

Cleaning Up

When you no longer need the cluster, you can delete it with the following

Next Article

In a follow-up article, I show how to create a GKE cluster using Terraform:

Resources

Here are some resources that may be useful in exploring GKE:

Google Blogs

Blog Source Code

I put the source code used in this blog here:

Conclusion

I hope this helps you get started quickly to build a GKE cluster and get started right away with your Kubernetes journey.

This article uses the default setup, which creates a public cluster. There are other options, such as a private cluster, but these are more involved, require building a network infrastructure to support a private GKE. I could cover this in a follow up article. If you would like to see this, drop a note.

In future articles, I intend to show how to build out a cluster with Terraform and use add-ons to integrate with CloudDNS, Google Managed SSL Certificates or Cert Manager, as well as using load balancer and ingress resources.

--

--

Joaquín Menchaca (智裕)
The Startup

DevOps/SRE/PlatformEng — k8s, o11y, vault, terraform, ansible