Manage swarm service networks (original) (raw)

This page describes networking for swarm services.

A Docker swarm generates two different kinds of traffic:

The following three network concepts are important to swarm services:

Tip

See alsoNetworking overview for more details about Swarm networking in general.

Docker daemons participating in a swarm need the ability to communicate with each other over the following ports:

When setting up networking in a Swarm, special care should be taken. Consult thetutorialfor an overview.

When you initialize a swarm or join a Docker host to an existing swarm, two new networks are created on that Docker host:

Create an overlay network

To create an overlay network, specify the overlay driver when using thedocker network create command:

The above command doesn't specify any custom options, so Docker assigns a subnet and uses default options. You can see information about the network usingdocker network inspect.

When no containers are connected to the overlay network, its configuration is not very exciting:

In the above output, notice that the driver is overlay and that the scope isswarm, rather than local, host, or global scopes you might see in other types of Docker networks. This scope indicates that only hosts which are participating in the swarm can access this network.

The network's subnet and gateway are dynamically configured when a service connects to the network for the first time. The following example shows the same network as above, but with three containers of a redis service connected to it.

Customize an overlay network

There may be situations where you don't want to use the default configuration for an overlay network. For a full list of configurable options, run the command docker network create --help. The following are some of the most common options to change.

Configure the subnet and gateway

By default, the network's subnet and gateway are configured automatically when the first service is connected to the network. You can configure these when creating a network using the --subnet and --gateway flags. The following example extends the previous one by configuring the subnet and gateway.

Using custom default address pools

To customize subnet allocation for your Swarm networks, you canoptionally configure them during swarm init.

For example, the following command is used when initializing Swarm:

Whenever a user creates a network, but does not use the --subnet command line option, the subnet for this network will be allocated sequentially from the next available subnet from the pool. If the specified network is already allocated, that network will not be used for Swarm.

Multiple pools can be configured if discontiguous address space is required. However, allocation from specific pools is not supported. Network subnets will be allocated sequentially from the IP pool space and subnets will be reused as they are deallocated from networks that are deleted.

The default mask length can be configured and is the same for all networks. It is set to /24 by default. To change the default subnet mask length, use the --default-addr-pool-mask-length command line option.

Note

Default address pools can only be configured on swarm init and cannot be altered after cluster creation.

Overlay network size limitations

Docker recommends creating overlay networks with /24 blocks. The /24 overlay network blocks limit the network to 256 IP addresses.

This recommendation addresseslimitations with swarm mode. If you need more than 256 IP addresses, do not increase the IP block size. You can either use dnsrrendpoint mode with an external load balancer, or use multiple smaller overlay networks. SeeConfigure service discovery for more information about different endpoint modes.

Configure encryption of application data

Management and control plane data related to a swarm is always encrypted. For more details about the encryption mechanisms, see theDocker swarm mode overlay network security model.

Application data among swarm nodes is not encrypted by default. To encrypt this traffic on a given overlay network, use the --opt encrypted flag on docker network create. This enables IPSEC encryption at the level of the vxlan. This encryption imposes a non-negligible performance penalty, so you should test this option before using it in production.

Note

You mustcustomize the automatically created ingressto enable encryption. By default, all ingress traffic is unencrypted, as encryption is a network-level option.

To attach a service to an existing overlay network, pass the --network flag todocker service create, or the --network-add flag to docker service update.

Service containers connected to an overlay network can communicate with each other across it.

To see which networks a service is connected to, use docker service ls to find the name of the service, then docker service ps <service-name> to list the networks. Alternately, to see which services' containers are connected to a network, use docker network inspect <network-name>. You can run these commands from any swarm node which is joined to the swarm and is in a running state.

Configure service discovery

Service discovery is the mechanism Docker uses to route a request from your service's external clients to an individual swarm node, without the client needing to know how many nodes are participating in the service or their IP addresses or ports. You don't need to publish ports which are used between services on the same network. For instance, if you have aWordPress service that stores its data in a MySQL service, and they are connected to the same overlay network, you do not need to publish the MySQL port to the client, only the WordPress HTTP port.

