Build and Deploy a Docker Image to a Kubernetes Cluster

In this article, we will go through the lab to Build and Deploy a Docker Image to a Kubernetes Cluster. In the previous, lab you will get familiar with Introduction to Docker, Kubernetes Engine, Orchestrating the Cloud with Kubernetes, Managing Deployments Using Kubernetes Engine, and Continuous Delivery with Jenkins in Kubernetes Engine.

The challenge contains 4 required tasks.

  • A new Kubernetes cluster exists (zone: us-central1-a).
  • An application image with a v1 tag has been pushed to the gcr.io repository.
  • Check that an application has been deployed to the cluster.
  • Test that a service exists that responds to requests like Echo-app.

To test the deployment, you need to download the sample application, then build the Docker container image using a tag that allows it to be stored on the Container Registry. Once the image has been built, you’ll push it out to the Container Registry before you can deploy it.

With the image prepared you can then create a Kubernetes cluster, then deploy the sample application to the cluster.

Note: In order to ensure accurate lab activity tracking you must use echo-app as the container repository image name, call your Kubernetes cluster echo-cluster, create the Kubernetes cluster in us-central1-a zone and use echo-web for the deployment name.

Build a Docker Image of Sample Application

  • Go to Cloud shell and write commands .
  • Now we have to copy and unzip our folder.
gsutil cp gs://sureskills-ql/challenge-labs/ch04-kubernetes-app-deployment/echo-web.tar.gz .gsutil cp gs://$DEVSHELL_PROJECT_ID/echo-web.tar.gz .tar -xvf echo-web.tar.gz
  • Now Build a docker image of the sample application with a tag called v1, and run it then push the image to Google Container Registry,
docker build -t echo-app:v1 .docker tag echo-app:v1 gcr.io/${PROJECT_ID}/echo-app:v1docker push gcr.io/${PROJECT_ID}/echo-app:v1

Replace $DEVSHELL_PROJECT_ID with your GCP Project ID.

Note: Getting an Error.

Kill all running containers with docker kill $(docker ps -q)

Delete all stopped containers with docker rm $(docker ps -a -q)

Delete all images with docker rmi $(docker images -q)

1.Create a Kubernetes Cluster

This step is to create a Kubernetes cluster.

  • In the GCP Console go to Cloud Shell copy and paste the command.
  • Configuration as follow:

Cluster Name: echo-cluster

Zone: us-central1-a

Instances Machine Type: n1-standard-2

gcloud container clusters create echo-cluster --num-nodes 2 --zone us-central1-a --machine-type n1-standard-2

2.Push the image to the Google Container Registry

  • Now create an application image with a v1 tag has been pushed to the gcr.io repository
kubectl run  echo-web \--image=gcr.io/[id-project]/echo-app:v1 \--port=8000

3.Deploy the application to the Kubernetes Cluster

  • 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
  • You need to Inspect the echo-web Service by running kubectl command.
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

kubectl — Command allows you to run commands against Kubernetes clusters.

Congratulations! Done with the challenge lab.

Stay tuned till the next blog.

If you Want to Connect with Me:

LinkedIn: https://www.linkedin.com/in/mayank-chourasia-38421a134/

Twitter: https://twitter.com/ChourasiaMayank.

Thank you stay safe, stay healthy.

Google Cloud Google Developers Qwiklabs Top Docker News KUBERNETS INC

I am Google Cloud Certified Associate Cloud Engineer and MS Azure Certifed. I have been working on different fields including Cloud Computing , Sap ABAP.