7.1 Learning overlay network
The overlay network is a distributed Docker network that lets containers running on different hosts interact with each other. It’s often used in the context of container orchestration, like Docker Swarm or Kubernetes, to enable communication between containers running on different nodes. In this lecture, we’ll explore how to create and use an overlay network, along with some examples of its application.
The overlay network creates a virtual distributed network on top of existing ones, uniting multiple hosts in a single network space. This allows containers to interact as if they were on the same local network, no matter where the hosts are physically located.
Advantages of using an overlay network:
- Communication between containers on different hosts: enables network interaction between containers running on various nodes.
- Isolation: allows you to create isolated network spaces for different applications.
- Scalability: supports the dynamic addition and removal of nodes with no interruptions.
7.2 Creating an overlay network
To create an overlay network, you first need to initialize the Docker Swarm cluster. This will create a management node (manager) and allow you to set up distributed networks.
Step 1: Initialize Docker Swarm
Run the command on the host that will act as the cluster manager. This command initializes the Swarm cluster and outputs a command to join other nodes to the cluster.
docker swarm init
Step 2: Add worker nodes to the cluster
On other hosts, run the command provided by docker swarm init to connect them to the cluster. Example command:
docker swarm join --token <SWARM_TOKEN> <MANAGER_IP>:2377
Step 3: Create the overlay network
Once the cluster is initialized, create the overlay network using the command:
docker network create -d overlay my_overlay_network
Step 4: Verify the network
To confirm that the network was created successfully, run the docker network ls command, which will list all the networks, including the new overlay network:
docker network ls
Example output:
NETWORK ID NAME DRIVER SCOPE
a1b2c3d4e5 my_overlay_network overlay swarm
Now the my_overlay_network is ready for use with distributed applications.
7.3 Using the overlay Network
Now that the network is created, you can run containers and services connected to this network.
Example of using the overlay network for services
In Docker Swarm, you can use services to manage containers. Let’s create a few services that will be connected to the overlay network.
Step 1: Creating a Web Server Service
docker service create --name webserver --network my_overlay_network -p 8080:80 nginx
This command creates a webserver service connected to the my_overlay_network network. The web server will be accessible at http://localhost:8080.
Step 2: Creating a Database Service
docker service create --name database --network my_overlay_network -e POSTGRES_PASSWORD=mysecretpassword postgres
Here, a database service is created, connected to the same network, with the environment variable POSTGRES_PASSWORD set.
Step 3: Checking Connectivity Between Services
Run a temporary container to test connectivity between services:
docker run -it --network my_overlay_network busybox
Inside the container, use ping commands to check connectivity with the web server and database:
ping webserver
ping database
If configured correctly, the ping will show a successful connection, confirming that the containers can interact using hostnames.
Step 4: Viewing Service Status
Use the docker service ls command to check the status of the created services:
docker service ls
The expected output will display information about running services, their status, and the number of replicas.
Note: The overlay network allows containers on different hosts to communicate if they are connected to the same Swarm cluster.
7.4 Examples of using overlay network in Docker Compose
Docker Compose makes managing multi-container applications and networks easier. You can define an overlay network and use it for communication between containers in the docker-compose.yml file.
Example of a docker-compose.yml file:
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
networks:
- my_overlay_network
db:
image: postgres:latest
environment:
POSTGRES_PASSWORD: mysecretpassword
networks:
- my_overlay_network
networks:
my_overlay_network:
driver: overlay
attachable: true
Starting Docker Compose:
docker stack deploy -c docker-compose.yml mystack
This command will deploy the stack using the docker-compose.yml file and create services connected to the my_overlay_network.
Practical tips:
- Network monitoring: Use Docker Swarm monitoring tools like Docker Dashboard or Prometheus to track the network's status and container interactions.
- Scalability management: Overlay network supports dynamic addition and removal of nodes. Ensure proper configuration of network resources to scale your apps.
- Backup configurations: Regularly save network and stack configurations for quick recovery in case of failures.
GO TO FULL VERSION