docker -swarm集群搭建

docker -swarm集群搭建,第1张

docker -swarm集群搭建

一共三台主机
192.168.19.4
192.168.19.5
192.168.19.6

192.168.19.4
设置主机名

[root@centos redis-cluster]# hostnamectl set-hostname manager
[root@centos redis-cluster]#

初始化swarm集群

[root@centos redis-cluster]# docker swarm init 
Swarm initialized: current node (lfjk87d0pguk7tkgoj69fu9nq) is now a manager.

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

    docker swarm join --token SWMTKN-1-526bzo5skkyhb509xk6mpbmsm5wepnbt12clg68ozr0n93emtf-adqinc1qrub0uyti0683hjqvw 192.168.19.4:2377

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

获取加入令牌

[root@centos redis-cluster]# docker swarm join-token manager
To add a manager to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-526bzo5skkyhb509xk6mpbmsm5wepnbt12clg68ozr0n93emtf-b1r2kkd4ccic3jpdlpsh8sjlq 192.168.19.4:2377

[root@centos redis-cluster]# docker swarm join-token worker
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-526bzo5skkyhb509xk6mpbmsm5wepnbt12clg68ozr0n93emtf-adqinc1qrub0uyti0683hjqvw 192.168.19.4:2377

[root@centos redis-cluster]# 

查询集群的所有节点

[root@centos redis-cluster]# docker node ls
ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
lfjk87d0pguk7tkgoj69fu9nq *   manager    Ready     Active         Leader           20.10.11

192.168.19.5
设置机器

[root@centos ~]# hostnamectl set-hostname worker

加入集群

[root@centos ~]# docker swarm join --token SWMTKN-1-526bzo5skkyhb509xk6mpbmsm5wepnbt12clg68ozr0n93emtf-adqinc1qrub0uyti0683hjqvw 192.168.19.4:2377
This node joined a swarm as a worker.

192.168.19.4
创建服务

[root@centos redis-cluster]# docker service create --replicas 1 --name mynginx -p 80:80 nginx
puajwi3kllcgd482vdyqdm4mn
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service converge

查看服务

[root@centos redis-cluster]# docker service ls
ID             NAME      MODE         REPLICAS   IMAGE          PORTS
puajwi3kllcg   mynginx   replicated   1/1        nginx:latest   *:80->80/tcp
[root@centos redis-cluster]# docker service inspect --pretty mynginx

ID:		yirs931yqmkzwjkuhhlrvyex3
Name:		mynginx
Service Mode:	Replicated
 Replicas:	1
Placement:
UpdateConfig:
 Parallelism:	1
 On failure:	pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Update order:      stop-first
RollbackConfig:
 Parallelism:	1
 On failure:	pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Rollback order:    stop-first
ContainerSpec:
 Image:		nginx:latest@sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
 Init:		false
Resources:
Endpoint Mode:	vip
Ports:
 PublishedPort = 80
  Protocol = tcp
  TargetPort = 80
  PublishMode = ingress 

动态扩容

[root@centos redis-cluster]# docker service scale mynginx=2
mynginx scaled to 2
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged
[root@centos redis-cluster]# docker service ls
ID             NAME      MODE         REPLICAS   IMAGE          PORTS
puajwi3kllcg   mynginx   replicated   2/2        nginx:latest   *:80->80/tcp

查看192.168.19.4机器

[root@centos redis-cluster]# docker ps -a
ConTAINER ID   IMAGE          COMMAND                  CREATED          STATUS                      PORTS     NAMES
fe5299cd8172   nginx:latest   "/docker-entrypoint.…"   7 minutes ago    Up 7 minutes                80/tcp    mynginx.1.4d3c535tl0jid28z5i54info9
5ae1db544afc   redis          "docker-entrypoint.s…"   29 minutes ago   Exited (0) 27 minutes ago             redis-6372

查看192.168.19.5机器

[root@centos ~]# docker ps -a
ConTAINER ID   IMAGE          COMMAND                  CREATED              STATUS              PORTS     NAMES
5d7aa8a46a8c   nginx:latest   "/docker-entrypoint.…"   about a minute ago   Up about a minute   80/tcp    mynginx.2.jqpo0xtpdo6usuavhmiptu0py

都部署了 nginx

查看服务跑在那台机器上 注意:只能在manage节点使用

[root@centos redis-cluster]# docker service ps mynginx
ID             NAME        IMAGE          NODE      DESIRED STATE   CURRENT STATE            ERROR     PORTS
4d3c535tl0ji   mynginx.1   nginx:latest   manager   Running         Running 12 minutes ago             
jqpo0xtpdo6u   mynginx.2   nginx:latest   worker    Running         Running 6 minutes ago 

服务应用滚动更新

[root@centos redis-cluster]# docker service update  --replicas 2 --update-delay 10s mynginx
mynginx
overall progress: 2 out of 2 tasks 
1/2: running   [==================================================>] 
2/2: running   [==================================================>] 
verify: Service converged

注意:

  1. manager节点加入不上;关闭机器的防火墙就可以了

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5669818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存