Qwiklab/Logbook: Build and Deploy a Docker Image to a Kubernetes Cluster

Chris K.Y. Fung
5 min readSep 22, 2019

Containerization with Docker and Kubernetes (K8s) is an emerging application architecture for deploying, scaling and managing distributed applications. The challenge lab GSP304 Build and Deploy a Docker Image to a Kubernetes Cluster is a test to assess the essential professional skills in deploying your application on GKE.

Brief Introduction of Challenge Scenario

When you open the page of this lab in Qwiklabs, you can find the task requirements by click the green activity tracker (on the top right of the page) to expand the score box.

Fig. 1 Screenshot of Qwiklabs hands-on-lab GSP304 scorebox

The screenshot above shows that there are 4 steps required for completing this lab. Combining with the instruction details, they are translated to the following mission statements.

  1. Use the sample application and Docker configuration to build a docker image, and push the image to the gcr.io repository with a v1 tag.
  2. A new Kubernetes cluster called echo-cluster exists.
  3. The application called echo-app has been deployed to the cluster.
  4. The service called echo-web exists that responds to requests like Echo-app.
Fig. 2 Schematic diagram of the echo-app deployment

Create a Kubernetes Cluster

In the web console, navigate to Kubernetes Engine > Clusters. Click Create a cluster with:

  • Cluster name: echo-cluster
  • Num of Nodes: 2
  • Machine type: N1-standard-2

I recommend starting from preparing the hardware because the process takes time. You can continue doing the steps in the next section. The cluster should be ready, when you finish building and pushing the docker image to Container Registry.

Build a Docker Image of Sample Application

If you do not remember how to build a docker image on GCP, I recommend you revise the lab Introduction to Docker before you start.

1. (Optional) While the provisioning of lab resources, you may click the link below the timer to download the given archive called echo-web.tar.gz. You may spend some time to study the contained files in your local storage.

Fig. 3 Download Sample Application with Docker Configuration

2. The echo-web.tar.gz file has already been copied to a Google Cloud Storage bucket called gs://[PROJECT_ID] during the lab provision. Navigate to Storage, confirm the file exists in the bucket. Then, click the file name and copy the URL of the file from its detail page.

Fig. 4 Verify the existence of the sample app file in Cloud Storage

3. Open a Cloud Shell, use the following commands to copy and unzip echo-web.tar.gz to the shell environment:

export PROJECT_ID=$(gcloud info --format='value(config.project)')
gsutil cp gs://${PROJECT_ID}/echo-web.tar.gz .
tar -xvzf echo-web.tar.gz

4. Build a docker image of the sample application with a tag called v1, and push the image to Google Container Registry,

docker build -t echo-app:v1 .
docker tag echo-app:v1 gcr.io/${PROJECT_ID}/echo-app:v1
docker push gcr.io/${PROJECT_ID}/echo-app:v1

5. In the web console, navigate to Container Registry > Images to confirm the docker image has been pushed to the cloud repositories.

Fig. 5 Confirm your docker image existing in Google Container Registry

Next, you need to deploy the application to the Kubernetes Cluster. There are two ways to do this: (1) deploy using web console, and (2) deploy using Cloud Shell. You can choose either way to finish the lab.

Deploy the Application to the Kubernetes Cluster Using Web Console (Method #1)

1. In the Container Registry page, click the image name echo-app. There should be an image version with a tag v1 . Click the three-dots icon (⋮) and select Deploy to GKE.

Fig. 6 Deploy the image to GKE using web console

2. The web console will be redirected to Kubernetes Engine > Create a deployment dialogue,

Fig. 7 Select a container image in Create a deployment dialogue

Click CONTINUE.

3. In the Configuration section, enter echo-app as the application name and choose echo-cluster as the cluster in which the deployment will be created.

Fig. 8 Edit configuration in Create a deployment dialogue

Click CREATE NEW CLUSTER.

4. Navigate to Kubernetes Engine > Workload page, wait the status of the deployment becomes OK.

Fig. 9 Confirm the deployment status

5. Click the name echo-app, then click Expose displayed at the top right corner of the Deployment Details page to create a service for the deployment.

Fig. 10 Snapshot of the Deployment details page

6. In the Expose a deployment dialogue, configure the service with a new port mapping as below:

  • Port: 80
  • Target port: 8000
  • Protocol: TCP
  • Service type: Load balancer
  • Service name: echo-web
Fig. 11 Configure port mapping in the Expose a deployment dialogue

Click Expose to create the service.

Fig. 12 Snapshot of the Service details page of the deployed GKE service

7. In the service details, copy and open the IP address of the external endpoints in a new tab of your browser. The sample application should look like:

Fig. 13 Snapshot of the deployed echo-web application

Deploy the Application to the Kubernetes Cluster Using Cloud Shell (Method #2)

Alternatively, you can deploy the application using cloud shell instead. After creating your cluster, you need to get authentication credentials to interact with the cluster.

To authenticate the cluster run the following command,

gcloud container clusters get-credentials echo-cluster

Run the following kubectl run command in Cloud Shell to create a New Deployment echo-app from the echo-app container image with opening TCP port 8000:

kubectl run echo-app --image=gcr.io/${PROJECT_ID}/echo-app:v1 --port 8000

Now create a Kubernetes Service, which is a Kubernetes resource that lets you expose your application (that responds on port 8000) to external traffic that responds to normal web requests on port 80, by running the following kubectl expose command:

kubectl expose deployment echo-app --name echo-web --type="LoadBalancer" --type LoadBalancer --port 80 --target-port 8000

Inspect the echo-web Service by running kubectl get:

kubectl get service echo-web

Copy and open the IP address of the external endpoints in a new tab of your browser, the sample application should look like:

Congratulations! You should accomplish the lab if you follow the above steps.

Next lab will continue this exercise and need to update the running echo-app application in the echo-web deployment from the v1 to the v2 .

Originally published at https://chriskyfung.github.io on September 22, 2019.

--

--

Chris K.Y. Fung

📝集中分享提升生產力、自動化至開發等技能📢📈 博客及開發人員,於香港理工大學MPhil畢業,並多年在大學從事學術研究,喜歡學習多元化知識和技術。歡迎訂閱・標註回應,共享交流啟發性話題🐱‍🏍 about.me/chriskyfungfb.com/chriskyfung 😪📘