您好,登錄后才能下訂單哦!
這篇文章給大家介紹如何進行Docker Compose中scale,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
環境準備,flask-redis項目,將項目啟動:
docker-compose up -d Starting flask-redis_web_1 ... done Starting flask-redis_redis_1 ... done
查看狀態:
docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------- flask-redis_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp flask-redis_web_1 python app.py Up 0.0.0.0:8080->5000/tcp
我們目前的docker-compose 中的service容器中各自只有一個,通過scale可以去擴展service。
docker-compose up --help --scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.
例如將web服務的數量變為三個:
docker-compose up --scale web=3 -d WARNING: The "web" service specifies a port on the host. If multiple containers for this service are created on a single host, the port will clash. Starting flask-redis_web_1 ... Starting flask-redis_web_1 ... done Creating flask-redis_web_2 ... error Creating flask-redis_web_3 ... error ERROR: for flask-redis_web_2 Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_2 (952f3eea8bdf14f0a94845fb6ef5039285f7ffe40620faca32f40643b802fa97): Bind for 0.0.0.0:8080 failed: port is already allocated ERROR: for flask-redis_web_3 Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_3 (50c776deb73b272d04181a8ab385f27cc31689aeeed804436fd2b92836cf25b9): Bind for 0.0.0.0:8080 failed: port is already allocated ERROR: for web Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_2 (952f3eea8bdf14f0a94845fb6ef5039285f7ffe40620faca32f40643b802fa97): Bind for 0.0.0.0:8080 failed: port is already allocated ERROR: Encountered errors while bringing up the project.
發現報錯了,原因是接口已經被分配了。每個容器的端口都想綁定到8080上面,顯然是不可能的。
將容器刪除:
docker-compose down Stopping flask-redis_web_1 ... done Stopping flask-redis_redis_1 ... done Removing flask-redis_web_3 ... done Removing flask-redis_web_2 ... done Removing flask-redis_web_1 ... done Removing flask-redis_redis_1 ... done Removing network flask-redis_default
將docker-compose.yml中端口綁定刪除:
version: "3" services: redis: image: redis web: build: context: . dockerfile: Dockerfile environment: REDIS_HOST: redis
執行命令docker-compose up --scale web=3 -d
docker-compose up --scale web=3 -d Creating network "flask-redis_default" with the default driver Creating flask-redis_web_1 ... done Creating flask-redis_web_2 ... done Creating flask-redis_web_3 ... done Creating flask-redis_redis_1 ... done
可以成功執行。
docker-compose ps Name Command State Ports ----------------------------------------------------------------------- flask-redis_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp flask-redis_web_1 python app.py Up 5000/tcp flask-redis_web_2 python app.py Up 5000/tcp flask-redis_web_3 python app.py Up 5000/tcp
發現web端口并沒有映射到主機的端口,全部是5000container端口。
想象一下假如有多個web服務,都會監聽本地的5000端口,都會訪問redis,那么我們就可以有一個負載均衡器,可以把訪問量平均的分配到web服務,可以減輕單個服務的壓力。如下圖所示:
如果我們用這種方式來部署web服務的話,以后的擴展性是非常強的。例如我們開啟10個web服務:
iie4bu@hostdocker:~/ddy/docker-compose-flask-redis$ docker-compose up --scale web=10 -d WARNING: The Docker Engine you're using is running in swarm mode. Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node. To deploy your application across the swarm, use `docker stack deploy`. Creating network "docker-compose-flask-redis_default" with the default driver Creating docker-compose-flask-redis_redis_1 ... done Creating docker-compose-flask-redis_web_1 ... done Creating docker-compose-flask-redis_web_2 ... done Creating docker-compose-flask-redis_web_3 ... done Creating docker-compose-flask-redis_web_4 ... done Creating docker-compose-flask-redis_web_5 ... done Creating docker-compose-flask-redis_web_6 ... done Creating docker-compose-flask-redis_web_7 ... done Creating docker-compose-flask-redis_web_8 ... done Creating docker-compose-flask-redis_web_9 ... done Creating docker-compose-flask-redis_web_10 ... done
iie4bu@hostdocker:~/ddy/docker-compose-flask-redis$ docker-compose ps Name Command State Ports -------------------------------------------------------------------------------------- docker-compose-flask-redis_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp docker-compose-flask-redis_web_1 python app.py Up 5000/tcp docker-compose-flask-redis_web_10 python app.py Up 5000/tcp docker-compose-flask-redis_web_2 python app.py Up 5000/tcp docker-compose-flask-redis_web_3 python app.py Up 5000/tcp docker-compose-flask-redis_web_4 python app.py Up 5000/tcp docker-compose-flask-redis_web_5 python app.py Up 5000/tcp docker-compose-flask-redis_web_6 python app.py Up 5000/tcp docker-compose-flask-redis_web_7 python app.py Up 5000/tcp docker-compose-flask-redis_web_8 python app.py Up 5000/tcp docker-compose-flask-redis_web_9 python app.py Up 5000/tcp
將app.py的端口由5000改為80,app.py如下:
from flask import Flask from redis import Redis import os import socket app = Flask(__name__) redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379) @app.route('/') def hello(): redis.incr('hits') return 'Hello Container World! I have been seen %s times and my hostname is %s.\n' % (redis.get('hits'),socket.gethostname()) if __name__ == "__main__": app.run(host="0.0.0.0", port=80, debug=True)
Dockerfile的expose也改成80,內容如下:
FROM python:3.5 LABEL maintaner="vincent" COPY . /app WORKDIR /app RUN pip install flask redis EXPOSE 80 CMD [ "python", "app.py" ]
docker-compose.yml中添加haproxy,內容如下:
version: "3" services: redis: image: redis web: build: context: . dockerfile: Dockerfile environment: REDIS_HOST: redis lb: image: dockercloud/haproxy links: - web ports: - 8082:80 volumes: - /var/run/docker.sock:/var/run/docker.sock
將上面的容器先docker-compose down,將容器刪掉。
iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ docker-compose up -d WARNING: The Docker Engine you're using is running in swarm mode. Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node. To deploy your application across the swarm, use `docker stack deploy`. Removing docker-compose-flask-redis-haproxy_lb_1 docker-compose-flask-redis-haproxy_redis_1 is up-to-date docker-compose-flask-redis-haproxy_web_1 is up-to-date Recreating d2b87be02ede_docker-compose-flask-redis-haproxy_lb_1 ... done
查看狀態:
iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------------------------------------------- docker-compose-flask-redis-haproxy_lb_1 /sbin/tini -- dockercloud- ... Up 1936/tcp, 443/tcp, 0.0.0.0:8082->80/tcp docker-compose-flask-redis-haproxy_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp docker-compose-flask-redis-haproxy_web_1 python app.py Up 80/tcp
測試訪問:
iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ curl 127.0.0.1:8082 Hello Container World! I have been seen b'1' times and my hostname is 9603ec0f2527.
測試通過。
接下來測試使用多個web服務:
先將之前的docker-compose down。
啟動三個web服務:
iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ docker-compose up --scale web=3 -d WARNING: The Docker Engine you're using is running in swarm mode. Compose does not use swarm mode to deploy services to multiple nodes in a swarm. All containers will be scheduled on the current node. To deploy your application across the swarm, use `docker stack deploy`. Creating network "docker-compose-flask-redis-haproxy_default" with the default driver Creating docker-compose-flask-redis-haproxy_web_1 ... done Creating docker-compose-flask-redis-haproxy_web_2 ... done Creating docker-compose-flask-redis-haproxy_web_3 ... done Creating docker-compose-flask-redis-haproxy_redis_1 ... done Creating docker-compose-flask-redis-haproxy_lb_1 ... done
iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ docker-compose ps Name Command State Ports ----------------------------------------------------------------------------------------------------------------------------- docker-compose-flask-redis-haproxy_lb_1 /sbin/tini -- dockercloud- ... Up 1936/tcp, 443/tcp, 0.0.0.0:8082->80/tcp docker-compose-flask-redis-haproxy_redis_1 docker-entrypoint.sh redis ... Up 6379/tcp docker-compose-flask-redis-haproxy_web_1 python app.py Up 80/tcp docker-compose-flask-redis-haproxy_web_2 python app.py Up 80/tcp docker-compose-flask-redis-haproxy_web_3 python app.py Up 80/tcp
依次訪問我們的HAproxy:
iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ curl 127.0.0.1:8082 Hello Container World! I have been seen b'1' times and my hostname is 9a2d714aadad. iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ curl 127.0.0.1:8082 Hello Container World! I have been seen b'2' times and my hostname is eda311882f3f. iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ curl 127.0.0.1:8082 Hello Container World! I have been seen b'3' times and my hostname is 43f69f733c5e. iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ curl 127.0.0.1:8082 Hello Container World! I have been seen b'4' times and my hostname is 9a2d714aadad. iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ curl 127.0.0.1:8082 Hello Container World! I have been seen b'5' times and my hostname is eda311882f3f. iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ curl 127.0.0.1:8082 Hello Container World! I have been seen b'6' times and my hostname is 43f69f733c5e.
發現是輪詢我們的web服務,haproxy會轉發請求到三個web服務上。
我們目前的擴展是單機環境下的scale,無論將service擴展到多少,只能限制在單機環境下。但是一臺服務器的資源是有限的,那么如何擴展多臺服務器?那么就是需要使用swarm技術了。
關于如何進行Docker Compose中scale就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。