Build and Deploy a Docker Image to a Kubernetes Cluster – Qwiklab

0 87

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.

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.

 

  1. Create a Kubernetes Cluster
    1. 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
    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.
  2. Build a Docker Image of Sample Application:
    1. 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.
    2. 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
    3. 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
  3. Deploy the Application to the Kubernetes Cluster Using Cloud Shell
    1. To authenticate the cluster run the following command,
      gcloud container clusters get-credentials echo-cluster
    2. 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
    3. 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
    4. Inspect the echo-web Service by running kubectl get:
      kubectl get service echo-web
      

Leave A Reply

Your email address will not be published.