Share this:
Share

Welcome to Ken Lomax’s world. He is our resident technology soothsayer, bringing you his impressive “Cloud Labs” series. Cloud Labs will focus on cloud-related technologies and their applications. These series will feature examples and hands-on tasks you can try on your own, to learn and practice common cloud-related stories. Some will be quite basic, and some much more advanced. The goal is to provide successful initial journeys for engineers in the cloud, and to refer you to excellent relevant material to help you go further. They will be bite-sized examples, to introduce you to these technologies without overwhelming you in information and noise.

Create a project on Google Cloud
• Go – Building a simple Go Micro Service
• Docker – Packaging it in Docker
• Kubernetes – Wrapping in Kubernetes
• Google Cloud – Taking to the Cloud
• Update – Doing a rolling update in the Cloud

Step one: Install Golang

Write a web server in Go that listens on port 8080 and returns “Hello world!” and the Host name

Compile and run the application.

Open localhost:8080 and note that the hostname is the name of your computer. This will be changing later, when we run the main application on virtual machines.

Compile your Go application to Linux

By default, a compiled Go program will link to dynamic libraries at run time. This is ok if you compile and then run your Go application on one (type of) machine. But if you want to compile your Go on a Mac, and then run it on Linux, you need to compile your Go application as one static binary, with all dependent libraries included. There is a good explanation of this here. To do this run:

This gives you a main program that will execute (only) on a (virtual, or real) Linux machine which is what you will do next using Docker!

Containerize your Go App: Install Docker

Create a Dockerfile alongside main.go describing how to build a Docker Image containing your Go App.

Build the Docker Image from the Dockerfile. The -t flag allows you to specify a name and optional tag (v1). Use your Docker Hub account name, in this example “kenlomax” (<yourDockerHubName>/cloudlab1:v1 ):

This Docker Image will contain everything that it needs to execute on top of a Linux kernel – libs, bins, executables
List your image with

A running instance of a Docker Image is called a Docker Container.
Run a Docker Container

Hit it at http://localhost:8080

Note that
• your Linux executable “main” is running on your (non Linux) computer. This is possible because Docker is executing on a virtual Linux machine.
• the hostname is NOT the name of your computer, but instead is the Linux VM in which the Docker Container is running.

      Aside
On Linux systems, Docker directly leverages the kernel of the host system, and file system mounts are native.
On Windows and Mac, it’s slightly different. These operating systems do not provide a Linux Kernel, so Docker starts a virtual machine with a small Linux installed and runs Docker containers in there. More..

Push to Docker hub: Create an account at hub.docker.com

Log in to Docker with your Docker ID. This allows you to push and pull images from Docker Hub.

Push your image to Docker Hub so others can see it there.

Check you can see it at https://hub.docker.com/

Note from docker’s output that it pushed only the topmost layer – the other layers are available to Docker Hub already.
Now any one with docker can download this image and run it with:

     Aside
Docker runs processes in isolated containers. A container is a process which runs on a host. The host may be local or remote. When an operator executes docker run, the container process that runs is isolated in that it has its own file system, its own networking, and its own isolated process tree separate from the host.

Wrap in Kubernetes so we can start scaling it

Create a Kubernetes Deployment file

Run in local Kubernetes (Install Minikube and Kubectl) *

Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a VM on your laptop for users looking to try out Kubernetes or develop with it day-to-day.
Start minikube*

Kubectl is Kubernetes’ command-line tool, to deploy and manage applications on Kubernetes from your command line.
Point your kubectl to your local minikube

Try querying your minkube using kubectl

Explore the Kubernetes dashboard

Deploy your Kubernetes application to your minikube

Note the output says serice and deployment are created – the two parts of your deployment file
The Kubernetes dashboard now shows new elements: 5 new pods, 1 new service and 1 new deployment

Query the kubernetes status using kubectl

Access your Kubernetes application
The –type=LoadBalancer flag indicates that you want to expose your Service outside of the cluster. On cloud providers that support load balancers, an external IP address would be provisioned to access the Service. On Minikube, the LoadBalancer type makes the Service accessible through the minikube service command:

Our deployment says we want 5 pods. Note that Kubernetes will immediately create replacement pods if any of them die.

Also we can scale up and down. This is very cool 🙂

Create a project on Google Cloud

Create a project named cloudlab1 in your cloud.google.com account and take a note of the Project ID that google assigns to it.
Note: Execute the rest of the shell commands in this cloudlab in your google cloud Shell

Deploy to Google Cloud
In your google cloud project, enable
Kubernetes Engine API (used for building and managing container-based applications, powered by the open source Kubernetes Technology)
Compute Engine API (Creates and runs virtual machines on Google Cloud Platform) for your Google Cloud Project

Create a container cluster from within your google cloud shell

Launch your Google Code Editor (with this link or with the pencil icon just above your google shell), to recreate the 3 files you wrote earlier, but this time save them in your google cloud’s root Directory:

  • deployment.yml,
  • Dockerfile,
  • main.go

Return to your google shell, and list the files to confirm you can see them from your google shell.

and deploy your application to this Kubernetes cluster, with the following command from within your google cloud shell:

Find the EXTERNAL-IP address where your application can be reached

Ping your service which now available as a Kubernetes Cluster on Google Cloud

Create a new Version and push a new Docker Image to Google Cloud (rather than Docker Hub): Install gcloud sdk

Modify your main.go

Enable the Container Registry API for your Google Account

Build and Tag the Docker Image and Push to Google Cloud Repository

View your container registry and find your uploaded Docker image at

Test locally

Deploy a new version of your app

Kubernetes Engine’s rolling update mechanism ensures that your application remains up and available even as the system replaces instances of your old container image with your new one across all the running replicas.
Apply a rolling update to the existing deployment with an image update:

Find the EXTERNAL-IP address where your application can be reached

Note the change is (soon) visible!

 

Hopefully it all worked seamlessly. If not, drop a comment or question below and Ken Lomax would gladly share Feedback. Keep an eye out for the second part of our Cloud Labs articles!

 

 

 

Share this:
Share