So you’re working hard at building a solid Kubernetes cluster — maybe using
kops to create a new instance group and BAM you are presented with an editor session to edit the details of that shiny new instance group. No biggie; you just need to add a simple little
detailedInstanceMonitoring: true to the spec and you are good to go.
Okay, now you need to do this several times a day to test the performance of the latest build and this is just one of several steps to get the cluster up and running. You want to automate building that cluster as much as possible but every time you get to the step to create that instance group, BAM there it is again — your favorite editor, you have to add that same line every time.
yq is great for digging through yaml files but it also has an in-place merge function that can modify a file directly just like any editor. And
kops, along with several other command line tools honor the
EDITOR environment variable so you can automate your yaml editing along with the rest of your cluster handy work.
Making it work
The first roadblock is that you can pass command line options via the
EDITOR environment variable but the file being edited in-place must be the last option (actually passed to the editor by
kops as it invokes the editor).
yq wants you to pass it the file to be edited followed by a patch file with instructions on editing the file (more on that below). To get around this issue I use a little bash script to invoke
yq and reorder the last two command line options like so (I’ll call the file
#!/usr/bin/env bash if [[ $# != 2 ]]; then echo "Usage: $0 <merge file (supplied by script)> <file being edited (supplied by invoker of EDITOR)>" exit 1 fi yq merge --inplace --overwrite $2 $1
In the above script, the
merge option tells
yq we want to merge yaml files and
--inplace says to edit the first file in-place. The
--overwrite option instructs
yq to overwrite existing sections of the file if they are defined in the merge file.
$2 is the file to be edited and
$1 is the merge file (the opposite order of what the script gets them in). There are other useful options available documented in the
yq merge documentation.
Example 1: Turning on detailed instance monitoring
The next step is to create a patch file containing the edit you want to perform. In this example, we will turn on detailed instance monitoring which is a useful way to get more metrics from your nodes. Here’s the merge file (we will call this file
spec: detailedInstanceMonitoring: true
To put it all together, you can invoke
kops with a custom editor command:
EDITOR="./yq-merge-editor.sh ./ig-monitoring.yaml" kops edit instancegroups nodes
kops creates a temporary file and invokes your editor script which invokes
yq edits the temporary file in-place and
kops takes the edited output and moves on.
Example 2: Temporarily add nodes
Say you want to temporarily add capacity to your cluster while performing some maintenance. This is a temporary change, so there’s no need to update your cluster’s configuration permanently. The following patch file will update the min and max node counts in an instance group:
spec: maxSize: 25 minSize: 25
Then invoke the same script from above followed by a
EDITOR="./yq-merge-editor.sh ig-nodes-25.yaml" kops edit instancegroups nodes kops update cluster $NAME --yes
These tips should make it easier to build lots of happy clusters!