docker swarm 介绍

为什么不建议在生产环境中使用docker-compose?

docker-compose 单节点的问题,多个实体机就无法适应的。

  • 多机器如何管理?
  • 如果跨机器做scale横向扩展?
  • 容器失败退出时如何新建容器确保服务正常运行?
  • 如何确保零宕机时间?
  • 如何管理密码,Key等敏感数据?
  • 其它

容器编排 swarm

在这里插入图片描述

Swarm的基本架构

在这里插入图片描述

docker swarm vs kubernetes

k8s在容器编排领域处于绝对领先的地位

2021年redhat调查:https://www.redhat.com/en/resources/kubernetes-adoption-security-market-trends-2021-overview

在这里插入图片描述

为什么还要学些了解docker swarm呢?

swarm作为分布式集群架构,相对k8s简单很多,也是一个典型分布式架构,通过学习能了解很多分布式知识的,很多内容和k8s想通的,去理解k8s就会很快,所以可以从swarm开始学起。

Swarm 单节点快速上手

PS F:\docker> docker swarm

Usage:  docker swarm COMMAND

Manage Swarm

Commands:
  ca          Display and rotate the root CA
  init        Initialize a swarm
  join        Join a swarm as a node and/or manager
  join-token  Manage join tokens
  leave       Leave the swarm
  unlock      Unlock swarm
  unlock-key  Manage the unlock key
  update      Update the swarm

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

初始化

docker info 这个命令可以查看我们的docker engine有没有激活swarm模式, 默认是没有的,我们会看到

Swarm: inactive

激活swarm,有两个方法:

  • 初始化一个swarm集群,自己成为manager
  • 加入一个已经存在的swarm集群
PS F:\docker> docker swarm init
Swarm initialized: current node (c1wjw7zp6gnk85rlwww5z5ot5) is now a manager.

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

    docker swarm join --token SWMTKN-1-4wm4icfpg9g4a429is7tjvsqfn28u7xnmkyjilzs4hpzfv031t-0kzmaqemqig5066ybdlwqletw 192.168.65.3:2377

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

PS F:\docker> docker node ls
ID                            HOSTNAME         STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
c1wjw7zp6gnk85rlwww5z5ot5 *   docker-desktop   Ready     Active         Leader           20.10.11
PS F:\docker>

docker swarm init 背后发生了什么

主要是PKI和安全相关的自动化

  • 创建swarm集群的根证书
  • manager节点的证书
  • 其它节点加入集群需要的tokens

创建Raft数据库用于存储证书,配置,密码等数据

退出swarm的环境,非激活的状态

--force牵制离开

PS F:\docker> docker swarm leave --force
Node left the swarm.

创建一个service

PS F:\docker> docker service create nginx:latest
zc60j740nf7drrogxem5zm4to
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
PS F:\docker> docker service ls
ID             NAME              MODE         REPLICAS   IMAGE          PORTS
zc60j740nf7d   beautiful_jones   replicated   1/1        nginx:latest

zc60j740nf7drrogxem5zm4to 是 serviceID

查看具体的一个service:docker service ps

PS F:\docker> docker service ls
ID             NAME              MODE         REPLICAS   IMAGE          PORTS
zc60j740nf7d   beautiful_jones   replicated   1/1        nginx:latest
PS F:\docker> docker service ps zc60j740nf7d
ID             NAME                IMAGE          NODE             DESIRED STATE   CURRENT STATE                ERROR     PORTS
y2os1b2wgyjx   beautiful_jones.1   nginx:latest   docker-desktop   Running         Running about a minute ago           
PS F:\docker>

swarm 扩展

扩展3份
docker service ls 后REPLICAS就是3个

PS F:\docker> docker service update zc60j740nf7d --replicas 3
zc60j740nf7d
overall progress: 3 out of 3 tasks
1/3: running   [==================================================>]
2/3: running   [==================================================>]
3/3: running   [==================================================>]
verify: Service converged
PS F:\docker> docker service ls
ID             NAME              MODE         REPLICAS   IMAGE          PORTS
zc60j740nf7d   beautiful_jones   replicated   3/3        nginx:latest
PS F:\docker> docker service ps zc60j740nf7d
ID             NAME                    IMAGE          NODE             DESIRED STATE   CURRENT STATE                ERROR                         PORTS
bqyqx1mhp715   beautiful_jones.1       nginx:latest   docker-desktop   Running         Running 5 minutes ago
vz5v8rkv60nh   beautiful_jones.2       nginx:latest   docker-desktop   Running         Running about a minute ago
0fj798yiptx8   beautiful_jones.3       nginx:latest   docker-desktop   Running         Running about a minute ago
PS F:\docker>

强制停止一个service里的容器
swarm会新启动一个容器,替代之前杀死的容器,会一直维护3个replicas

PS F:\docker> docker container ls
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS     NAMES
2d749497f171   nginx:latest   "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes   80/tcp    beautiful_jones.2.vz5v8rkv60nhfksky49wbrj86
373421e8191e   nginx:latest   "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes   80/tcp    beautiful_jones.3.0fj798yiptx8xagfyldbi7i1c
7770585ac16e   nginx:latest   "/docker-entrypoint.…"   6 minutes ago   Up 6 minutes   80/tcp    beautiful_jones.1.bqyqx1mhp715msvnoxzccagsf
PS F:\docker> docker container rm -f 7770585ac16e
7770585ac16e
PS F:\docker> docker service ps zc60j740nf7d
ID             NAME                    IMAGE          NODE             DESIRED STATE   CURRENT STATE           ERROR                         PORTS
o968hahg6k8m   beautiful_jones.1       nginx:latest   docker-desktop   Running         Running 6 seconds ago
bqyqx1mhp715    \_ beautiful_jones.1   nginx:latest   docker-desktop   Shutdown        Failed 12 seconds ago   "task: non-zero exit (137)"
vz5v8rkv60nh   beautiful_jones.2       nginx:latest   docker-desktop   Running         Running 3 minutes ago
0fj798yiptx8   beautiful_jones.3       nginx:latest   docker-desktop   Running         Running 3 minutes ago
PS F:\docker>

swarm不光会创建service,还是维护service

swarm删除service

PS F:\docker> docker service rm zc60j740nf7d
zc60j740nf7d
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