参考:https://www.cnblogs.com/JulianHuang/p/12568814.html

    单台机器,多实例,场景还没遇到过,先记一下吧。

    原理:利用nginx和docker内置DNS转发web接口。

    1. version: "3"
    2. services:
    3. webapp:
    4. image: "luksa/kubia"
    5. depends_on:
    6. - db
    7. ports:
    8. - "8080" # 主机Port: 容器暴露Port

    nginx.conf

    1. user nginx;
    2. events {
    3. worker_connections 1000;
    4. }
    5. http {
    6. server {
    7. listen 80;
    8. location / {
    9. proxy_pass http://webapp:8080;
    10. }
    11. }
    12. }
    1. version: "3"
    2. services:
    3. webapp:
    4. image: "luksa/kubia"
    5. nginx:
    6. image: nginx:latest
    7. volumes:
    8. - type: bind
    9. source: /home/root/test/nginx.conf
    10. target: /etc/nginx/nginx.conf
    11. depends_on:
    12. - webapp
    13. ports:
    14. - "80:80"

    :::info docker-compose up —scale webapp=3 :::

    1. IMAGE COMMAND PORTS NAMES
    2. luksa/kubia "node app.js" test_webapp_1
    3. luksa/kubia "node app.js" test_webapp_3
    4. luksa/kubia "node app.js" test_webapp_2
    5. nginx:latest "nginx -g 'daemon of…" 0.0.0.0:80->80/tcp test_nginx_1