docker swarm 搭建三

TwoAdmin 2025-10-7 76 10/7

docker-compose.yml

version: '3.8'

services:
  
  # Python后端
  python-app:
    image: ${PYTHON_IMAGE} #镜像,PYTHON_IMAGE为环境变量,也可以直接这里写镜像地址
    networks:
      app-network: 
        aliases:
          - python-app  
    volumes:
      - /data/app/config/config.prod.yaml:/app/config.prod.yaml:ro 
      - /data/app/config/host_hardware_code.txt:/app/host_hardware_code.txt:ro      
      - /data/uploads:/app/uploads:rw   
      - /data/logs/python:/app/logs:rw    
    environment:
      - CONFIG_PATH=/app/config.prod.yaml
      - GUNICORN_WORKERS=8
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
        window: 120s
      update_config:
        parallelism: 1 # 每次更新1个副本
        delay: 30s  # 每次更新间隔10秒
        order: start-first # 先启动新容器,再停止旧容器(实现零停机)
        failure_action: rollback # 如果失败,自动回滚到上一版本
        monitor: 60s
        max_failure_ratio: 0.3
      rollback_config:
        parallelism: 1
        delay: 10s
        order: stop-first
      resources:
        limits:
          cpus: '4.0'
          memory: '8192M'
        reservations:
          cpus: '1.0'
          memory: '1024M'
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/api/common/index/console"]
      interval: 15s
      timeout: 10s
      retries: 5
      start_period: 60s

  # Vue前端
  vue-app:
    image: ${VUE_IMAGE}
    networks:
      app-network:  
        aliases:
          - vue-app  
    environment:
      - NODE_ENV=production
      - VUE_APP_API_URL=/api
      - PORT=3000
      - HOST=0.0.0.0
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
      update_config:
        parallelism: 1
        delay: 20s
        order: start-first
        failure_action: rollback
        monitor: 30s
      rollback_config:
        parallelism: 1
        delay: 5s
        order: stop-first
      resources:
        limits:
          cpus: '2.0'
          memory: '4096M'
        reservations:
          cpus: '1.0'
          memory: '2048M'
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:3000"]
      interval: 15s
      timeout: 10s
      retries: 5
      start_period: 60s

  # Nginx
  nginx:
    image: myapp-nginx:latest
    ports:
      - target: 80
        published: 80
        protocol: tcp
        mode: ingress
      - target: 443
        published: 443
        protocol: tcp
        mode: ingress
    volumes:
      - /data/nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - /data/logs/nginx:/var/log/nginx:rw
      - /data/nginx/ssl:/etc/nginx/ssl:ro
    networks:
      app-network:  
        aliases:
          - nginx  
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 5
      update_config:
        parallelism: 1
        delay: 15s
        order: start-first
        failure_action: rollback
      rollback_config:
        parallelism: 1
        delay: 5s
        order: stop-first
      resources:
        limits:
          cpus: '1.0'
          memory: '512M'
        reservations:
          cpus: '0.25'
          memory: '128M'
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost/nginx-health"]
      interval: 10s
      timeout: 5s
      retries: 3
      start_period: 10s

networks:
  app-network:
    external: true  

 

- THE END -
Tag:

TwoAdmin

12月05日16:51

最后修改:2025年12月5日
0

非特殊说明,本博所有文章均为博主原创。