In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to carry out scale in Docker Compose, the content is very detailed, interested friends can use for reference, I hope it can be helpful to you.
Environment preparation, flask-redis project, start the project:
Docker-compose up-dStarting flask-redis_web_1... DoneStarting flask-redis_redis_1... Done
View status:
Docker-compose ps Name Command State Ports-flask- Redis_redis_1 docker-entrypoint.sh redis... Up 6379/tcpflask-redis_web_1 python app.py Up 0.0.0.0 8080-> 5000/tcpdocker-compose scale
We currently have only one service container in docker-compose, and we can extend service through scale.
Docker-compose up-help-scale SERVICE=NUM Scale SERVICE to NUM instances. Overrides the `scale` setting in the Compose file if present.
For example, change the number of web services to three:
Docker-compose up-scale web=3-dWARNING: 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... DoneCreating flask-redis_web_2... ErrorCreating flask-redis_web_3... ErrorERROR: 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 Bind for 8080 failed: port is already allocatedERROR: 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.015 8080 failed: port is already allocatedERROR: for web Cannot start service web: driver failed programming external connectivity on endpoint flask-redis_web_ 2 (952f3eea8bdf14f0a94845fb6ef5039285f7ffe40620faca32f40643b802fa97): Bind for 0.0.0.0 failed 8080: port is already allocatedERROR: Encountered errors while bringing up the project.
An error was found because the interface has been assigned. The port of each container wants to bind to 8080, which is obviously impossible.
Delete the container:
Docker-compose downStopping flask-redis_web_1... DoneStopping flask-redis_redis_1... DoneRemoving flask-redis_web_3... DoneRemoving flask-redis_web_2... DoneRemoving flask-redis_web_1... DoneRemoving flask-redis_redis_1... DoneRemoving network flask-redis_default
Delete the port binding in docker-compose.yml:
Version: "3" services: redis: image: redis web: build: context:. Dockerfile: Dockerfile environment: REDIS_HOST: redis
Execute the command docker-compose up-- scale web=3-d
Docker-compose up-scale web=3-dCreating network "flask-redis_default" with the default driverCreating flask-redis_web_1... DoneCreating flask-redis_web_2... DoneCreating flask-redis_web_3... DoneCreating flask-redis_redis_1... Done
It can be executed successfully.
Docker-compose ps Name Command State Ports----flask-redis_redis_1 docker-entrypoint.sh redis... Up 6379/tcpflask-redis_web_1 python app.py Up 5000/tcpflask-redis_web_2 python app.py Up 5000/tcpflask-redis_web_3 python app.py Up 5000/tcp
It is found that the web port is not mapped to the host port, it is all 5000container port.
Imagine that if there are multiple web services that listen on local port 5000 and access redis, then we can have a load balancer that distributes visits evenly to web services, reducing the pressure on a single service. As shown in the following figure:
If we deploy web services in this way, the future scalability will be very strong. For example, we open 10 web services:
Iie4bu@hostdocker:~/ddy/docker-compose-flask-redis$ docker-compose up-scale web=10-dWARNING: 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 driverCreating docker-compose-flask-redis_redis_1... DoneCreating docker-compose-flask-redis_web_1... DoneCreating docker-compose-flask-redis_web_2... DoneCreating docker-compose-flask-redis_web_3... DoneCreating docker-compose-flask-redis_web_4... DoneCreating docker-compose-flask-redis_web_5... DoneCreating docker-compose-flask-redis_web_6... DoneCreating docker-compose-flask-redis_web_7... DoneCreating docker-compose-flask-redis_web_8... DoneCreating docker-compose-flask-redis_web_9... DoneCreating docker-compose-flask-redis_web_10... Doneiie4bu@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/tcpdocker-compose-flask-redis_web_1 python app.py Up 5000/tcpdocker-compose-flask-redis_web_10 python app.py Up 5000/tcpdocker-compose-flask-redis_web_2 python app.py Up 5000/tcpdocker-compose-flask-redis_web_3 python app.py Up 5000/tcpdocker-compose-flask-redis_web_4 python app.py Up 5000/tcpdocker-compose-flask-redis_web_5 python app.py Up 5000/tcpdocker-compose-flask-redis_web_6 python app.py Up 5000/tcpdocker-compose-flask-redis_web_7 python app.py Up 5000/tcpdocker-compose-flask-redis_web_8 python app.py Up 5000/tcpdocker-compose-flask-redis_web_9 python app.py Up 5000/tcp, in order to demonstrate the real architecture, Using HAproxy for load balancing
Change the port of app.py from 5000 to 80j app.py as follows:
From flask import Flaskfrom redis import Redisimport osimport socketapp = 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's expose has also been changed to 80, which reads as follows:
FROM python:3.5LABEL maintaner= "vincent" COPY. / appWORKDIR / appRUN pip install flask redisEXPOSE 80CMD ["python", "app.py"]
Add haproxy to docker-compose.yml as follows:
Version: "3" services: redis: image: redis web: build: context:. Dockerfile: Dockerfile environment: REDIS_HOST: redis lb: image: dockercloud/haproxy links:-web ports:-8082 dockerfile 80 volumes:-/ var/run/docker.sock:/var/run/docker.sock
Docker-compose down the above container first and delete the container.
Iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ docker-compose up-dWARNING: 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_1docker-compose-flask-redis-haproxy_redis_1 is up-to-datedocker-compose-flask-redis-haproxy_web_1 is up-to-dateRecreating d2b87be02ede_docker-compose-flask-redis-haproxy_lb_1... Done
View status:
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 80/tcpdocker-compose-flask-redis-haproxy_redis_1 docker-entrypoint.sh redis 8082-> 80/tcpdocker-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
Test access:
Iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ curl 127.0.0.1:8082Hello Container World! I have been seen baked 1 'times and my hostname is 9603ec0f2527.
The test passed.
Next, the test uses multiple web services:
First of all, the previous docker-compose down.
Start three web services:
Iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ docker-compose up-scale web=3-dWARNING: 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 driverCreating docker-compose-flask-redis-haproxy_web_1... DoneCreating docker-compose-flask-redis-haproxy_web_2... DoneCreating docker-compose-flask-redis-haproxy_web_3... DoneCreating docker-compose-flask-redis-haproxy_redis_1... DoneCreating docker-compose-flask-redis-haproxy_lb_1... Doneiie4bu@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 80/tcpdocker-compose-flask-redis-haproxy_redis_1 docker-entrypoint.sh redis 8082-> 80/tcpdocker-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
Visit our HAproxy in turn:
Iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ curl 127.0.0.1:8082Hello Container World! I have been seen baked 1' times and my hostname is 9a2d714aadad.iie4bugs hostdocker mortars Dyke and Dypex, composehouse, composehouse, house, curl 127.0.0.1:8082Hello Container World, etc. I have been seen baked 2' times and my hostname is eda311882f3f.iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ curl 127.0.0.1:8082Hello Container World! I have been seen baked 3 'times and my hostname is 43f69f733c5e.iie4bugs hostdocker motto DdyGen Dyke Mustang composeMube FLASKLIREDIQUTHAPHY $curl 127.0.0.1:8082Hello Container World! I have been seen baked 4' times and my hostname is 9a2d714aadad.iie4bugs hostdocker movement.Dydypex, composehouse, composehouse, house, etc. $curl 127.0.0.1:8082Hello Container World! I have been seen baked 5' times and my hostname is eda311882f3f.iie4bu@hostdocker:~/ddy/docker-compose-flask-redis-haproxy$ curl 127.0.0.1:8082Hello Container World! I have been seen baked 6 'times and my hostname is 43f69f733c5e.
Discovery polls our web service, and haproxy forwards the request to the three web services.
Our current expansion is scale in a stand-alone environment. No matter how much service is extended, it can only be limited to a stand-alone environment. But the resources of one server are limited, so how to expand multiple servers? Then you need to use swarm technology.
On how to carry out scale in Docker Compose to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.