Skip to content

Commit d90fa5c

Browse files
committed
Merge pull request #37 from sachja/master
Use docker containers as hosts for testing netplugin
2 parents 13a3d50 + 976b00c commit d90fa5c

11 files changed

+200
-0
lines changed

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ demo: build
2424
clean-demo:
2525
vagrant destroy -f
2626

27+
start-dockerdemo:
28+
scripts/dockerhost/start-dockerhosts
29+
30+
clean-dockerdemo:
31+
scripts/dockerhost/cleanup-dockerhosts
32+
2733
unit-test: build
2834
CONTIV_HOST_GOBIN=$(HOST_GOBIN) CONTIV_HOST_GOROOT=$(HOST_GOROOT) ./scripts/unittests -vagrant
2935

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ In the examples directory [two_hosts_multiple_tenants.json](examples/two_hosts_m
112112
[two_hosts_multiple_tenants_mix_vlan_vxlan.json](examples/two_hosts_multiple_tenants_mix_vlan_vxlan.json) shows the creation of a multi-tenant
113113
(disjoint, overlapping) networks within a cluster.
114114

115+
####Trying the multi-host tests on a single machine using docker as hosts
116+
If you cannot launch VM on your host, especially if your host is itself a VM, one can test the multi-host network by simulating hosts using docker containers. Please see [docs/Dockerhost.md](docs/Dockerhost.md) for instructions.
117+
115118
####Auto-allocation of IP addresses
116119
The plugin can automatically manage the IP address pools and assign an appropriate IP address based on the subnet that was associated with the network. However this doesn't take away the flexibility to keep a specific IP address of a container, which can always be specified as shown earlier. To automatically allocate the IP address, just avoid specifying the IP address during endpoint creation or endpoint description
117120

docs/Dockerhost.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
The multi node container networking can be tested on a single VM by running docker inside docker.
2+
https://blog.docker.com/2013/09/docker-can-now-run-within-docker/
3+
4+
The outside docker containers act like physical hosts in our test and are connected using a standard linux bridge. Inside each "host container" we run a namespaced instance of docker, OVS , etcd and netplugin instance. One can now launch containers from within each "host containers" and use netplugin networking to connect them.
5+
6+
Prerequisites
7+
-------------
8+
The following needs to be installed on the host machine
9+
1. Docker
10+
2. nsenter (Pls see https://github.com/jpetazzo/nsenter)
11+
3. Bridge utils
12+
```
13+
apt-get install bridge-utils
14+
````
15+
16+
Step to launch docker hosts are :
17+
--------------------------------
18+
```
19+
make build
20+
CONTINV_NODES=2 make start-dockerdemo
21+
```
22+
23+
This will start CONTIV_NODES number of containers with docker image called ubuntu_netplugin which is just ubuntu image with docker, etcd and ovs installed.
24+
25+
Now start a shell within any of the "host containers" using following convenience wrapper around nsenter :
26+
```
27+
scripts/dockerhost/docker-sh netplugin-node<x>
28+
```
29+
30+
Start netplugin, post netplugin config and launch containers inside the "host containers" the same way you do on VMs.
31+
Note : currently the demo is working only if config is posted before containers are started .. need to debug why the reverse is not working.
32+
33+
To cleanup all the docker hosts and the virtual interfaces created do
34+
```
35+
make cleanup-dockerdemo
36+
```
37+
38+
Example for testing TwoHostMultiVlan you can do :
39+
40+
1. Launch the two host containers
41+
42+
```
43+
export CONTIV_NODES=2
44+
make start-dockerdemo
45+
```
46+
47+
2. Load the netplugin configuration
48+
```
49+
docker-sh netplugin-node1
50+
/netplugin/bin/netdcli -cfg /netplugin/examples/two_hosts_multiple_vlans_nets.json
51+
```
52+
53+
3. Launch container1 on host1
54+
55+
```
56+
docker-sh netplugin-node1
57+
docker run -it --name=myContainer1 --hostname=myContainer1 ubuntu /bin/bash
58+
```
59+
60+
4. Launch container3 on host2
61+
62+
```
63+
docker-sh netplugin-node2
64+
docker run -it --name=myContainer3 --hostname=myContainer1 ubuntu /bin/bash
65+
```
66+
67+
5. Test connectivity between the containers using ping. Go to the shell for container1
68+
```
69+
root@myContainer1:/# ping -c3 11.1.2.2
70+
PING 11.1.2.2 (11.1.2.2) 56(84) bytes of data.
71+
64 bytes from 11.1.2.2: icmp_seq=1 ttl=64 time=3.15 ms
72+
64 bytes from 11.1.2.2: icmp_seq=2 ttl=64 time=1.36 ms
73+
```
74+

scripts/dockerhost/Dockerfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM ubuntu
2+
MAINTAINER Sachin Jain <[email protected]>
3+
4+
RUN apt-get update && apt-get install -y curl
5+
6+
RUN cd /tmp && \
7+
curl -L https://github.com/coreos/etcd/releases/download/v2.0.0/etcd-v2.0.0-linux-amd64.tar.gz -o etcd-v2.0.0-linux-amd64.tar.gz && \
8+
tar -xzf etcd-v2.0.0-linux-amd64.tar.gz && \
9+
cd /usr/bin && \
10+
ln -s /tmp/etcd-v2.0.0-linux-amd64/etcd && \
11+
ln -s /tmp/etcd-v2.0.0-linux-amd64/etcdctl
12+
13+
RUN curl -sSL https://get.docker.com/ubuntu/ | sh > /dev/null
14+
15+
RUN apt-get install -y openvswitch-switch
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
#!/bin/bash
3+
4+
num_nodes=1
5+
if [ -n "$CONTIV_NODES" ];
6+
then
7+
num_nodes=$CONTIV_NODES
8+
fi
9+
echo $num_nodes
10+
11+
for i in `seq 1 $num_nodes`;
12+
do
13+
hostname="netplugin-node$i"
14+
echo "Cleaning $hostname"
15+
sudo docker exec $hostname service docker stop
16+
sudo docker rm -f $hostname
17+
sudo ip link delete $i-ext
18+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
for i in `seq 9 20`;
3+
do
4+
sudo /bin/mknod -m640 /dev/loop$i b 7 $i
5+
sudo /bin/chown root:disk /dev/loop$i
6+
done

scripts/dockerhost/docker-ip

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' "$@"

scripts/dockerhost/docker-pid

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
sudo docker inspect --format '{{ .State.Pid }}' "$@"

scripts/dockerhost/docker-sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
scriptdir=`dirname "$BASH_SOURCE"`
3+
sudo nsenter --target $($scriptdir/docker-pid $1) --mount --uts --ipc --net --pid

scripts/dockerhost/start-dockerhosts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
3+
scriptdir=`dirname "$BASH_SOURCE"`
4+
echo $scriptdir
5+
6+
# Create a linux bridge between containers
7+
brctl addbr br-em1
8+
ip link set br-em1 up
9+
10+
num_nodes=1
11+
if [ -n "$CONTIV_NODES" ];
12+
then
13+
num_nodes=$CONTIV_NODES
14+
fi
15+
echo "Num nodes = "$num_nodes
16+
17+
netplugin_path=$(pwd)
18+
19+
# Create the docker image for hosts
20+
sudo docker build -t ubuntu_netplugin $scriptdir
21+
22+
cluster=""
23+
first="true"
24+
for i in `seq 1 $num_nodes`;
25+
do
26+
hostname="netplugin-node$i"
27+
echo "Starting $hostname"
28+
sudo docker run -d -i -t --name $hostname --privileged -v /var/lib/docker -v $netplugin_path:/netplugin ubuntu_netplugin bash -c "/netplugin/scripts/dockerhost/start-service.sh & bash"
29+
sudo nsenter -t $($scriptdir/docker-pid $hostname) -n hostname $hostname
30+
sudo ip link add $i-int type veth peer name $i-ext
31+
sudo brctl addif br-em1 $i-ext
32+
sudo ip link set netns $($scriptdir/docker-pid $hostname) dev $i-int
33+
sudo nsenter -t $($scriptdir/docker-pid $hostname) -n ip link set dev $i-int name eth2
34+
sudo nsenter -t $($scriptdir/docker-pid $hostname) -n ip link set eth2 up
35+
if [ $i -gt "1" ]; then
36+
cluster=$cluster",";
37+
fi
38+
sudo docker exec $hostname service docker start
39+
addr=$($scriptdir/docker-ip $hostname)
40+
cluster=$cluster$hostname"=http://"$addr":2380"
41+
first="false"
42+
done
43+
44+
echo $cluster
45+
46+
for i in `seq 1 $num_nodes`;
47+
do
48+
hostname="netplugin-node$i"
49+
echo "Starting etcd on $hostname"
50+
addr=$($scriptdir/docker-ip $hostname)
51+
sudo docker exec $hostname etcd -name $hostname -data-dir /opt/etcd -listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 -advertise-client-urls http://$addr:2379,http://$addr:4001 -initial-advertise-peer-urls http://$addr:2380 -listen-peer-urls http://$addr:2380 -initial-cluster $cluster -initial-cluster-state new &
52+
done
53+
54+
for i in `seq 1 $num_nodes`;
55+
do
56+
hostname="netplugin-node$i"
57+
echo "Starting netplugin on $hostname"
58+
sudo docker exec $hostname /netplugin/bin/netplugin -host-label host$i &
59+
done
60+

scripts/dockerhost/start-service.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
/etc/init.d/openvswitch-switch restart
3+
ovs-vsctl set-manager tcp:127.0.0.1:6640
4+
ovs-vsctl set-manager ptcp:6640
5+
while true
6+
do
7+
echo service is running >> service.log
8+
sleep 10
9+
done

0 commit comments

Comments
 (0)