google cloud kubernetes engine

How to Deploy an Application to Kubernetes running on Google Cloud Kubernetes Engine (GKE)

In this article you will learn how to deploy a simple java application to Kubernetes running on Google Cloud Kubernetes Engine (GKE). Kubernetes is an open-source container orchestration system for automating the application deployment, scaling and management.

Google Kubernetes Engine Lab Exercise – Objective

The objective of this exercise is to run our web application (that is developed in our local machine or from Git source repository) as a replicated application running on Kubernetes cluster in the Google Container Engine or Google Kubernetes Engine (GKE).

What you need to do is, just take the code, turn it into Docker container image, and then run that image on Google Kubernetes Engine.

google cloud kubernetes engine

What is Google Container Registry?

Google Container Registry is a single place where you can store, manage and secure your Docker container images.

Docker is computer program that helps in performing OS level virtualization. It is used to run software packages called Containers. Docker Containers are isolated from each other and bundle their own application, libraries configuration files etc., and then communication between Docker Containers can happen only through well-defined channels. Containers are created using Images. 

What is Google Kubernetes Engine (GKE)?

Google Kubernetes Engine is a managed, production-ready environment for deploying containerized applications. In simple terms Kubernetes Engine is a Google-hosted version of Kubernetes running on Compute Engine.

When you use Kubernetes Engine to run your applications, you no need to worry about how to install, manage and operate your own Kubernetes Clusters as Kubernetes Engine allows you to do those stuff in no time.

Now let’s see how to deploy simple application to kubernetes on Google Kubernetes Engine.

1. Get source code from Git repository

For our lab exercise let’s use sample source code from Github. Launch the cloud shell and run the following git command to clone the Spring Boot Hello World source code in the home directory.

2. Run and test the application locally

Once you had cloned the example source code in the home directory, change the current directory to ‘gs-spring-boot/complete‘ and start the Spring Boot Hello World App normally using the Spring Boot plugin as shown below.

Once the application is started, click on the web preview icon in the cloud shell toolbar and choose “preview on port 8080” from the drop down

kubernetes engine preview on port

Then it will open an URL in new tab in the browser, connects to the server that you had started and displays the following output.

kubernetes engine preview on port

3. Package the application as a Docker Container

Next step is to you need to create the JAR deployable for the application and use the Jib to create the container image and push the image to Google Container Registry.

Once the image is successfully built and pushed to gcr.io (Google Container Registry), then you could manually check whether the container image is listed in the console as shown below. Note, at this point this hello-spring image is project-wide Docker image.

google container registry docker image

You can also test the image locally using the following Docker command. This command will run a Docker container as a daemon on port 8080 from your newly created container image. You can use web preview feature of CloudShell to see the output.

You can stop running container by entering “Ctrl+c

4. Create your Kubernetes Engine Cluster

Navigate to Google Kubernetes Engine section in the console and wait for few seconds for the system to initialize.

Google Cloud Console -> Kubernetes Engine (Left Navigation Panel)

A cluster consists of a Kubernetes master server managed by Google and set of worker nodes. The worker nodes here are nothing but Google Compute Engine VM’s. Let’s use gcloud command to create kubernetes engine cluster as shown below

You should now have fully-functioning Kubernetes Cluster using Google Kubernetes Engine.

5. Deploy your application to Kubernetes

Now, its time to deploy Spring Boot Hello World application to Kubernetes cluster. Let’s use ‘kubectl‘ command line in the CloudShell for Kubernetes deployment.

Kubernetes deployment can create, manage, and scale multiple instances of your application using the container image that you have just created in Container Registry. Run the following kubectl command to deploy one instance of Spring Boot Hello World application into Kubernetes.

To get the deployments run the following kubectl command

To get the application instances created by the deployment, run the following command

Further Learning

References

Leave a Reply

avatar
  Subscribe  
Notify of