-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdo-primary-remote-multinet.sh
executable file
·373 lines (341 loc) · 9.25 KB
/
do-primary-remote-multinet.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/bin/bash
# This script setups two clusters on two networks, that is,
# the two clusters are on different IP spaces, workload cannot
# reach workloads running on other cluster without an Istio
# gateway. Cluster1 is considered primary, Cluster2 is considered
# remote.
ColorOff='\033[0m' # Text Reset
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
CLUSTER1_NAME=cluster1
CLUSTER2_NAME=cluster2
NAMESPACE=istio-system
if [[ $1 != '' ]]; then
setupmcs -d
exit 0
fi
LOADIMAGE=""
HUB="istio"
istioctlversion=$(istioctl version 2>/dev/null|head -1)
if [[ "${istioctlversion}" == *"-dev" ]]; then
LOADIMAGE="-l"
HUB="localhost:5000"
if [[ -z "${TAG}" ]]; then
TAG=$(docker images "localhost:5000/pilot:*" --format "{{.Tag}}")
fi
fi
TAG="${TAG:-${istioctlversion}}"
echo ""
echo -e "Hub: ${Green}${HUB}${ColorOff}"
echo -e "Tag: ${Green}${TAG}${ColorOff}"
echo ""
# Use the script to setup a k8s cluster with Metallb installed and setup
cat <<EOF | setupmcs ${LOADIMAGE}
[
{
"kind": "Kubernetes",
"clusterName": "${CLUSTER1_NAME}",
"podSubnet": "10.10.0.0/16",
"svcSubnet": "10.255.10.0/24",
"network": "network1",
"meta": {
"kubeconfig": "/tmp/work/${CLUSTER1_NAME}"
}
},
{
"kind": "Kubernetes",
"clusterName": "${CLUSTER2_NAME}",
"podSubnet": "10.20.0.0/16",
"svcSubnet": "10.255.20.0/24",
"network": "network2",
"meta": {
"kubeconfig": "/tmp/work/${CLUSTER2_NAME}"
}
}
]
EOF
# Create the namespace for cluster1
kubectl create --context kind-${CLUSTER1_NAME} namespace ${NAMESPACE}
kubectl --context kind-${CLUSTER1_NAME} label namespace ${NAMESPACE} topology.istio.io/network=network1
# Setup the cacerts
./makecerts.sh -d
./makecerts.sh -c kind-${CLUSTER1_NAME} -s ${NAMESPACE} -n ${CLUSTER1_NAME}
# Create the namespace for cluster2
kubectl create --context kind-${CLUSTER2_NAME} namespace ${NAMESPACE}
kubectl --context kind-${CLUSTER2_NAME} label namespace ${NAMESPACE} topology.istio.io/network=network2
kubectl --context kind-${CLUSTER2_NAME} annotate namespace ${NAMESPACE} topology.istio.io/controlPlaneClusters=*
# Setup the cacerts
./makecerts.sh -c kind-${CLUSTER2_NAME} -s ${NAMESPACE} -n ${CLUSTER2_NAME}
# Install istio onto the first cluster
cat <<EOF | istioctl install --context="kind-${CLUSTER1_NAME}" -y -f -
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
values:
global:
hub: ${HUB}
tag: ${TAG}
meshID: mesh1
multiCluster:
clusterName: ${CLUSTER1_NAME}
network: network1
components:
ingressGateways:
- name: istio-ingressgateway
label:
istio: ingressgateway
app: istio-ingressgateway
topology.istio.io/network: network1
enabled: true
k8s:
env:
- name: ISTIO_META_ROUTER_MODE
value: "sni-dnat"
- name: ISTIO_META_REQUESTED_NETWORK_VIEW
value: network1
service:
ports:
- name: status-port
port: 15021
targetPort: 15021
- name: tls
port: 15443
targetPort: 15443
- name: https
port: 443
targetPort: 8443
- name: tls-istiod
port: 15012
targetPort: 15012
- name: tls-webhook
port: 15017
targetPort: 15017
pilot:
enabled: true
k8s:
env:
- name: INJECTION_WEBHOOK_CONFIG_NAME
value: "istio-sidecar-injector"
- name: VALIDATION_WEBHOOK_CONFIG_NAME
value: "istiod-default-validator"
- name: EXTERNAL_ISTIOD
value: "true"
EOF
# Expose the control plan
cat << EOF | kubectl apply --context="kind-${CLUSTER1_NAME}" -n ${NAMESPACE} -f -
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: istiod-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
name: tls-istiod
number: 15012
protocol: tls
tls:
mode: PASSTHROUGH
hosts:
- "*"
- port:
name: tls-istiodwebhook
number: 15017
protocol: tls
tls:
mode: PASSTHROUGH
hosts:
- "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: istiod-vs
spec:
hosts:
- "*"
gateways:
- istiod-gateway
tls:
- match:
- port: 15012
sniHosts:
- "*"
route:
- destination:
host: istiod.${NAMESPACE}.svc.cluster.local
port:
number: 15012
- match:
- port: 15017
sniHosts:
- "*"
route:
- destination:
host: istiod.${NAMESPACE}.svc.cluster.local
port:
number: 443
EOF
# add the cluster2 credential to cluster1
istioctl x create-remote-secret --context="kind-${CLUSTER2_NAME}" \
--name=${CLUSTER2_NAME} --namespace ${NAMESPACE} | \
kubectl apply --context="kind-${CLUSTER1_NAME}" -f -
# Get the ingress gateway IP address
while : ; do
DISCOVERY_ADDRESS=$(kubectl --context="kind-${CLUSTER1_NAME}" get svc istio-ingressgateway \
-n ${NAMESPACE} -o=jsonpath='{.status.loadBalancer.ingress[0].ip }')
if [[ ! -z ${DISCOVERY_ADDRESS} ]]; then
break
fi
echo -e ${Green}Waiting${ColorOff} for Ingress Gateway to be ready...
sleep 3
done
# Install istio onto the second cluster
cat <<EOF | istioctl install --context="kind-${CLUSTER2_NAME}" -y -f -
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
profile: external
values:
global:
hub: ${HUB}
tag: ${TAG}
meshID: mesh1
multiCluster:
clusterName: ${CLUSTER2_NAME}
network: network2
remotePilotAddress: ${DISCOVERY_ADDRESS}
istiodRemote:
injectionPath: /inject/cluster/${CLUSTER2_NAME}/net/network2
components:
base:
enabled: true
ingressGateways:
- name: istio-ingressgateway
label:
istio: ingressgateway
app: istio-ingressgateway
topology.istio.io/network: network2
enabled: true
k8s:
env:
- name: ISTIO_META_ROUTER_MODE
value: "sni-dnat"
- name: ISTIO_META_REQUESTED_NETWORK_VIEW
value: network2
service:
ports:
- name: tls
port: 15443
targetPort: 15443
EOF
function fixupIstiodServiceAndEndpoints(){
action="${1,,}"
if [[ "${action}" == "delete" ]]; then
kubectl delete --context="kind-${CLUSTER2_NAME}" -n ${NAMESPACE} endpoints istiod
kubectl delete --context="kind-${CLUSTER2_NAME}" -n ${NAMESPACE} services istiod
else
cat << EOF | kubectl apply --context="kind-${CLUSTER2_NAME}" -f -
apiVersion: v1
kind: Service
metadata:
name: istiod
namespace: ${NAMESPACE}
spec:
type: ClusterIP
clusterIP:
ports:
- port: 15012
name: tcp-istiod
protocol: TCP
- port: 443
targetPort: 15017
name: tcp-webhook
protocol: TCP
---
apiVersion: v1
kind: Endpoints
metadata:
name: istiod
namespace: ${NAMESPACE}
subsets:
- addresses:
- ip: ${DISCOVERY_ADDRESS}
ports:
- port: 15012
name: tcp-istiod
protocol: TCP
- port: 15017
name: tcp-webhook
protocol: TCP
EOF
fi
}
# Use version compare sort to figure out if we need fixes
verValue=$(echo -e "1.15\n${TAG,,}" | sort -V)
verValue=$(echo $verValue)
# The version is less than 1.15, need to fix up the services and endpoints
if [[ "${verValue}" != "1.15 ${TAG,,}" ]]; then
fixupIstiodServiceAndEndpoints delete
fixupIstiodServiceAndEndpoints apply
fi
function createCrossNetworkGateway() {
CLUSTERNAME=$1
# Expose the services in the first cluster
cat << EOF | kubectl --context="kind-${CLUSTERNAME}" apply -n $NAMESPACE -f -
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: cross-network-gateway
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 15443
name: tls
protocol: TLS
tls:
mode: AUTO_PASSTHROUGH
hosts:
- "*.local"
EOF
}
createCrossNetworkGateway ${CLUSTER1_NAME}
createCrossNetworkGateway ${CLUSTER2_NAME}
exit 0
# these steps are for verify traffic happens across the boundary of the two clusters.
# deploy helloworld and sleep onto cluster1 as v1 helloworld
ColorOff='\033[0m' # Text Reset
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
CLUSTER1_NAME=cluster1
CLUSTER2_NAME=cluster2
WORKLOADNS=sample
export CTX_NS=${WORKLOADNS}
export CTX_CLUSTER=kind-${CLUSTER1_NAME}
verification/helloworld.sh v1
# deploy helloworld and sleep onto cluster2 as v2 helloworld
export CTX_CLUSTER=kind-${CLUSTER2_NAME}
verification/helloworld.sh v2
# verify the traffic
function verify() {
CLUSTERNAME=$1
# get sleep podname
PODNAME=$(kubectl get pod --context="${CLUSTERNAME}" -n ${WORKLOADNS} -l \
app=sleep -o jsonpath='{.items[0].metadata.name}')
echo -e ${Green}Ready to hit the helloworld service from ${PODNAME} in ${CLUSTERNAME} ${ColorOff}
x=1; while [ $x -le 5 ]; do
kubectl exec --context="${CLUSTERNAME}" -n ${CTX_NS} -c sleep ${PODNAME} \
-- curl -sS helloworld.${CTX_NS}.svc.cluster.local:5000/hello
x=$(( $x + 1 ))
done
# show proxy config endpoints
echo "Endpoints for sleep pod"
istioctl --context="${CLUSTERNAME}" proxy-config endpoints ${PODNAME}.${CTX_NS}
}
verify kind-${CLUSTER1_NAME}
verify kind-${CLUSTER2_NAME}