容器化,雲原生越演越烈,新概念很是之多。信息爆炸的同時,帶來層層迷霧。我嘗試從擴容出發理解其脈路,通過實踐探索,整理造成一個入門教程,包括下面四篇文章。python
這是第二篇,使用compose部署應用,一樣演示都在docker2istio目錄。nginx
Compose是一個用於定義和運行多容器Docker應用程序的工具,採用python編寫。git
compose部署應用,採用編寫docker-compose.yml:github
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
複製代碼
這裏描述了下面幾件事情:redis
depends_on
決定。build
命令,自動編譯 flaskapp:0.0.2
。links
命令,描述服務間依賴。ports
導出端口,使用volumes
掛載數據卷。這一過程,把第一篇中啓動容器的過程,語義化,流程更清晰。docker
啓動應用,使用docker-compose up
命令:flask
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
複製代碼
啓動日誌中,展現了建立網絡,編譯鏡像,啓動容器這幾個過程bash
使用 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
:app
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 已經實現了容器擴容自動擋:
不過compose的自動擋,充其量只是一個摩托版,做爲小型/測試應用的部署方案仍是不錯。若是是大型/正式應用,還有如下缺點:
要實現多機部署擴容,就須要使用到 kubernetes
的容器編排方案了。從部署到編排,單字面理解,看起來可以維護的容器量都增加了。相比 docker 家的swarm+machine
方案, kubernetes
已是容器編排領域的事實標準,更值得學習瞭解。
harbor harbor應用包括多個服務,推薦部署方式就是compose。
gogs-drone-docker 。演示瞭如何快速使用docker compose 搭建一個ci系統。