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:
- Google Cloud SDK: these tools are needed to manage Google Cloud
- 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/rawCLUSTER_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:
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
- https://codelabs.developers.google.com/codelabs/cloud-deploy-website-on-gke/index.html
- https://cloud.google.com/solutions/prep-kubernetes-engine-for-prod
- https://cloud.google.com/kubernetes-engine/docs/how-to/alias-ips
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.