Swarm集群管理

5.1 安装

1、拉取镜像

root@localhost \~# docker pull swarm

2、查看版本

root@localhost \~# docker run --rm swarm:latest -v

swarm version 1.2.9 (527a849)

5.2 集群

5.2.1 主机规划

主机名 IP地址 角色
manager1 192.168.72.47 管理节点
worker1 192.168.72.48 工作节点
worker2 192.168.72.49 工作节点

5.2.2 环境准备

5.2.2.1 manager1

1、克隆一台虚拟机

2、修改主机名

root@localhost \~# hostnamectl set-hostname manager1

root@localhost \~# bash

3、修改IP

root@manager1 \~# nmcli connection modify ens160 ipv4.method manual ipv4.addr47/24 ipv4.gateway 192.168.72.2 ipv4.dns 223.5.5.5 connection.autoconnect yes

root@manager1 \~# nmcli connection up ens160

4、配置hosts

root@manager1 \~# cat >> /etc/hosts <<EOF

> 192.168.72.47 manager1

192.168.72.48 worker1

192.168.72.49 worker2

> EOF

root@manager1 \~# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.72.47 manager1

192.168.72.48 worker1

192.168.72.49 worker2

5.2.2.2 worker1

1、克隆一台虚拟机

2、设置主机名

root@localhost \~# hostnamectl set-hostname worker1

root@localhost \~# bash

3、修改IP

root@worker1 \~# nmcli connection modify ens160 ipv4.method manual ipv4.addresses 192.162 ipv4.dns 223.5.5.5 connection.autoconnect yes

root@worker1 \~# nmcli c up ens160

4、映射hosts

root@worker1 \~# cat >> /etc/hosts <<EOF

> 192.168.72.47 manager1

192.168.72.48 worker1

192.168.72.49 worker2

> EOF

root@worker1 \~# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.72.47 manager1

192.168.72.48 worker1

192.168.72.49 worker2

5.2.2.3 worker2

1、克隆一台虚拟机

2、设置主机名

root@localhost \~# hostnamectl set-hostname worker2 && bash

3、修改IP

root@worker2 \~# nmcli connection modify ens160 ipv4.method manual ipv4.addre2 ipv4.dns 223.5.5.5 connection.autoconnect yes

root@worker2 \~# nmcli c up ens160

4、映射hosts

root@worker2 \~# cat >> /etc/hosts <<EOF

192.168.72.47 manager1

192.168.72.48 worker1

192.168.72.49 worker2

> EOF

root@worker2 \~# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.72.47 manager1

192.168.72.48 worker1

192.168.72.49 worker2

5.2.3 安装swarm

分别在三台(manager1、worker1和worker2)上安装 swarm。

root@manager1 \~# docker pull swarm

Using default tag: latest

latest: Pulling from library/swarm

2064f1a73c6b: Pull complete

38e5683d7755: Pull complete

083aff163606: Pull complete

Digest: sha256:2de8883e2933840ed7ee7360ea1eed314bf8aeac37c0692b9ca651630fde3b7f

Status: Downloaded newer image for swarm:latest

docker.io/library/swarm:latest

root@manager1 \~# docker images

IMAGE ID DISK USAGE CONTENT SIZE EXTRA

swarm:latest 2de8883e2933 16.6MB 3.85MB

root@worker1 \~# docker pull swarm

Using default tag: latest

latest: Pulling from library/swarm

2064f1a73c6b: Pull complete

083aff163606: Pull complete

38e5683d7755: Pull complete

Digest: sha256:2de8883e2933840ed7ee7360ea1eed314bf8aeac37c0692b9ca651630fde3b7f

Status: Downloaded newer image for swarm:latest

docker.io/library/swarm:latest

root@worker1 \~# docker images

IMAGE ID DISK USAGE CONTENT SIZE EXTRA

swarm:latest 2de8883e2933 16.6MB 3.85MB

root@worker2 \~# docker pull swarm

Using default tag: latest

latest: Pulling from library/swarm

2064f1a73c6b: Pull complete

083aff163606: Pull complete

38e5683d7755: Pull complete

Digest: sha256:2de8883e2933840ed7ee7360ea1eed314bf8aeac37c0692b9ca651630fde3b7f

Status: Downloaded newer image for swarm:latest

docker.io/library/swarm:latest

root@worker2 \~# docker images

IMAGE ID DISK USAGE CONTENT SIZE EXTRA

swarm:latest 2de8883e2933 16.6MB 3.85MB

5.2.4 创建集群

5.2.4.1 初始化swarm

创建集群在管理节点上执行,语法格式如下:

root@manager1 \~# docker swarm --help

Usage: docker swarm COMMAND

Manage Swarm

Commands:

init Initialize a swarm

join Join a swarm as a node and/or manager

Run 'docker swarm COMMAND --help' for more information on a command.

然后通过 docker swarm init 命令来初始化一个集群:

root@manager1 \~# docker swarm init --help

Usage: docker swarm init OPTIONS

Initialize a swarm

Options:

--advertise-addr string Advertised address

(format:

"<ip|interface>:port")

--autolock Enable manager

autolocking (requiring

an unlock key to start

a stopped manager)

--availability string Availability of the

node ("active",

"pause", "drain")

(default "active")

--cert-expiry duration Validity period for

node certificates

(ns|us|ms|s|m|h)

(default 2160h0m0s)

--data-path-addr string Address or interface to

use for data path

traffic (format:

"<ip|interface>")

--data-path-port uint32 Port number to use for

data path traffic (1024

  • 49151). If no value

is set or is set to 0,

the default port (4789)

is used.

