Search code examples
docker-swarm

Docker Swarm Overlay VIP


I've got Docker Swarm 12.1 running on my nodes. I've got a 4 task service to run my Drupal site and a single task for the database:

drupalapp     mastermindg/rpi-apache2php7  
drupaldb      mysql:latest

I can now reach my site on any node that's running the container.

The problem is that I can only set a single IP address for Port Forwarding on my router and if a single node goes down then that IP address won't be accessible anymore.

Is it possible to set a Virtual IP address for load-balancing over the overlay network?


Solution

  • Docker Swarm uses mesh networking so any request to a single host will be routed to whichever host that's running on. The difficult part here is that your service is not running on ingress so it's not available to the world. In order to make it available use HAProxy:

    docker network create --driver overlay proxy
    docker network create --driver overlay drupal
    docker service create --name proxy \
        -p 80:80 \
        -p 443:443 \
        -p 8080:8080 \
        --network proxy \
        -e MODE=swarm \
        vfarcic/docker-flow-proxy
    docker service create --name drupalapp \
        --network proxy \
        --network drupal \
        mastermindg/rpi-apache2php7
    docker service create --name drupaldb \
        --network drupal \
        mysql:latest
    

    So here you need to create two networks: 1 for internal use between drupaldb and drupalapp (called drupal) and one for haproxy use to expose your ports. Your drupalapp service will need to be included in the proxy overlay network as well as the drupal network.

    Register your HAProxy service:

    curl "$masterip:8080/v1/docker-flow-proxy/reconfigure?serviceName=drupalapp&servicePath=/&port=80"
    

    Now your Drupal site can be reached on ANY node in your swarm via:

    http://nodeX/index.php
    

    The advantage now is that you can port forward to any node and it'll resolve correctly.