從零開始學習docker(十三)Docker Compose--scale

環境準備,上一節中的flask-redis項目,將項目啓動:node

docker-compose up -d
Starting flask-redis_web_1   ... done
Starting flask-redis_redis_1 ... done

查看狀態:python

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 scale 

咱們目前的docker-compose 中的service容器中各自只有一個,經過scale能夠去擴展service。web

docker-compose up --help
    --scale SERVICE=NUM        Scale SERVICE to NUM instances. Overrides the
                               `scale` setting in the Compose file if present.

例如將web服務的數量變爲三個:redis

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

將容器刪除:flask

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

能夠成功執行。app

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

咱們爲了演示真實的架構,使用HAproxy作負載均衡

將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技術了。

相關文章
相關標籤/搜索