# Network Chaos Testing
Pumba provides two complementary tools for network chaos testing:
- **netem** - Manipulates _outgoing_ traffic using Linux `tc` (traffic control): delay, packet loss, corruption, duplication, and rate limiting
- **iptables** - Manipulates _incoming_ traffic using Linux `iptables`: packet loss with random or nth-packet matching
By combining both, you can create realistic asymmetric network conditions. For container chaos commands (kill, stop, pause, etc.), see the [User Guide](guide.md).
## How It Works
Pumba injects network chaos by creating a helper container that shares the target container's network namespace. This helper container runs `tc` or `iptables` commands to manipulate traffic without modifying the target container itself.
```mermaid
graph LR
subgraph "Shared Network Namespace"
direction TB
App["Target Container
(your application)"]
NT["Nettools Container
(pumba helper)"]
end
Incoming["Incoming Traffic"] -->|"iptables rules
(packet loss)"| App
App -->|"tc/netem rules
(delay, loss, rate...)"| Outgoing["Outgoing Traffic"]
NT -.->|"NET_ADMIN capability"| App
style App fill:#d9edf7,stroke:#31708f
style NT fill:#fcf8e3,stroke:#8a6d3b
style Incoming fill:#f2dede,stroke:#d9534f
style Outgoing fill:#dff0d8,stroke:#5cb85c
```
The helper container is created with the `NET_ADMIN` capability and attached to the target's network stack, so `tc` and `iptables` rules apply directly to the target's traffic.
## Preview without injecting
```bash
pumba --dry-run netem --duration 30s --target 10.0.0.1 delay --time 100 web
pumba --dry-run --run-id preview-1 -o ndjson iptables --duration 30s --source 10.0.0.1 loss --probability 0.2 web
```
Netem/iptables dry-runs print the exact apply/revert commands and sidecar image
(or direct-exec mode) without pulling images, starting sidecars, executing
probes or commands, or waiting for the duration. NDJSON carries the argv in
`target.result.plan` instead of text; see [Dry-run plans](agents.md#dry-run-plans).
Plans include the same ownership checks and iptables run comments as execution,
but do not run those checks: a plan is not proof the real injection will succeed.
Target selection still queries the runtime, and rootless Podman is unsupported.
## Network Tools Images
Pumba needs `tc` (from `iproute2`) and/or `iptables` to be available. Netem also uses a POSIX `sh` to inspect, apply, verify, and roll back its owned qdisc. You have two options:
1. Install a POSIX shell, `iproute2`, and `iptables` inside the target container; `awk` is also required there when using netem `--match-all`
2. Use the `--tc-image` or `--iptables-image` flags to specify a helper image (recommended). A custom netem helper image must include `sh`, `grep`, and `tc`, plus `awk` for `--match-all`; Docker/Podman also require `which` and `tail`, while containerd requires `sleep`.
### Recommended Images
| Image | Base | Includes |
| ------------------------------------------------- | ------ | ------------------------------------------------------ |
| `ghcr.io/alexei-led/pumba-alpine-nettools:latest` | Alpine | sh + grep + awk + which + tail + sleep + tc + iptables |
| `ghcr.io/alexei-led/pumba-debian-nettools:latest` | Debian | sh + grep + awk + which + tail + sleep + tc + iptables |
Both images are multi-architecture (`amd64` and `arm64`). Docker automatically pulls the correct image for your platform.
Since both images include tc _and_ iptables, you can use the same image for both `--tc-image` and `--iptables-image` flags.
`status` and `revert` inspect through `iptables --version` and the matching named
binary (`iptables-nft` or `iptables-legacy`), not the mutable default alias. A legacy
default also needs `iptables-nft`; an nft default needs `iptables-legacy` when it
reports legacy tables. Custom images and exec-mode targets must provide those
binaries. Both shipped images include them; incomplete coverage fails closed.
### Building Network Tools Images
Build locally or push to a registry:
```bash
# Build single-arch images for local testing
make build-local-nettools
# Build multi-architecture images locally (doesn't push)
make build-nettools-images
# Build and push to GitHub Container Registry
make push-nettools-images
```
Pushing to GHCR requires authentication:
```bash
export GITHUB_USERNAME=your-github-username
export GITHUB_TOKEN=your-personal-access-token
echo $GITHUB_TOKEN | docker login ghcr.io -u $GITHUB_USERNAME --password-stdin
make push-nettools-images
```
## Netem Commands
Network emulation (`netem`) manipulates **outgoing** traffic using Linux traffic control (`tc`). Pumba first verifies that the interface has its default root qdisc and refuses to alter a foreign or stale qdisc. If Pumba is killed with `SIGKILL` after setup, it cannot run cleanup; `pumba status` lists the Pumba-owned `504d:` root qdisc it left (see [Finding Leftovers](guide.md#finding-leftovers)), and you remove only that qdisc by hand.
qdiscs have no comment field, so a qdisc cannot be attributed to a run. `status` reports a `504d:` root qdisc as `pumba-owned` when its topology is what Pumba installs (a netem root, or a prio root with its sfq and netem bands), and as `suspicious` otherwise. Its run is always `null`.
All netem commands require a `--duration` flag and support these common options:
| Flag | Description | Default |
| ------------------------------- | ------------------------------------------------------------ | ------------------------------------------------- |
| `--duration`, `-d` | Emulation duration (must be shorter than `--interval`) | required |
| `--interface`, `-i` | Network interface to apply rules on | `eth0` |
| `--target`, `-t` | Target filter (repeatable); IPv4, CIDR, or container name/ID | all |
| `--egress-port`, `egressPort` | Egress (source) port filter (comma-separated) | all |
| `--ingress-port`, `ingressPort` | Ingress (destination) port filter (comma-separated) | all |
| `--tc-image` | Docker image with `tc` tool | `ghcr.io/alexei-led/pumba-alpine-nettools:latest` |
| `--pull-image` | Force pull the tc-image | `true` |
Use `--match-all` on the `netem` or `iptables` parent command to require all configured selector dimensions (for example, source/destination address and source/destination port) to match. Values within one dimension are alternatives; dimensions are combined with AND. Without this flag, the existing OR behavior is unchanged. Empty dimensions remain unrestricted. The compiler bounds expansion at 256 combinations and each rendered script at 64 KiB; a request over either limit fails before injection (including dry-run).
Run `pumba netem --help` for the full list of options.
`--target` accepts an IPv4 address, CIDR block, or a running container's name
or ID — mix and match, and repeat the flag for multiple values. A container
name/ID is resolved on every command run, including each interval run. If the
container is attached to more than one network, every IPv4 address is added as
a separate filter, so traffic to/from any of them is affected. IPv6 targets
fail explicitly because netem's current filter planner emits IPv4 rules only.
Resolution requires an unambiguous match — a name or ID prefix
that matches more than one running container is a hard error rather than an
arbitrary pick — and a value that is neither a valid IP/CIDR nor a
resolvable container name/ID also fails with a clear error instead of
silently matching nothing.
```bash
# Delay only traffic to/from container "service-b" (resolved to its IP)
pumba netem --duration 10m --target service-b delay --time 1000 service-a
```
### delay
Add latency to outgoing packets.
```bash
# Add 3 seconds delay to mydb for 5 minutes
pumba netem --duration 5m delay --time 3000 mydb
# Add 3000ms ± 30ms delay with 20% correlation on eth1
pumba netem --duration 5m --interface eth1 delay \
--time 3000 --jitter 30 --correlation 20 "re2:^test"
# Normal distribution delay on a random container
pumba --random netem --duration 5m \
delay --time 3000 --jitter 40 --distribution normal \
container1 container2 container3
```
Options: `--time` (ms), `--jitter` (ms), `--correlation` (%), `--distribution` (uniform|normal|pareto|paretonormal).
### loss
Drop outgoing packets based on independent (Bernoulli) probability.
```bash
# Drop 5% of outgoing packets from mydb for 5 minutes
pumba netem --duration 5m loss --percent 5 mydb
```
Options: `--percent` (0-100), `--correlation` (%).
### loss-state
Drop outgoing packets based on a 4-state Markov model, useful for simulating bursty loss patterns.
The four states are:
1. Packet received successfully
2. Packet received within a burst
3. Packet lost within a burst
4. Isolated packet lost within a gap
```bash
# Simulate bursty packet loss
pumba netem --duration 5m loss-state --p13 5 --p31 90 mydb
```
Options: `--p13`, `--p31`, `--p32`, `--p23`, `--p14` (transition probabilities between states).
See: [Burst Loss Models](http://www.voiptroubleshooter.com/indepth/burstloss.html)
### loss-gemodel
Drop outgoing packets using the Gilbert-Elliot loss model, which models a channel alternating between "good" and "bad" states.
```bash
pumba netem --duration 5m loss-gemodel --pg 5 --pb 90 mydb
```
Options: `--pg` (transition probability into the bad state), `--pb` (transition probability into the good state), `--one-h` (loss probability in bad state), `--one-k` (loss probability in good state).
### duplicate
Duplicate outgoing packets based on independent probability.
```bash
pumba netem --duration 5m duplicate --percent 10 mydb
```
Options: `--percent` (0-100), `--correlation` (%).
### corrupt
Corrupt outgoing packets based on independent probability.
```bash
# Corrupt 10% of outgoing packets from mydb for 5 minutes
pumba netem --duration 5m corrupt --percent 10 mydb
```
Options: `--percent` (0-100), `--correlation` (%).
### rate
Limit outgoing bandwidth.
```bash
# Limit egress to 100kbit for mydb
pumba netem --duration 5m rate --rate 100kbit mydb
# Limit to 1mbit with packet overhead
pumba netem --duration 10m rate --rate 1mbit --packetoverhead 20 mydb
```
Options: `--rate` (e.g., `100kbit`, `1mbit`), `--packetoverhead` (bytes), `--cellsize` (bytes), `--celloverhead` (bytes).
### combine
Apply two or more effects in one owned netem qdisc. Enable each effect with its
switch, then set its namespaced options. Put `--` before container selectors so
they cannot be confused with effect flags.
```bash
# 100ms delay with 20% packet loss
pumba netem --duration 5m combine \
--delay --delay-time 100 \
--loss --loss-percent 20 \
-- mydb
# Delay, corruption, duplication, and a 1mbit rate limit
pumba netem --duration 10m combine \
--delay --delay-time 100 --delay-jitter 20 \
--corrupt --corrupt-percent 5 \
--duplicate --duplicate-percent 2 \
--rate --rate-value 1mbit \
-- myapp
```
Supported switches and option prefixes:
| Switch | Options |
| ------------- | ------------------------------------------------------------------------------------ |
| `--delay` | `--delay-time`, `--delay-jitter`, `--delay-correlation`, `--delay-distribution` |
| `--loss` | `--loss-percent`, `--loss-correlation` |
| `--corrupt` | `--corrupt-percent`, `--corrupt-correlation` |
| `--duplicate` | `--duplicate-percent`, `--duplicate-correlation` |
| `--rate` | `--rate-value`, `--rate-packet-overhead`, `--rate-cell-size`, `--rate-cell-overhead` |
`combine` requires at least two effect switches. Existing single-effect commands
remain unchanged. Stateful loss models (`loss-state` and `loss-gemodel`) are not
part of `combine`; use them as standalone commands.
## IPTables Commands
The `iptables` command manipulates **incoming** traffic by adding packet filtering rules. All iptables commands support these common options:
| Flag | Description | Default |
| ------------------------- | ----------------------------------------- | ------------------------------------------------- |
| `--duration`, `-d` | Emulation duration | required |
| `--interface`, `-i` | Network interface | `eth0` |
| `--protocol`, `-p` | Protocol filter (any, tcp, udp, icmp) | `any` |
| `--source`, `--src` | Source IP filter (supports CIDR) | all |
| `--destination`, `--dest` | Destination IP filter (supports CIDR) | all |
| `--src-port`, `--sport` | Source port filter (comma-separated) | all |
| `--dst-port`, `--dport` | Destination port filter (comma-separated) | all |
| `--iptables-image` | Docker image with `iptables` tool | `ghcr.io/alexei-led/pumba-alpine-nettools:latest` |
| `--pull-image` | Force pull the image | `true` |
Run `pumba iptables --help` for the full list of options.
Every rule carries `-m comment --comment pumba:` (the global `--run-id`, or a generated one), in both the add and the delete spec, so `pumba status` can find rules a crashed run left behind. This needs the `xt_comment` kernel module (iptables-legacy) or the nft comment match (iptables-nft). Without it the rule is not installed and the error names `xt_comment`. Rules from Pumba 1.4 and earlier have no comment and are invisible to `status`.
### loss
Drop incoming packets using either random probability or every-nth-packet matching.
#### Random mode (default)
```bash
# Drop 20% of incoming packets for 5 minutes
pumba iptables --duration 5m loss --probability 0.2 web
# Drop 15% of incoming ICMP packets for 5 minutes
pumba iptables --duration 5m --protocol icmp loss --probability 0.15 "re2:database"
```
#### Nth mode
```bash
# Drop every 5th packet from a specific source on port 8080 for 5 minutes
pumba iptables --duration 5m --protocol tcp --source 192.168.1.100 --dst-port 8080 \
loss --mode nth --every 5 api
```
#### Complex filtering
```bash
# Drop 25% of TCP traffic to port 443 from a specific subnet, for 30 seconds
pumba iptables --duration 30s --protocol tcp --source 10.0.0.0/24 --dst-port 443 \
loss --probability 0.25 mycontainer
```
Options: `--mode` (random|nth), `--probability` (0.0-1.0), `--every` (nth mode), `--packet` (nth initial counter).
## Advanced Scenarios
Combining netem (outgoing) and iptables (incoming) creates realistic network conditions. Run commands concurrently using `&` in your shell.
### Asymmetric Network Conditions
Simulate different upload and download quality:
```bash
# Slow uploads: 500ms delay on outgoing traffic
pumba netem --tc-image ghcr.io/alexei-led/pumba-alpine-nettools:latest \
--duration 5m delay --time 500 --jitter 50 myapp &
# Unreliable downloads: 10% loss on incoming traffic
pumba iptables --iptables-image ghcr.io/alexei-led/pumba-alpine-nettools:latest \
--duration 5m loss --probability 0.1 myapp &
```
### Combined Network Degradation
Test bandwidth limits combined with packet loss:
```bash
# Limit outgoing bandwidth to 1Mbit/s
pumba netem --tc-image ghcr.io/alexei-led/pumba-alpine-nettools:latest \
--duration 10m rate --rate 1mbit myapp &
# 5% loss on incoming traffic
pumba iptables --iptables-image ghcr.io/alexei-led/pumba-alpine-nettools:latest \
--duration 10m loss --probability 0.05 myapp &
```
### Protocol-Specific Degradation
Target specific protocols differently:
```bash
# Add latency to all outgoing traffic
pumba netem --tc-image ghcr.io/alexei-led/pumba-alpine-nettools:latest \
--duration 15m delay --time 200 myapp &
# Drop 20% of incoming UDP packets only
pumba iptables --iptables-image ghcr.io/alexei-led/pumba-alpine-nettools:latest \
--duration 15m --protocol udp loss --probability 0.2 myapp &
```
### Microservices Resilience
Test how services handle degraded inter-service communication:
```bash
# High latency between service-a and service-b
# --target accepts an IP/CIDR or, as here, a container name/ID to resolve
pumba netem --tc-image ghcr.io/alexei-led/pumba-alpine-nettools:latest \
--target service-b --duration 10m \
delay --time 1000 --jitter 200 service-a &
# Packet loss from service-c to service-b
pumba iptables --iptables-image ghcr.io/alexei-led/pumba-alpine-nettools:latest \
--source service-c-ip --duration 10m \
loss --probability 0.15 service-b &
```
### Port-Specific Degradation
Target specific service ports:
```bash
# Corrupt outgoing database traffic (port 5432)
pumba netem --tc-image ghcr.io/alexei-led/pumba-alpine-nettools:latest \
--target db-server-ip --egress-port 5432 --duration 5m \
corrupt --percent 5 myapp &
# Drop 10% of incoming HTTP traffic (port 80)
pumba iptables --iptables-image ghcr.io/alexei-led/pumba-alpine-nettools:latest \
--dst-port 80 --duration 5m \
loss --probability 0.1 myapp &
```
## Best Practices
- Use the same nettools image for both `--tc-image` and `--iptables-image` for consistency
- Always set `--duration` so chaos stops automatically
- Start with simple scenarios and increase complexity gradually
- Monitor your application during chaos tests to understand failure modes
- Avoid overlapping or conflicting rules on the same container
- Use `--dry-run` to verify targeting before running destructive tests
## Troubleshooting
- Verify the target container is running before starting chaos
- Ensure the nettools image is accessible (try `docker pull` manually)
- Check that you have permissions to create containers with `NET_ADMIN` capability
- On minikube, `pumba netem` commands won't work because the `sch_netem` kernel module is missing
- `iptables comment match (xt_comment) unavailable`: load the module on the host (`modprobe xt_comment`)
- After a crash or `kill -9`, run `pumba status --check ` to see what is left
- When using Kubernetes, ensure Pumba's service account has sufficient RBAC permissions
## Further Reading
- [User Guide](guide.md) - Container chaos commands and general usage
- [Stress Testing](stress-testing.md) - CPU, memory, and I/O stress
- [Deployment](deployment.md) - Docker, Kubernetes, and OpenShift
- [Example script](../examples/pumba_combined.sh) - Complete combined chaos testing script