Introduction to Weave Networking
Weave Net is a powerful, resilient, and easy-to-use container networking solution. It creates a virtual network that connects Docker containers across multiple hosts, enabling them to communicate seamlessly as if they were on the same local network. Weave Net automatically handles routing and network topology, making it an excellent choice for microservices architectures and distributed applications.
Why does Weave Net matter? In modern containerized environments, applications are often split across multiple servers. Traditional networking struggles to keep up with the dynamic nature of containers starting, stopping, and moving between hosts. Weave Net solves this by providing a flat, overlay network that abstracts away the underlying host infrastructure. It supports service discovery, built-in encryption, and requires minimal configuration, allowing developers to focus on application logic rather than network plumbing.
Prerequisites and Environment Setup
Before installing Weave Net, ensure your environment meets the following requirements:
- Two or more Linux hosts (e.g., Ubuntu, CentOS, or Debian).
- Docker installed and running on all hosts.
- Root or sudo privileges on each host.
- Open firewall ports: TCP 6783 and UDP 6783/6784 for Weave data and control plane traffic.
Installing Weave Net
Installing Weave Net is straightforward thanks to its self-contained installation script. You will need to perform these steps on all nodes that will participate in the Weave network.
Downloading the Weave Binary
Download the Weave script and make it executable on every host:
sudo curl -L git.io/weave -o /usr/local/bin/weave
sudo chmod +x /usr/local/bin/weave
Launching Weave on the First Node
On your primary node (e.g., Node A with IP 192.168.1.10), launch Weave. This command starts the Weave router and sets up the overlay network.
sudo weave launch
You can verify that the Weave containers are running by executing:
docker ps | grep weave
Joining Additional Nodes
To connect a second host (e.g., Node B with IP 192.168.1.11) to the Weave network, launch Weave on Node B and provide the IP address of the first node.
sudo weave launch 192.168.1.10
Weave will automatically establish a peer connection between Node A and Node B. The network is now ready to host containers.
Configuring Weave Net
Weave Net comes with sensible defaults, but it also offers robust configuration options for security and network management.
Network Encryption
If your nodes are communicating over an untrusted network (like the public internet), you should enable encryption. Weave uses NaCl-based encryption to secure traffic between peers. You can launch Weave with a password to enable this feature.
sudo weave launch --password YourSecurePassword123
When joining other nodes, you must supply the same password:
sudo weave launch --password YourSecurePassword123 192.168.1.10
IP Address Management (IPAM)
By default, Weave automatically assigns IP addresses to containers from a default subnet. However, in production environments, you often need to specify a custom IP range to avoid conflicts with existing infrastructure. You can define a custom IP allocation range during launch.
sudo weave launch --ipalloc-range 10.50.0.0/16
This ensures all containers attached to the Weave network will receive an IP address within the 10.50.0.0/16 block.
Deploying Applications with Weave
Once Weave is running, you can attach containers to the network. Weave provides a simple wrapper command, weave run, which is analogous to docker run but automatically attaches the container to the Weave network.
Starting a Container on Node A
On Node A, start a simple Nginx web server and let Weave assign it an IP address:
sudo weave run -d --name web-server nginx
Starting a Container on Node B
On Node B, start a busybox container and test the connectivity to the Nginx server using the container name. Weave provides built-in DNS resolution for container names.
sudo weave run -it --name client busybox /bin/sh
Inside the busybox container, use wget to fetch the default Nginx page:
wget -qO- http://web-server
If configured correctly, you will see the HTML output of the Nginx welcome page, proving that cross-host container communication is working flawlessly.
Best Practices for Weave Networking
- Always use IPAM: Rely on Weave's IP Address Management rather than assigning static IPs manually. This prevents IP conflicts and simplifies container orchestration.
- Secure your network: Always use the
--passwordflag in production environments, especially if your nodes are spread across different cloud providers or data centers. - Monitor with Weave Scope: Install Weave Scope, a companion tool, to get a visual, real-time map of your containers and microservices. It greatly simplifies troubleshooting.
- Keep peers updated: Ensure all nodes in the Weave network are running the same version of Weave Net to prevent protocol mismatches and unexpected behavior.
- Use Docker Compose integration: For complex applications, integrate Weave with Docker Compose by setting the
WEAVE_CIDRenvironment variable in yourdocker-compose.ymlfile.
Conclusion
Weave Net provides a robust and developer-friendly approach to container networking, bridging the gap between isolated hosts and creating a unified, flat network for your applications. By following this setup and configuration guide, you can quickly deploy a secure, encrypted overlay network that supports automatic IP allocation and built-in DNS. Whether you are running a small cluster of Docker hosts or scaling out a complex microservices architecture, Weave Net's simplicity and powerful feature set make it an excellent choice for managing container communications across distributed environments.