forked from antrea-io/antrea
/
kind-setup.sh
executable file
·337 lines (304 loc) · 9.18 KB
/
kind-setup.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env bash
# Copyright 2020 Antrea Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# The script creates and deletes kind testbeds. Kind testbeds may be created with
# docker images preloaded, antrea-cni preloaded, antrea-cni's encapsulation mode,
# and docker bridge network connecting to worker Node.
CLUSTER_NAME=""
ANTREA_IMAGE="antrea/antrea-ubuntu:latest"
IMAGES=$ANTREA_IMAGE
ANTREA_CNI=true
POD_CIDR="10.10.0.0/16"
NUM_WORKERS=2
SUBNETS=""
ENCAP_MODE=""
set -eo pipefail
function echoerr {
>&2 echo "$@"
}
_usage="
Usage: $0 create CLUSTER_NAME [--pod-cidr POD_CIDR] [--antrea-cni true|false ] [--num-workers NUM_WORKERS] [--images IMAGES] [--subnets SUBNETS]
destroy CLUSTER_NAME
modify-node NODE_NAME
help
where:
create: create a kind cluster with name CLUSTER_NAME
destroy: delete a kind cluster with name CLUSTER_NAME
modify-node: modify kind node with name NODE_NAME
--pod-cidr: specifies pod cidr used in kind cluster, default is $POD_CIDR
--encap-mode: inter-node pod traffic encap mode, default is encap
--antrea-cni: specifies install Antrea CNI in kind cluster, default is true.
--num-workers: specifies number of worker nodes in kind cluster, default is $NUM_WORKERS
--images: specifies images loaded to kind cluster, default is $IMAGES
--subnets: a subnet creates a separate docker bridge network with assigned subnet that worker nodes may connect to. Default is empty all worker
Node connected to docker0 bridge network
"
function print_usage {
echoerr "$_usage"
}
function print_help {
echoerr "Try '$0 help' for more information."
}
function get_encap_mode {
if [[ $ENCAP_MODE == "" ]]; then
echo ""
return
fi
echo "--encap-mode $ENCAP_MODE"
}
function modify {
node="$1"
peerIdx=$(docker exec "$node" ip link | grep eth0 | awk -F[@:] '{ print $3 }' | cut -c 3-)
peerName=$(docker run --net=host antrea/ethtool:latest ip link | grep ^"$peerIdx": | awk -F[:@] '{ print $2 }' | cut -c 2-)
echo "Disabling TX checksum offload for node $node ($peerName)"
docker run --net=host --privileged antrea/ethtool:latest ethtool -K "$peerName" tx off
# Workaround for https://github.com/vmware-tanzu/antrea/issues/324
docker exec "$node" sysctl -w net.ipv4.tcp_retries2=4
}
function configure_networks {
echo "Configuring networks"
networks=$(docker network ls -f name=$CLUSTER_NAME --format '{{.Name}}')
networks="$(echo $networks)"
if [[ -z $SUBNETS ]] && [[ -z $networks ]]; then
echo "Using default docker bridges"
return
fi
# Inject allow all iptables to preempt docker bridge isolation rules
if [[ ! -z $SUBNETS ]]; then
set +e
sudo iptables -C DOCKER-USER -j ACCEPT > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
sudo iptables -I DOCKER-USER -j ACCEPT
fi
set -e
fi
# remove old networks
nodes="$(kind get nodes --name $CLUSTER_NAME | grep worker)"
node_cnt=$(kind get nodes --name $CLUSTER_NAME | grep worker | wc -l)
nodes=$(echo $nodes)
networks+=" bridge"
echo "removing worker nodes $nodes from networks $networks"
for n in $networks; do
rm_nodes=$(docker network inspect $n --format '{{range $i, $conf:=.Containers}}{{$conf.Name}} {{end}}')
for rn in $rm_nodes; do
if [[ $nodes =~ $rn ]]; then
docker network disconnect $n $rn > /dev/null 2>&1
echo "disconnected worker $rn from network $n"
fi
done
if [[ $n != "bridge" ]]; then
docker network rm $n > /dev/null 2>&1
echo "removed network $n"
fi
done
# create new bridge network per subnet
i=0
networks=()
for s in $SUBNETS; do
network=$CLUSTER_NAME-$i
echo "creating network $network with $s"
docker network create -d bridge --subnet $s $network >/dev/null 2>&1
networks+=($network)
i=$((i+1))
done
num_networks=${#networks[@]}
if [[ $num_networks -eq 0 ]]; then
networks+=("bridge")
num_networks=$((num_networks+1))
fi
i=0
for node in $nodes; do
network=${networks[i]}
docker network connect $network $node >/dev/null 2>&1
echo "connected worker $node to network $network"
node_ip=$(docker inspect $node --format '{{range $i, $conf:=.NetworkSettings.Networks}}{{$conf.IPAddress}}{{end}}')
# reset network
docker exec -t $node ip link set eth1 down
docker exec -t $node ip link set eth1 name eth0
docker exec -t $node ip link set eth0 up
gateway=$(echo "${node_ip/%?/1}")
docker exec -t $node ip route add default via $gateway
echo "node $node is ready with ip change to $node_ip with gw $gateway"
# change kubelet config before reset network
docker exec -t $node sed -i "s/node-ip=.*/node-ip=$node_ip/g" /var/lib/kubelet/kubeadm-flags.env
docker exec -t $node pkill kubelet
docker exec -t $node pkill kube-proxy
i=$((i+1))
if [[ $i -ge $num_networks ]]; then
i=0
fi
done
for node in $nodes; do
node_ip=$(docker inspect $node --format '{{range $i, $conf:=.NetworkSettings.Networks}}{{$conf.IPAddress}}{{end}}')
while true; do
tmp_ip=$(kubectl describe node $node | grep InternalIP)
if [[ "$tmp_ip" == *"$node_ip"* ]]; then
break
fi
echo "current ip $tmp_ip, wait for new node ip $node_ip"
sleep 2
done
done
}
function delete_networks {
networks=$(docker network ls -f name=$CLUSTER_NAME --format '{{.Name}}')
networks="$(echo $networks)"
if [[ ! -z $networks ]]; then
docker network rm $networks > /dev/null 2>&1
echo "deleted networks $networks"
fi
}
function load_images {
echo "load images"
set +e
for img in $IMAGES; do
docker image inspect $img > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echoerr "docker image $img not found"
continue
fi
kind load docker-image $img --name $CLUSTER_NAME > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echoerr "docker image $img failed to load"
continue
fi
echo "loaded image $img"
done
set -e
}
function create {
if [[ -z $CLUSTER_NAME ]]; then
echoerr "cluster-name not provided"
exit 1
fi
# Having a simple validation check for now.
# TODO: Making this comprehensive check confirming with rfc1035/rfc1123
if [[ "$CLUSTER_NAME" =~ [^a-z0-9-] ]]; then
echoerr "Invalid string. Conform to rfc1035/rfc1123"
exit 1
fi
set +e
kind get clusters | grep $CLUSTER_NAME > /dev/null 2>&1
if [[ $? -eq 0 ]]; then
echoerr "cluster $CLUSTER_NAME already created"
exit 0
fi
set -e
config_file="/tmp/kind.yml"
cat <<EOF > $config_file
kind: Cluster
apiVersion: kind.sigs.k8s.io/v1alpha3
networking:
disableDefaultCNI: true
podSubnet: $POD_CIDR
nodes:
- role: control-plane
EOF
for i in $(seq 1 $NUM_WORKERS); do
echo -e "- role: worker" >> $config_file
done
kind create cluster --name $CLUSTER_NAME --config $config_file
# force coredns to run on control-plane node because it
# is attached to docker0 bridge and uses host dns.
# Worker Node may be configured to attach to custom bridges
# which use dockerd as dns, causing coredns to detect
# dns loop and crash
patch=$(cat <<EOF
spec:
template:
spec:
nodeSelector:
kubernetes.io/hostname: $CLUSTER_NAME-control-plane
EOF
)
kubectl patch deployment coredns -p "$patch" -n kube-system
configure_networks
load_images
nodes="$(kind get nodes --name $CLUSTER_NAME)"
nodes="$(echo $nodes)"
for node in $nodes; do
modify $node
done
if [[ $ANTREA_CNI == true ]]; then
cmd=$(dirname $0)
cmd+="/../../hack/generate-manifest.sh"
echo "$cmd --kind $(get_encap_mode) | kubectl apply --context kind-$CLUSTER_NAME -f -"
eval "$cmd --kind $(get_encap_mode) | kubectl apply --context kind-$CLUSTER_NAME -f -"
fi
# wait for cluster info
while [[ -z $(kubectl cluster-info dump | grep cluster-cidr) ]]; do
echo "waiting for k8s cluster readying"
sleep 2
done
}
function destroy {
if [[ -z $CLUSTER_NAME ]]; then
echoerr "cluster-name not provided"
exit 1
fi
kind delete cluster --name $CLUSTER_NAME
delete_networks
}
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
create)
CLUSTER_NAME="$2"
shift 2
;;
destroy)
CLUSTER_NAME="$2"
destroy
exit 0
;;
modify-node)
modify "$2"
exit 0
;;
--pod-cidr)
POD_CIDR="$2"
shift 2
;;
--encap-mode)
ENCAP_MODE="$2"
shift 2
;;
--subnets)
SUBNETS="$2"
shift 2
;;
--images)
IMAGES="$2"
shift 2
;;
--antrea-cni)
ANTREA_CNI="$2"
shift 2
;;
--num-workers)
NUM_WORKERS="$2"
shift 2
;;
help)
print_usage
exit 0
;;
*) # unknown option
echoerr "Unknown option $1"
exit 1
;;
esac
done
create