首页 > 解决方案 > haproxy 没有对 docker swarm 中的测试应用程序进行负载平衡

问题描述

我有 3 个虚拟机(虚拟机)。所有这些都设置为使用带有 keepalived 的单个 VIP。(192.168.100.200)。我在每个 vm 上有一个代理,在每个 vm 上有一个测试应用程序。(我正在测试一个高可用性场景,其中丢失一个或两个节点,让设置继续进行)。我让keepalived正常工作。只是请求没有负载平衡,它总是去同一个实例。

出了什么问题?

version: "3.8"
services:

  # HAproxy
  haproxy                   :
    image                   : haproxy:2.3.2
    container_name          : haproxy
    networks                :
                            - app-net
    ports                   :
                            - 80:80
    volumes                 :
                            - /etc/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
    deploy                  :
      mode                  : global
      restart_policy        :
        condition           : on-failure
        delay               : 5s
        max_attempts        : 3
        window              : 120s

  # Nginx test site
  wwwsite                 :
    image                 : nginxdemos/hello
    networks              :
                          - app-net
    ports                 :
                          - 8080:80
    deploy                :
      mode                : global

  networks    :
  app-net     :
    driver    : overlay
    name      : app-net
    attachable: true

haproxy.conf

global
    stats socket /var/run/haproxy.stat mode 660 level admin
    stats timeout 30s
    user root
    group root

resolvers docker
    nameserver dns1 127.0.0.11:53
    resolve_retries 3
    timeout resolve 1s
    timeout retry   1s
    hold other      10s
    hold refused    10s
    hold nx         10s
    hold timeout    10s
    hold valid      10s
    hold obsolete   10s

defaults
    timeout connect 10s
    timeout client 30s
    timeout server 30s
    mode http

frontend  fe_web
    mode http
    bind *:80
    default_backend nodes

backend nodes
    balance roundrobin
    server node1 192.168.100.201:8080 check
    server node2 192.168.100.202:8080 check
    server node3 192.168.100.203:8080 check


listen stats 
    bind *:8081
    mode http
    stats enable
    stats uri /
    stats hide-version

标签: dockerdocker-swarmhaproxydocker-stackkeepalived

解决方案


推荐阅读