行业报告 AI展会 数据标注 标注供求
数据标注数据集
主页 > 技术社区 > 云计算 > 正文

Kubectl Foreach 在多个集群中执行 Kubectl 命令

上周在写 K8s 多集群的流量调度 的 demo 部分时需要不停地在多个集群中安装组件、部署应用,或者执行各种命令。当时是通过 Linux shell 脚本并通过工具 kubectx 进行集群的切换,像这样:

图片

或者这样:

图片

操作繁琐,很是痛苦。

今天偶然间发现了一个 kubectl 插件 kubectl foreach​ ,可以在多个集群(contexts​)上执行 kubectl 命令。比如 kubectl foreach cluster-1 cluster-2 -- get po -n kube-system 。

插件安装和使用很简单,通过 krew 进行安装:

kubectl krew install foreach

使用也很简单:

kubectl foreach -h
Usage:
    kubectl foreach [OPTIONS] [PATTERN]... -- [KUBECTL_ARGS...]

Patterns can be used to match context names from kubeconfig:
      (empty): matches all contexts
         NAME: matches context with exact name
    /PATTERN/: matches context with regular expression
        ^NAME: remove context with exact name from the matched results
   ^/PATTERN/: remove contexts matching the regular expression from the results

Options:
    -c=NUM     Limit parallel executions (default: 0, unlimited)
    -I=VAL     Replace VAL occurring in KUBECTL_ARGS with context name
    -q         Disable and accept confirmation prompts ($KUBECTL_FOREACH_DISABLE_PROMPTS)
    -h/--help  Print help

Examples:
    # get nodes on contexts named a b c
    kubectl foreach a b c -- get nodes

    # get nodes on all contexts named c0..9 except c1 (note the escaping)
    kubectl foreach '/^c[0-9]/' ^c1  -- get nodes

    # get nodes on all contexts that has "prod" but not "foo"
    kubectl foreach /prod/ ^/foo/ -- get nodes

    # use 'kubectl tail' plugin to follow logs of pods in contexts named *test*
    kubectl foreach -I _ /test/ -- tail --cnotallow=_ -l app=foo

接下来测试下,使用 k3d 创建 3 个集群 (k3d 貌似不支持同时创建多个集群,还是需要 for 脚本来操作):

for CLUSTER_NAME in cluster-1 cluster-2 cluster-3
do
  k3d cluster create ${CLUSTER_NAME} \
    --image docker.io/rancher/k3s:v1.23.8-k3s2 \
    --servers-memory 4g \
    --k3s-arg "--disable=traefik@server:0" \
    --no-lb \
    --timeout 120s \
    --wait
done

集群安装完成:

k3d cluster list
NAME        SERVERS   AGENTS   LOADBALANCER
cluster-1   1/1       0/0      false
cluster-2   1/1       0/0      false
cluster-3   1/1       0/0      false

注意,k3d 安装的集群的 context 都带有前缀 k3d-​ ,在使用 kubectl foreach 的时候要注意:

kubectx
k3d-cluster-1
k3d-cluster-2
k3d-cluster-3

比如查看各个集群中的 kube-sysmte 下的 pod:

kubectl foreach -q k3d-cluster-1 k3d-cluster-2 k3d-cluster-3 -- get po -n kube-system

图片

或者试试创建 deployment,这次我们不列出完整的 context name,而是使用正则 /cluster/:

kubectl foreach -q /cluster/ -- create deploy pipy --image flomesh/pipy -n default
Will run command in context(s):
  - k3d-cluster-1
  - k3d-cluster-2
  - k3d-cluster-3
k3d-cluster-1 | deployment.apps/pipy created
k3d-cluster-3 | deployment.apps/pipy created
k3d-cluster-2 | deployment.apps/pipy created

然后查看下 pod:

kubectl foreach -q /cluster/ -- get pod -n default
Will run command in context(s):
  - k3d-cluster-1
  - k3d-cluster-2
  - k3d-cluster-3
k3d-cluster-1 | NAME                   READY   STATUS    RESTARTS   AGE
k3d-cluster-1 | pipy-df659b55f-bnr27   1/1     Running   0          25s
k3d-cluster-3 | NAME                   READY   STATUS    RESTARTS   AGE
k3d-cluster-3 | pipy-df659b55f-p9j49   1/1     Running   0          25s
k3d-cluster-2 | NAME                   READY   STATUS    RESTARTS   AGE
k3d-cluster-2 | pipy-df659b55f-9bjgf   1/1     Running   0          25s

查看日志:

kubectl foreach -q /cluster/ -- logs -l app=pipy -n default --tail 3
Will run command in context(s):
  - k3d-cluster-1
  - k3d-cluster-2
  - k3d-cluster-3
k3d-cluster-2 | 2022-11-30 10:40:56.520 [INF] [listener] Listening on TCP port 8080 at 0.0.0.0
k3d-cluster-2 | 2022-11-30 10:40:56.520 [INF] [listener] Listening on TCP port 8081 at 0.0.0.0
k3d-cluster-2 | 2022-11-30 10:40:56.520 [INF] [listener] Listening on TCP port 8082 at 0.0.0.0
k3d-cluster-1 | 2022-11-30 10:40:56.551 [INF] [listener] Listening on TCP port 8080 at 0.0.0.0
k3d-cluster-1 | 2022-11-30 10:40:56.551 [INF] [listener] Listening on TCP port 8081 at 0.0.0.0
k3d-cluster-1 | 2022-11-30 10:40:56.551 [INF] [listener] Listening on TCP port 8082 at 0.0.0.0
k3d-cluster-3 | 2022-11-30 10:40:55.813 [INF] [listener] Listening on TCP port 8080 at 0.0.0.0
k3d-cluster-3 | 2022-11-30 10:40:55.813 [INF] [listener] Listening on TCP port 8081 at 0.0.0.0
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 参数会跳过要操作的集群提醒,直接执行命令。​

微信公众号

声明:本站部分作品是由网友自主投稿和发布、编辑整理上传,对此类作品本站仅提供交流平台,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,不为其版权负责。如果您发现网站上有侵犯您的知识产权的作品,请与我们取得联系,我们会及时修改或删除。

相关文章:

相关推荐:

网友评论:

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
SEM推广服务

Copyright©2005-2026 Sykv.com 可思数据 版权所有    京ICP备14056871号

关于我们   免责声明   广告合作   版权声明   联系我们   原创投稿   网站地图  

可思数据 数据标注行业联盟

扫码入群
扫码关注

微信公众号

返回顶部