Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 38 ยท Networking and Overlay

Service Discovery and VIPs

Understand how containers find each other in Swarm: the embedded DNS server, virtual IPs, load balancing with IPVS, tasks.servicename and DNS round-robin.

In a swarm, tasks come and go and their IP addresses change constantly. Hard-coding an address would be hopeless. Instead, Swarm gives every service a name and a stable virtual IP (VIP), and an embedded DNS server resolves names for containers on the same network. This lesson explains how a container connecting to db:5432 ends up at a live task.

The embedded DNS server

Every container has a resolver at 127.0.0.11 (Docker's embedded DNS). It answers questions about:

  • service names on the overlay networks the container is attached to,
  • task names (like web.1.abc...) and network aliases,
  • everything else is forwarded to the host's normal DNS servers.

Only containers on the same network can resolve each other. A container attached to frontend cannot resolve a service that exists only on backend.

Virtual IPs (the default)

For each service and each network it is attached to, Swarm allocates one VIP. The service name resolves to that single address:

CONSOLE
$ docker service create --name api --network backend --replicas 3 myorg/api:1
$ docker run --rm --network backend --name probe alpine nslookup api   # on an attachable network

(For a standalone probe the network must be attachable; see Overlay networks.) The answer is the VIP, such as 10.20.0.5, not one of the three tasks.

The kernel on each node (using IPVS, Linux's in-kernel load balancer, set up by Swarm) forwards packets sent to a VIP to one of the healthy tasks, round-robin. The caller does not know how many tasks exist. Advantages:

  • Stable address that never changes during the service's life.
  • Load balancing at connection level without a proxy process.
  • Only healthy tasks receive traffic, so health checks matter (health checks).
  • Scaling and rolling updates are invisible to clients.
flowchart LR
  C["client container"] -->|"DNS: api"| D["Embedded DNS"]
  D -->|"VIP 10.20.0.5"| C
  C -->|"connect VIP"| L["IPVS on the node"]
  L --> T1["api.1"]
  L --> T2["api.2"]
  L --> T3["api.3"]

The name resolves to a virtual IP; the node's kernel picks a task.

tasks.servicename

Sometimes you need all the tasks, for example a clustered application that must contact every peer. Query the special name tasks.<service>:

CONSOLE
$ docker run --rm --network backend alpine nslookup tasks.api

This returns the individual task IPs (one A record each), not the VIP. Use it for peer discovery (Elasticsearch, Redis cluster, Cassandra seeds), then handle connections yourself.

DNS round-robin mode (dnsrr)

You can skip VIPs altogether:

CONSOLE
$ docker service create --name cache --network backend --endpoint-mode dnsrr --replicas 3 redis:7

With dnsrr the service name itself resolves to the list of task IPs, in rotating order. Consequences:

  • The client does the choosing and must cope with DNS caching: a long-lived client may keep stale addresses.
  • Required when you must see client connections without NAT/IPVS in the way or have a very large number of tasks (VIPs have limits).
  • Not compatible with ingress-published ports (--publish in ingress mode); you can only publish with mode=host.

Use vip (default) unless you have a reason.

Connection behaviour to remember

  • VIP load balancing is per connection, not per request. A client that opens one long-lived connection (HTTP keep-alive, gRPC, a database pool) sticks to one task. To spread gRPC load, use client-side balancing with tasks.<service> or a proxy.
  • Idle connections through the VIP may be dropped after a long period (IPVS has timeouts, often hours by default, lower with some kernel settings). Use keep-alive in clients that hold connections.
  • Names are case-insensitive and service names should be valid DNS labels.

Aliases

CONSOLE
$ docker service create --name db --network name=backend,alias=postgres,alias=database postgres:17

The extra aliases resolve on that network like the service name. Handy when migrating or when applications expect a particular host name.

Debugging DNS

CONSOLE
$ docker exec -it <container> getent hosts api
$ docker exec -it <container> nslookup tasks.api 127.0.0.11

If the name fails: the containers do not share a network; the service does not exist yet; or the image lacks the tool (BusyBox nslookup exists in Alpine). Check docker service inspect --format '{{json .Endpoint.VirtualIPs}}' api for the VIPs.

Stand-in: VIP versus dnsrr

The simulation resolves a name both ways and balances a series of connections, so you can see why a long-lived connection pins to one task in VIP mode.

Python Stand-in: VIP and DNS round-robin

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in: healthy tasks only

VIP load balancing skips unhealthy tasks. This shows the effect of a failing health check on the targets that receive connections.

Python Stand-in: skip unhealthy tasks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. What does the service name resolve to by default?

    In vip endpoint mode the name resolves to a VIP, and IPVS balances connections to healthy tasks.
  2. 2. Which name returns the individual IPs of all tasks of service api?

    tasks.<service> resolves to one A record per task, useful for peer discovery.
  3. 3. Which publishing mode cannot be combined with --endpoint-mode dnsrr?

    dnsrr services cannot publish ports through the ingress routing mesh; use mode=host.

Summary

  • Embedded DNS (127.0.0.11) resolves service and task names on shared networks.
  • Default vip mode gives a stable virtual IP balanced by IPVS per connection.
  • tasks.<service> returns task IPs; dnsrr returns them for the service name itself.
  • Long-lived connections stick to one task; use health checks so only healthy tasks get traffic.

Next: The Routing Mesh and Published Ports