Docker Swarm

1 购买至少4台机器并安装Docker

可以在阿里云平台购买按时间收费的(2核2G即可),在购买是选取四个机器在同一内网下,安装docker并配置加速。

2 工作模式

2.1 How nodes work

Docker Engine 1.12 introduces swarm mode that enables you to create a cluster of one or more Docker Engines called a swarm. A swarm consists of one or more nodes: physical or virtual machines running Docker Engine 1.12 or later in swarm mode.

There are two types of nodes: managers and workers.

在这里插入图片描述

If you haven’t already, read through the swarm mode overview and key concepts.

有工作节点喝管理节点之分,只能在管理节点上操作。
备用机器有三台+,才能区分出来大多数。

2.2 Manager nodes

Manager nodes handle cluster management tasks:

maintaining cluster state
scheduling services
serving swarm mode HTTP API endpoints
Using a Raft implementation, the managers maintain a consistent internal state of the entire swarm and all the services running on it. For testing purposes it is OK to run a swarm with a single manager. If the manager in a single-manager swarm fails, your services continue to run, but you need to create a new cluster to recover.

To take advantage of swarm mode’s fault-tolerance features, Docker recommends you implement an odd number of nodes according to your organization’s high-availability requirements. When you have multiple managers you can recover from the failure of a manager node without downtime.

A three-manager swarm tolerates a maximum loss of one manager.
A five-manager swarm tolerates a maximum simultaneous loss of two manager nodes.
An N manager cluster tolerates the loss of at most (N-1)/2 managers.
Docker recommends a maximum of seven manager nodes for a swarm.

Important Note: Adding more managers does NOT mean increased scalability or higher performance. In general, the opposite is true.

2.3 Worker nodes

Worker nodes are also instances of Docker Engine whose sole purpose is to execute containers. Worker nodes don’t participate in the Raft distributed state, make scheduling decisions, or serve the swarm mode HTTP API.

You can create a swarm of one manager node, but you cannot have a worker node without at least one manager node. By default, all managers are also workers. In a single manager node cluster, you can run commands like docker service create and the scheduler places all tasks on the local Engine.

To prevent the scheduler from placing tasks on a manager node in a multi-node swarm, set the availability for the manager node to Drain. The scheduler gracefully stops tasks on nodes in Drain mode and schedules the tasks on an Active node. The scheduler does not assign new tasks to nodes with Drain availability.

Refer to the docker node update command line reference to see how to change node availability.

2.4 Raft协议

双主双从: 假设一个节点挂了!其他节点是否可以用!
Raft协议: 保证大多数节点存活才可以用。 只要>1 ,集群至少大于3台!

保证大多数节点存活,才可以使用,高可用!

3 如何工作

3.1 准备节点并加入swarm中

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
加入一个节点:

docker swarm join --token SWMTKN-1-1wm6z3k6044575nlqlnfyrqj8uf6rf6r5tkkijvfjy483fd3cw-2q0fhwf1glr0w8hj2njotzyb3 114.115.145.119:2377

获得管理员令牌或者worker:

docker swarm join-token manager
docker swarm join-token worker

ok各节点都加入后:
在这里插入图片描述
或者作为manager加入
在这里插入图片描述
此时我们命令xiaoni02离开集群,在xiaoni01处使用docker node ls,会报错:Error response from daemon: rpc error: code = Unknown desc = The swarm does not have a leader. It‘s
解决方案

再次放xiaoni02作为worker加入
在这里插入图片描述

3.2 service相关(创建、扩展、更新等等)

在这里插入图片描述
灰度发布:金丝雀发布!: 在升级中是不影响提供服务的。

在这里插入图片描述

docker run 容器启动!不具有扩缩容器
docker service 服务! 具有扩缩容器,滚动更新!

在这里插入图片描述
在这里插入图片描述
此时可以看到他存在于一个副本上
也可以开启10个:
在这里插入图片描述

//下面两条扩缩容量都可
root@xiaoni-01:~# docker service scale my-nginx=4

root@xiaoni-01:~# docker service update --replicas 10 my-nginx

4 总结

4.1 概念

swarm
集群的管理和编号。 docker可以初始化一个 swarm 集群,其他节点可以加入。(管理、工作者)

Node
就是一个docker节点。多个节点就组成了一个网络集群。(管理、工作者)

Service
任务,可以在管理节点或者工作节点来运行。核心。!用户访问!

Task
容器内的命令,细节任务

在这里插入图片描述
命令 -> 管理 -> api -> 调度 -> 工作节点(创建Task容器维护创建!)

在这里插入图片描述
调整service以什么方式运行

--mode string
Service mode (replicated or global) (default "replicated")
docker service create --mode replicated --name mytom tomcat:7 默认的
docker service create --mode global --name haha alpine ping baidu.com
#场景?日志收集
每一个节点有自己的日志收集器,过滤。把所有日志最终再传给日志中心
服务监控,状态性能。

4.2 体会

弹性、扩缩容!集群!
以后告别 docker run!
docker-compose up! 启动一个项目。单机!
集群: 我们使用swarm docker serivce

把容器 => 服务,屏蔽的底层的差异,比如需要访问redis,此时有三个机器上在跑,要想扩展到10个,对于用户他是不知道的,屏蔽底层差异。

副本:保证在前面的挂掉后,可以替补(和nginx相比,不需要多么的配置,只需要动态扩缩容即可),例如redis 服务有十个10个副本相当于同时开启了10个容器。

步骤:创建服务、动态扩展服务、动态更新服务。(在主节点)

5 后续学习计划

  1. Docker stack
    docker-compose 单机部署项目!
    Docker Stack部署,集群部署!
  2. Docker Secret
  3. Docker Config
  4. K8S
  5. Go语言
    • Docker 是Go开发的
    • K8S也是Go开发的
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoni61

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值