Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Docker 部署后,一个Rancher负载平衡RESTful应用程序实例返回404,而另一个实例返回结果_Docker_Load Balancing_Devops_Gitlab Ci_Rancher - Fatal编程技术网

Docker 部署后,一个Rancher负载平衡RESTful应用程序实例返回404,而另一个实例返回结果

Docker 部署后,一个Rancher负载平衡RESTful应用程序实例返回404,而另一个实例返回结果,docker,load-balancing,devops,gitlab-ci,rancher,Docker,Load Balancing,Devops,Gitlab Ci,Rancher,我希望你能在这个问题上帮助我 我已经设置了一个CI/CD管道,每当签入Spring boot RESTful服务应用程序时,它就会触发构建、打包和部署在Gitlab online上执行的任务。这三个阶段和任务成功运行,但每当我通过在浏览器中导航到负载平衡器URL来测试应用程序时,我发现一个负载平衡器目标返回(type=notfound,status=404)错误,而另一个目标返回预期的JSON响应。负载平衡器在向目标分发请求时默认采用循环算法 基础设施提供商是数字海洋 我做错了什么 请在下面找到

我希望你能在这个问题上帮助我

我已经设置了一个CI/CD管道,每当签入Spring boot RESTful服务应用程序时,它就会触发构建、打包和部署在Gitlab online上执行的任务。这三个阶段和任务成功运行,但每当我通过在浏览器中导航到负载平衡器URL来测试应用程序时,我发现一个负载平衡器目标返回(type=notfound,status=404)错误,而另一个目标返回预期的JSON响应。负载平衡器在向目标分发请求时默认采用循环算法

基础设施提供商是数字海洋

我做错了什么

请在下面找到.gitlab-ci.yml文件

image: docker:latest
services:
  - docker:dind

variables:
  DOCKER_DRIVER: overlay
  SPRING_PROFILES_ACTIVE: gitlab-ci

stages:
  - build
  - package
  - deploy

maven-build:
  image: maven:3-jdk-8
  stage: build
  script: "mvn package -B"
  artifacts:
    paths:
      - target/*.jar

docker-build:
  stage: package
  script:
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
  - docker build -t registry.gitlab.com/username/mta-hosting-optimizer .
  - docker push registry.gitlab.com/username/mta-hosting-optimizer

digitalocean-deploy:
  image: cdrx/rancher-gitlab-deploy
  stage: deploy
  script:
  - upgrade --environment Default --stack mta-hosting-optimizer --service web --new-image registry.gitlab.com/username/mta-hosting-optimizer
  - upgrade --environment Default --stack mta-hosting-optimizer --service web2 --new-image registry.gitlab.com/username/mta-hosting-optimizer
docker-compose.yml

version: '2'
services:
  web:
    image: registry.gitlab.com/username/mta-hosting-optimizer:latest
    ports:
    - 8082:8080/tcp
  mta-hosting-optimizer-lb:
    image: rancher/lb-service-haproxy:v0.9.1
    ports:
    - 80:80/tcp
    labels:
      io.rancher.container.agent.role: environmentAdmin,agent
      io.rancher.container.agent_service.drain_provider: 'true'
      io.rancher.container.create_agent: 'true'
  web2:
    image: registry.gitlab.com/username/mta-hosting-optimizer:latest
    ports:
    - 8082:8080/tcp
version: '2'
services:
  web:
    scale: 1
    start_on_create: true
  mta-hosting-optimizer-lb:
    scale: 1
    start_on_create: true
    lb_config:
      certs: []
      port_rules:
      - path: ''
        priority: 1
        protocol: http
        service: web
        source_port: 80
        target_port: 8080
      - priority: 2
        protocol: http
        service: web2
        source_port: 80
        target_port: 8080
    health_check:
      response_timeout: 2000
      healthy_threshold: 2
      port: 42
      unhealthy_threshold: 3
      initializing_timeout: 60000
      interval: 2000
      reinitializing_timeout: 60000
  web2:
    scale: 1
    start_on_create: true
rancher-compose.yml

version: '2'
services:
  web:
    image: registry.gitlab.com/username/mta-hosting-optimizer:latest
    ports:
    - 8082:8080/tcp
  mta-hosting-optimizer-lb:
    image: rancher/lb-service-haproxy:v0.9.1
    ports:
    - 80:80/tcp
    labels:
      io.rancher.container.agent.role: environmentAdmin,agent
      io.rancher.container.agent_service.drain_provider: 'true'
      io.rancher.container.create_agent: 'true'
  web2:
    image: registry.gitlab.com/username/mta-hosting-optimizer:latest
    ports:
    - 8082:8080/tcp
version: '2'
services:
  web:
    scale: 1
    start_on_create: true
  mta-hosting-optimizer-lb:
    scale: 1
    start_on_create: true
    lb_config:
      certs: []
      port_rules:
      - path: ''
        priority: 1
        protocol: http
        service: web
        source_port: 80
        target_port: 8080
      - priority: 2
        protocol: http
        service: web2
        source_port: 80
        target_port: 8080
    health_check:
      response_timeout: 2000
      healthy_threshold: 2
      port: 42
      unhealthy_threshold: 3
      initializing_timeout: 60000
      interval: 2000
      reinitializing_timeout: 60000
  web2:
    scale: 1
    start_on_create: true
编辑以提供以下haproxy.cfg文件,以响应@leodotcloud的 请求


您的问题没有直截了当的答案,但以下是一些调试问题的提示:

  • 我看到有两个web服务(web,web2)部署在端口8082上。是否有足够的主机公开这两个端口
  • web和web2容器是否都成功运行,或者是否存在任何故障
  • 网络插件是否在群集中成功运行
  • 群集中主机之间的跨主机网络是否正常工作
  • 负载平衡器配置是否正确?(在lb容器中执行shell并检查haproxy.cfg)

如果这些都不能帮助您解决问题的根本原因,请向提交问题。

可能发生的情况是,要启动的第一个web容器获得端口(8080),而第二个容器无法绑定到该端口。即使这两个容器运行在不同的节点上,Swarm也无法将该端口分配给它们,因为它通过服务网格路由请求

见:

关于这个问题:

要解决此问题,请将web容器配置为在不同端口上运行。或者,由于您希望运行同一容器的两个实例,因此增加
web
服务的副本数量。Swarm将为您在可用节点上分发容器


HAProxy只需要对节点进行负载平衡,但实际上,如果您点击了节点的任何IP地址,请求可能会在Swarm内路由到另一个节点,因为整个机器集群在Swarm内实现负载平衡。这是两层负载平衡:请求到达一个节点后,在Swarm集群内,以及在客户端和基础设施之间。

非常感谢;一切似乎都很好。每个服务容器和负载平衡器容器都以每台主机一个容器的方式在主机上独占运行。web和web 2服务正在成功运行,没有任何故障。Rancher推荐的默认托管网络是我正在使用的,因此它适合跨主机联网。我已经编辑了上面的响应,以便从负载平衡器容器添加haproxy.cfg文件。