Sample code for implementing rolling updates of services using Docker Swarm

Sample code for implementing rolling updates of services using Docker Swarm

1. What is Docker Swarm?

Docker Swarm is a cluster management tool officially provided by Docker. Its main function is to abstract several Docker hosts into a whole and manage various Docker resources on these Docker hosts through a unified entrance. Swarm is similar to Kubernetes, but is lighter and has fewer features than Kubernetes.

Docker Swarm, like Docker Compose, is Docker's official container orchestration project. The difference is that Docker Compose is a tool for creating multiple containers on a single server or host, while Docker Swarm can create container cluster services on multiple servers or hosts. For the deployment of microservices, Docker Swarm is obviously more suitable.

Since Docker 1.12.0, Docker Swarm has been included in the Docker engine (docker swarm), and a service discovery tool has been built in. We no longer need to configure Etcd or Consul for service discovery as before.

2. Docker Swarm Architecture

This diagram as a whole is actually in a so-called cluster, which may correspond to one or more actual servers. Docker is installed on each server and the HTTP-based Docker API is enabled. This cluster has a SwarmManager manager, which is used to manage container resources in the cluster. The manager's management object is not at the server level but at the cluster level. That is to say, through the Manager, we can only issue general instructions to the cluster but cannot specify what to do on a specific server (this is also the essence of Swarm). As for the specific management implementation method, the Manager exposes an HTTP interface to the outside world, and external users manage the cluster through this HTTP interface. For slightly larger clusters, it is best to set aside an actual server as a dedicated manager. For learning purposes, you can also put the manager and the managed on the same server.

3. Rolling Update of Docker Swarm Service

Docker Swarm can achieve smooth service upgrades, that is, services are updated without downtime and clients are unaware. Let us demonstrate this through a specific example. Here we will deploy an nginx-based web application service on the node node. We will create two versions of the same application: version 1 and version 2

Create a Dockerfile and compile it using docker build.

FROM nginx
RUN echo '<h1>Swarm:Version 1 <h1>' > /usr/share/nginx/html/index.html

Note: In order to make the image accessible to every node in the Swarm cluster, we upload the generated image to our own image repository.

docker login
docker build -t collenzhao/mynginx:v1 .
docker push collenzhao/mynginx:v1

Create a Swarm service, that is, start the container through the image

docker service create -p 7788:80 --replicas 3 --name myswarmtest collenzhao/mynginx:v1

View the deployed services through docker service ls.

View the detailed information of the deployed service through docker service ps myswarmtest

The effect is shown in the figure below

Update the previous Dockerfile, note that the version number becomes: 2

FROM nginx
RUN echo '<h1>Swarm:Version 2 <h1>' > /usr/share/nginx/html/index.html

Compile using docker build

docker build -t collenzhao/mynginx:v2 .

Upload to Docker Hub using Docker push

docker push collenzhao/mynginx:v2

Update the service deployed in Swarm before, the version number becomes 2

docker service update --image collenzhao/mynginx:v2 myswarmtest

The effect is as follows

This concludes this article on sample code for implementing rolling updates of services with Docker Swarm. For more information about Docker Swarm rolling updates, please search for previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to use Docker Swarm to build WordPress
  • Docker Swarm from deployment to basic operations
  • How does docker swarm run a specified container on a specified node?
  • Detailed explanation of Docker Swarm service discovery and load balancing principles
  • Detailed explanation of docker swarm cluster failures and exceptions
  • How to use Docker Swarm to build a cluster
  • Docker Swarm Getting Started Example
  • Detailed explanation of using Docker 1.12 to build a multi-host Docker swarm cluster
  • Detailed explanation of Docker Swarm concepts and usage

<<:  Methods and steps to upgrade MySql5.x to MySql8.x

>>:  js+css to realize three-level navigation menu

Recommend

Teach you to implement a simple promise step by step

Table of contents Step 1: Build the framework Ste...

W3C Tutorial (5): W3C XML Activities

XML is designed to describe, store, transmit and ...

Implementing shopping cart function based on vuex

This article example shares the specific code of ...

Solution to the problem of passing values ​​between html pages

The first time I used the essay, I felt quite awkw...

Installing MySQL 8.0.12 based on Windows

This tutorial is only applicable to Windows syste...

A brief discussion on Axios's solution to remove duplicate requests

Table of contents 1. Cancel duplicate requests 2....

MySQL 8.0.18 Installation Configuration Optimization Tutorial

Mysql installation, configuration, and optimizati...

Mysql 8.0.18 hash join test (recommended)

Hash Join Hash Join does not require any indexes ...

Several practical scenarios for implementing the replace function in MySQL

REPLACE Syntax REPLACE(String,from_str,to_str) Th...

JavaScript to implement a simple clock

This article example shares the specific code for...

How webpack implements static resource caching

Table of contents introduction Distinguish betwee...