In the previous lab, you deployed a single pod. In this lab, you will deploy the same application but use the Kubernetes Deployment object to deploy it. A Deployment manages the release of new versions of your application.
To begin, create a dedicated directory for this lab and switch into it:
cd ~
mkdir random-facts-app-deployment && cd random-facts-app-deployment
Using your Namespace manifest from the previous lab as a starting point, create a new manifest for this lab giving the namespace the name: random-facts-app-deployment. Ensure you also update the label to be random-facts-app-deployment.
Use the kubectl apply command to create your namespace.
Create the Deployment manifest for the application with the following contents:
apiVersion: apps/v1
kind: Deployment
metadata:
name: random-facts-app
namespace: random-facts-app-deployment
labels:
lab: random-facts-app-deployment
spec:
replicas: 1
selector:
matchLabels:
lab: random-facts-app-deployment
template:
metadata:
labels:
lab: random-facts-app-deployment
spec:
containers:
- name: random-facts-app
image: us-central1-docker.pkg.dev/<YOUR_PROJECT_ID>/<YOUR_REGISTRY_NAME>/random-facts-app:1.0
ports:
- containerPort: 5000
Use the kubectl apply command again to deploy your Deployment.
Once deployed, use kubectl get to view the status of your Deployment. You will see an output similar to the following:
NAME READY UP-TO-DATE AVAILABLE AGE
random-facts-app 1/1 1 1 22s
This tells you that your Deployment is expected to deploy 1 pod and that the pod is running and healthy. Validate that your Pod is running with the kubectl get command. You will see an output similar to the following:
NAME READY STATUS RESTARTS AGE
random-facts-app-f7f7f64cd-clm6g 1/1 Running 0 39s
Scaling Up #
Next, let’s scale up your deployment to three running instances. Update your Deployment manifest to deploy three Pods and apply your changes to your cluster. Validate Kubernetes has now rolled out two additional pods with the kubectl get command. You will see an output similar to the following:
NAME READY STATUS RESTARTS AGE
random-facts-app-f7f7f64cd-2cf99 1/1 Running 0 7s
random-facts-app-f7f7f64cd-c6s5r 1/1 Running 0 7s
random-facts-app-f7f7f64cd-clm6g 1/1 Running 0 91s
Note: This can also be done with the
kubectl scalecommand. See if you can scale your deployment up to 5 pods with this method. Once done, be sure to update your manifest to reflect 5 replicas as well.
Updating Deployments #
Let’s now roll out a new version of the application. To do this, you will update your Deployment manifest to:
-
Update the container image tag to
us-central1-docker.pkg.dev/<YOUR_PROJECT_ID>/<YOUR_REGISTRY_NAME>/random-facts-app:latest -
Add the following annotation under
spec.template.metadatato describe what has changed:annotations: kubernetes.io/change-cause: "Upgrade to latest tag."
Apply your updated Deployment manifest to your cluster. You can validate that the Deployment has rolled out successfully with the kubectl rollout status command. You will see an output similar to the following:
deployment "random-facts-app" successfully rolled out
You can also see a history of all the revisions of a specific deployment (this is where the annotation is useful) with the kubectl rollout history command. You will see an output similar to the following:
REVISION CHANGE-CAUSE
1 <none>
2 Upgrade to latest tag.
Rolling Back a Deployment #
Let’s assume that by upgrading to a new version of your application, your application is not behaving as expected and you need to go back to a previous version. See if you can rollback your deployment using the kubectl rollout undo command and validate your deployment is successfully running. You will see an output similar to the following:
deployment.apps/random-facts-app rolled back
Frontend Your Deployment with a Service #
Right now, you have multiple instances of the same application running. Let’s create a single entrypoint to your application. This is done with a Kubernetes Service. Use the following Service manifest as a starting point to send traffic to your Deployment.
apiVersion: v1
kind: Service
metadata:
name: random-facts-app-service
namespace: random-facts-app-deployment
labels:
lab: random-facts-app-deployment
spec:
selector:
lab: random-facts-app-deployment
ports:
- name: http
port: 5000
protocol: TCP
targetPort: 5000
type: ClusterIP
Apply your Service manifest to your cluster and validate it created successfully. You will see an output similar to the following:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
random-facts-app-service ClusterIP 172.30.127.4 <none> 5000/TCP 12s
The service you created is a ClusterIP type, which means it will only get an internal IP and will not be accessible from outside of the cluster. Accessing services from outside of the cluster will be covered later in the workshop.
Kubernetes does provide a way to access a ClusterIP service type via its port-forwarding capabilities. This is helpful when you want to access an application running in your cluster, without actually exposing it outside of the cluster.
Use the kubectl port-forward command to port-forward your application. This will allow you to access your application via the Web Preview function in Cloud Shell. Click the Web Preview button and then click Preview on port 8080. This will open a new tab in your browser to your application.
Are you able to see your application? Go back to your Service manifest and see if you can fix the error.
Once you can see your application in the Web Preview, you can move onto the next lab.