docker 容器入门(四)

写在前面的话

每篇一句

桃李不言,下自成蹊。

利用docker-compose.yml 构建整套可伸缩的服务

docker-compose.yml是一种YAML文件,定义了容器的一系列完整的行为。

docker-compose.yml

docker-compose.yml定义了如下步骤:

  • 从远程仓库拉取镜像
  • 运行3个容器实例作为web服务,并限制每个容器运行10%CPU,50MB内存
  • 将web容器的80端口映射到宿主机的8080端口
  • web容器通过webnet进行负载均衡
  • webnet使用默认的负载均衡overlay设置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    version: "3"
    services:
    web:
    # replace username/repo:tag with your name and image details
    #image: username/repo:tag
    image: zakizqzhang/get-started:20171028
    deploy:
    replicas: 3
    resources:
    limits:
    cpus: "0.1"
    memory: 50M
    restart_policy:
    condition: on-failure
    ports:
    - "8080:80"
    networks:
    - webnet
    networks:
    webnet:

运行web负载均衡服务

swarm初始化

1
2
3
4
5
6
7
8
[root@localhost docker]# docker swarm init
Swarm initialized: current node (qmjxu1mlwksrxfwyq1sgcerrw) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-5rww1e0vi13ye8blfyt6oq6nzd3cbfxlc22f3xt1ga93g6apjr-0pgu4dk6fkq3nw7kuu5urzp4k 10.254.36.124:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

假设有第2台宿主机要加入只需运行,刚初始化的第一台默认为manager

1
docker swarm join --token SWMTKN-1-5rww1e0vi13ye8blfyt6oq6nzd3cbfxlc22f3xt1ga93g6apjr-0pgu4dk6fkq3nw7kuu5urzp4k 10.254.36.124:2377

web服务运行

1
2
3
4
5
6
7
8
[root@localhost docker]# docker stack deploy -c docker-compose.yml testapp
Creating network testapp_webnet
Creating service testapp_web
[root@localhost docker]# netstat -tlunp |grep 8080
tcp6 0 0 :::8080 :::* LISTEN 4013/dockerd
[root@localhost docker]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
0qbok4qrbv13 testapp_web replicated 3/3

验证运行了3个容器

1
2
3
4
5
[root@localhost docker]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a16ea7161064 zakizqzhang/get-started:20171028 "python app.py" 33 seconds ago Up 32 seconds 80/tcp testapp_web.1.caohbftrljwcd5r2oh1c4l4rj
52d904f5aedd zakizqzhang/get-started:20171028 "python app.py" 34 seconds ago Up 33 seconds 80/tcp testapp_web.2.viroif27tho9kyl4u7joko9tw
62f6ea377033 zakizqzhang/get-started:20171028 "python app.py" 37 seconds ago Up 36 seconds 80/tcp testapp_web.3.7au0dpwv8tdh1t9of1fcstqt6

浏览器访问,默认轮询方式负载到后端容器

查看testapp运行的容器

1
2
3
4
5
[root@localhost docker]# docker service ps testapp
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
caohbftrljwc testapp_web.1 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 11 minutes ago
viroif27tho9 testapp_web.2 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 11 minutes ago
7au0dpwv8tdh testapp_web.3 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 11 minutes ago

扩容成5个容器,运行testapp web服务

将replicas 后面3改成5,重新deploy,docker stack rm 关闭testapp应用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost docker]# docker container ls -q
fce3c2f3d922
9d73cdd1b15a
9b5234fd0ac2
[root@localhost docker]# docker stack ls
NAME SERVICES
testapp 1
[root@localhost docker]# docker stack rm testapp
Removing service testapp_web
Removing network testapp_webnet
[root@localhost docker]# vi docker-compose.yml
[root@localhost docker]# docker stack deploy -c docker-compose.yml testapp
Creating network testapp_webnet
Creating service testapp_web
[root@localhost docker]# docker service ps testapp
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
i423v1euglfh testapp_web.1 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 29 seconds ago
r4nsghgl6sv3 testapp_web.2 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 29 seconds ago
rlrtevdw49df testapp_web.3 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 29 seconds ago
jhcaukbhyrv3 testapp_web.4 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 29 seconds ago
lc3jhwhrugn3 testapp_web.5 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 29 seconds ago

swarms集群

上面虽然实现了负载均衡,但是5个容器都运行在一台host上,下面我们通过swarms集群将testapp应用运行在2台host上。
刚刚第一台主机ip为:10.254.36.124
第二天主机ip 为10.254.36.42

10.254.36.42 加入swarms集群

1
2
root@ubuntu:~# docker swarm join --token SWMTKN-1-5rww1e0vi13ye8blfyt6oq6nzd3cbfxlc22f3xt1ga93g6apjr-0pgu4dk6fkq3nw7kuu5urzp4k 10.254.36.124:2377
This node joined a swarm as a worker.

查看目前加入的节点情况

在第一台manager上运行,后加入的worker节点查看不了。

1
2
3
4
[root@localhost docker]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
gtb0ip6zpu4hazqg4bgp5z19m ubuntu Ready Active
qmjxu1mlwksrxfwyq1sgcerrw * localhost.localdomain Ready Active Leader

重新运行,注意要在第二台机器先拉取镜像,不然运行失败。

10.254.36.42

1
2
3
4
5
6
7
8
9
10
11
root@ubuntu:~# docker pull zakizqzhang/get-started:20171028
20171028: Pulling from zakizqzhang/get-started
85b1f47fba49: Pull complete
a5e401c4a903: Pull complete
9f744658dd62: Pull complete
562843cc69f8: Pull complete
361ab7a9bb73: Pull complete
72c3035db3bc: Pull complete
68e97c9c12b4: Pull complete
Digest: sha256:208754af6f21fc4a14eacc22b2734f490cb3c3a495c7595a74a79529f68429ee
Status: Downloaded newer image for zakizqzhang/get-started:20171028

10.254.36.124

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost docker]# docker stack rm testapp
Removing service testapp_web
Removing network testapp_webnet
[root@localhost docker]# docker stack deploy -c docker-compose.yml testapp
Creating network testapp_webnet
Creating service testapp_web
[root@localhost docker]# docker stack ps testapp
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
2pybph62tyfd testapp_web.1 zakizqzhang/get-started:20171028 ubuntu Running Running 5 seconds ago
36gqfjvz23zw testapp_web.2 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 29 seconds ago
r35bc1sihxre testapp_web.3 zakizqzhang/get-started:20171028 ubuntu Running Running 2 seconds ago
nt00z4ykjhrm testapp_web.4 zakizqzhang/get-started:20171028 localhost.localdomain Running Running 28 seconds ago
09d742ecerbp testapp_web.5 zakizqzhang/get-started:20171028 ubuntu Running Running 3 seconds ago

到现在testapp应用的web服务,高可用就妥妥的了,随便一台host宕机,不会影响对外服务。

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