-
Notifications
You must be signed in to change notification settings - Fork 0
/
vxlan2.sh
207 lines (169 loc) · 5.92 KB
/
vxlan2.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#
# based on flannel1.sh but instead of routing between container subnet and flannel subnet,
# build a vxlan enabled L2 container subnet across 2 hosts. There is no independent flannel IP subnet.
# so containers talk to each other directly on an overlay L2 subnet
#
set -x
echo "start"
# set up 3 namespaces with diff hostnames
unshare --net --uts -r /bin/bash &
export pid_netns1=$!
sudo nsenter -t $pid_netns1 -u hostname netns1
unshare --net --uts -r /bin/bash &
export pid_netns2=$!
sudo nsenter -t $pid_netns2 -u hostname netns2
unshare --net --uts -r /bin/bash &
export pid_netns3=$!
sudo nsenter -t $pid_netns3 -u hostname netns3
# set up bridge and veth links
sudo ip link add ens192 netns $pid_netns1 type veth peer name eth1 netns $pid_netns3
sudo ip link add ens192 netns $pid_netns2 type veth peer name eth2 netns $pid_netns3
sudo nsenter -t $pid_netns3 -u -n /bin/bash -c "
brctl addbr brx
ip link set brx up
"
# set up container-like netns in host 1 - container 4/5
unshare --net --uts -r /bin/bash &
export pid_netns4=$!
sudo nsenter -t $pid_netns4 -u hostname netns4
unshare --net --uts -r /bin/bash &
export pid_netns5=$!
sudo nsenter -t $pid_netns5 -u hostname netns5
# set up bridge and veth links in container 4/5
sudo ip link add eth0 netns $pid_netns4 type veth peer name veth14 netns $pid_netns1
sudo ip link add eth0 netns $pid_netns5 type veth peer name veth15 netns $pid_netns1
sudo nsenter -t $pid_netns1 -u -n /bin/bash -c "
brctl addbr cni0
ip link set cni0 up
"
export vxlan1_vtep_mac_4=$(sudo nsenter -t $pid_netns4 -u -n /bin/bash -c "ip link show eth0" | grep ether | awk '{print $2}')
echo $vxlan1_vtep_mac_4
export vxlan1_vtep_mac_5=$(sudo nsenter -t $pid_netns5 -u -n /bin/bash -c "ip link show eth0" | grep ether | awk '{print $2}')
echo $vxlan1_vtep_mac_5
# set up container-like netns in host 2 - container 6/7
unshare --net --uts -r /bin/bash &
export pid_netns6=$!
sudo nsenter -t $pid_netns6 -u hostname netns6
unshare --net --uts -r /bin/bash &
export pid_netns7=$!
sudo nsenter -t $pid_netns7 -u hostname netns7
# set up bridge and veth links in container 6/7
sudo ip link add eth0 netns $pid_netns6 type veth peer name veth26 netns $pid_netns2
sudo ip link add eth0 netns $pid_netns7 type veth peer name veth27 netns $pid_netns2
sudo nsenter -t $pid_netns2 -u -n /bin/bash -c "
brctl addbr cni0
ip link set cni0 up
"
export vxlan2_vtep_mac_6=$(sudo nsenter -t $pid_netns6 -u -n /bin/bash -c "ip link show eth0" | grep ether | awk '{print $2}')
echo $vxlan2_vtep_mac_6
export vxlan2_vtep_mac_7=$(sudo nsenter -t $pid_netns7 -u -n /bin/bash -c "ip link show eth0" | grep ether | awk '{print $2}')
echo $vxlan2_vtep_mac_7
# set up L3
sudo nsenter -t $pid_netns1 -u -n /bin/bash -c "
ip addr add 192.168.5.1/24 brd 192.168.5.255 dev ens192
ip link set ens192 up
ip link set lo up
ip a s
"
sudo nsenter -t $pid_netns2 -u -n /bin/bash -c "
ip addr add 192.168.5.2/24 brd 192.168.5.255 dev ens192
ip link set ens192 up
ip link set lo up
ip a s
"
# finish bridge setup
sudo nsenter -t $pid_netns3 -u -n /bin/bash -c "
ip link set eth1 up
ip link set eth2 up
brctl addif brx eth1
brctl addif brx eth2
"
# vxlan setup
sudo nsenter -t $pid_netns1 -u -n /bin/bash -c "
ip link add vxlan1 type vxlan id 100 dstport 8472 local 192.168.5.1 nolearning proxy
"
sudo nsenter -t $pid_netns2 -u -n /bin/bash -c "
ip link add vxlan2 type vxlan id 100 dstport 8472 local 192.168.5.2 nolearning proxy
"
sudo nsenter -t $pid_netns1 -u -n /bin/bash -c "
ip neigh add 10.245.0.6 lladdr $vxlan2_vtep_mac_6 dev vxlan1
ip neigh add 10.245.0.7 lladdr $vxlan2_vtep_mac_7 dev vxlan1
bridge fdb append $vxlan2_vtep_mac_6 dev vxlan1 dst 192.168.5.2
bridge fdb append $vxlan2_vtep_mac_7 dev vxlan1 dst 192.168.5.2
"
sudo nsenter -t $pid_netns1 -u -n /bin/bash -c "
ip l set vxlan1 up
ip -d link show vxlan1
"
sudo nsenter -t $pid_netns2 -u -n /bin/bash -c "
ip neigh add 10.245.0.4 lladdr $vxlan1_vtep_mac_4 dev vxlan2
ip neigh add 10.245.0.5 lladdr $vxlan1_vtep_mac_5 dev vxlan2
bridge fdb append $vxlan1_vtep_mac_4 dev vxlan2 dst 192.168.5.1
bridge fdb append $vxlan1_vtep_mac_5 dev vxlan2 dst 192.168.5.1
"
sudo nsenter -t $pid_netns2 -u -n /bin/bash -c "
ip l set vxlan2 up
ip -d l show vxlan2
"
# set up L3
sudo nsenter -t $pid_netns4 -u -n /bin/bash -c "
ip addr add 10.245.0.4/24 brd 10.245.0.255 dev eth0
ip link set eth0 up
ip link set lo up
ip a s
ip route add default via 10.245.0.1 dev eth0
"
sudo nsenter -t $pid_netns5 -u -n /bin/bash -c "
ip addr add 10.245.0.5/24 brd 10.245.0.255 dev eth0
ip link set eth0 up
ip link set lo up
ip a s
ip route add default via 10.245.0.1 dev eth0
"
# finish bridge setup
sudo nsenter -t $pid_netns1 -u -n /bin/bash -c "
ip link set veth14 up
ip link set veth15 up
brctl addif cni0 veth14
brctl addif cni0 veth15
brctl addif cni0 vxlan1
ip addr add 10.245.0.1/24 brd 10.245.0.255 dev cni0
ip link set cni0 up
ip link set lo up
ip a s
"
# set up L3
sudo nsenter -t $pid_netns6 -u -n /bin/bash -c "
ip addr add 10.245.0.6/24 brd 10.245.0.255 dev eth0
ip link set eth0 up
ip link set lo up
ip a s
ip route add default via 10.245.0.2 dev eth0
"
sudo nsenter -t $pid_netns7 -u -n /bin/bash -c "
ip addr add 10.245.0.7/24 brd 10.245.0.255 dev eth0
ip link set eth0 up
ip link set lo up
ip a s
ip route add default via 10.245.0.2 dev eth0
"
# finish bridge setup
sudo nsenter -t $pid_netns2 -u -n /bin/bash -c "
ip link set veth26 up
ip link set veth27 up
brctl addif cni0 veth26
brctl addif cni0 veth27
brctl addif cni0 vxlan2
ip addr add 10.245.0.2/24 brd 10.245.0.255 dev cni0
ip link set cni0 up
ip link set lo up
ip a s
"
set +x
echo "export pid_netns1=$pid_netns1"
echo "export pid_netns2=$pid_netns2"
echo "export pid_netns3=$pid_netns3"
echo "export pid_netns4=$pid_netns4"
echo "export pid_netns5=$pid_netns5"
echo "export pid_netns6=$pid_netns6"
echo "export pid_netns7=$pid_netns7"