我正在通过命令"docker-compose up --scale"在单个Linux主机上扩展服务的多个容器。但是,每个容器都可以使用主机的所有资源(CPU 和 RAM(。像这样扩展似乎没有用。
因此,我尝试通过docker-compose.yml文件中的"cpus"和"mem_limit"标签来限制每个容器(我目前正在使用docker-compose版本2(的可用性CPU和RAM。
这是我的 docker-compose.yml 文件
version: "2.2"
services:
test:
image: test
mem_limit: 500000000
container_name: test
build: ./test
restart: always
mem_limit: 500000000
mem_reservation: 300m
cpus: 0.5
networks:
- test-network
ports:
- "9000:80"
但我不知道在单个主机上扩展服务的多个容器是否有用?另外,是否有任何方法可以自动缩放此服务(按需缩放(?谢谢
version: "3"
services:
test:
image: test
deploy:
replicas: 5
resources:
limits:
cpus: "0.5"
memory: 500M
restart_policy:
condition: always
ports:
- "9000:80"
networks:
- test-network
networks:
test-network:
docker swarm init #Initialize machine as docker swarm manager
docker stack deploy -c docker-compose.yml testing #This will start 5 containers for the same image as replica
可以通过在 docker-compose.yml 中将副本更改为 6 值、保存更改并重新运行来缩放应用
docker stack deploy -c docker-compose.yml testing #Docker performs an in-place update, no need to tear the stack down first or kill any containers
有关详细说明,您可以按照本文进行操作,https://docs.docker.com/get-started/part3/。但我建议您从本教程的第一部分开始。
对于任何 docker 实验,您可以尝试 https://labs.play-with-docker.com/