Challenge 3: Deploy and Expose the Application

Previous Challenge Next Challenge

Introduction

Now that you containerized your website and pushed the container to your docker repository in Artifact Registry, you can deploy it to Kubernetes.

To deploy and manage applications on a GKE cluster, you must communicate with the Kubernetes cluster-management system. You typically do this by using the kubectl command-line tool.

Kubernetes represents applications with the Pod resource. Pods are units that represent a container (or group of tightly coupled containers). The Pod is the smallest deployable unit in Kubernetes and it is the unit of scale. In our application, each Pod will only contain your monolith container.

Deploying applications in Kubernetes is done using the Deployment resource. A Deployment manages multiple copies (aka Replicas) of your application and schedules them to run on the individual nodes in your cluster. To begin with, the Deployment will run only one Pod of your application. Deployments manage replicas by creating a ReplicaSet resource. The ReplicaSet is responsible for making sure that the number of replicas specified are always running.

Once you’ve deployed an application in Kubernetes you will need a way of accessing it outside of the cluster. By default, the containers you run on Kubernetes are not accessible from the internet because they do not have external IP addresses. You must explicitly expose your application to traffic from the internet via a Service Resource. A Service provides networking and IP support for your Pods. GKE creates an external IP and a load balancer for your application when a Service is created.

Resources in Kubernetes are created using files in yaml format that describe the items and state that you want to deploy to the cluster. This is called declarative instantiation, where we describe WHAT we want to see, not procedural steps in a script describing HOW to build what we need.

Description

In this challenge you will create a Deployment resource for your application and then a Service resource to expose it to your customers.

We will create yaml files describing the Deployment and Service resources you will need to create to achieve this.

Your Deployment resource will need to:

  • Use the container image that we built and pushed to your docker repository in Artifact Registry.
  • Deploy only 1 replica of our container
  • Expose the container on port 8080
  • Use an appropriate label to identify the pod

Your Service resource will need to:

  • Be exposed externally on port 80
  • Send requests to the application on the port it uses
  • Use a selector that matches the label on the application’s pod

Issue a kubectl command to look at all the Kubernetes resources created to make sure that the Deployment, Pod, ReplicaSet and Service resources are there.

In a browser, access your application on the Service’s external IP.

Lastly, simulate a Pod crash by deleting a Pod with a kubectl command. Immediately after deleting, issue the command to see all resources and notice a new Pod being created by the ReplicaSet.

Success Criteria

  • You’ve created yaml files with definitions of a Deployment and a Service
  • Your deployment uses the container image built and pushed to your Artifact Registry docker repository.
  • Your deployment creates 1 replica of the pod
  • Your service is available externally
  • Your application can be accessed and used in a browser
  • You’ve demonstrated to your coach that deleting a Pod results in a new one being created immediately.

Learning Resources

Tips

  • You can view your Kubernetes deployments, pods and services via the Cloud Console.
  • If you see unexpected errors or statuses, then you can debug your resources by using the describe command in kubectl for the various resources.

Previous Challenge Next Challenge