Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit a kubernetes resource from a shell script

Tags:

shell

sed

kubectl

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. 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"`
like image 539
ambikanair Avatar asked May 08 '18 06:05

ambikanair


People also ask

How do I edit a file in a Kubernetes pod?

There are two ways to edit a file in an existing pod: either by using kubectl exec and console commands to edit the file in place, or kubectl cp to copy an already edited file into the pod.

How do I edit my Kubernetes deployment?

You can edit a Deployment by changing the container image from one version to the other, decreasing or increasing the number of instances by changing the ReplicaSet value.

How do I edit Kubernetes service?

Synopsis. Edit a resource from the default editor. The edit command allows you to directly edit any API resource you can retrieve via the command line tools. It will open the editor defined by your KUBE_EDITOR, or EDITOR environment variables, or fall back to 'vi' for Linux or 'notepad' for Windows.

How do I edit a Edit in kubectl?

To use the kubectl edit command, create a KUBE_EDITOR environment variable and specify your preferred text editor as the variable value. In addition, append the watch flag ( -w ) to the value so that kubectl knows when you have committed (saved) your changes.


1 Answers

I just found a less convoluted way of doing this:

KUBE_EDITOR="sed -i s/SOMETHING TO CHANGE/CHANGED/g" kubectl edit resource -n your-ns
like image 52
Nebril Avatar answered Oct 03 '22 05:10

Nebril