当创建一个容器使用了-v时,同时也就创建了一个卷
[root@foundation25 ~]# docker run -it --name vm1 -v /data ubuntu
# 查看vm1的卷
[root@foundation25 ~]# docker inspect vm1 | grep vol
"Type": "volume",
"Source": "/var/lib/docker/volumes/40f8a91941a6c474405153b034704a024a02ad7db0a58b27a066d202b6112f12/_data",
# 可读也可写
[root@foundation25 ~]# cd /var/lib/docker/volumes/40f8a91941a6c474405153b034704a024a02ad7db0a58b27a066d202b6112f12/_data
[root@foundation25 _data]# ls
[root@foundation25 _data]# cp /etc/passwd .
[root@foundation25 _data]# ls
passwd
[root@foundation25 _data]# docker run -dd --name vm2 -v /usr/share/nginx/html nginx
[root@foundation25 _data]# docker inspect vm2 | grep vol "Type": "volume",
"Source": "/var/lib/docker/volumes/bba6a4012d7b674020d74ff37f4fc64044263415c2d4b207b5bfeb7961425e11/_data",
[root@foundation25 _data]# cd /var/lib/docker/volumes/bba6a4012d7b674020d74ff37f4fc64044263415c2d4b207b5bfeb7961425e11/_data
# 数据卷的内容
[root@foundation25 _data]# ls
50x.html index.html
[root@foundation25 kiosk]# docker rm -f vm2
# 依然在这个目录下可以查看到
[root@foundation25 _data]# ls
50x.html index.html
[root@foundation25 _data]# cd
[root@foundation25 ~]# docker volume rm bba6a4012d7b674020d74ff37f4fc64044263415c2d4b207b5bfeb7961425e11
[root@foundation25 ~]# cd /var/lib/docker/volumes/bba6a4012d7b674020d74ff37f4fc64044263415c2d4b207b5bfeb7961425e11/_data
bash: cd: /var/lib/docker/volumes/bba6a4012d7b674020d74ff37f4fc64044263415c2d4b207b5bfeb7961425e11/_data: No such file or directory
[root@foundation25 test]# vim index.html
lin
[root@foundation25 test]# mkdir nginx/html -p
[root@foundation25 test]# mv index.html nginx/html/
[root@foundation25 test]# tar cf html.tar nginx/
[root@foundation25 test]# ls
[root@foundation25 test]# vim Dockerfile
FROM rhel7
ADD html.tar /usr/share
VOLUME ["/usr/share/nginx/html"]
[root@foundation25 test]# docker build -t rhel7:v4 .
/tmp/docker/test/Dockerfile文件:
[root@foundation25 test]# docker create --name vol rhel7:v4 bash
[root@foundation25 test]# docker run -d --name vm1 --volumes-from vol nginx
[root@foundation25 test]# docker inspect vm1
[root@foundation25 ~]# docker tag nginx server/nginx
[root@foundation25 ~]# docker push server/nginx
例:haproxy负载均衡
[root@foundation25 docker]# mkdir compose
[root@foundation25 docker]# cd compose/
[root@foundation25 compose]# vim docker-compose.yml
apache:
image: rhel7:v1
expose:
- 80
volumes:
- ./web:/var/www/html
nginx:
image: nginx
expose:
- 80
haproxy:
image: haproxy
volumes:
- ./haproxy:/usr/local/etc/haproxy
links:
- apache
- nginx
ports:
- "8080:80"
expose:
- 80
[root@foundation25 compose]# mkdir haproxy
[root@foundation25 compose]# cd haproxy/
[root@foundation25 haproxy]# vim haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
stats uri /status
frontend balancer
bind 0.0.0.0:80
default_backend web_backends
backend web_backends
balance roundrobin
server weba apache:80 check
server webb nginx:80 check
[root@foundation25 haproxy]# cd /usr/local/bin/
[root@foundation25 bin]# chmod +x docker-compose-Linux-x86_64-1.22.0
[root@foundation25 bin]# ln -s docker-compose-Linux-x86_64-1.22.0 docker-compose
[root@foundation25 compose]# cp -r ../web/ .
[root@foundation25 compose]# docker-compose up
manger:server1—>172.25.25.11
worker:server2—->172.25.25.12
worker:server3—->172.25.25.13
建立集群
server1:
[root@server1 ~]# docker swarm init
[root@server2 ~]# docker swarm join \
> --token SWMTKN-1-3ntv1ke9tv9y0m2sn0w30mdruscy5au37cwjq6olwlg6n6exfu-e4lmixvqksfey2g3ygptyld4c \
> 172.25.25.11:2377
[root@server3 ~]# docker swarm join \
> --token SWMTKN-1-3ntv1ke9tv9y0m2sn0w30mdruscy5au37cwjq6olwlg6n6exfu-e4lmixvqksfey2g3ygptyld4c \
> 172.25.25.11:2377
[root@server1 ~]# docker node ls
# 需要以下镜像:
[root@server1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest c82521676580 4 weeks ago 109 MB
dockersamples/visualizer latest 17e55a9b2354 11 months ago 148 MB
# 用nginx镜像创建服务
[root@server1 docker]# docker service create --name nginx --publish 80:80 --replicas 3 nginx
[root@server1 docker]# docker service ls
# 扩容
[root@server1 docker]# docker service scale nginx=6
# 创建监控服务
[root@server1 ~]# docker service create --name=viz --publish=8080:8080/tcp --constraint=node.role==manager --mount=type=bind,src=/var/run/docker.sock,dst=/var/run/docker.sock dockersamples/visualizer
[root@server1 ~]# docker service ls
扩容:
浏览器访问172.25.25.11:8080(访问server,server2或server3都可以)
# 缩容
[root@server1 ~]# docker service scale nginx=3
[root@server1 ~]# docker service ps nginx
[root@server1 ~]# vim index.html
[root@server1 ~]# docker container cp index.html nginx.1.v4zw0uaokwk9s992nqbhxe6ar:/usr/share/nginx/html/ ## 导入容器(nginx后面的字符可以tab建补齐)
[root@server1 ~]# cat index.html
server1
[root@server2 ~]# vim index.html
[root@server2 ~]# docker container cp index.html nginx.2.eoeg3c1wgbg6vxsk3aqyqw0k3:/usr/share/nginx/html/
[root@server2 ~]# cat index.html
server2
[root@server3 ~]# vim index.html
[root@server3 ~]# docker container cp index.html nginx.3.ti25smtf55djr97llgee1911l:/usr/share/nginx/html/
[root@server3 ~]# cat index.html
server3
[root@foundation25 ~]# for i in {1..10}; do curl 172.25.25.11; done
[root@server3 ~]# systemctl stop docker.service
更新服务
打开server3的docker
[root@server3 ~]# systemctl stop docker.service
server1:
# 扩容为20个
[root@server1 ~]# docker service scale nginx=20
# 使用game2018镜像更新,一次更新三个,十妙更新一次
[root@server1 ~]# docker service update --image game2048 --update-parallelism 3 --update-delay 10s nginx
server1上查看服务
监控:正在更新中。。。一部分镜像已经更新为game2018(图上只能看到一个,下面一部分没有截上图)