--default-addr-pool ipNetSlice default address pool in

CIDR format (default \[\])

--default-addr-pool-mask-length uint32 default address pool

subnet mask length

(default 24)

--dispatcher-heartbeat duration Dispatcher heartbeat

period (ns|us|ms|s|m|h)

(default 5s)

--external-ca external-ca Specifications of one

or more certificate

signing endpoints

--force-new-cluster Force create a new

cluster from current state

--listen-addr node-addr Listen address (format:

"<ip|interface>:port") (default 0.0.0.0:2377)

--max-snapshots uint Number of additional

Raft snapshots to retain

--snapshot-interval uint Number of log entries

between Raft snapshots

(default 10000)

--task-history-limit int Task history retention

limit (default 5)

通过 192.168.72.47 来创建一个集群:

root@manager1 \~# docker swarm init --advertise-addr 192.168.72.47

Swarm initialized: current node (osyz257ok5knqoyni4wtvirr2) is now a manager.

To add a worker to this swarm, run the following command:

docker swarm join --token SWMTKN-1-3n3dz2sahk8bqqim0i7xfoxkordn9p1segqm7y5d621ht0o2as-05vyytdd94vgsipmud58dtk34 192.168.72.47:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

5.2.4.2 查看节点

root@manager1 \~# docker node ls

ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION

osyz257ok5knqoyni4wtvirr2 * manager1 Ready Active Leader 29.1.5

5.2.5 加入工作节点

1、将worker1节点加入到集群中,注意:在worker1节点上执行如下命令

root@worker1 \~# docker swarm join --token SWMTKN-1-3n3dz2sahk8bqqim0i7xfoxkordn9p1segqm7y5d621ht0o2as-05vyytdd94vgsipmud58dtk34 192.168.72.47:2377

This node joined a swarm as a worker.

查看节点:

root@manager1 \~# docker node ls

ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION

osyz257ok5knqoyni4wtvirr2 * manager1 Ready Active Leader 29.1.5

caq3v6kyrsjc8xkkqlcbt2jfd worker1 Ready Active 29.1.5

2、将worker2节点加入到集群中,注意:在worker2节点上执行如下命令

root@worker2 \~# docker swarm join --token SWMTKN-1-3n3dz2sahk8bqqim0i7xfoxkordn9p1segqm7y5d621ht0o2as-05vyytdd94vgsipmud58dtk34 192.168.72.47:2377

This node joined a swarm as a worker.

查看节点:

root@manager1 \~# docker node ls

ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION

osyz257ok5knqoyni4wtvirr2 * manager1 Ready Active Leader 29.1.5

caq3v6kyrsjc8xkkqlcbt2jfd worker1 Ready Active 29.1.5

vzejmojj7gciu54malpilxv8j worker2 Ready Active 29.1.5

5.2.6 节点命令

我们可以使用 docker node 命令来查看节点信息,语法如下:

root@manager1 \~# docker node --help

Usage: docker node COMMAND

Manage Swarm nodes

Commands:

demote Demote one or more nodes from manager in the swarm

inspect Display detailed information on one or more nodes

ls List nodes in the swarm

promote Promote one or more nodes to manager in the swarm

ps List tasks running on one or more nodes, defaults to current node

rm Remove one or more nodes from the swarm

update Update a node

例如,查看所有节点

root@manager1 \~# docker node ls

再如,查看节点详细信息:

root@manager1 \~# docker node inspect

5.2.7 发布服务

例如:在集群中运行 nginx:1.28.1 服务,并且运行3个副本。

root@manager1 \~# docker service create --replicas 3 --name nginx -p 80:80 nginx:1.28.1

xprlwjn0nxmm3qj4ksyg0qeup

overall progress: 3 out of 3 tasks

1/3: running

2/3: running

3/3: running

verify: Service xprlwjn0nxmm3qj4ksyg0qeup converged

查看服务:

root@manager1 \~# docker service ls

ID NAME MODE REPLICAS IMAGE PORTS

xprlwjn0nxmm nginx replicated 3/3 nginx:1.28.1 *:80->80/tcp

访问测试:

root@manager1 \~# curl 192.168.72.47

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

html { color-scheme: light dark; }

body { width: 35em; margin: 0 auto;

font-family: Tahoma, Verdana, Arial, sans-serif; }

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>

<p>For online documentation and support please refer to

<a href="http://nginx.org/">nginx.org</a>.<br/>

Commercial support is available at

<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>

</body>

</html>

5.2.8 删除服务

使用 docker service rm 命令,语法如下:

root@manager1 \~# docker service rm --help

Usage: docker service rm SERVICE SERVICE...

Remove one or more services

Aliases:

docker service rm, docker service remove

例如,删除 nginx 服务

root@manager1 \~# docker service rm nginx

nginx

root@manager1 \~# docker service ls

ID NAME MODE REPLICAS IMAGE PORTS

相关推荐
乘云数字DATABUFF3 天前
5分钟部署开源APM Databuff:OpenTelemetry全链路追踪入门实战
运维·后端
荣--5 天前
一键部署不是为了省时间 —— 它是把"买来的 PaaS"变成"自己的平台"的拐点
运维·zabbix·工程化·一键部署·平台化·边界设计
江华森5 天前
动手实战学 Docker — 从零到集群编排完全指南
运维
Avan_菜菜6 天前
FRP 内网穿透完整实战:从 HTTP 映射到 HTTPS 自签代理
运维·nginx·https
SelectDB7 天前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
XIAOHEZIcode8 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220709 天前
如何搭建本地yum源(上)
运维
ping某10 天前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
大树8812 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠12 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql