Deploy an Update

Deploy an UpdateRequirementsPush New Image to ECRUpdate the Deployment Image ReferenceVerify the Update


So you've updated your code recently, and you need to rollout the change.

Each of your codebases is associated with a Deployment, which will spin up several instances based on whatever Docker Image you provide it. In order to rollout an update, you need to update the Docker Image in your Deployment.

There are three stages to this:

  1. Push the new Docker Image to the associated repository in the AWS Elastic Container Registry
  2. Update the Deployment specification with the new Image
  3. Kubernetes will then fetch the new image and roll out the update

img

 

Requirements

To do this, you'll need to know some pieces of information:

If you don't know these, instructions on finding them are here, depending. Remember to sub them in wherever you see orange text in the code blocks.

For the NAMESPACE, you can either switch to the correct namespace at the beginning, or use -n NAMESPACE throughout for all kubectl commands. See the appropriate section for details.

 

Push New Image to ECR

First, make sure you're logged in to the ECR:

aws ecr get-login-password --region ap-southeast-1 | docker login --username AWS --password-stdin 238288103121.dkr.ecr.ap-southeast-1.amazonaws.com
Login Succeeded

Go to your repository root folder and make sure you've built your docker image locally:

docker build -t myimage .

Tag your local docker image with the ECR repository location, name and tag. Make sure the tag is unique :

docker tag myimage:latest 238288103121.dkr.ecr.ap-southeast-1.amazonaws.com/REPOSITORY_NAME:TAG

Push the image to the ECR:

docker push 238288103121.dkr.ecr.ap-southeast-1.amazonaws.com/REPOSITORY_NAME:TAG

 

Update the Deployment Image Reference

This is where it all comes together. Run this command to update the image:

kubectl set image deployment DEPLOYMENT_NAME CONTAINER_NAME=238288103121.dkr.ecr.ap-southeast-1.amazonaws.com/REPOSITORY_NAME:TAG

This will perform a rolling update. This means that it will spin up new Pods with the new image and ensure those are running properly before terminating the current pods, in order to ensure continuous availability.

 

Verify the Update

The update is now being pushed out. You can check on its progression in a couple of ways.

To watch the progress:

kubectl rollout status deployment DEPLOYMENT_NAME
Waiting for deployment "hello-node" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "hello-node" rollout to finish: 1 old replicas are pending termination...
deployment "hello-node" successfully rolled out

If there seems to be an issue, you can see what's happening with the pods in more detail:

kubectl get pods -l app=DEPLOYMENT_NAME
Name Ready Status Restarts Age
hello-node-5776cf4bcf-4sjrt 1/1 Running 0 3s
hello-node-7694cbfdd4-zfrms 1/1 Terminating 0 4d

This will tell you the status of all your Pods. Some may take awhile to be scheduled (Pending status) if a new node needs to be deployed. If any of the pods have failed or seem stuck, note down the POD_NAME and check for the reasons by looking at the output of:

kubectl describe pods POD_NAME
...

Events:
Type Reason Age From Message
Normal Scheduled 2m default-scheduler Successfully assigned default/hello-node-5776cf4bcf-4sjrt to ip-172-31-41-3.ap-southeast-1.compute.internal
...

The Deployment has successfully rolled out when you get:

kubectl rollout status deployment DEPLOYMENT_NAME
deployment "hello-node" successfully rolled out