Upgrading Your Application In Azure Managed Kubernetes

In the previous article, we deployed an ASP.NET Core 2.0 API app in AKS (Azure Managed Kubernetes). Now, let's quickly look into how to upgrade the application when there is a change in code. Please make sure you read the previous article to follow through this one.

Let's start with a code change in our previously created controller class. Just alter the position of the concatenated output from the 2nd Get method, move the machine name at the end.

  1. return Ok(ListOfQuotes.OrderBy(_ => r.Next(ListOfQuotes.Length)).Take(count).Select(x => $"{x}-{Environment.MachineName}"));  

Once the controller is updated, let's create a new docker image locally with a new tag (consider this as a new version). Once done, you should be able to see the new image created with tag 'linuxV2'.

  1. // Build the image  
  2. docker build -t sanjaysrepo.azurecr.io/quotesgenerator:linuxV2 .  
  3.   
  4. // Validate the new image is created  
  5. docker images  

Azure

Now, let's publish the same in our previously created Azure Container Registry (ACR). Make sure you are authenticated.

  1. // Authenticate to ACR, you may need az login before this if cached credentials are cleared by now  
  2. az acr login --name sanjaysrepo  
  3.    
  4. // Push new image to ACR  
  5. docker push sanjaysrepo.azurecr.io/quotesgenerator:linuxV2  
  6.    
  7. // Validate we have both the images (linux + linuxV2) in ACR  
  8. az acr repository show-tags --name sanjaysrepo --repository quotesgenerator --output table  

Cool! It's time to update our Kubernetes cluster deployment with this new image. Before we start, validate that you still have a deployment with 5 pods (spread across 2 VMs). To ensure maximum uptime, multiple instances of the application pod must be running, else as you can guess your app will go into offline mode as the only one pod is getting upgraded. Scale-up your pods (as you saw in the previous article) if you don't have more than one instance of the service running.

Azure

Now to upgrade your application please execute the following command. Also while the pods are being updated you can check the status of the pods by 'kubectl get pods' command. You can see how the pods restart themselves. At the end, after few moments all pods will be up and running again with the latest image.

  1. // Upgrade image in all the pods of a deployment  
  2. kubectl set image deployment quotes quotes=sanjaysrepo.azurecr.io/quotesgenerator:linuxV2  
  3.    
  4. // Monitor the pods  
  5. kubectl get pods  

Azure

While this was in progress quickly fire up Fiddler and hit the API URL as before 'http://your-public-ip/api/quotes/12'. Do it multiple times as quickly as possible, you can actually see that a few nodes that are not upgraded are still returning old formatted values, and a few nodes that are already upgraded are returning newly formatted values. This is so cool!

Azure

All done! We just successfully updated our code in the existing/running AKS cluster.


Similar Articles