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

MySQL aggregate function sorting

Table of contents MySQL result sorting - Aggregat...

In-depth interpretation of /etc/fstab file in Linux system

Preface [root@localhost ~]# cat /etc/fstab # # /e...

A brief discussion on VUE uni-app conditional coding and page layout

Table of contents Conditional compilation Page La...

Summary of some situations when Docker container disk is full

Preface This article describes two situations I h...

Tips and precautions for using MySQL index

1. The role of index In general application syste...

How to play local media (video and audio) files using HTML and JavaScript

First of all, for security reasons, JavaScript ca...

In-depth analysis of the slow query problem of MySQL Sending data

Through an example, I shared with you the solutio...

js implementation of verification code case

This article example shares the specific code of ...

Tutorial on installing Apache 2.4.41 on Windows 10

1. Apache 2.4.41 installation and configuration T...

MySQL grouping queries and aggregate functions

Overview I believe we often encounter such scenar...

Detailed explanation of meta tags and usage in html

I won’t waste any more time talking nonsense, let...

A detailed guide to custom directives in Vue

Table of contents 1. What is a custom instruction...

A simple method to be compatible with IE6's min-width and min-height

If a website is widescreen, you drag the browser ...

Mysql cannot select non-aggregate columns

1. Introduction I recently upgraded my blog and a...