前言

容器化,云原生越演越烈,新概念非常之多。信息爆炸的同时,带来层层迷雾。我尝试从扩容出发理解其脉路,经过实践探索,整理形成一个入门教程,包括下面四篇文章。

这是第二篇,使用compose部署应用,同样演示都在docker2istio目录。

compose

Compose是一个用于定义和运行多容器Docker应用程序的工具,采用python编写。

部署应用及测试

编写应用部署文件

compose部署应用,采用编写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
version: '3'
services:

  redis:
    image: redis:4-alpine3.8
    restart: always
  
  flaskapp:
    depends_on:
      - redis
    build: ./app
    image: flaskapp:0.0.2
    links:
      - redis

  nginx:
    image: nginx:1.15.8-alpine
    depends_on:
      - flaskapp
    volumes:
      - ./nginx:/etc/nginx/conf.d
    restart: always
    ports:
      - "80:80"
    environment:
      - NGINX_PORT=80
    links:
      - flaskapp

这里描述了下面几件事情:

  1. 依次启动redisflaskappnginx 三个服务。服务顺序由depends_on 决定。
  2. 使用build命令,自动编译 flaskapp:0.0.2
  3. 使用links命令,描述服务间依赖。
  4. 使用ports导出端口,使用volumes挂载数据卷。

这一过程,把第一篇中启动容器的过程,语义化,流程更清晰。

启动应用

启动应用,使用docker-compose up 命令:

 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
Creating network "docker2istio_default" with the default driver
Building flaskapp
Step 1/5 : FROM python:3.6-alpine
 ---> 1d981af1e3b4
Step 2/5 : WORKDIR /code
 ---> Using cache
 ---> 7f2b07b16752
Step 3/5 : RUN pip install redis flask
 ---> Using cache
 ---> 79e39b6c2e93
Step 4/5 : ADD . /code
 ---> 4266029c0709
Step 5/5 : CMD ["python", "flaskapp.py"]
 ---> Running in 56e799a2fb61
Removing intermediate container 56e799a2fb61
 ---> 1a61773c4c07

Successfully built 1a61773c4c07
Successfully tagged flaskapp:0.0.2
WARNING: Image for service flaskapp was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating docker2istio_redis_1 ... done
Creating docker2istio_flaskapp_1 ... done
Creating docker2istio_nginx_1    ... done
Attaching to docker2istio_redis_1, docker2istio_flaskapp_1, docker2istio_nginx_1
flaskapp_1  |  * Serving Flask app "flaskapp" (lazy loading)
flaskapp_1  |  * Environment: production
flaskapp_1  |    WARNING: Do not use the development server in a production environment.
flaskapp_1  |    Use a production WSGI server instead.
flaskapp_1  |  * Debug mode: on
redis_1     | 1:C 09 Apr 12:06:15.892 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1     | 1:C 09 Apr 12:06:15.893 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1     | 1:C 09 Apr 12:06:15.893 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
flaskapp_1  |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
flaskapp_1  |  * Restarting with stat
flaskapp_1  |  * Debugger is active!
redis_1     | 1:M 09 Apr 12:06:15.894 * Running mode=standalone, port=6379.
redis_1     | 1:M 09 Apr 12:06:15.894 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
redis_1     | 1:M 09 Apr 12:06:15.894 # Server initialized
flaskapp_1  |  * Debugger PIN: 323-612-506
redis_1     | 1:M 09 Apr 12:06:15.894 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
redis_1     | 1:M 09 Apr 12:06:15.894 * Ready to accept connections

启动日志中,展示了创建网络,编译镜像,启动容器这几个过程

访问应用

使用 docker-compose ps 检查服务状况:

1
2
3
4
5
         Name                        Command               State         Ports
-------------------------------------------------------------------------------------
docker2istio_flaskapp_1   python flaskapp.py               Up
docker2istio_nginx_1      nginx -g daemon off;             Up      0.0.0.0:80->80/tcp
docker2istio_redis_1      docker-entrypoint.sh redis ...   Up      6379/tcp

当然,compose也是使用docker,也可以 docker ps

1
2
3
4
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                NAMES
c96fd468c415        nginx:1.15.8-alpine   "nginx -g 'daemon of…"   3 minutes ago       Up 3 minutes        0.0.0.0:80->80/tcp   docker2istio_nginx_1
b61d1d0ca201        flaskapp:0.0.2        "python flaskapp.py"     3 minutes ago       Up 3 minutes                             docker2istio_flaskapp_1
73a2359655d2        redis:4-alpine3.8     "docker-entrypoint.s…"   3 minutes ago       Up 3 minutes        6379/tcp             docker2istio_redis_1

对比可见 docker-compose ps ,更偏应用层。

然后访问服务:

1
2
➜  ~ curl http://127.0.0.1
Hello World by 172.19.0.3 from 172.19.0.1 ! 该页面已被访问 1 次。

应用扩容

使用 docker-compose up --scale flaskapp=2flaskapp进行扩容。

查看扩容的结果:

1
2
3
4
5
6
7
➜  docker2istio docker-compose ps
         Name                        Command               State         Ports
