Creating and Managing Deployments

Deployments abstract away the low level details of managing Pods. Pods are tied to the lifetime of the node they are created on. When the node goes away so does the Pod. ReplicaSets can be used to ensure one or more replicas of a Pods are always running, even when nodes fail.

Deployments sit on top of ReplicaSets and add the ability to define how updates to Pods should be rolled out.

In this chapter we will combine everything we learned about Pods and Services to breakup the monolith application into smaller Services. You will create 3 deployments, one for each service:

  • frontend

  • auth

  • hello

You will also define internal services for the auth and hello deployments and an external service for the frontend deployment.

Tutorial: Creating Deployments

Create and Expose the Auth Deployment

Explore and create the Deployment for service auth:

kubectl apply -f manifests/app/deployments/auth.yaml
kubectl describe deployments auth

Notice in the Deployment definition:

  • replicas is the number of pod we ask Kubernetes to run on the cluster. It’s used by the ReplicaSet.

  • In template you’ll recognize the definition of Pods seen previously. This template is used by the ReplicaSet to provision Pods.

Now create the corresponding service:

kubectl apply -f manifests/app/services/auth.yaml

Notice the match between pods' labels and service’s selectors.

Create and Expose the Hello Deployment

Do the same for the hello deployment and services:

kubectl apply -f manifests/app/deployments/hello.yaml
kubectl describe deployments hello
kubectl apply -f manifests/app/services/hello.yaml

Create and Expose the Frontend Deployment

And at least for the frontend:

kubectl create configmap nginx-frontend-conf --from-file=manifests/app/nginx/frontend.conf
kubectl apply -f manifests/app/deployments/frontend.yaml
kubectl apply -f manifests/app/services/frontend.yaml

Tutorial: Scaling Deployments

Behind the scenes Deployments manage ReplicaSets. Each deployment is mapped to one active ReplicaSet. Use the kubectl get replicasets command to view the current set of replicas.

kubectl get replicasets

ReplicaSets are scaled through the Deployment for each service and can be scaled independently. Use the kubectl scale command to scale the hello deployment:

kubectl scale deployments hello --replicas=3
kubectl describe deployments hello

Notice the Replicas values in the describe command and the scaling events at the end.

kubectl get pods
kubectl get replicasets

There is now 3 replicas of your hello microservice.

Exercise: Scaling Deployments

In this exercise you will scale the frontend deployment using an existing deployment configuration file. You’ll have to change the replicas: property to 3.

Hints

You can change this value in the manifests/app/deployments/frontend.yaml file with the Cloud Shell editor. Type Ctrl+S to save the file

kubectl apply -f manifests/app/deployments/frontend.yaml

You can watch configuration change applying by refreshing the Workloads list in GCP console. You’ll soon have 3/3 pods for the frontend Deployment.

Exercise: Interact with the Frontend Service

Your frontend service is of type LoadBalancer. This means that on cloud providers (AWS, Azure, GCP, …​), your service will be accessible through an infrastructure load balancing service managed by the provider.

The external IP of the service is the IP of this load balancing service, on which you can join the service.

Hints

kubectl get services frontend
curl -k https://<EXTERNAL-IP>

Summary

Deployments are the preferred way to manage application deployments. You learned how to create, expose and scale deployments.