Image for post
Image for post

Building a GKE with Cloud SDK

Provision Google Kubernetes Engine with Google Cloud SDK

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:

acme-quality-team

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:

gcloud container clusters create \
--num-nodes 1 \
--region us-central1 \
my-new-cluster

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:

# variables for readability
GIST_PREFIX=https://gist.githubusercontent.com/darkn3rd
GIST_DOC_HASH
=f913dc0876e9352999154b135e447ac4
GIST_URL=$GIST_PREFIX/$GIST_DOC_HASH/raw
CLUSTER_NAME=test-cluster
CLUSTER_REGION=us-central1
# download script
curl
--silent $GIST_URL > create_gke_cluster.sh
# run script: create_gke_cluster.sh CLUSTER_NAME REGION PROJECT
bash create_gke_cluster.sh $CLUSTER_NAME $CLUSTER_REGION

Testing the Cluster

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

kubectl config get-contexts

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

kubectl get all --all-namespaces

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:

kubectl apply --filename hello-k8s-deploy.yaml

You can check the status with:

kubectl get deployment

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:

kubectl apply --filename hello-k8s-svc.yaml

You can check the status with:

kubectl get service

Testing the Deployment

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

kubectl port-forward service/hello-kubernetes 8080:8080

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

Image for post
Image for post
http://localhost:8080

Cleaning Up

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

gcloud container clusters delete \
--region $CLUSTER_REGION \
$CLUSTER_NAME

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.

The Startup

Medium's largest active publication, followed by +718K people. Follow to join our community.

Joaquín Menchaca (智裕)

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

The Startup

Medium's largest active publication, followed by +718K people. Follow to join our community.

Joaquín Menchaca (智裕)

Written by

Linux NinjaPants Automation Engineering Mutant — exploring DevOps, Kubernetes, CNI, IAC

The Startup

Medium's largest active publication, followed by +718K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store