-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.bashrc.k8s
424 lines (378 loc) · 11.3 KB
/
.bashrc.k8s
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
### Generic kubectl shortcuts ###
# Shortcut for kubectl
k()
{
kubectl "$@"
}
# Shortcut for kubectl that executes the command in kube-system namespace
ks()
{
kubectl -n kube-system "$@"
}
# Shortcut for kubectl that executes the command in all namespaces
kan()
{
kubectl "$@" --all-namespaces
}
# Watch for Kubernetes, All resources
wka() {
watch kubectl get all --all-namespaces -o wide
}
# Kubectl Get Events (everywhere)
kge()
{
kubectl get events --sort-by='.lastTimestamp' --all-namespaces
}
get_k8s_pod()
{
kubectl get pods -l id="$*" -o jsonpath='{.items[0].metadata.name}'
}
get_k8s_svc()
{
kubectl get svc "$*" -o jsonpath='{.spec.clusterIP}'
}
k8s_curl_path()
{
pod=$(get_k8s_pod ${1})
shift
kubectl exec ${pod} -- curl -s $*
}
k8s_curl()
{
pod=$1
svc=$(get_k8s_svc ${2})
shift; shift
k8s_curl_path $pod $svc $*
}
# $1 - container_id
pod_by_container_id()
{
local container="$1"
if [ $# -lt 1 ]; then
echo "usage: pod_by_container_id <container_id>"
return
fi
kubectl get pods --all-namespaces -o json | jq '[ .items[] | select( .metadata.uid | contains("'$container'")) | .metadata ]'
}
# Pod By Container ID - shortcut
pbci()
{
pod_by_container_id "$@"
}
### Cilium ###
set_cilium_namespace()
{
cilium_ns=$(kubectl --request-timeout 3s get namespaces -o json | jq -r '.items[] | select(.metadata.name == "cilium") | .metadata.name')
if [[ $cilium_ns == "cilium" ]]; then
export CILIUM_NAMESPACE="cilium"
else
export CILIUM_NAMESPACE="kube-system"
fi
}
# $1 - namespace
# $2 - labels
# $3+ - extra agruments to kubectl
get_pods_by_label()
{
ns=$1
labels=$2
shift
shift
kubectl -n $ns get pods -l "$labels" $@
}
get_cilium_pod()
{
set_cilium_namespace
get_pods_by_label $CILIUM_NAMESPACE k8s-app=cilium | tail -n 1 | sed 's/\([^ ]*\).*$/\1/'
}
# Get cilium pod on node
# $1 - node
gcpn()
{
set_cilium_namespace
node=$1
get_pods_by_label $CILIUM_NAMESPACE k8s-app=cilium -o wide | grep "$node" | sed 's/\([^ ]*\).*$/\1/'
}
# Get Cilium and exec in on node
# $1 - node
gcx()
{
set_cilium_namespace
node=$1
shift
if [ "$1" == "--" ]; then
shift
fi
req="$@"
if [ "$req" == "" ]; then
kubectl exec -n $CILIUM_NAMESPACE -ti $(gcpn $node) -c cilium-agent -- bash
else
kubectl exec -n $CILIUM_NAMESPACE $(gcpn $node) -c cilium-agent -- $req
fi
}
# Kube-System Logs for Cilium
klc()
{
set_cilium_namespace
kubectl -n $CILIUM_NAMESPACE logs $(get_cilium_pod) "$@"
}
# Kubectl eXec in random Cilium pod
#
# $1 - Cilium command to run (Optional; omit for shell access)
kxc()
{
set_cilium_namespace
if [ $# -ge 1 ]; then
kubectl -n $CILIUM_NAMESPACE exec -ti $(get_cilium_pod) -c cilium-agent -- cilium "$@"
else
kubectl -n $CILIUM_NAMESPACE exec -ti $(get_cilium_pod) -c cilium-agent -- bash
fi
}
# Kubectl Set cilium Image
ksi()
{
set_cilium_namespace
if [ $# -eq 1 ]; then
kubectl -n $CILIUM_NAMESPACE set image daemonset/cilium cilium-agent=$1
else
>&2 echo "usage: ksi <image>"
>&2 echo " eg: ksi docker.io/cilium/cilium:v1.8.2"
return 1
fi
}
# Kubectl Set cilium Image to Local dev image
ksil()
{
local port="5000"
local image="localhost:$port/${DOCKER_DEV_ACCOUNT:-cilium}/cilium-dev:local"
if [ $# -eq 0 ]; then
echo "Using $image"
ksi $image
else
>&2 echo "usage: ksil"
return 1
fi
}
# Kubectl Set cilium Image to Stable
ksis()
{
ksi docker.io/cilium/cilium:stable
}
# Kubectl Restart Cilium
krc() {
set_cilium_namespace
kubectl -n $CILIUM_NAMESPACE patch ds cilium -p '{"spec":{"template":{"spec":{"containers":[{"name":"cilium-agent","env":[{"name":"RESTART_","value":"'$(date +%s)'"}]}]}}}}'
}
k8s_get_cilium()
{
set_cilium_namespace
kubectl get pods -n $CILIUM_NAMESPACE -l k8s-app=cilium -o jsonpath='{.items[0].metadata.name}'
}
# Deploy Connectivity Check
#
# $1 - kubectl command (default 'apply')
dcc() {
local command=${1:-apply}
echo "Running $command https://raw.githubusercontent.com/cilium/cilium/HEAD/examples/kubernetes/connectivity-check/connectivity-check.yaml"
kubectl $command -f https://raw.githubusercontent.com/cilium/cilium/HEAD/examples/kubernetes/connectivity-check/connectivity-check.yaml
}
# Deploy Proxy Check
#
# $1 - kubectl command (default 'apply')
dpc() {
local command=${1:-apply}
echo "Running $command https://raw.githubusercontent.com/cilium/cilium/HEAD/examples/kubernetes/connectivity-check/connectivity-check-proxy.yaml"
kubectl $command -f https://raw.githubusercontent.com/cilium/cilium/HEAD/examples/kubernetes/connectivity-check/connectivity-check-proxy.yaml
}
# Deploy 'kind' Cilium (from git)
dkc() {
cilium_path="$HOME/git/cilium"
set_cilium_namespace
if ! kind get clusters | grep -q kind; then
kind create cluster --config=$cilium_path/.github/kind-config.yaml
fi
helm upgrade -i cilium $cilium_path/install/kubernetes/cilium \
--wait \
--namespace $CILIUM_NAMESPACE \
--set nodeinit.enabled=true \
--set kubeProxyReplacement=partial \
--set hostServices.enabled=false \
--set externalIPs.enabled=true \
--set nodePort.enabled=true \
--set hostPort.enabled=true \
--set ipam.mode=kubernetes
}
### Cloud deploy convenience wrappers ###
# $1 - EKS cluster name (optional)
function configure_eks()
{
export KUBECONFIG=~/.kube/config-eks
export EKS_CLUSTER="joeks"
if [ $# -ge 1 ]; then
export EKS_CLUSTER="$1"
fi
if [ -z "$EKS_CLUSTER" ]; then
>&2 echo "$EKS_CLUSTER must be specified."
return 1
fi
local ami_family_arg=""
if [ -z "$AMI_FAMILY" ]; then
ami_family_arg="--node-ami-family=$AMI_FAMILY"
fi
if ! eksctl get cluster $EKS_CLUSTER 2>/dev/null; then
eksctl create cluster --name $EKS_CLUSTER --without-nodegroup $ami_family_arg
kubectl -n kube-system delete daemonset aws-node
fi
}
# $1 - number of nodes to scale to
function eks_scale() {
nodes="$1"; shift
configure_eks "$@"
if [[ $nodes =~ ^[0-9]+$ ]]; then
local ami_family_arg=""
if [ -z "$AMI_FAMILY" ]; then
ami_family_arg="--node-ami-family=$AMI_FAMILY"
fi
if [[ $(eksctl get nodegroup --cluster joeks -o json) = "[]" ]]; then
eksctl create nodegroup --cluster $EKS_CLUSTER \
--nodes-min 0 --nodes-max $nodes $ami_family_arg \
--instance-types="t3.medium,t3a.medium" --node-volume-type="gp3" \
--node-volume-size="10" --managed --spot
fi
export EKS_NODEGROUP=$(eksctl get nodegroup --cluster $EKS_CLUSTER -o json | jq -r '.[0].Name')
eksctl scale nodegroup --cluster $EKS_CLUSTER --name $EKS_NODEGROUP --nodes $nodes
else
echo "Invalid number of nodes \"$nodes\"" >&2
echo "usage: eks_scale <nodes> [cluster-name]" >&2
return 1
fi
}
function eks()
{
(
set -e
eks_scale "$@"
)
}
function eks_stop()
{
eksctl delete cluster "$EKS_CLUSTER"
}
# Scale a (new) GKE cluster up to the specified number of nodes.
#
# $1 - number of nodes to scale to
# $2 - if "--install", install Cilium into the new cluster.
# $3 - if installing Cilium, Helm target directory
# $4 - if installing Cilium, target Cilium version to install
function gke_scale()
{
image_type="ubuntu" # Makes swapping kernels easier. Default: "cos"
machine_type="e2-custom-2-4096"
disk_type="pd-standard"
disk_size="10GB"
nodes="$1"; shift
install="$1"; shift
if [[ -z "$GKE_PROJECT" ]]; then
>&2 echo "\$GKE_PROJECT must be defined to install cilium in GKE."
return 1
fi
if [[ -z "$GKE_CLUSTER" ]]; then
>&2 echo "\$GKE_CLUSTER must be defined to install cilium in GKE."
return 1
fi
if [[ -z "$GKE_ZONE" ]]; then
>&2 echo "\$GKE_ZONE must be defined to install cilium in GKE."
return 1
fi
if [[ -z "$GKE_USER" ]]; then
>&2 echo "\$GKE_USER must be defined to install cilium in GKE."
return 1
fi
if [[ $nodes =~ ^0$ ]]; then
gke_stop
return
elif [[ $nodes =~ ^[0-9]+$ ]]; then
gcloud container --project $GKE_PROJECT clusters create $GKE_CLUSTER \
--username "admin" --image-type $image_type \
--machine-type $machine_type --num-nodes $nodes \
--disk-type $disk_type --disk-size $disk_size --preemptible \
--zone ${GKE_ZONE}
else
echo "Invalid number of nodes \"$nodes\"" >&2
echo "usage: gke_scale <NODES> [--install [HELM-TARGET [CILIUM-VERSION]]]" >&2
return 1
fi
kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user "$GKE_USER"
kubectl create namespace cilium
export NATIVE_CIDR="$(gcloud container clusters describe "$GKE_CLUSTER" | grep -i clusterIpv4Cidr | awk '{print $2}')"
if [[ $install = "--install" ]]; then
gke_cilium "cilium/cilium" "$@"
fi
}
function gke_env()
{
export CILIUM_NAMESPACE=cilium
export NATIVE_CIDR="$(gcloud container clusters describe "$GKE_CLUSTER" | grep -i clusterIpv4Cidr | awk '{print $2}')"
}
function gkc()
{
nodes="$1"; shift
if [[ -z $nodes ]]; then
>&2 echo "usage: gkc <NODES> [HELM-TARGET [CILIUM-VERSION]]"
return 1
fi
(
set -e
gke_scale "$nodes" --install "$@"
)
}
function gke_cilium()
{
export CILIUM_NAMESPACE="cilium"
local target="${1:-cilium/cilium}"
local version=""
if [[ $# -ge 2 ]]; then
version="--version=$(echo $2 | sed 's/^v//')"
fi
if [[ -z "$GKE_CLUSTER" ]]; then
>&2 echo "\$GKE_CLUSTER must be defined to install cilium in GKE."
return 1
fi
(
set -e
NATIVE_CIDR=$(gcloud container clusters describe $GKE_CLUSTER | grep -i clusterIpv4Cidr | awk '{print $2}')
echo "Using native CIDR \"$NATIVE_CIDR\""
helm repo add cilium https://helm.cilium.io/
helm repo update
if [[ ! -z "$CILIUM_LEGACY_HELM" ]]; then
helm upgrade -i cilium $target \
--namespace cilium \
$version \
--set global.nodeinit.enabled=true \
--set nodeinit.reconfigureKubelet=true \
--set nodeinit.removeCbrBridge=true \
--set nodeinit.restartPods=true \
--set global.cni.binPath=/home/kubernetes/bin \
--set global.gke.enabled=true \
--set config.ipam=kubernetes \
--set global.nativeRoutingCIDR="$NATIVE_CIDR"
else
helm upgrade -i cilium $target \
--namespace cilium \
$version \
--set nodeinit.enabled=true \
--set nodeinit.reconfigureKubelet=true \
--set nodeinit.removeCbrBridge=true \
--set nodeinit.restartPods=true \
--set cni.binPath=/home/kubernetes/bin \
--set gke.enabled=true \
--set ipam.mode=kubernetes \
--set nativeRoutingCIDR="$NATIVE_CIDR"
fi
kubectl -n cilium rollout status ds/cilium
)
}
function gke_stop()
{
gcloud container --project "$GKE_PROJECT" clusters delete "$GKE_CLUSTER" --zone "$GKE_ZONE"
}