Docker Swarm
Docker Swarm
docker swarm init --advertise-addr 192.168.122.171 # 初始化创建一个集群
docker network ls # 查看网络
docker node ls # 查看节点 只能在主节点上查看
docker swarm join-token worker # 生成工作节点令牌命令
docker swarm join-token manager # 生成主节点令牌命令
双主双从,不合理,Raft原则:保证大多数的服务器可用,
当关闭一个主节点时:
[root@node1 ~]# docker node ls
Error response from daemon: rpc error: code = 4 desc = context deadline exceeded
docker swarm leave # 让node3节点离开
[root@node4 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
mh1cxxjetuk4odh51az2vlzmr node3 Down Active
ngi2kf7g0fyxjvvqoqtydhba5 node2 Ready Active
pi76wh4e6m2ijwd9t1r2b8zoo * node4 Ready Active Reachable
wno67f1ibrfrmy502hx49mw2v node1 Ready Active Leader
# 将node3 也设置为manager
[root@node3 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
mh1cxxjetuk4odh51az2vlzmr node3 Down Active
ngi2kf7g0fyxjvvqoqtydhba5 node2 Ready Active
pi76wh4e6m2ijwd9t1r2b8zoo node4 Ready Active Reachable
wno67f1ibrfrmy502hx49mw2v node1 Ready Active Leader
zfsh8qbgvnlx8lume3svwnez5 * node3 Ready Active Reachable
# 让node4 挂掉
[root@node3 ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
mh1cxxjetuk4odh51az2vlzmr node3 Down Active
ngi2kf7g0fyxjvvqoqtydhba5 node2 Ready Active
pi76wh4e6m2ijwd9t1r2b8zoo node4 Down Active Unreachable
wno67f1ibrfrmy502hx49mw2v node1 Ready Active Leader
zfsh8qbgvnlx8lume3svwnez5 * node3 Ready Active Reachable
docker node ls # 还是可以查到,说明集群还是可用的,这就说明了Raft原则
docker service COMMAND
> Commands:
> create Create a new service
> inspect Display detailed information on one or more services
> ls List services
> ps List the tasks of a service
> rm Remove one or more services
> scale Scale one or multiple replicated services
> update Update a service
docker service create -p 8888:80 --name mynginx nginx
[root@node1 ~]# docker service ps mynginx # 查看服务进程
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
woz6eqmtw55e mynginx.1 nginx:latest node4 Running Running 37 seconds ago
docker-compose up 启动的一个项目,还是单机的,这是不现实的,
只有集群才可以
redis 3份 容器
三个redis 不一定在同一个服务器上
容器变成服务,服务==》 副本
redis服务 =》 10个副本 同时开启10个redis容器
扩缩容:
# update 方式
[root@node1 ~]# docker service update --replicas 3 mynginx
mynginx
[root@node1 ~]# docker service ps mynginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
woz6eqmtw55e mynginx.1 nginx:latest node4 Running Running 4 minutes ago
27hlhqn6h21q mynginx.2 nginx:latest node2 Running Preparing 9 seconds ago
l4di878yi5m9 mynginx.3 nginx:latest node1 Running Preparing 9 seconds ago
[root@node1 ~]# docker service update --replicas 2 mynginx
mynginx
[root@node1 ~]# docker service ps mynginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
27hlhqn6h21q mynginx.2 nginx:latest node2 Running Running 3 minutes ago
l4di878yi5m9 mynginx.3 nginx:latest node1 Running Running 3 minutes ago
# 另一种方式 scale
[root@node1 ~]# docker service scale mynginx=6
mynginx scaled to 6
# 查看mynginx的服务进程
[root@node1 ~]# docker service ps mynginx
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
yso77fxq2igf mynginx.1 nginx:latest node4 Running Running 12 seconds ago
27hlhqn6h21q mynginx.2 nginx:latest node2 Running Running 4 minutes ago
l4di878yi5m9 mynginx.3 nginx:latest node1 Running Running 4 minutes ago
w52awqqvvrox mynginx.4 nginx:latest node3 Running Preparing 14 seconds ago
u8i43jr046sm mynginx.5 nginx:latest node3 Running Preparing 14 seconds ago
ddvpu4olognu mynginx.6 nginx:latest node1 Running Running 13 seconds ago
移除一个服务
# 移除一个服务
docker service rm mynginx
访问:
访问任意一个节点,都可以得到结果,即使这个节点上真实没有运行,但是他们是一个整体集群