1.数据卷/逻辑卷的管理
##删除逻辑卷
docker volume rm `docker volume ls -q`
[root@foundation52 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@foundation52 ~]# docker run -it --name vm1 -v /data1 ubuntu
root@6da242a86634:/# df
Filesystem 1K-blocks Used Available Use% Mounted on
overlay 307430744 47214244 260216500 16% /
tmpfs 1963368 0 1963368 0% /dev
tmpfs 1963368 0 1963368 0% /sys/fs/cgroup
/dev/mapper/rhel_foundation52-root 307430744 47214244 260216500 16% /data1
shm 65536 0 65536 0% /dev/shm
tmpfs 1963368 0 1963368 0% /sys/firmware
root@6da242a86634:/# cd /data1
root@6da242a86634:/data1# ls
root@6da242a86634:/data1# cd /data1[root@foundation52 ~]#
[root@foundation52 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/rhel_foundation52-root 307430744 47198700 260232044 16% /
devtmpfs 1949996 0 1949996 0% /dev
tmpfs 1963368 476 1962892 1% /dev/shm
tmpfs 1963368 9072 1954296 1% /run
tmpfs 1963368 0 1963368 0% /sys/fs/cgroup
/dev/sda1 1038336 143456 894880 14% /boot
/dev/loop0 3704296 3704296 0 100% /source7.3
/dev/loop1 3704296 3704296 0 100% /var/www/html/source7.3
/dev/loop2 3762278 3762278 0 100% /source6.5
/dev/loop3 3762278 3762278 0 100% /var/www/html/source6.5
tmpfs 392676 20 392656 1% /run/user/1000
tmpfs 392676 0 392676 0% /run/user/0
overlay 307430744 47198700 260232044 16% /var/lib/docker/overlay/9f45a5ca7d7d90f0389332fa7098427ebe38679c90f1b022c142bb1fc7dafbb7/merged
shm 65536 0 65536 0% /var/lib/docker/containers/6da242a86634366e14e1eb95121c2672fc7dd0d4cecd725b57224134e29ce049/shm
[root@foundation52 ~]# docker volume ls
DRIVER VOLUME NAME
local 1abbc4241d114c48ed7eed995e492a30dac0398d546abf69323251abda556520
##/var/lib/docker数据卷的默认存储位置
[root@foundation52 ~]# docker inspect vm1 | grep vol
"Type": "volume",
"Source": "/var/lib/docker/volumes/1abbc4241d114c48ed7eed995e492a30dac0398d546abf69323251abda556520/_data",
[root@foundation52 ~]# cd /var/lib/docker/volumes/1abbc4241d114c48ed7eed995e492a30dac0398d546abf69323251abda556520/_data
[root@foundation52 _data]# ls
[root@foundation52 _data]# cp /etc/passwd .
[root@foundation52 _data]# ls
passwd
##再打开一个shell
##
[root@foundation52 kiosk]# docker container attach vm1
root@6da242a86634:/data1# cd /data1
root@6da242a86634:/data1# ls
passwd
[root@foundation52 kiosk]# docker run -d --name vm2 -v /usr/share/nginx/html nginx
26dd3467aeecb22d35eb44b51789a75cb2b927a4569ba25b90a6ae2fde0c8249
[root@foundation52 kiosk]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
26dd3467aeec nginx "nginx -g 'daemon ..." 11 seconds ago Up 9 seconds 80/tcp vm2
6da242a86634 ubuntu "/bin/bash" 19 minutes ago Up 18 minutes vm1
[root@foundation52 kiosk]# docker inspect vm2 | grep vol
"Type": "volume",
"Source": "/var/lib/docker/volumes/f25c8cb89bd0e8072016ff6a13398d60f162cacb5f4aa96dcaeac56782dbb860/_data",
[root@foundation52 kiosk]# cd /var/lib/docker/volumes/f25c8cb89bd0e8072016ff6a13398d60f162cacb5f4aa96dcaeac56782dbb860/_data
[root@foundation52 _data]# ls
50x.html index.html
##在另外一个shell中删除容器
[root@foundation52 kiosk]# docker stop vm2
vm2
[root@foundation52 kiosk]# docker rm vm2
vm2
##发现存储在容器中的数据仍然存在
[root@foundation52 _data]# ls
50x.html index.html
##查看id
[root@foundation52 kiosk]# docker inspect vm2 | grep vol
"Type": "volume",
"Source": "/var/lib/docker/volumes/f25c8cb89bd0e8072016ff6a13398d60f162cacb5f4aa96dcaeac56782dbb860/_data",
删除数据卷:
方法1:
##直接删除数据卷
[root@foundation52 kiosk]# docker volume rm f25c8cb89bd0e8072016ff6a13398d60f162cacb5f4aa96dcaeac56782dbb860
f25c8cb89bd0e8072016ff6a13398d60f162cacb5f4aa96dcaeac56782dbb860
##数据被删除
[root@foundation52 _data]# ls
##方法2:
[root@foundation52 kiosk]# docker stop vm1
vm1
[root@foundation52 kiosk]# docker rm -v vm1
vm1
##查看
[root@foundation52 ~]# docker inspect vm1 | grep vol
"Type": "volume",
"Source": "/var/lib/docker/volumes/1abbc4241d114c48ed7eed995e492a30dac0398d546abf69323251abda556520/_data",
[root@foundation52 ~]# cd /var/lib/docker/volumes/1abbc4241d114c48ed7eed995e492a30dac0398d546abf69323251abda556520/_data
bash: cd: /var/lib/docker/volumes/1abbc4241d114c48ed7eed995e492a30dac0398d546abf69323251abda556520/_data: No such file or directory
2.数据封装
将数据卷直接封装到镜像中
[root@foundation52 kiosk]# docker volume ls
DRIVER VOLUME NAME
[root@foundation52 kiosk]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@foundation52 kiosk]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@foundation52 ~]# cd /tmp/docker
[root@foundation52 docker]# cp web/index.html test/
[root@foundation52 docker]# cd test/
[root@foundation52 test]# ls
Dockerfile index.html
[root@foundation52 test]# pwd
/tmp/docker/test
[root@foundation52 test]# mkdir nginx/html -p
[root@foundation52 test]# mv index.html nginx/html/
[root@foundation52 test]# cat nginx/html/index.html
<h1>westos.org</h1>
[root@foundation52 test]# tar cf html.tar nginx/
[root@foundation52 test]# ls
Dockerfile html.tar nginx
[root@foundation52 test]# tar tf html.tar
nginx/
nginx/html/
nginx/html/index.html
[root@foundation52 test]# vim Dockerfile
#########################
FROM rhel7
ADD html.tar /usr/share
VOLUME ["/usr/share/nginx/html"]
[root@foundation52 test]# docker build -t rhel7:v1 .
Sending build context to Docker daemon 14.85 kB
Step 1/3 : FROM rhel7
---> 0a3eb3fde7fd
Step 2/3 : ADD html.tar /usr/share
---> 368dffa1785b
Removing intermediate container 7645fea35814
Step 3/3 : VOLUME /usr/share/nginx/html
---> Running in a3837636e717
---> 2371e0c45aeb
Removing intermediate container a3837636e717
Successfully built 2371e0c45aeb
[root@foundation52 test]# docker create --name vol rhel7:v1 bash
24954302ec50fc4286fe147d6f8c857885c45d4991349b050c9b46d01d36d405
[root@foundation52 test]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@foundation52 test]# docker run -d --name vm1 --volumes-from vol nginx
4e983b5ef1036e030196e8fb722084713a55a138a2da556c4511c6f3ac2e4540
[root@foundation52 test]# docker inspect vm1
"Gateway": "172.17.0.1",
"IPAddress": "172.17.0.2",
"IPPrefixLen": 16,
"IPv6Gateway": "",
"GlobalIPv6Address": "",
"GlobalIPv6PrefixLen": 0,
"MacAddress": "02:42:ac:11:00:02"
[root@foundation52 test]# curl 172.17.0.2
<h1>westos.org</h1>
3.批量管理多个容器
实现负载均衡
##下载haproxy镜像
lftp 172.25.254.251:/pub/docs/docker> get haproxy.tar
145223680 bytes transferred in 36 seconds (3.89M/s)
##导入镜像
[root@foundation52 test]# docker load -i haproxy.tar
917c0fc99b35: Loading layer 130.9 MB/130.9 MB
5f70bf18a086: Loading layer 1.024 kB/1.024 kB
c205bb11f213: Loading layer 4.684 MB/4.684 MB
ffef890bdf7b: Loading layer 9.549 MB/9.549 MB
3ec368642ee3: Loading layer 2.048 kB/2.048 kB
Loaded image: haproxy:latest
##查看镜像
[root@foundation52 test]# dcoker images
haproxy latest fbd1f55f79b3 2 years ago 139 MB
##下载docker-compose-Linux-x86_64-1.22.0
[root@foundation52 ~]# lftp 172.25.254.251
lftp 172.25.254.251:~> cd pub/docs/docker
cd ok, cwd=/pub/docs/docker
lftp 172.25.254.251:/pub/docs/docker> get docker-compose-Linux-x86_64-1.22.0
11750136 bytes transferred in 1 second (10.32M/s)
lftp 172.25.254.251:/pub/docs/docker> quit
[root@foundation52 ~]# cp docker-compose-Linux-x86_64-1.22.0 /usr/local/bin/
cp: overwrite ‘/usr/local/bin/docker-compose-Linux-x86_64-1.22.0’? y
[root@foundation52 ~]# cd /usr/local/bin
[root@foundation52 bin]# ls
docker-compose-Linux-x86_64-1.22.0 rht-vmctl rht-vmicons rht-vmsetkeyboard
[root@foundation52 bin]# chmod +x docker-compose-Linux-x86_64-1.22.0
##制作软连接
[root@foundation52 bin]# ln -s docker-compose-Linux-x86_64-1.22.0 docker-compose
[root@foundation52 bin]# ll
total 11504
lrwxrwxrwx 1 root root 34 Aug 22 12:12 docker-compose -> docker-compose-Linux-x86_64-1.22.0
-rwxr-xr-x 1 root root 11750136 Aug 22 12:10 docker-compose-Linux-x86_64-1.22.0
-rwxr-xr-x. 1 root root 12782 Aug 23 2014 rht-vmctl
-rwxr-xr-x. 1 root root 1876 Aug 20 2014 rht-vmicons
-rwxr-xr-x. 1 root root 4998 Jul 11 2014 rht-vmsetkeyboard
##编写文件
[root@foundation52 ~]# cd /tmp/docker/
[root@foundation52 docker]# ls
Dockerfile dvd.repo ssh supervisord.conf test web
[root@foundation52 docker]# mkdir compose
[root@foundation52 docker]# cd compose/
##注意:v3为之前部署过apahe的镜像
[root@foundation52 compose]# vim docker-compose.yml
############################
apache:
image: rhel7:v3
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@foundation52 compose]# cd ..
[root@foundation52 docker]# ls
compose Dockerfile dvd.repo ssh supervisord.conf test web
[root@foundation52 docker]# cp -r web/ compose/
[root@foundation52 docker]# ls
compose Dockerfile dvd.repo ssh supervisord.conf test web
[root@foundation52 docker]# cd compose/
[root@foundation52 compose]# ls web/
index.html
[root@foundation52 compose]# mkdir haproxy
[root@foundation52 compose]# cd haproxy
[root@foundation52 compose]# pwd
/tmp/docker/compose/haproxy
[root@foundation52 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 web1 apache:80 check
server web2 nginx:80 check
##注意不要退出来
[root@foundation52 compose]# docker-compose up
compose_nginx_1 is up-to-date
Creating compose_apache_1 ... done
Creating compose_haproxy_1 ... done
Attaching to compose_nginx_1, compose_apache_1, compose_haproxy_1
....................
测试:
实现负载均衡
监控信息
##crtl+c退出
^CGracefully stopping... (press Ctrl+C again to force)
Stopping compose_haproxy_1 ... done
Stopping compose_apache_1 ...
Stopping compose_nginx_1 ... done
^C[root@foundation52 compose]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@foundation52 compose]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
daf7d47b7129 haproxy "/docker-entrypoin..." 8 minutes ago Exited (0) 19 seconds ago compose_haproxy_1
078b604b8bb0 rhel7:v3 "/usr/bin/supervisord" 8 minutes ago Exited (0) 18 seconds ago compose_apache_1
7f4d3635ea8e d8b4f01e35b7 "/bin/sh -c 'rpmdb..." 10 minutes ago Exited (1) 9 minutes ago quirky_heisenberg
8ab20dc69087 d8b4f01e35b7 "/bin/sh -c 'rpmdb..." 10 minutes ago Exited (1) 10 minutes ago trusting_agnesi
79456af0e263 nginx "nginx -g 'daemon ..." 59 minutes ago Exited (0) 18 seconds ago compose_nginx_1
4e983b5ef103 nginx "nginx -g 'daemon ..." 2 hours ago Up 2 hour
[root@foundation52 compose]# docker-compose start
Starting apache ... done
Starting nginx ... done
Starting haproxy ... done
[root@foundation52 compose]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
daf7d47b7129 haproxy "/docker-entrypoin..." 9 minutes ago Up 45 seconds 0.0.0.0:8080->80/tcp compose_haproxy_1
078b604b8bb0 rhel7:v3 "/usr/bin/supervisord" 9 minutes ago Up 46 seconds 22/tcp, 80/tcp compose_apache_1
79456af0e263 nginx "nginx -g 'daemon ..." About an hour ago Up 46 seconds 80/tcp compose_nginx_1
##关闭apache
[root@foundation52 compose]# docker stop compose_apache_1
compose_apache_1
监控显示web1的状态为DOWN
此时负载均衡被破坏