使用docker-compose部署某个服务,使用同一个镜像,这里有两种方式来部署:
共用端口
services:
test-service:
image: myimage
ports:
- "5000:5000"
deploy:
replicas: 3
这样配置三个服务共用一个端口,相当于多节点负载均衡的运行三个服务。
不同端口
services:
test-service:
image: myimage
ports:
- "5000-5002:5000"
deploy:
replicas: 3
这样配置三个服务会被分配到三个不同的端口,5000,5001,5002,如果需要负载均衡,可以在nginx中进行更详细的配置,比如根据IP分配:
upstream my_service {
ip_hash;
server 127.0.0.1:5000;
server 127.0.0.1:5001;
server 127.0.0.1:5002;
}
server {
listen 443 ssl http2;
server_name api.test.com;
location / {
proxy_pass http://my_service;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}