Search code examples
dockerload-balancingdocker-swarm

Docker swarm forawrd requests to only a single node


I have 2 machines in digital ocean cloud. 188.226.167.12, 146.185.135.232. My application show the name of the host.

Each node contain a single replica of my service. When I access multiple times to: http://188.226.167.12:8080/ or http://146.185.135.232:8080/ , the host name doesn't change.

It means there is no load balancing for the requests.

docker-compose.yml:

version: "3"
services:
  web:
    image: stavalfi/projecty:latest
    deploy:
      replicas: 2
      restart_policy:
        condition: on-failure
    ports:
      - "8080:8080"
    networks:
      - webnet
networks:
  webnet:

How can I make a load balancer for my requests?


Solution

  • The RR load balancing runs on the ingress network which is an overlay network. For overlay networking you need three ports opened:

    • 7946/tcp (control)
    • 7946/udp (control)
    • 4789/udp (data)

    Under the covers, this is VXLAN on Linux. If you enable IPSec on your overlay networks, you also need protocol 50. On the iptables CLI, this looks like:

    iptables -A INPUT -p tcp -m tcp --dport 7946 -j ACCEPT
    iptables -A INPUT -p tcp -m udp --dport 7946 -j ACCEPT
    iptables -A INPUT -p tcp -m tcp --dport 4789 -j ACCEPT
    iptables -A INPUT -p 50 -j ACCEPT
    

    You can further limit those rules to only allow connections between docker nodes.