Zero-Downtime Deployments with Docker Swarm and Portainer - Part 2/2
Nico Saia 3 Minuten Lesezeit

Zero-Downtime Deployments with Docker Swarm and Portainer - Part 2/2

High availability fun with Docker Swarm: GitOps deployments with Portainer.
docker-swarm apps

What have we achieved so far?

In Part 1, we explored the differences between rolling updates and zero-downtime deployments. We then prepared our stack to perform a zero-downtime deployment.

In this part, we will look at how we can update our application via Portainer. We will trigger an update automatically via GitHub, so we don’t have to do anything in Portainer.

Prerequisites

At this point, I assume there is a running Portainer instance and the project from Part 1 is either on GitHub (used in this example) or another version control system.

If the project is in a private repository, we need to create a GitHub Personal Access Token. How to do this is described below:

Create GitHub Personal Access Token

Since our project is in a private repository, we need to create a Personal Access Token. We can then store this in Portainer so that Portainer can access our repository.

On GitHub, this is quite simple, and we only need to follow these steps:

  1. Log in to GitHub
  2. Navigate to “Settings” and click on “Developer Settings”
  3. Click on “Personal access tokens” and then “Generate new token”
  4. Enter a name for the token and select the “repo” permission
  5. After creating the token, copy it and save it in a secure place.

This can also be read in the GitHub documentation.


Linking Portainer Stack with GitHub

Now we create a new stack in Portainer and link it with our GitHub repository. We set it up so that Portainer automatically triggers an update as soon as we push a new version to GitHub.

First, we create a new stack in Portainer:

  1. Enter a name for the stack
  2. Select that our project is in a Git repository
  3. Since our project is in a private repository, enable Authentication so that Portainer can access our repository
  4. Enter our GitHub username
  5. Now use the previously created GitHub Personal Access Token and insert it here. Portainer now also offers the possibility to store these tokens so that they only need to be selected.
  6. Enter the repository URL
  7. If the YAML file is not in the root directory, specify the path here.

At this point, we have linked our project on GitHub with Portainer. Now we need to set up Portainer to automatically trigger an update as soon as we push a new version to GitHub.

  1. Enable the option GitOps updates

  2. Here we have several options for how Portainer can fetch the updates. By default, Polling is used, which means Portainer checks for updates at regular intervals. We can also use Webhooks, where Portainer is notified by GitHub as soon as an update is available. To do this, we would need to create a webhook in GitHub, but that would go beyond the scope here. Therefore, we use Polling.

    We can also see that Re-pull image and Force redeployment are premium features available only in the business version of Portainer.

    Re-pull image means that Portainer downloads the image again as soon as an update is available.

    Force redeployment means that Portainer restarts the application as soon as an update is available.

  3. Finally, click the Deploy the stack button, and Portainer will deploy the project from the repository.

I recommend checking out the documentation on Portainer. From this point, we can make changes to our stack on GitHub, and Portainer will automatically deploy them. The application will not go offline, achieving a zero-downtime deployment.

At this point, I won’t explicitly show the update process via GitHub and Portainer, as it would be too extensive. I recommend you take a look at it yourself. It’s really very simple and intuitive.

Ähnliche Artikel