iT邦幫忙

1

[已解決] kubectl 搭配 xargs 指令批量 scale 問題

  • 分享至 

  • xImage

沒事了,自己打指令搞了個烏龍,其實是沒問題的

原本這樣下都可以成功

kubectl get deploy | grep web | awk '{print $1}'| xargs -I {} kubectl scale deploy {} --replicas 0

可能因為版本較新關係

Client Version: version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.4", GitCommit:"872a965c6c6526caa949f0c6ac028ef7aff3fb78", GitTreeState:"clean", BuildDate:"2022-11-09T13:36:36Z", GoVersion:"go1.19.3", Compiler:"gc", Platform:"darwin/amd64"}
Kustomize Version: v4.5.7
Server Version: version.Info{Major:"1", Minor:"24", GitVersion:"v1.24.8-gke.2000", GitCommit:"63bd400145c58fe450325af1d249c3072fed68ef", GitTreeState:"clean", BuildDate:"2022-12-07T09:33:04Z", GoVersion:"go1.18.8b7", Compiler:"gc", Platform:"linux/amd64"}

現在會出現以下錯誤

error: there is no need to specify a resource type as a separate argument when passing arguments in resource/name form (e.g. 'kubectl get resource/<resource_name>' instead of 'kubectl get resource resource/<resource_name>' 

家裡舊版沒問題

Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.4", GitCommit:"d360454c9bcd1634cf4cc52d1867af5491dc9c5f", GitTreeState:"clean", BuildDate:"2020-11-11T13:17:17Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"21", GitVersion:"v1.21.0", GitCommit:"cb303e613a121a29364f75cc67d3d580833a7479", GitTreeState:"clean", BuildDate:"2021-04-08T16:25:06Z", GoVersion:"go1.16.1", Compiler:"gc", Platform:"linux/amd64"}

於是改成這樣下

kubectl get deploy | grep web | awk '{print $1}'| xargs -I {} kubectl scale deploy/{} --replicas 0

會出現以下錯誤

error: arguments in resource/name form may not have more than one slash
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

3
Ray
iT邦大神 1 級 ‧ 2023-03-05 18:58:31
最佳解答

我瞎猜:
因為他說你 more than one slash, 可是你又只寫了一個 / 符號, 所以合理推測, awk 拆出來的 Resource nme 已經包含了開頭的 /, 因此你又寫一個, 才會變成太多...

若改成這樣?

kubectl get deploy | grep web | awk '{print $1}'| xargs -I {} kubectl scale deploy{} --replicas 0

或者不確定的話, 把 awk '{print $1}' 的結果先丟出來看看, 不要急著餵給 xargs?

vicentli iT邦研究生 4 級 ‧ 2023-03-05 19:03:37 檢舉

謝謝雷神大,解決了。原來我用公司電腦下指令時,一直忘了 awk (在公司下的指令更長) 而且我都按上改最後 deploy {} 的部份做各種測試,沒整個重打。而我在家下一開始就有加 awk,所以家裡能成功公司不行 XD,所以其實用空格或 /{} 都還是能用的。抱歉搞烏龍了

我要發表回答

立即登入回答