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

commit 9b743b3108f7ca3084604add712f7f901da38677
parent 99e0402bf836d03944de758c2fa526c5e8231602
Author: Paco Esteban <paco@e1e0.net>
Date:   Tue, 11 Aug 2020 12:53:17 +0200

new article about kubectl plugins

Msrc/gophermap | 3++-
Asrc/kubectl-plugins.md | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 100 insertions(+), 1 deletion(-)

diff --git a/src/gophermap b/src/gophermap @@ -12,6 +12,7 @@ Sometimes I write things so I do not forget ... o--o-- Random (usually tech) stuff +0kubectl plugins /kubectl-plugins.md e1e0.net 70 0Aliases and helper functions for the shell /aliases-helper-functions-shell.md e1e0.net 70 0Easy DNS zone management across providers /easy-dns-zone-management-accross-providers.md e1e0.net 70 0Manage Kubernetes clusters from OpenBSD /manage-k8s-from-openbsd.md e1e0.net 70 @@ -51,5 +52,5 @@ Have any comments ? Send an email to <comments@e1e0.net> o- o -- -------------------------------------------------------- -- o -- -Last updated: Fri, 03 Jul 2020 08:38:16 +0000 +Last updated: Tue, 11 Aug 2020 10:52:35 +0000 o- o -- -------------------------------------------------------- -- o -- diff --git a/src/kubectl-plugins.md b/src/kubectl-plugins.md @@ -0,0 +1,98 @@ +Title: kubectl plugins +Author: paco +Date: 2020-08-11 +Type: article + +Following the `kubernetes` management theme, there's something that may be of +interest for `kubectl` users. + +That management tool accepts what they call _plugins_. Those are simply +executables that reside in `$PATH`, and they follow a naming convention, that is +`kubectl-name`. Where `name` is a subcommand you want to create. +Sub-sub-commands can nested by creating executables with the form +`kubectl-sub-sub-command` (so dash separated words for them). + +They can be compiled binaries written in your language of choice, or scripts +made in your preferred interpreted programming language. + +I use this to simplify or wrap around existing subcommands, for tasks that I do +regularly and annoy me, but can be used to develop new functionalities. + +For reference, [this is the official documentation][1] +I'll show a couple of examples here. + +## Restart a deployment + +This one is as simple as it gets. Actually there's no way of restarting +a deployment on kubernetes. Pods are (or should be) stateless and expendable, +so you really kill them and the scheduler re-creates them when the deployment +definition requirements are not met. + +A simple way of _"emulating"_ a restart, is modifying the deployment so the +scheduler re-creates all the pods in it. For that, just changing the value of +an environment variable in the deployment definition will suffice. For that +I created this plugin: + +``` +#!/bin/sh + +set -eu + +usage() { + echo "kubectl restart <deployment>" + exit 1 +} + +deployment=${1:-x} + +test $deployment != "x" || usage + +kubectl set env \ + "deployment/$deployment" \ + LAST_MANUAL_RESTART="$(date +%Y%m%d.%H%M%S)" +``` + +## Custom log output + +Even with all the log aggregation and the cloudy stuff doing cloudy things, one +sometimes needs to take a peek at the logs of an specific container. That can +be done with the `kubectl logs` command, which will dump all the things to your +terminal. + +Some of the apps at work spit out some sort of structured JSON as logs, which is +hard to read when they come in one line and without any time stamp on them. +For that I created this: + +``` +#!/bin/sh + +set -eu + +if ! command -v jq > /dev/null; then + echo 'You need to have jq installed to run this plugin.' + exit 1 +fi + +usage() { + echo "kubectl pslog <pod> [container] [options]" + echo "(options can be any option passed to kubectl logs)" + exit 1 +} + +pod=${1:-x} + +test $pod != "x" || usage + +kubectl logs $* |\ + grep -E '^{' |\ + jq -r '"\(.timestamp.seconds | strftime("%Y-%m-%d %H:%M:%S %Z")) | \(.message)"' +``` + +`kubectl pslog` uses the power of `jq` to print the time stamp in a human +readable manner in front of each log entry, and then _prettifies_ it in the +process, which is way more readable. + +Not much more to say, hope this gives you some ideas to make your life easier +when using `kubectl`. + +[1]: https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins/