无法使用docker swarm运行应用程序,但使用docker-compose进行相同的运行

时间:2018-11-26 10:12:58

标签: docker docker-compose docker-swarm

我正在尝试在docker中创建一个Web应用程序。该应用程序的目标是,每当用户发出请求时,都应通过nginx将其路由到任何应用程序服务器。当前应用程序拥有3个应用程序服务器和3个数据库服务器。以下是应用程序内部组件的列表:

1) MYSQLDB
2) MongoDB
3) RedisDB
4) Load Balancer (nginx)
5) Application Server (Running in Node.js built on top of socket.io)

请在docker-compose.yml文件下面找到已成功创建应用程序环境的文件:

version: '3.0'
services:
  appserver1:
      container_name: appserver1
      depends_on:
      - "redis"
      - "mongo"
      - "mariadb"
      build: ./application-module
      tty: true
      volumes:
        - /Volumes:/var/www/html/files
      command: bash -c "sleep 5 && forever app.js"
  appserver2:
      container_name: appserver2
      depends_on:
      - "redis"
      - "mongo"
      - "mariadb"
      build: ./application-module
      tty: true
      volumes:
         - /Volumes:/var/www/html/files
      command: bash -c "sleep 10 && forever app.js"
  appserver3:
      container_name: appserver3
      depends_on:
      - "redis"
      - "mongo"
      - "mariadb"
      build: ./application-module
      tty: true
      volumes:
        - /Volumes:/var/www/html/files
      command: bash -c "sleep 15 && forever app.js"
  adminserver:
      container_name: adminserver
      build: ./admin-module
      tty: true
      volumes:
        - ./var/www/html/files:/var/www/html/files     
  loadbalancer:
      container_name: loadbalancer
      build: ./load-balancer
      tty: true
      links:
          - appserver1
          - appserver2
          - appserver3
      ports:
          - '91:91'
  redis:
      container_name: redisdb
      build: ./redis
      restart: always
      volumes:
        - redisvolume:/data
  mongo:
      container_name: mongodb
      build: ./mongo
    #   environment:
    #       - http_proxy=$http_proxy
    #       - https_proxy=$https_proxy
    #       - no_proxy=$no_proxy
    #       - HTTP_PROXY=$HTTP_PROXY
    #       - HTTPS_PROXY=$HTTPS_PROXY
    #       - NO_PROXY=$NO_PROXY
      volumes:
        - mongovolume:/data/db
  mariadb:
    container_name: mariadb
    image: mariadb:10.3
    restart: always
    volumes:
      - mariadbvolume:/var/lib/mysql
      - ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
    # networks: 
    #  - overlay
    ports: 
    - "3307:3307"
    environment:
      - 'MYSQL_ROOT_PASSWORD=Mys@l123'
      - 'MYSQL_DATABASE=copconnect_dev'
      - 'MYSQL_USER=root'
      - 'MYSQL_PASSWORD=*****'
volumes:
 mariadbvolume:
 mongovolume:
 redisvolume:
 adminvolume:
 appvolume:

请在nginx.conf文件下面找到:

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;
    client_max_body_size 20M;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

  server {
   listen 91;
   server_name localhost;

   location / {
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_http_version 1.1;
    proxy_pass http://io_nodes;
            }
    }



    map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
    }



  upstream io_nodes {
  ip_hash;

  server copconnect_appserver.1:3001;

  server copconnect_appserver.2:3001;

  server copconnect_appserver.3:3001;

  }
}

在运行docker-compose.yml后,应用程序成功创建了平台并运行了该应用程序,但是当我尝试以群体模式执行时,会产生很多问题-无法为loadbalancer和appserver创建服务。

在下面找到docker-compose-stack.yml文件:

version: "3"
services:
  redis:
    image: redis:alpine
    ports:
      - "6379"
    networks:
      - backend
    deploy:
      replicas: 1
      update_config:
        parallelism: 2
        delay: 10s
      restart_policy:
        condition: on-failure
  mariadb:
    image: mariadb:10.3
    volumes:
      - mariadbvolume:/var/lib/mysql
      #- ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
    ports: 
    - "3307:3307"
    environment:
      - 'MYSQL_ROOT_PASSWORD=******'
      - 'MYSQL_DATABASE=copconnect_dev'
      - 'MYSQL_USER=root'
      - 'MYSQL_PASSWORD=********'
    networks:
      - backend
    deploy:
      placement:
        constraints: [node.role == manager]
  mongo:
    image: mongo:latest
    ports: 
    - "27017:27017"
    volumes:
      - mongovolume:/data/db
    networks:
      - backend
    deploy:
      placement:
        constraints: [node.role == manager]
  appserver:
    image: localhost:5000/appserver:latest
    ports:
      - 3001:3001
    volumes:
         - ./var/www/html/files:/var/www/html/files
    networks:
      - backend
    depends_on:
      - redis
      - mongo
      - mariadb
    deploy:
      mode: replicated
      replicas: 3
      labels: [APP=appserver]
      restart_policy:
        condition: on-failure
        delay: 10s
        max_attempts: 3
        window: 120s
      placement:
        constraints: [node.role == manager]

  visualizer:
    image: dockersamples/visualizer
    ports:
      - "8080:8080"
    stop_grace_period: 1m30s
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]
  loadbalancer:
    image: localhost:5000/nginx:1.0
    ports:
      - "91:91"
    stop_grace_period: 1m30s
    networks:
      - backend  
networks:
  backend:

volumes:
  mariadbvolume:
  mongovolume:
  redisvolume:
  appvolume:

在创建群集集群并尝试运行该应用程序时,它在日志中提到appserver无法连接到数据库。  负载平衡器服务没有启动,因为它提到不存在appserver.1,appserver.2,appserver.3。

0 个答案:

没有答案