Zero Downtime Deployment using Azure App Service Deployment Slots
Dimitar Iliev ??
Azure AI Solutions Architect ● B. Sc. Computer Science and Engineering ● 7 x Microsoft Certified ● 23 x Microsoft Applied Skills ● Speaker ● Generative AI ● Scrum Master Certified ● 1 x GitHub Certified
What is zero downtime deployment??
Zero downtime deployment is a method where your application is never down during the deployment process. With this, we successfully introduce a new version of our application without any downtime of the same.
How to do a zero downtime deployment using Azure App Service Deployment Slots?
According to the official MS Docs (Set up staging environments - Azure App Service | Microsoft Docs) when you deploy your app to an Azure App Service, you can use a separate deployment slot.?
Deploying your app to a non-production deployment slot has the following benefits:?
Now let us see how we can implement this?in a real scenario.?
Implementing zero downtime deployment
The first thing we need to do is create an Azure App Service to which we will deploy our application.
When creating the App Service, remember to choose an app service plan that has deployment slots in the “Included Features” part.?
After the creation is complete, let's first publish our API without any changes to it to the production deployment slot.
If we make an HTTP GET request to our API we can see the following response:?
Now let's make a change by extending the WeatherForecast.cs model to include an additional field named Description.
namespace ZeroDowntimeDeployment
{
? ? public class WeatherForecast
? ? {
? ? ? ? public DateTime Date { get; set; }
? ? ? ? public int TemperatureC { get; set; }
? ? ? ? public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
? ? ? ? public string? Summary { get; set; }
? ? ? ? public string Description { get; set; } = "Zero Downtime Deployment";
? ? }
}
When we deploy this change to production, we want to do it with zero downtime of our API.
For this to work, first we need to create a new deployment slot.?
Open the App Service that you previously created and click on?“Deployment slots” from the “Deployment” section.
Currently we have only our production deployment slot. We want to create an additional preview deployment slot, so we click on “Add Slot”.
Add a name for the slot and choose to clone the settings from the production slot.?
We click on “Add” and now our deployment slot is created.
领英推荐
If we open our preview deployment slot, we can see that it is the same as our production slot. One thing to note here is that this slot has its own URL.
Perfect. Now since we need a zero downtime deployment, we will start deploying our new changes (and all future changes) to our preview deployment slot.
After we successfully deploy, we can see the changes if we make an HTTP GET request to the URL of our preview deployment slot.?
Let's compare the responses of both of our deployment slots by making the same HTTP GET request to both of them.
We notice that both our slots are working and both have the appropriate deployment we did. Now we need to bring the changes from the preview deployment slot to our production deployment slot.?
This will be done with the Swap operation.?
If you open the “Deployment slots” tab, at the top menu, you can see a “Swap” button.
Clicking on the “Swap” button will open the following blade:?
Note that the Source slot is the preview app and the Target slot is the production app. Additionally, you can perform a swap with preview (which I won’t go into details here) but for those interested I recommend you read the official MS Docs (Set up staging environments - Azure App Service | Microsoft Docs).?
After we confirm that everything looks good, we click on “Swap”.?
When the swap operation finishes, we can make the same HTTP GET request to both of our deployment slots and compare their responses.
First thing to note here is that our production API returned a response without any downtime. This is because deploying an app to a slot first and swapping it into production makes sure that all instances of the slot are warmed up before being swapped.?
Second thing to note is that our changes from the preview slot are now on production, and our production code is on the preview slot.?
With this, we successfully did a zero downtime deployment of our API to the production environment.
Thanks for sticking to the end of another article from?"Iliev Talks Tech". #ilievtalkstech?
The API used in this example can be found on my GitHub repository on the following link: