容器化,云原生越演越烈,新概念非常之多。信息爆炸的同时,带来层层迷雾。我尝试从扩容出发理解其脉路,经过实践探索,整理形成一个入门教程,包括下面四篇文章。
这是第二篇,使用compose部署应用,同样演示都在docker2istio目录。
Compose是一个用于定义和运行多容器Docker应用程序的工具,采用python编写。
compose部署应用,采用编写docker-compose.yml:
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 . 依次启动redis 、 flaskapp 、 nginx 三个服务。服务顺序由 depends_on
决定。
2 . 使用 build
命令,自动编译 flaskapp:0.0.2
。
3 . 使用 links
命令,描述服务间依赖。
4 . 使用 ports
导出端口,使用 volumes
挂载数据卷。
这一过程,把第一篇中启动容器的过程,语义化,流程更清晰。
启动应用,使用 docker-compose up
命令:
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 Transpare
启动日志中,展示了创建网络,编译镜像,启动容器这几个过程
使用 docker-compose ps
检查服务状况:
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` :
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
,更偏应用层。
然后访问服务:
➜ ~ curl http://127.0.0.1
Hello World by 172.19.0.3 from 172.19.0.1 ! 该页面已被访问 1 次。
使用 docker-compose up--scale flaskapp=2
对 flaskapp进行扩容。
查看扩容的结果:
➜ 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
刷新访问应用
➜ 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 次。
同时观察服务日志输出:
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
一键清理应用
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
已经是容器编排领域的事实标准,更值得学习了解。
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8