docker 容器入门(五)

写在前面的话

每篇一句

学如逆水行舟,不进则退。心似平原跑马,易放难收。

持久化docker数据

上篇通过docker-compose.yml + swarms 构建了testapp web服务,今天我们来持久化数据,通过redis记录web 已访问过的数量。
由于笔记本电脑换地方连wifi了,虚拟机ip变了(dhcp获取的)
centos7 :192.168.2.104
ubuntu :192.168.2.102

前置环境准备,清理

开启2台虚拟机后,发现swarm集群ubuntu 节点没有连接上,是因为ip变了。testapp还是保持5个节点(容器)都运行在centos7上。

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
gtb0ip6zpu4hazqg4bgp5z19m ubuntu Down Active
qmjxu1mlwksrxfwyq1sgcerrw * localhost.localdomain Ready Active Leader
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9163c5a14c46 zakizqzhang/get-started:20171028 "python app.py" 11 minutes ago Up 11 minutes 80/tcp testapp_web.1.mn37pcrl4qdapvocnzdkbf5fb
f2d05226f3c4 zakizqzhang/get-started:20171028 "python app.py" 11 minutes ago Up 11 minutes 80/tcp testapp_web.4.z0djkare7lccee71repnd895n
c0c549d6c746 zakizqzhang/get-started:20171028 "python app.py" 11 minutes ago Up 11 minutes 80/tcp testapp_web.2.0rfc20le7umx8kxycb8tk0xwm
36b81da1dd06 zakizqzhang/get-started:20171028 "python app.py" 11 minutes ago Up 11 minutes 80/tcp testapp_web.3.pmgnd8dot1o2vi5minpd3awqh
f96819397bbc zakizqzhang/get-started:20171028 "python app.py" 11 minutes ago Up 11 minutes 80/tcp testapp_web.5.uwgl3onvcav65rjgv7q3rzkdm

清理步骤

删除testapp应用
centos7

1
2
3
4
5
[root@localhost ~]# docker stack rm testapp
Removing service testapp_web
Removing network testapp_webnet
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

