Swarm Networking
Imagine a city with multiple districts (nodes). To keep traffic flowing, you build highways (overlay networks) that connect all districts. No matter where people live, they can reach each other smoothly. In Docker Swarm, Swarm networking works the same way — it connects containers across nodes, balances traffic, and ensures services can find each other by name.
Networking Foundations
1. Networking in Swarm
- Swarm uses overlay networks to connect containers across nodes.
- Each service gets a virtual IP (VIP) for load balancing.
- Built‑in DNS allows services to resolve each other by name.
2. Key Networking Features
- Overlay Networks: Enable cross‑node communication.
- Ingress Network: Default network for exposing services externally.
- Service Discovery: Containers resolve services by name (e.g.,
db). - Load Balancing: Requests are distributed across replicas automatically.
3. Ingress vs Custom Overlay Networks
- Ingress Network:
- Automatically created by Swarm.
- Handles external traffic routing to services.
- Custom Overlay Networks:
- User‑defined for internal service communication.
- Provide isolation between applications.
4. Swarm Networking Workflow
- Initialize Swarm cluster.
- Create an overlay network.
- Deploy services attached to the network.
- Swarm handles DNS resolution and load balancing.
Things to Remember
- Swarm networking enables multi‑host communication with built‑in DNS and load balancing.
- The ingress network handles external traffic, while custom overlay networks handle internal communication.
- Service names act as hostnames, eliminating the need for IP addresses.
Hands‑On Lab
Step 1: Create an Overlay Network
docker network create -d overlay appnet
Step 2: Deploy Services in the Overlay Network
docker service create --name web --network appnet -p 8080:80 nginx
docker service create --name db --network appnet redis
Step 3: Verify Networking
docker network ls
docker network inspect appnet
Step 4: Test Service Discovery
Inside the web container:
ping db
- Resolves
dbby name.
Step 5: Observe Load Balancing
Access http://<node_ip>:8080 multiple times.
- Requests are distributed across replicas.
Practice Exercise
- Create a custom overlay network
swarmnet. - Deploy two services (
frontendandbackend) inswarmnet. - Expose
frontendon port 8081. - Verify that
frontendcan resolvebackendby name. - Scale
frontendto 3 replicas and observe load balancing.
Visual Learning Model
Swarm Cluster
├── Ingress Network (external traffic)
└── Overlay Network: appnet
├── Service: web (nginx)
└── Service: db (redis)
DNS + Load Balancing ensure seamless communication
The Hackers Notebook
Swarm networking connects containers across nodes using overlay networks, built‑in DNS, and load balancing. The ingress network handles external traffic, while custom overlay networks provide internal communication. Service names act as hostnames, making networking simple and scalable in distributed environments.