Service discovery can work in two different ways: internal connection-based load-balancing at Layers 3 and 4 using the embedded DNS and a virtual IP (VIP), or external and customized request-based load-balancing at Layer 7 using DNS round robin (DNSRR). You can configure this per service.

Most users never need to configure the ingress network, but Docker allows you to do so. This can be useful if the automatically-chosen subnet conflicts with one that already exists on your network, or you need to customize other low-level network settings such as the MTU, or if you want toenable encryption.

Customizing the ingress network involves removing and recreating it. This is usually done before you create any services in the swarm. If you have existing services which publish ports, those services need to be removed before you can remove the ingress network.

During the time that no ingress network exists, existing services which do not publish ports continue to function but are not load-balanced. This affects services which publish ports, such as a WordPress service which publishes port 80.

  1. Inspect the ingress network using docker network inspect ingress, and remove any services whose containers are connected to it. These are services that publish ports, such as a WordPress service which publishes port 80. If all such services are not stopped, the next step fails.
  2. Remove the existing ingress network:
  3. Create a new overlay network using the --ingress flag, along with the custom options you want to set. This example sets the MTU to 1200, sets the subnet to 10.11.0.0/16, and sets the gateway to 10.11.0.2.

    Note

    You can name your ingress network something other thaningress, but you can only have one. An attempt to create a second one fails.

  4. Restart the services that you stopped in the first step.

The docker_gwbridge is a virtual bridge that connects the overlay networks (including the ingress network) to an individual Docker daemon's physical network. Docker creates it automatically when you initialize a swarm or join a Docker host to a swarm, but it is not a Docker device. It exists in the kernel of the Docker host. If you need to customize its settings, you must do so before joining the Docker host to the swarm, or after temporarily removing the host from the swarm.

You need to have the brctl application installed on your operating system in order to delete an existing bridge. The package name is bridge-utils.

  1. Stop Docker.
  2. Use the brctl show docker_gwbridge command to check whether a bridge device exists called docker_gwbridge. If so, remove it usingbrctl delbr docker_gwbridge.
  3. Start Docker. Do not join or initialize the swarm.
  4. Create or re-create the docker_gwbridge bridge with your custom settings. This example uses the subnet 10.11.0.0/16. For a full list of customizable options, seeBridge driver options.
  5. Initialize or join the swarm.

By default, all swarm traffic is sent over the same interface, including control and management traffic for maintaining the swarm itself and data traffic to and from the service containers.

You can separate this traffic by passing the --data-path-addr flag when initializing or joining the swarm. If there are multiple interfaces, --advertise-addr must be specified explicitly, and--data-path-addr defaults to --advertise-addr if not specified. Traffic about joining, leaving, and managing the swarm is sent over the--advertise-addr interface, and traffic among a service's containers is sent over the --data-path-addr interface. These flags can take an IP address or a network device name, such as eth0.

This example initializes a swarm with a separate --data-path-addr. It assumes that your Docker host has two different network interfaces: 10.0.0.1 should be used for control and management traffic and 192.168.0.1 should be used for traffic relating to services.

This example joins the swarm managed by host 192.168.99.100:2377 and sets the--advertise-addr flag to eth0 and the --data-path-addr flag to eth1.

Swarm services connected to the same overlay network effectively expose all ports to each other. For a port to be accessible outside of the service, that port must be published using the -p or --publish flag on docker service create or docker service update. Both the legacy colon-separated syntax and the newer comma-separated value syntax are supported. The longer syntax is preferred because it is somewhat self-documenting.

Flag value Description
-p 8080:80 or-p published=8080,target=80 Map TCP port 80 on the service to port 8080 on the routing mesh.
-p 8080:80/udp or-p published=8080,target=80,protocol=udp Map UDP port 80 on the service to port 8080 on the routing mesh.
-p 8080:80/tcp -p 8080:80/udp or-p published=8080,target=80,protocol=tcp -p published=8080,target=80,protocol=udp Map TCP port 80 on the service to TCP port 8080 on the routing mesh, and map UDP port 80 on the service to UDP port 8080 on the routing mesh.