Course contentsShow
← Docker, Kubernetes & CI/CD
Lesson 224 of 8023. Docker Orchestration and StorageFree lesson

Implementing Swarm Ingress Load Balancing

Learners will configure the Swarm routing mesh to distribute incoming external requests across multiple container replicas regardless of their host node.

In this lesson, you will learn how the Docker Swarm "Routing Mesh" automatically distributes external traffic across your service replicas, no matter which node the request hits.

The Magic of the Routing Mesh

When you run a service in a Docker Swarm cluster with multiple replicas, you don't need to worry about which specific node is hosting a container. Docker Swarm provides a built-in feature called the Routing Mesh. It allows every node in your cluster to accept connections for any service, even if that node isn't actually running a replica of that service.

How It Works

Imagine you have a 3-node Swarm cluster and a web service with 2 replicas spread across "Node A" and "Node B." If an external user sends a request to "Node C" on the service’s published port, the Routing Mesh transparently intercepts that traffic. It recognizes the destination and internally routes (or "masquerades") the request over the cluster's overlay network to a node that has an available container replica.

This means you can point your external load balancer or DNS record to any node in the cluster, and the Swarm will handle the distribution for you.

Configuration Example

To enable this, you simply publish a port in your service definition. The Swarm handles the rest automatically:

services:
  web-app:
    image: my-web-service:latest
    deploy:
      replicas: 3
    ports:
      - "8080:80"  # Swarm enables the Routing Mesh on port 8080

In this example, hitting http://<any-node-ip>:8080 will automatically load balance your traffic across the 3 replicas of your web-app, ensuring high availability and seamless request distribution. You don't need to manually configure external load balancers to track individual container IPs; the Swarm mesh acts as a virtual IP (VIP) load balancer that maps the published port to the healthy task set.

Key Takeaway: The Routing Mesh allows any node in a Swarm cluster to act as an entry point for your services by automatically forwarding incoming traffic to healthy replicas across the network.