删除swarm节点后清理swarm
centos7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
gtb0ip6zpu4hazqg4bgp5z19m ubuntu Down Active
qmjxu1mlwksrxfwyq1sgcerrw * localhost.localdomain Ready Active Leader
[root@localhost ~]# docker node rm gtb0ip6zpu4hazqg4bgp5z19m
gtb0ip6zpu4hazqg4bgp5z19m
[root@localhost ~]# docker node rm qmjxu1mlwksrxfwyq1sgcerrw
Error response from daemon: rpc error: code = 9 desc = node qmjxu1mlwksrxfwyq1sgcerrw is a cluster manager and is a member of the raft cluster. It must be demoted to worker before removal
[root@localhost ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
qmjxu1mlwksrxfwyq1sgcerrw * localhost.localdomain Ready Active Leader
[root@localhost ~]# docker swarm leave --force
Node left the swarm.
[root@localhost ~]# docker node ls
Error response from daemon: This node is not a swarm manager. Use "docker swarm init" or "docker swarm join" to connect this node to swarm and try again.

ubuntu

1
2
root@ubuntu:~# docker swarm leave
Node left the swarm.

重新初始化swarm集群

centos 7

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# docker swarm init
Swarm initialized: current node (jkkb3lg7o5x1oel28wtyeu1b3) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3msmax4yocfing9j7cb7ls2ns6d24femvlr6x1lhekc2ee9wn3-a63thuor7e94589s4rh4vt667 192.168.2.104:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[root@localhost ~]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
bl0domocxtrmcvch2mscnulhy ubuntu Ready Active
jkkb3lg7o5x1oel28wtyeu1b3 * localhost.localdomain Ready Active Leader

ubuntu

1
2
root@ubuntu:~# docker swarm join --token SWMTKN-1-3msmax4yocfing9j7cb7ls2ns6d24femvlr6x1lhekc2ee9wn3-a63thuor7e94589s4rh4vt667 192.168.2.104:2377
This node joined a swarm as a worker.

增加visualizer服务

更新docker-compose.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
27
28
29
30
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: zakizqzhang/get-started:20171028
deploy:
replicas: 5
restart_policy:
condition: on-failure
resources:
limits:
cpus: "0.1"
memory: 50M
ports:
- "80:80"
networks:
- webnet
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
- webnet
networks:
webnet:

部署运行

1
2
3
4
5
6
7
8
9
[root@localhost docker]# docker stack deploy -c docker-compose.yml testappv2
Creating network testappv2_webnet
Creating service testappv2_visualizer
Creating service testappv2_web
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
80fd4a609479 dockersamples/visualizer:stable "npm start" About a minute ago Up About a minute 8080/tcp testappv2_visualizer.1.3is8k7vyiapxpezpzyc46y66q
a75752bb6504 zakizqzhang/get-started:20171028 "python app.py" About a minute ago Up About a minute 80/tcp testappv2_web.1.xxykn8aa5n8l2kzdijzlgdw96
2fb39081b542 zakizqzhang/get-started:20171028 "python app.py" About a minute ago Up About a minute 80/tcp testappv2_web.3.lodsgr7aic4aj0dr8i6ofyjkf


可以明确看到cento7上运行了2个web,1个visualizer容器;ubunut上运行了3个web容器

1
2
3
4
5
6
7
8
[root@localhost docker]# docker stack ps testappv2
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
xxykn8aa5n8l testappv2_web.1 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 6 minutes ago
3is8k7vyiapx testappv2_visualizer.1 dockersamples/visualizer:stable localhost.localdomain Running Running 6 minutes ago
uk75cr884jr4 testappv2_web.2 zakizqzhang/get-started:20171028 ubuntu Running Running 6 minutes ago
lodsgr7aic4a testappv2_web.3 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 6 minutes ago
26nxthxl9f77 testappv2_web.4 zakizqzhang/get-started:20171028 ubuntu Running Running 6 minutes ago
j1t2qiu5g046 testappv2_web.5 zakizqzhang/get-started:20171028 ubuntu Running Running 6 minutes ago

数据持久化,增加redis服务记录访客数量

更新docker-compose.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
[root@localhost docker]# cat docker-compose.yml
version: "3"
services:
web:
# replace username/repo:tag with your name and image details
image: zakizqzhang/get-started:20171028
deploy:
replicas: 5
restart_policy:
condition: on-failure
resources:
limits:
cpus: "0.1"
memory: 50M
ports:
- "80:80"
networks:
- webnet
visualizer:
image: dockersamples/visualizer:stable
ports:
- "8080:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
- webnet
redis:
image: redis
ports:
- "6379:6379"
volumes:
- /home/docker/data:/data
deploy:
placement:
constraints: [node.role == manager]
command: redis-server --appendonly yes
networks:
- webnet
networks:
webnet:

建目录用于存放redis数据,再部署应用

1
2
3
4
5
6
[root@localhost docker]# mkdir -p /home/docker/data
[root@localhost docker]# docker stack deploy -c docker-compose.yml testappv3
Creating network testappv3_webnet
Creating service testappv3_redis
Creating service testappv3_web
Creating service testappv3_visualizer


登录redis容器看看和本地是否一致

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost data]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
386bbd476d31 dockersamples/visualizer:stable "npm start" 2 minutes ago Up 2 minutes 8080/tcp testappv3_visualizer.1.kfm592mrtksanhyr4nl2jozam
3b8ef34422a9 zakizqzhang/get-started:20171028 "python app.py" 2 minutes ago Up 2 minutes 80/tcp testappv3_web.5.v3wdlxmyrb83ula8mzwhi38tz
1ec62591ee51 zakizqzhang/get-started:20171028 "python app.py" 2 minutes ago Up 2 minutes 80/tcp testappv3_web.2.kycjsxycbq7rxv14sn4zkkvru
4fab48a2c61d redis:latest "docker-entrypoint..." 3 minutes ago Up 3 minutes 6379/tcp testappv3_redis.1.mdx6ce4zef7a2vt9zey3o95te
[root@localhost ~]# docker exec -it 4fab48a2c61d bash
root@4fab48a2c61d:/data# ls -l
total 4
-rw-r--r-- 1 redis redis 527 Nov 6 14:08 appendonly.aof
root@4fab48a2c61d:/data# du -sh appendonly.aof
4.0K appendonly.aof
root@4fab48a2c61d:/data# redis-cli
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> config get dir
1) "dir"
2) "/data"
127.0.0.1:6379> keys *
1) "counter"
127.0.0.1:6379> get counter
"14"
127.0.0.1:6379>

本地查看验证redis容器/data的数据存放在host /home/docker/data/ 目录。

1
2
3
4
5
6
7
8
[root@localhost ~]# cd /home/docker/data/
[root@localhost data]# pwd
/home/docker/data
[root@localhost data]# ls -l
总用量 4
-rw-r--r-- 1 systemd-bus-proxy ssh_keys 527 11月 6 22:08 appendonly.aof
[root@localhost data]# du -sh appendonly.aof
4.0K appendonly.aof

----纸上得来终觉浅绝知此事要躬行----
最好的赞赏是您的阅读!
0%