e1e0.net

sources for e1e0 website
git clone https://git.e1e0.net/e1e0.net.git
Log | Files | Refs

kubectl-plugins.md (2960B)


      1 Title: kubectl plugins
      2 Author: paco
      3 Date: 2020-08-11
      4 Type: article
      5 
      6 Following the `kubernetes` management theme, there's something that may be of
      7 interest for `kubectl` users.
      8 
      9 That management tool accepts what they call _plugins_.  Those are simply
     10 executables that reside in `$PATH`, and they follow a naming convention, that is
     11 `kubectl-name`.  Where `name` is a subcommand you want to create.
     12 Sub-sub-commands can nested by creating executables with the form
     13 `kubectl-sub-sub-command` (so dash separated words for them).
     14 
     15 They can be compiled binaries written in your language of choice, or scripts
     16 made in your preferred interpreted programming language.
     17 
     18 I use this to simplify or wrap around existing subcommands, for tasks that I do
     19 regularly and annoy me, but can be used to develop new functionalities.
     20 
     21 For reference, [this is the official documentation][1]
     22 I'll show a couple of examples here.
     23 
     24 ## Restart a deployment
     25 
     26 This one is as simple as it gets.  Actually there's no way of restarting
     27 a deployment on kubernetes.  Pods are (or should be) stateless and expendable,
     28 so you really kill them and the scheduler re-creates them when the deployment
     29 definition requirements are not met.
     30 
     31 A simple way of _"emulating"_ a restart, is modifying the deployment so the
     32 scheduler re-creates all the pods in it.  For that, just changing the value of
     33 an environment variable in the deployment definition will suffice.  For that
     34 I created this plugin:
     35 
     36 ```
     37 #!/bin/sh
     38 
     39 set -eu
     40 
     41 usage() {
     42     echo "kubectl restart <deployment>"
     43     exit 1
     44 }
     45 
     46 deployment=${1:-x}
     47 
     48 test $deployment != "x" || usage
     49 
     50 kubectl set env \
     51     "deployment/$deployment" \
     52     LAST_MANUAL_RESTART="$(date +%Y%m%d.%H%M%S)"
     53 ```
     54 
     55 ## Custom log output
     56 
     57 Even with all the log aggregation and the cloudy stuff doing cloudy things, one
     58 sometimes needs to take a peek at the logs of an specific container.  That can
     59 be done with the `kubectl logs` command, which will dump all the things to your
     60 terminal.
     61 
     62 Some of the apps at work spit out some sort of structured JSON as logs, which is
     63 hard to read when they come in one line and without any time stamp on them.
     64 For that I created this:
     65 
     66 ```
     67 #!/bin/sh
     68 
     69 set -eu
     70 
     71 if ! command -v jq > /dev/null; then
     72     echo 'You need to have jq installed to run this plugin.'
     73     exit 1
     74 fi
     75 
     76 usage() {
     77     echo "kubectl pslog <pod> [container] [options]"
     78     echo "(options can be any option passed to kubectl logs)"
     79     exit 1
     80 }
     81 
     82 pod=${1:-x}
     83 
     84 test $pod != "x" || usage
     85 
     86 kubectl logs $* |\
     87     grep -E '^{' |\
     88     jq -r '"\(.timestamp.seconds | strftime("%Y-%m-%d %H:%M:%S %Z")) | \(.message)"'
     89 ```
     90 
     91 `kubectl pslog` uses the power of `jq` to print the time stamp in a human
     92 readable manner in front of each log entry, and then _prettifies_ it in the
     93 process, which is way more readable.
     94 
     95 Not much more to say, hope this gives you some ideas to make your life easier
     96 when using `kubectl`.
     97 
     98 [1]: https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins/