ASPNET Core Zero Downtime Deployment to Linux with Nginx

Prerequisites:

On Your Application:

  • A healthcheck url that returns the version of the application with json format like below.
Environment.GetEnvironmentVariable("DEPLOYMENT_VERSION")
  • Create your deployment package as tar.gz.
  • Add the file named appsettings.json.tmpl to change the appsettings.json variables for your deployment environment.
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default":"Information",
"Microsoft": "Information",
"System": "Information"
}
}
}
{
"Logging": {
"LogLevel": {
"Microsoft": "{{.LOG_LEVEL_MICROSOFT}}",
"System": "{{.LOG_LEVEL_SYSTEM}}"
}
}
}
  • ASPNET Core runtime.
  • Nginx.
  • jq package.
  • Bonus: Increase the max open file limit for the system.
echo "fs.file-max = 640000" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
  • Bonus: A crontab line to delete the old deployment files that excluded last of 3 version. (every 3 days.)
crontab -l | { cat; echo '0 0 */3 * * $(for folder in /var/aspnetcore/*; do [ -d $folder ] && (cd $folder && ls -t | tail -n +4 | xargs sudo rm -rf --); done)'; } | crontab -

Here is the things we need to do

  • Create an ssh connection to Linux machine you want the deploy.
  • Fetch your application package to the machine as a tar.gz file.
  • Replace appsetting.json variables for the environment via the machine environment variables.
  • Run the application and wait until it is up for it up to 3 minutes.
  • Configure and reload the Nginx to rotate incoming requests to the newly deployed one.
  • Wait for the correct version is up while we requesting it via the machine IP. (up to 1 minute.)
  • Destroy the old one.

Folder and Configuration Structure

var
|_ aspnetcore
|_ Example.Application.Name
|_ 1001
|_ 1005
|_ 1005-01
etc
|_ systemd
|_ system
|_ Example.Application.Name-1005-01.service
|_ nginx
|_ sites-available
|_ Example.Application.Name

The Bash Script

Configure the variables at the top of the script by your requirements.

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store