1.bind+mount 方式管理数据。
bind mount 模式:将宿主机某个目录映射到docker容器,很适合于网站,
同时把宿主机的这个目录作为git版本目录,每次update 代码的时候,容器就会更新。
2.创建数据目录。
在 mananger、worker01、worker02 上创建web 网站目录:
mkdir -p /data/web
echo "outside website" >> /data/web/index.html
3.将本地的
192.168.1.7:
docker service create --replicas 3 --mount type=bind,src=/data/web,dst=/usr/share/nginx/html -p 8012:80 \
--name mynginx3 192.168.1.11:443/myharbor/nginx:latest
[root@es1 data]# docker service create --replicas 3 --mount type=bind,src=/data/web,dst=/usr/share/nginx/html -p 8012:80 \
> --name mynginx3 192.168.1.11:443/myharbor/nginx:latest
mvjwtb0rixsrilnyglq0gvf72
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
docker volume ls --会自动创建nginxdata;
docker service ps -f 'desired-state=running' mynginx3 --检查running状态的服务。
docker service rm fgeduweb8012
[root@es1 data]# docker service ps -f 'desired-state=running' mynginx3
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
tmsrh710erch mynginx3.1 192.168.1.11:443/myharbor/nginx:latest es1 Running Running 2 minutes ago
glaonlq28tr1 mynginx3.2 192.168.1.11:443/myharbor/nginx:latest es3 Running Running 2 minutes ago
x6fokwgpi5ww mynginx3.3 192.168.1.11:443/myharbor/nginx:latest es2 Running Running 2 minutes ago
这两种挂载方式在使用上,应用上几乎没有区别。
4.测试打开:
curl http://192.168.1.7:8012/index.html
[root@es1 web]# curl http://192.168.1.7:8012/index.html
I'm nginx,mynginx3
[root@es1 web]# curl http://192.168.1.7:8012/index.html
I'm nginx,mynginx2
[root@es1 web]# curl http://192.168.1.7:8012/index.html
I'm nginx,mynginx1
[root@es1 web]# curl http://192.168.1.7:8012/index.html
I'm nginx,mynginx3
[root@es1 web]# curl http://192.168.1.7:8012/index.html
I'm nginx,mynginx2
[root@es1 web]# curl http://192.168.1.7:8012/index.html
I'm nginx,mynginx1