如何从Shell脚本编辑kubernetes资源 [英] How to edit a kubernetes resource from a shell script

查看:504
本文介绍了如何从Shell脚本编辑kubernetes资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遍历了文档,使用 kubectl edit 命令.执行命令后,将在编辑器中打开YAML格式的文件,在其中可以根据需要更改值并保存.我试图通过sed执行这些步骤.如何实现以下步骤?

I went through the documentation to edit kubernetes resource using kubectl edit command. Once I execute the command, the file in YAML-format is opened in the editor where I can change the values as per requirement and save it. I am trying to execute these steps by means of sed. How can the following steps be achieved?

  1. 对部署资源执行kubectl edit
  2. truefalse中设置一个值(使用sed)
  3. 保存更改
  1. Execute kubectl edit for a deployment resource
  2. Set a value from true to false (using sed)
  3. Save the changes

我试图通过以下方式实现这一目标:

I tried to achieve this in the following way :

$ kubectl edit deployment tiller-deploy -n kube-system | \
   sed -i "s/\(automountServiceAccountToken:.*$\)/automountServiceAccountToken: true/g"`

推荐答案

您的命令缺少反引号.但是,即使您将其放在此处,也无法正常工作.原因是因为当您执行kubectl edit ...时,它将在vim上编辑文件.我不确定sed可以在vim上正常工作.即使这样做,输出也将保存到一个文件中,所以您会收到Vim: Warning: Output is not to a terminal错误,我不知道该如何解决.

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.

注意:无需添加-n kube-system,因为yaml文件已经包含名称空间.

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

这篇关于如何从Shell脚本编辑kubernetes资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