-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup-kind-multicluster.sh
executable file
·221 lines (192 loc) · 5.67 KB
/
setup-kind-multicluster.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
#!/usr/bin/env bash
#
# This script requires the following to be installed and available on your path:
#
# - jq
# - yq
# - kustomize
# - kind
set -e
registry_name='kind-registry'
registry_port='5000'
num_clusters=1
cluster_names="kind"
kind_node_version="v1.21.2"
kind_worker_nodes=3
while test $# -gt 0; do
case "$1" in
--clusters)
shift
if test $# -gt 0; then
export num_clusters=$1
else
echo "no num clusters specified"
exit 1
fi
shift
;;
--cluster-names)
shift
if test $# -gt 0; then
export cluster_names=$1
else
echo "no clusters names specified"
exit 1
fi
shift
;;
--kind-node-version)
shift
if test $# -gt 0; then
export kind_node_version=$1
else
echo "no kind node version specified"
exit 1
fi
shift
;;
--kind-worker-nodes)
shift
if test $# -gt 0; then
export kind_worker_nodes=$1
else
echo "no kind worker nodes specified"
exit 1
fi
shift
;;
-h|--help)
echo
echo "Syntax: create-kind-clusters.sh [options]"
echo "Options:"
echo "clusters The number of clusters to create."
echo "cluster-names A comma-delimited list of cluster names to create. Takes precedence over clusters option."
echo "kind-node-version The image version of the kind nodes."
echo "kind-worker-nodes The number of worker nodes to deploy."
exit
;;
--)
shift;
break
;;
*)
break
;;
esac
done
function create_registry() {
running="$(docker inspect -f '{{.State.Running}}' "${registry_name}" 2>/dev/null || true)"
if [ "${running}" != 'true' ]; then
docker run -d --restart=always -p "${registry_port}:5000" --name "${registry_name}" registry:2
fi
}
function create_cluster() {
cluster_id=$1
cluster_name=$2
num_workers=$3
node_version=$4
cat <<EOF | kind create cluster --name $cluster_name --image kindest/node:$node_version --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
- |-
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."localhost:${registry_port}"]
endpoint = ["http://${registry_name}:${registry_port}"]
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30080
hostPort: 3${cluster_id}080
protocol: TCP
- containerPort: 30942
hostPort: 3${cluster_id}942
protocol: TCP
- containerPort: 30090
hostPort: 3${cluster_id}090
protocol: TCP
$(for ((i=1; i<=$num_workers; i++)); do
cat << EOF2
- role: worker
kubeadmConfigPatches:
- |
kind: JoinConfiguration
nodeRegistration:
kubeletExtraArgs:
node-labels: "topology.kubernetes.io/zone=rack$i"
EOF2
done)
EOF
docker network connect "kind" "$registry_name" || true
}
function delete_clusters() {
echo "Deleting existing clusters..."
for ((i=0; i<$num_clusters; i++))
do
echo "Deleting cluster $((i+1)) out of $num_clusters"
kind delete cluster --name "k8ssandra-$i" || echo "Cluster k8ssandra-$i doesn't exist yet"
done
echo
}
function create_clusters() {
echo "Creating clusters..."
for ((i=0; i<$num_clusters; i++))
do
echo "Creating cluster $((i+1)) out of $num_clusters"
create_cluster $i "k8ssandra-$i" $kind_worker_nodes $kind_node_version
done
echo
}
# Creates a kubeconfig file that has entries for each of the clusters created.
# The file created is <project-root>/build/kubeconfig and is intended for use
# primarily by tests running out of cluster.
function create_kubeconfig() {
echo "Generating kubeconfig"
mkdir -p build/kubeconfigs
for ((i=0; i<$num_clusters; i++))
do
kubeconfig_base="build/kubeconfigs/k8ssandra-$i.yaml"
kind get kubeconfig --name "k8ssandra-$i" > $kubeconfig_base
done
yq ea '. as $item ireduce({}; . *+ $item)' build/kubeconfigs/*.yaml > build/kubeconfig
}
# Creates a kubeconfig file that has entries for each of the clusters created.
# This file created is <project-root>/build/in_cluster_kubeconfig and is
# intended for in-cluster use primarily by the k8ssandra cluster controller.
# This file differs from the one created in create_kubeconfig in that the
# server addresses are set to their pod IPs which are docker container
# adddresses.
function create_in_cluster_kubeconfig() {
echo "Generating in-cluster kubeconfig"
mkdir -p build/kubeconfigs/updated
for ((i=0; i<$num_clusters; i++))
do
kubeconfig_base="build/kubeconfigs/k8ssandra-$i.yaml"
kubeconfig_updated="build/kubeconfigs/updated/k8ssandra-$i.yaml"
kind get kubeconfig --name "k8ssandra-$i" > $kubeconfig_base
api_server_ip_addr=$(kubectl --context kind-k8ssandra-$i -n kube-system get pod -l component=kube-apiserver -o json | jq -r '.items[0].status.podIP')
api_server_port=6443
yq eval ".clusters[0].cluster.server |= \"https://$api_server_ip_addr:$api_server_port\"" "$kubeconfig_base" > "$kubeconfig_updated"
done
yq ea '. as $item ireduce({}; . *+ $item)' build/kubeconfigs/updated/*.yaml > build/in_cluster_kubeconfig
}
function deploy_cass_operator() {
echo "Deploying Cass Operator"
for ((i=0; i<$num_clusters; i++))
do
kustomize build config/cass-operator | kubectl --context kind-k8ssandra-$i apply -f -
done
}
function create_k8s_contexts_secret() {
echo "Creating Kubernetes contexts secrets"
for ((i=0; i<$num_clusters; i++))
do
kubectl --context kind-k8ssandra-$i create secret generic k8s-contexts --from-file=./build/kubeconfig
done
}
create_registry
delete_clusters
create_clusters
create_kubeconfig
create_in_cluster_kubeconfig
#deploy_cass_operator
#create_k8s_contexts_secret