-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdo-iks-external-control-plane.sh
More file actions
executable file
·293 lines (264 loc) · 8.95 KB
/
do-iks-external-control-plane.sh
File metadata and controls
executable file
·293 lines (264 loc) · 8.95 KB
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
#!/bin/bash
# This script setup istio using external control plane.
# One cluster named cluster1 is used as external cluster
# One cluster named cluster2 is used as remote cluster
# The script uses metallb to expose istio instance
# installed in external-istiod namespace which is
# considered as istio external control plane
ColorOff='\033[0m' # Text Reset
Black='\033[0;30m' # Black
Red='\033[0;31m' # Red
Green='\033[0;32m' # Green
# Get all available kubernetes clusters
clusters=($(kubectl config get-clusters | tail +2))
# Sort the clusters so that we always get two first clusters
IFS=$'\n' clusters=($(sort -r <<<"${clusters[*]}"))
if [[ "${#clusters[@]}" < 2 ]]; then
echo "Need at least two clusters to do external control plane, found ${#clusters[@]}"
exit 1
fi
# Setup cluster context information
C1_CTX="${clusters[0]}"
C2_CTX="${clusters[1]}"
# Extract cluster names
C1_NAME=$(echo $C1_CTX|cut -d '/' -f 1)
C2_NAME=$(echo $C2_CTX|cut -d '/' -f 1)
ISTIO_NAMESPACE=external-istiod
# If there is a parameter, that means this is a delete, so remove everything
if [[ $1 != '' ]]; then
echo "Removing istio from ${C1_NAME}"
istioctl --context ${C1_CTX} uninstall --purge -y --force || true
# Do not delete the service, since it will take a while to allocate
# reuse it
# kubectl delete --context ${C1_CTX} -n ${ISTIO_NAMESPACE} \
# --ignore-not-found=true service/istio-endpoint-service || true
echo "Removing istio from ${C2_NAME}"
istioctl --context ${C2_CTX} uninstall --purge -y || true
exit 0
fi
set -e
LOADIMAGE=""
HUB="istio"
istioctlversion=$(istioctl version 2>/dev/null|head -1|cut -d ':' -f 2)
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 ""
# utility function to wait for pods to be ready
function waitForPods() {
ns=$1
lb=$2
waittime=$3
ctx=$4
# Wait for the pods to be ready in the given namespace with lable
while : ; do
res=$(kubectl wait --context "${ctx}" -n ${ns} pod \
-l ${lb} --for=condition=Ready --timeout=${waittime}s 2>/dev/null ||true)
if [[ "${res}" == *"condition met"* ]]; then
break
fi
echo -e "Waiting for pods in namespace ${Green}${ns}${ColorOff} with label ${Green}${lb}${ColorOff} in ${Green}${ctx}${ColorOff} to be ready..."
sleep ${waittime}
done
}
# Now create the namespace in external cluster and setup istio certs
kubectl create --context="${C1_CTX}" namespace $ISTIO_NAMESPACE --dry-run=client -o yaml \
| kubectl apply --context="${C1_CTX}" -f -
# Create a loadBalancer service to expose Istio service to other clusters
cat <<EOF | kubectl apply --context="${C1_CTX}" -f -
apiVersion: v1
kind: Service
metadata:
name: istio-endpoint-service
namespace: $ISTIO_NAMESPACE
spec:
type: LoadBalancer
selector:
istio: pilot
app: istiod
ports:
- name: status-port
port: 15021
targetPort: 15021
- name: tls-istiod
port: 15012
targetPort: 15012
- name: tls-webhook
port: 15017
targetPort: 15017
EOF
# Wait for the public IP address to be allocated
while : ; do
EXTERNAL_ISTIOD_ADDR=$(kubectl get --context ${C1_CTX} -n $ISTIO_NAMESPACE \
services/istio-endpoint-service -o jsonpath='{ .status.loadBalancer.ingress[0].hostname}')
if [[ ! -z $EXTERNAL_ISTIOD_ADDR ]]; then
echo "Public address ${EXTERNAL_ISTIOD_ADDR} is now available"
# We need to make sure that DNS entry has been propergated, otherwise, config
# Clusters can not be configured correctly.
while : ; do
ENTRY=$(nslookup ${EXTERNAL_ISTIOD_ADDR}|tail +5)
if [[ ! -z $ENTRY ]]; then
# This means that the DNS entry has been now available. we can go on
break
fi
echo -e "Wait for ${Green}${EXTERNAL_ISTIOD_ADDR}${ColorOff} DNS entry to be propagated..."
sleep 10
done
break
fi
echo 'Waiting for public IP address to be available...'
sleep 3
done
# The following steps are to setup the remote config cluster
# Create the namespace in the remote cluster
kubectl create --context="${C2_CTX}" namespace $ISTIO_NAMESPACE --dry-run=client -o yaml \
| kubectl apply --context="${C2_CTX}" -f -
kubectl --context="${C2_CTX}" --overwrite=true label namespace $ISTIO_NAMESPACE topology.istio.io/network=network2
# Setup Istio config cluster
echo -e "Setting up ${Green}config cluster${ColorOff}: ${Green}${C2_NAME}${ColorOff}"
istioctl install --context="${C2_CTX}" -y -f - <<EOF
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
namespace: $ISTIO_NAMESPACE
spec:
profile: external
meshConfig:
accessLogFile: /dev/stdout
values:
global:
istioNamespace: $ISTIO_NAMESPACE
configCluster: true
hub: ${HUB}
tag: ${TAG}
pilot:
configMap: true
istiodRemote:
injectionURL: https://${EXTERNAL_ISTIOD_ADDR}:15017/inject/:ENV:cluster=${C2_NAME}:ENV:net=network2
base:
validationURL: https://${EXTERNAL_ISTIOD_ADDR}:15017/validate
EOF
# Create a service account in namespace $ISTIO_NAMESPACE of external cluster
kubectl create sa istiod-service-account -n $ISTIO_NAMESPACE --context="${C1_CTX}"
# Create a secret to access remote cluster apiserver and install it in external cluster
# this is to ensure that the services in external cluster will be able to access remote cluster
# apiserver
istioctl x create-remote-secret --context="${C2_CTX}" \
--type=config --namespace=$ISTIO_NAMESPACE --service-account=istiod --name "${C2_NAME}" \
--create-service-account=false | kubectl apply -f - --context="${C1_CTX}"
# Setup the control plane in external cluster
echo -e "Setting up ${Green}control plane${ColorOff}: ${Green}${C1_NAME}${ColorOff}"
cat <<EOF | istioctl install --context="${C1_CTX}" -y -f -
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
namespace: $ISTIO_NAMESPACE
spec:
profile: empty
meshConfig:
accessLogFile: /dev/stdout
rootNamespace: $ISTIO_NAMESPACE
defaultConfig:
discoveryAddress: $EXTERNAL_ISTIOD_ADDR:15012
components:
base:
enabled: true
pilot:
enabled: true
k8s:
overlays:
- kind: Deployment
name: istiod
patches:
- path: spec.template.spec.volumes[100]
value: |-
name: config-volume
configMap:
name: istio
- path: spec.template.spec.volumes[100]
value: |-
name: inject-volume
configMap:
name: istio-sidecar-injector
- path: spec.template.spec.containers[0].volumeMounts[100]
value: |-
name: config-volume
mountPath: /etc/istio/config
- path: spec.template.spec.containers[0].volumeMounts[100]
value: |-
name: inject-volume
mountPath: /var/lib/istio/inject
env:
- name: INJECTION_WEBHOOK_CONFIG_NAME
value: "istio-sidecar-injector-${ISTIO_NAMESPACE}"
- name: VALIDATION_WEBHOOK_CONFIG_NAME
value: "istio-validator-${ISTIO_NAMESPACE}"
- name: EXTERNAL_ISTIOD
value: "true"
- name: CLUSTER_ID
value: ${C2_NAME}
- name: SHARED_MESH_CONFIG
value: istio
values:
global:
caAddress: ${EXTERNAL_ISTIOD_ADDR}:15012
istioNamespace: ${ISTIO_NAMESPACE}
operatorManageWebhooks: true
hub: ${HUB}
tag: ${TAG}
meshID: mesh1
logging:
level: "default:debug"
EOF
# Wait for control plane to be ready
waitForPods ${ISTIO_NAMESPACE} app=istiod,istio=pilot 10 ${C1_CTX}
# deploy ingress gateway
echo -e "Deploy ingress gateway in ${Green}${C2_NAME}${ColorOff}"
(cat <<EOF | istioctl manifest generate --set values.global.istioNamespace=${ISTIO_NAMESPACE} --context="${C2_CTX}" -f -
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
profile: empty
components:
ingressGateways:
- namespace: ${ISTIO_NAMESPACE}
name: istio-ingressgateway
enabled: true
k8s:
overlays:
- kind: Deployment
name: istio-ingressgateway
patches:
- path: spec.template.spec.containers[0].imagePullPolicy
value: IfNotPresent
values:
global:
hub: ${HUB}
tag: ${TAG}
gateways:
istio-ingressgateway:
injectionTemplate: gateway
EOF
) | kubectl apply --context ${C2_CTX} -n ${ISTIO_NAMESPACE} -f -
# Wait for the ingress gateway to be ready
waitForPods ${ISTIO_NAMESPACE} app=istio-ingressgateway,istio=ingressgateway 10 ${C2_CTX}
exit 0
# verify
ColorOff='\033[0m' # Text Reset
Green='\033[0;32m' # Green
function verify() {
EP=$1
echo -e ${Green}Ready to hit the helloworld service @ ${EP} in ${ColorOff}
x=1; while [ $x -le 10 ]; do
curl -sS ${EP}/hello
x=$(( $x + 1 ))
done
}