今天偶然间发现了一个 kubectl 插件 kubectl foreach ,可以在多个集群(contexts)上执行 kubectl 命令。比如 kubectl foreach cluster-1 cluster-2 -- get po -n kube-system 。
上周在写 K8s 多集群的流量调度 的 demo 部分时需要不停地在多个集群中安装组件、部署应用,或者执行各种命令。当时是通过 Linux shell 脚本并通过工具 kubectx 进行集群的切换,像这样:
或者这样:
操作繁琐,很是痛苦。
今天偶然间发现了一个 kubectl 插件 kubectl foreach ,可以在多个集群(contexts)上执行 kubectl 命令。比如 kubectl foreach cluster-1 cluster-2 -- get po -n kube-system 。
插件安装和使用很简单,通过 krew 进行安装:
复制
1. kubectl krew install foreach
使用也很简单:
复制
1. kubectl foreach -h
2. Usage:
3. kubectl foreach [OPTIONS] [PATTERN]... -- [KUBECTL_ARGS...]
4.
5. Patterns can be used to match context names from kubeconfig:
6. (empty): matches all contexts
7. NAME: matches context with exact name
8. /PATTERN/: matches context with regular expression
9. ^NAME: remove context with exact name from the matched results
10. ^/PATTERN/: remove contexts matching the regular expression from the results
11.
12. Options:
13. -c=NUM Limit parallel executions (default: 0, unlimited)
14. -I=VAL Replace VAL occurring in KUBECTL_ARGS with context name
15. -q Disable and accept confirmation prompts ($KUBECTL_FOREACH_DISABLE_PROMPTS)
16. -h/--help Print help
17.
18. Examples:
19. # get nodes on contexts named a b c
20. kubectl foreach a b c -- get nodes
21.
22. # get nodes on all contexts named c0..9 except c1 (note the escaping)
23. kubectl foreach '/^c[0-9]/' ^c1 -- get nodes
24.
25. # get nodes on all contexts that has "prod" but not "foo"
26. kubectl foreach /prod/ ^/foo/ -- get nodes
27.
28. # use 'kubectl tail' plugin to follow logs of pods in contexts named *test*
29. kubectl foreach -I _ /test/ -- tail --cnotallow=_ -l app=foo
接下来测试下,使用 k3d 创建 3 个集群 (k3d 貌似不支持同时创建多个集群,还是需要 for 脚本来操作):
复制
1. for CLUSTER_NAME in cluster-1 cluster-2 cluster-3
2. do
3. k3d cluster create ${CLUSTER_NAME} \\
4. --image docker.io/rancher/k3s:v1.23.8-k3s2 \\
5. --servers-memory 4g \\
6. --k3s-arg "--disable=traefik@server:0" \\
7. --no-lb \\
8. --timeout 120s \\
9. --wait
10. done
集群安装完成:
复制
1. k3d cluster list
2. NAME SERVERS AGENTS LOADBALANCER
3. cluster-1 1/1 0/0 false
4. cluster-2 1/1 0/0 false
5. cluster-3 1/1 0/0 false
注意,k3d 安装的集群的 context 都带有前缀 k3d- ,在使用 kubectl foreach 的时候要注意:
复制
1. kubectx
2. k3d-cluster-1
3. k3d-cluster-2
4. k3d-cluster-3
比如查看各个集群中的 kube-sysmte 下的 pod:
复制
1. kubectl foreach -q k3d-cluster-1 k3d-cluster-2 k3d-cluster-3 -- get po -n kube-system
或者试试创建 deployment,这次我们不列出完整的 context name,而是使用正则 /cluster/:
复制
1. kubectl foreach -q /cluster/ -- create deploy pipy --image flomesh/pipy -n default
2. Will run command in context(s):
3. - k3d-cluster-1
4. - k3d-cluster-2
5. - k3d-cluster-3
6. k3d-cluster-1 | deployment.apps/pipy created
7. k3d-cluster-3 | deployment.apps/pipy created
8. k3d-cluster-2 | deployment.apps/pipy created
然后查看下 pod:
复制
1. kubectl foreach -q /cluster/ -- get pod -n default
2. Will run command in context(s):
3. - k3d-cluster-1
4. - k3d-cluster-2
5. - k3d-cluster-3
6. k3d-cluster-1 | NAME READY STATUS RESTARTS AGE
7. k3d-cluster-1 | pipy-df659b55f-bnr27 1/1 Running 0 25s
8. k3d-cluster-3 | NAME READY STATUS RESTARTS AGE
9. k3d-cluster-3 | pipy-df659b55f-p9j49 1/1 Running 0 25s
10. k3d-cluster-2 | NAME READY STATUS RESTARTS AGE
11. k3d-cluster-2 | pipy-df659b55f-9bjgf 1/1 Running 0 25s
查看日志:
复制
1. kubectl foreach -q /cluster/ -- logs -l app=pipy -n default --tail 3
2. Will run command in context(s):
3. - k3d-cluster-1
4. - k3d-cluster-2
5. - k3d-cluster-3
6. k3d-cluster-2 | 2022-11-30 10:40:56.520 [INF] [listener] Listening on TCP port 8080 at 0.0.0.0
7. k3d-cluster-2 | 2022-11-30 10:40:56.520 [INF] [listener] Listening on TCP port 8081 at 0.0.0.0
8. k3d-cluster-2 | 2022-11-30 10:40:56.520 [INF] [listener] Listening on TCP port 8082 at 0.0.0.0
9. k3d-cluster-1 | 2022-11-30 10:40:56.551 [INF] [listener] Listening on TCP port 8080 at 0.0.0.0
10. k3d-cluster-1 | 2022-11-30 10:40:56.551 [INF] [listener] Listening on TCP port 8081 at 0.0.0.0
11. k3d-cluster-1 | 2022-11-30 10:40:56.551 [INF] [listener] Listening on TCP port 8082 at 0.0.0.0
12. k3d-cluster-3 | 2022-11-30 10:40:55.813 [INF] [listener] Listening on TCP port 8080 at 0.0.0.0
13. k3d-cluster-3 | 2022-11-30 10:40:55.813 [INF] [listener] Listening on TCP port 8081 at 0.0.0.0
14. k3d-cluster-3 | 2022-11-30 10:40:55.813 [INF] [listener] Listening on TCP port 8082 at 0.0.0.0
注意,多集群的操作要谨慎,尤其是使用正则来匹配 context name;还有 -q 参数会跳过要操作的集群提醒,直接执行命令。
来源: 云原生指北
>>>>>>点击进入云计算专题
¥499.00
¥99.00
¥499.00
¥10500.00