-------------------------------------------------------------------------------------
docker2istio_flaskapp_1   python flaskapp.py               Up
docker2istio_flaskapp_2   python flaskapp.py               Up
docker2istio_nginx_1      nginx -g daemon off;             Up      0.0.0.0:80->80/tcp
docker2istio_redis_1      docker-entrypoint.sh redis ...   Up      6379/tcp

刷新访问应用

1
2
3
4
➜  docker2istio curl http://127.0.0.1
Hello World by 172.20.0.4 from 172.20.0.1 ! 该页面已被访问 101 次。
➜  docker2istio curl http://127.0.0.1
Hello World by 172.20.0.3 from 172.20.0.1 ! 该页面已被访问 102 次。

同时观察服务日志输出:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
nginx_1     | 172.22.0.1 - - [10/Apr/2019:01:38:02 +0000] "GET / HTTP/1.0" 200 76 "-" "ApacheBench/2.3" "-"
flaskapp_1  | [2019-04-10 01:38:02,140] DEBUG in flaskapp: Hello out 172.22.0.3 172.22.0.1 65:
flaskapp_1  | 172.22.0.5 - - [10/Apr/2019 01:38:02] "GET / HTTP/1.0" 200 -
flaskapp_1  | [2019-04-10 01:38:02,141] DEBUG in flaskapp: Hello out 172.22.0.3 172.22.0.1 63:
flaskapp_2  | [2019-04-10 01:38:02,145] DEBUG in flaskapp: hello in
flaskapp_1  | 172.22.0.5 - - [10/Apr/2019 01:38:02] "GET / HTTP/1.0" 200 -
nginx_1     | 172.22.0.1 - - [10/Apr/2019:01:38:02 +0000] "GET / HTTP/1.0" 200 76 "-" "ApacheBench/2.3" "-"
flaskapp_1  | [2019-04-10 01:38:02,150] DEBUG in flaskapp: hello in
flaskapp_2  | [2019-04-10 01:38:02,151] DEBUG in flaskapp: hello in
nginx_1     | 172.22.0.1 - - [10/Apr/2019:01:38:02 +0000] "GET / HTTP/1.0" 200 76 "-" "ApacheBench/2.3" "-"
flaskapp_2  | [2019-04-10 01:38:02,153] DEBUG in flaskapp: Hello out 172.22.0.4 172.22.0.1 67:
flaskapp_2  | 172.22.0.5 - - [10/Apr/2019 01:38:02] "GET / HTTP/1.0" 200 -
nginx_1     | 172.22.0.1 - - [10/Apr/2019:01:38:02 +0000] "GET / HTTP/1.0" 200 76 "-" "ApacheBench/2.3" "-"
flaskapp_2  | [2019-04-10 01:38:02,156] DEBUG in flaskapp: Hello out 172.22.0.4 172.22.0.1 69:
flaskapp_1  | [2019-04-10 01:38:02,156] DEBUG in flaskapp: hello in
flaskapp_2  | 172.22.0.5 - - [10/Apr/2019 01:38:02] "GET / HTTP/1.0" 200 -
flaskapp_1  | [2019-04-10 01:38:02,159] DEBUG in flaskapp: hello in
flaskapp_2  | [2019-04-10 01:38:02,161] DEBUG in flaskapp: hello in
nginx_1     | 172.22.0.1 - - [10/Apr/2019:01:38:02 +0000] "GET / HTTP/1.0" 200 76 "-" "ApacheBench/2.3" "-"
flaskapp_1  | [2019-04-10 01:38:02,160] DEBUG in flaskapp: Hello out 172.22.0.3 172.22.0.1 70:

对比纯docker的方式,扩容变简单了。

需要注意的是:扩容过程中要重启nginx服务,否则虽然容器扩充成多个,但是服务流量并不会分配到新容器。不过容器启动和创建非常迅速,可以先docker-compose down再行扩容启动。

docker-compose scale Note: This command is deprecated. Use the up command with the –scale flag instead. Beware that using up with –scale flag has some subtle differences with the scale command as it incorporates the behaviour of up command.

清理应用

使用 docker-compose down 一键清理应用

1
2
3
4
5
Removing docker2istio_nginx_1    ... done
Removing docker2istio_flaskapp_2 ... done
Removing docker2istio_flaskapp_1 ... done
Removing docker2istio_redis_1    ... done
Removing network docker2istio_default

总结

compose 已经实现了容器扩容自动挡:

  1. 更直观的控制容器启动顺序及依赖。
  2. 使用便捷,扩容方便。

不过compose的自动挡,充其量只是一个摩托版,作为小型/测试应用的部署方案还是不错。如果是大型/正式应用,还有以下缺点:

  1. 扩容不能够无缝,需要重启服务。
  2. 单纯的compose不支持多机互联。

要实现多机部署扩容,就需要使用到 kubernetes的容器编排方案了。从部署到编排,单字面理解,看起来能够维护的容器量都增长了。相比 docker 家的swarm+machine方案, kubernetes 已经是容器编排领域的事实标准,更值得学习了解。

推荐

  1. harbor harbor应用包括多个服务,推荐部署方式就是compose。

  2. gogs-drone-docker 。演示了如何快速使用docker compose 搭建一个ci系统。