How to edit a kubernetes resource from a shell script

给你一囗甜甜゛ 提交于 2019-12-05 16:54:51

Your command is missing a backtick. But even though you put it there, it won't work. The reason is because when you do kubectl edit ..., it edits the file on vim. I am not sure sed would work on vim though. Even though if it does, the output goes to a file, so you get the Vim: Warning: Output is not to a terminal error, which I don't know how to solve.

I would recommend you to get the file and save it. Replace the desired parameters and run it again:

kubectl get deploy tiller-deploy -n kube-system -o yaml > tiller.yaml && sed -i "s/automountServiceAccountToken:.*$/automountServiceAccountToken: true/g" tiller.yaml && kubectl replace -f tiller.yaml

I tried the command above and it worked.

Note: no need to add -n kube-system as the yaml file already contains the namespace.

I automate through piping the commands through sed command without creating a temporary file. Take the below example, where I am replacing nameserver 8.8.8.8 with 1.1.1.1

$ kubectl -n kube-system get configmap/kube-dns -o yaml | sed "s/8.8.8.8/1.1.1.1/" | kubectl replace -f -

I don't know kubectl but doc seems to explain that it extract data, edit from an editor than send back, not sure sed pipe work in this case

if piping wokrs Don't use -i, you don't change a file in a pipe

kubectl edit deployment tiller-deploy -n kube-system | \
 sed 's/automountServiceAccountToken:.*$/automountServiceAccountToken: true/g'

if editing a file (and using group in sed)

kubectl edit deployment tiller-deploy -n kube-system > YourCOnfigFile && \
 sed -i 's/\(automountServiceAccountToken:\).*$/\1 true/g' YourConfigFile \
 && Some kubectl to send back YourConfigFile
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!