日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
KubectlForeach在多個(gè)集群中執(zhí)行Kubectl命令

kubectl Foreach 在多個(gè)集群中執(zhí)行 Kubectl 命令

作者:Addo Zhang 2022-12-02 19:37:36

云計(jì)算

云原生 今天偶然間發(fā)現(xiàn)了一個(gè) kubectl 插件 kubectl foreach? ,可以在多個(gè)集群(contexts?)上執(zhí)行 kubectl 命令。比如 kubectl foreach cluster-1 cluster-2 -- get po -n kube-system 。

上周在寫 K8s 多集群的流量調(diào)度 的 demo 部分時(shí)需要不停地在多個(gè)集群中安裝組件、部署應(yīng)用,或者執(zhí)行各種命令。當(dāng)時(shí)是通過 Linux shell 腳本并通過工具 kubectx 進(jìn)行集群的切換,像這樣:

或者這樣:

操作繁瑣,很是痛苦。

今天偶然間發(fā)現(xiàn)了一個(gè) kubectl 插件 kubectl foreach? ,可以在多個(gè)集群(contexts?)上執(zhí)行 kubectl 命令。比如 kubectl foreach cluster-1 cluster-2 -- get po -n kube-system 。

插件安裝和使用很簡(jiǎn)單,通過 krew 進(jìn)行安裝:

kubectl krew install foreach

使用也很簡(jiǎn)單:

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

接下來測(cè)試下,使用 k3d 創(chuàng)建 3 個(gè)集群 (k3d 貌似不支持同時(shí)創(chuàng)建多個(gè)集群,還是需要 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 的時(shí)候要注意:

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

比如查看各個(gè)集群中的 kube-sysmte 下的 pod:

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

或者試試創(chuàng)建 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

注意,多集群的操作要謹(jǐn)慎,尤其是使用正則來匹配 context name;還有 ?-q 參數(shù)會(huì)跳過要操作的集群提醒,直接執(zhí)行命令。?


分享文章:KubectlForeach在多個(gè)集群中執(zhí)行Kubectl命令
本文URL:http://www.5511xx.com/article/dhggpej.html