Docker集群swarm安装和简单使用搭建nginx

docker安装swarm

操作的ip地址:192.168.0.200

1、下载swarm

下载swarm插件

1
docker pull swarm

2、安装swarm

1
docker swarm init --advertise-addr 192.168.0.200:2377 --listen-addr 192.168.0.200:2377

输出

1
2
3
4
5
6
7
8
[root@localhost ~]# docker swarm init --advertise-addr 192.168.0.200:2377 --listen-addr 192.168.0.200:2377
Swarm initialized: current node (r3a32j3o9gr964xoksyha2i8m) is now a manager.

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

docker swarm join --token SWMTKN-1-1f48vzkywen673g6fuj6xl9m6pq8niuizw3tm1b75zjt7ak8o6-7c87bf6qurrp3vrlpq6cysd82 192.168.43.49:2377

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

3、将本机加入节点

1
docker swarm join-token manager

输出

1
2
3
[root@localhost ~]# docker swarm join-token manager
To add a manager to this swarm, run the following command:
docker swarm join --token SWMTKN-1-1f48vzkywen673g6fuj6xl9m6pq8niuizw3tm1b75zjt7ak8o6-2ofub2vc7qteetwyi92d1krek 192.168.43.49:2377

用yml文件启动nginx

1、下载nginx

1
docker pull nginx:1.19.5

2、编写启动文件nginx.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@localhost nginx]# cat nginx.yml 
version: '3'

services:
nginx:
image: nginx:1.19.5
deploy:
replicas: 3
ports:
- "18081:80"
networks:
- net
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
- net

networks:
net:

3.启动nginx

1
docker stack deploy --with-registry-auth -c nginx.yml rb

4.查看启动情况

1
docker stack services rb

输出

1
2
3
4
[root@localhost nginx]# docker stack services rb
ID NAME MODE REPLICAS IMAGE PORTS
0p33122t1wy5 rb_nginx replicated 3/3 nginx:1.19.5 *:18081->80/tcp
tyzq1h1cz0v0 rb_visualizer replicated 1/1 dockersamples/visualizer:stable *:8080->8080/tcp
继开 wechat
欢迎加我的微信,共同交流技术