Google is the inventor of Kubernetes and, to this date, the driving force behind it. One would thus expect that Google has a compelling offering around hosted Kubernetes. Let's have a peek into it. To continue, you need to either have an existing account with Google Cloud or you can create a test account here at https://console.cloud.google.com/freetrial. Proceed with the following steps:
- In the main menu, select Kubernetes Engine. The first time you do that, it will take a few moments until the Kubernetes engine is initialized.
- Once this is ready, we can create a cluster by clicking on CREATE CLUSTER.
- Name the cluster as pets-cluster and leave all other settings in the Create a Kubernetes Cluster form with their default values and click on Create.
It will again take a few moments to provision the cluster for us. Once the cluster has been created, we can open the Cloud Shell. This should look similar to the following screenshot:
We can now clone our labs GitHub repository to this environment with the following command:
$ git clone https://github.com/fundamentalsofdocker/labs
$ cd labs/ch14/gce
We should now find a pets.yaml file in the current folder, which we can use to deploy the pets application into our Kubernetes cluster. Have a look at the file:
$ less pets.yaml
It has pretty much the same content as the same file we used in the previous chapter. The two differences are:
- We use a service of type LoadBalancer (instead of NodePort) to publicly expose the component web.
- We do not use volumes for the PostgreSQL database since configuring StatefulSet correctly on GCE is a bit more involved than in Minikube. The consequence of this is that our pets application will not persist the state if the db pod crashes. How to use persistent volumes on GCE lies outside the scope of this book.
Before we can continue, we need to first set up gcloud and kubectl credentials:
$ gcloud container clusters get-credentials pets-cluster \
--zone us-central1-a
Having done that, it's time to deploy the application:
$ kubectl create -f pets.yaml
Once the objects have been created, we can observe the LoadBalancer service web until it is assigned a public IP address:
$ kubectl get svc/web --watch
This should look similar to the following screenshot:
We can then use this IP address and navigate to http://<IP address>:3000/pet and we should be greeted by the familiar cat image.
To clean up and delete all resources, run this script:
kubectl delete deploy/web
kubectl delete deploy/db
kubectl delete svc/web
kubectl delete svc/db
We have created a hosted Kubernetes cluster in GCE. We have then used the Cloud Shell provided through the GCE portal to first clone our labs GitHub repository and then the kubectl tool to deploy the pets application into the Kubernetes cluster.
When looking into a hosted Kubernetes solution, GCE is a compelling solution. It makes it very easy to start and since Google is the main driving force behind Kubernetes, we can rest assured that we will always be able to leverage the full uncrippled functionality of Kubernetes.