| title | `cnitool` |
|---|---|
| description | README.md |
| date | 2020-06-10 |
| draft | false |
| toc | true |
| weight | 300 |
cnitool is a simple program that executes a CNI configuration. It will
add or remove an interface in an already-created network namespace.
NETCONFPATH: This environment variable needs to be set to a directory. It defaults to/etc/cni/net.d. Thecnitoolsearches for CNI configuration files in this directory with the extension*.confor*.json. It loads all the CNI configuration files in this directory and if it finds a CNI configuration with thenetwork namegiven to the cnitool it returns the corresponding CNI configuration, else it returnsnil.CNI_PATH: For a given CNI configurationcnitoolwill search for the corresponding CNI plugin in this path.
First, install cnitool:
go get github.com/containernetworking/cni
go install github.com/containernetworking/cni/cnitoolThen, check out and build the plugins. All commands should be run from this directory.
git clone https://github.com/containernetworking/plugins.git
cd plugins
./build_linux.sh
# or
./build_windows.shCreate a network configuration
echo '{"cniVersion":"0.4.0","name":"myptp","type":"ptp","ipMasq":true,"ipam":{"type":"host-local","subnet":"172.16.29.0/24","routes":[{"dst":"0.0.0.0/0"}]}}' | sudo tee /etc/cni/net.d/10-myptp.confCreate a network namespace. This will be called testing:
sudo ip netns add testingAdd the container to the network:
sudo CNI_PATH=./bin cnitool add myptp /var/run/netns/testingCheck whether the container's networking is as expected (ONLY for spec v0.4.0+):
sudo CNI_PATH=./bin cnitool check myptp /var/run/netns/testingTest that it works:
sudo ip -n testing addr
sudo ip netns exec testing ping -c 1 4.2.2.2And clean up:
sudo CNI_PATH=./bin cnitool del myptp /var/run/netns/testing
sudo ip netns del testing