Day 4: Krew and Plugins
Goal: Extend kubectl with plugins: set up Krew, answer seven everyday questions faster than with kubectl alone using tree, neat, view-secret, who-can, access-matrix, resource-capacity and images, and write your own plugin in three lines · Duration: approx. 75 min · Prerequisite: Day 3
Briefing
- A kubectl plugin is an executable file named
kubectl-<name>in thePATH. Nothing more.kubectl <name>calls it and passes all arguments through. - Krew is the package manager for it: an index with
several hundred plugins, installation, updates. Krew is itself a plugin
(
kubectl krew). - Caution: plugins run with your permissions and your kubeconfig. The Krew index checks that a plugin discloses its source code, not what it does. Install only what you need.
| Question | Plugin |
|---|---|
| What belongs to this Deployment? | tree |
| What does the manifest look like without runtime clutter? | neat |
| What is in this Secret, in plain text? | view-secret |
| Who may read Secrets? | who-can |
| What may this ServiceAccount do, at a glance? | access-matrix |
| How full are the nodes (requests, limits)? | resource-capacity |
| Which images run here? | images |
Drill 1: Setting Up Krew
The official installation instructions, with the same architecture detection for amd64 and arm64:
cd "$(mktemp -d)"
OS=$(uname | tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')
KREW=krew-${OS}_${ARCH}
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/${KREW}.tar.gz"
tar zxf "${KREW}.tar.gz"
./"${KREW}" install krew
echo 'export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"' >> ~/.bashrc
export PATH="$HOME/.krew/bin:$PATH"
cd ~
kubectl krew version | head -3
kubectl krew search | wc -lExpected (shortened):
OPTION VALUE
GitTag v0.5.0
GitCommit 8a4a6ff
428
428 lines, so a good 400 plugins in the index. The seven for today:
kubectl krew install tree neat view-secret who-can access-matrix resource-capacity images
for p in tree neat view-secret who-can access-matrix resource-capacity images; do echo "$p $(kubectl krew info $p | grep -i '^VERSION')"; doneExpected (shortened):
Installed plugin: tree
Installed plugin: neat
Installed plugin: view-secret
Installed plugin: who-can
Installed plugin: access-matrix
Installed plugin: resource-capacity
Installed plugin: images
tree VERSION: v0.6.0
neat VERSION: v2.0.4
view-secret VERSION: v0.17.0
who-can VERSION: v0.4.0
access-matrix VERSION: v0.5.0
resource-capacity VERSION: v0.8.0
images VERSION: v0.6.5
After each installation, Krew reminds you that plugins from the index
are not vetted. kubectl krew upgrade updates all of them,
kubectl krew list shows what is installed.
Save your progress? Sign up for free
Drill 2: tree and neat
What belongs to the Deployment web? tree
follows the ownerReferences downwards:
kubectl tree deploy web -n ladenExpected (names and times will differ for you):
NAMESPACE NAME READY REASON STATUS AGE
laden Deployment/web - - 12m
laden ├─ReplicaSet/web-5d987b6cd9 - - 5m43s
laden ├─ReplicaSet/web-67649cfddb - - 12m
laden │ ├─Pod/web-67649cfddb-6d48k True Current 12m
laden │ │ └─CiliumEndpoint/web-67649cfddb-6d48k - - 11m
laden │ ├─Pod/web-67649cfddb-fzrd6 True Current 12m
laden │ │ └─CiliumEndpoint/web-67649cfddb-fzrd6 - - 11m
laden │ └─Pod/web-67649cfddb-tggck True Current 12m
laden │ └─CiliumEndpoint/web-67649cfddb-tggck - - 12m
laden └─ReplicaSet/web-95dbfc6f6 - - 5m44s
Three ReplicaSets: the two empty ones come from the patches of Day 2
and stay in place for rollout undo
(revisionHistoryLimit, default 10). Under each Pod hangs a
CiliumEndpoint, an object that Cilium creates.
kubectl get does not show such relationships.
neat removes everything from an object that the cluster
has added: status, managedFields, uid,
timestamps. What remains is what you would store in Git:
kubectl -n laden get deploy cache -o yaml | wc -l
kubectl -n laden get deploy cache -o yaml | kubectl neat | wc -l
kubectl -n laden get deploy cache -o yaml | kubectl neat | sed -n '1,12p'Expected:
67
39
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
labels:
app: cache
name: cache
namespace: laden
spec:
progressDeadlineSeconds: 600
replicas: 1
Of 67 lines, 39 remain. The default values (strategy,
dnsPolicy, terminationGracePeriodSeconds) are
still in there; neat removes runtime data, not
defaults.
Save your progress? Sign up for free
Drill 3: Reading Secrets
Decoding Base64 by hand (Day 1) is cumbersome.
view-secret takes care of it:
kubectl view-secret db-zugang DB_PASSWORD -n laden; echo
kubectl view-secret db-zugang --all -n ladenExpected:
lab-geheim-123
DB_PASSWORD='lab-geheim-123'
DB_USER='laden'
Tested: Without a key and without
--all(kubectl view-secret db-zugang), the plugin opens an interactive selection. In scripts or through a pipe without a terminal, it then waits forever. In automation, therefore always specify the key or--all.
Convenient, but also a warning: anyone with get secrets
reads passwords just as easily. Who is allowed to do that is answered by
the next drill.
Save your progress? Sign up for free
Drill 4: Who May Do That?
kubectl auth can-i (Day 2) asks “may X do this?”.
who-can asks the other way round: “who may do this?”.
kubectl who-can get secrets -n ladenExpected:
No subjects found with permissions to get secrets assigned through RoleBindings
CLUSTERROLEBINDING SUBJECT TYPE SA-NAMESPACE
cilium-operator cilium-operator ServiceAccount kube-system
cluster-admin system:masters Group
kubeadm:cluster-admins kubeadm:cluster-admins Group
system:controller:generic-garbage-collector generic-garbage-collector ServiceAccount kube-system
system:controller:namespace-controller namespace-controller ServiceAccount kube-system
system:kube-controller-manager system:kube-controller-manager User
Besides admins and controllers, the cilium-operator may also read Secrets cluster-wide. Cilium needs this (certificates, Clustermesh), but it is good to know: whoever takes over this Pod reads all Secrets. And our shop API?
kubectl who-can list pods -n lager | grep -E 'CLUSTERROLEBINDING|api-alles-lesen'Expected:
CLUSTERROLEBINDING SUBJECT TYPE SA-NAMESPACE
api-alles-lesen api ServiceAccount laden
The shop’s API reads Pods in lager (warehouse), through
the ClusterRoleBinding from Day 0.
Save your progress? Sign up for free
Drill 5: The Permission Matrix
access-matrix (formerly rakkess) shows all permissions
of a subject as a table:
kubectl access-matrix --sa laden:api -n laden | grep -E '^NAME|^configmaps |^pods |^secrets |^deployments.apps |^serviceaccounts 'Expected:
NAME LIST CREATE UPDATE DELETE
configmaps ✔ ✖ ✖ ✖
deployments.apps ✔ ✖ ✖ ✖
pods ✔ ✖ ✖ ✖
secrets ✖ ✖ ✖ ✖
serviceaccounts ✔ ✖ ✖ ✖
Or, for a resource, who may do what with it:
kubectl access-matrix for secrets -n laden | head -12Expected:
NAME KIND SA-NAMESPACE LIST CREATE UPDATE DELETE
cilium-operator ServiceAccount kube-system ✔ ✖ ✖ ✖
generic-garbage-collector ServiceAccount kube-system ✔ ✖ ✔ ✔
horizontal-pod-autoscaler ServiceAccount kube-system ✔ ✖ ✖ ✖
kubeadm:cluster-admins Group ✔ ✔ ✔ ✔
legacy-service-account-token-cleaner ServiceAccount kube-system ✖ ✖ ✖ ✔
namespace-controller ServiceAccount kube-system ✔ ✖ ✖ ✔
resourcequota-controller ServiceAccount kube-system ✔ ✖ ✖ ✖
storage-version-migrator-controller ServiceAccount kube-system ✔ ✖ ✖ ✖
system:kube-controller-manager User ✔ ✔ ✔ ✔
system:masters Group ✔ ✔ ✔ ✔
who-can answers for one verb, access-matrix
shows the four most important ones at once. For an audit, the matrix is
clearer; for a concrete question, who-can is more precise
(Day 11 goes deeper into RBAC checks).
Save your progress? Sign up for free
Drill 6: Capacity and Images
How much have the Pods on each node requested?
kubectl describe node shows it per node,
resource-capacity for all at once:
kubectl resource-capacity
kubectl resource-capacity --pods -n laden | head -14Expected (shortened):
NODE CPU REQUESTS CPU LIMITS MEMORY REQUESTS MEMORY LIMITS
* 1300m (10%) 3000m (25%) 366Mi (1%) 3604Mi (15%)
werkzeuge-control-plane 750m (18%) 1000m (25%) 110Mi (1%) 1024Mi (12%)
werkzeuge-worker 350m (8%) 1000m (25%) 182Mi (2%) 1428Mi (17%)
werkzeuge-worker2 200m (5%) 1000m (25%) 74Mi (0%) 1152Mi (14%)
NODE POD CPU REQUESTS CPU LIMITS MEMORY REQUESTS MEMORY LIMITS
* * 150m (1%) 0m (0%) 96Mi (0%) 192Mi (0%)
werkzeuge-worker api-7d5f77c79b-gjmtc 0m (0%) 0m (0%) 0Mi (0%) 0Mi (0%)
werkzeuge-worker web-67649cfddb-tggck 50m (1%) 0m (0%) 32Mi (0%) 64Mi (0%)
werkzeuge-worker2 api-7d5f77c79b-725kc 0m (0%) 0m (0%) 0Mi (0%) 0Mi (0%)
werkzeuge-worker2 cache-55b4555575-xkp72 0m (0%) 0m (0%) 0Mi (0%) 0Mi (0%)
The zeros for api and cache are the missing
requests from Day 0: for the scheduler, these Pods cost nothing, and it
stacks them up until the node is really full. --util
additionally shows the real usage, but needs the metrics-server for that
(Day 9).
Which images run in laden? What was a JSONPath line on
Day 1 is a single command here:
kubectl images -n laden | head -12Expected (shortened):
[Summary]: 1 namespaces, 7 pods, 7 containers and 4 different images
+------------------------+-----------+------------------------+
| Pod | Container | Image |
+------------------------+-----------+------------------------+
| api-7d5f77c79b-725kc | whoami | traefik/whoami:v1.11.0 |
+------------------------+ + +
| api-7d5f77c79b-gjmtc | | |
+------------------------+-----------+------------------------+
| cache-55b4555575-xkp72 | redis | redis:8.2.3-alpine |
+------------------------+-----------+------------------------+
| web-67649cfddb-6d48k | nginx | nginx:1.30.7-alpine |
+------------------------+ + +
Save your progress? Sign up for free
Exercise: Speed Drills
- Which plugins are installed? Target time: 15 s
- What is the database user in the Secret
db-zugang? Target time: 20 s - Who may list Pods in the namespace
lagerthrough a ClusterRoleBinding namedapi-alles-lesen? Target time: 30 s
Solution
kubectl krew list
kubectl view-secret db-zugang DB_USER -n laden; echo
kubectl who-can list pods -n lager | grep api-alles-lesenExpected:
access-matrix
images
krew
neat
resource-capacity
tree
view-secret
who-can
laden
api-alles-lesen api ServiceAccount laden
Save your progress? Sign up for free
Challenge
After every change, the team asks: “Is everything still healthy?”
Write your own plugin kubectl ungesund (unhealthy) that
shows all Pods that are not Running or
Succeeded or have a container that is not ready. It should
understand -n, -A and label selectors like
kubectl get. Test it with a Pod whose image does not
exist.
Hint: A plugin is an executable file
kubectl-ungesund in the PATH.
"$@" passes all arguments on to
kubectl get pods, and jq filters (Day 1, Drill 4).
Solution
mkdir -p ~/.local/bin
cat > ~/.local/bin/kubectl-ungesund <<'EOF'
#!/bin/sh
# kubectl ungesund [-n NS | -A]: Pods, die nicht Running/Succeeded oder nicht bereit sind
kubectl get pods "$@" -o json | jq -r '.items[]
| select(.status.phase != "Succeeded")
| select(.status.phase != "Running" or any(.status.containerStatuses[]?; .ready == false))
| "\(.metadata.namespace)/\(.metadata.name) \(.status.phase)"'
EOF
chmod +x ~/.local/bin/kubectl-ungesund
export PATH="$HOME/.local/bin:$PATH"
kubectl plugin list | grep ungesund
kubectl ungesund -A; echo "exit $?"
kubectl -n laden run kaputt --image=nginx:9.99
sleep 15
kubectl ungesund -n laden
kubectl -n laden delete pod kaputt --wait=falseExpected:
/home/ubuntu/.local/bin/kubectl-ungesund
exit 0
pod/kaputt created
laden/kaputt Pending
pod "kaputt" deleted from laden namespace
No output means healthy. The broken Pod appears with
Pending: with an image error, the phase stays
Pending, and the reason ImagePullBackOff is in
the container status. If you want to print it as well, add
.status.containerStatuses[0].state.waiting.reason.
kubectl plugin list finds the plugin like any from Krew.
Day 14 rebuilds the same principle in Go, with tests and its own Krew
manifest.
Save your progress? Sign up for free
Checkpoint
kubectl krew list | wc -l
kubectl ungesund -A; echo "exit $?"Expected:
8
exit 0
Interview Note
- What is a kubectl plugin technically? An executable
file
kubectl-<name>in thePATH; hyphens in the command become underscores in the file name (view-secret→kubectl-view_secret). - Is Krew safe? Krew checks the origin and checksums of downloads, not the behavior of the plugins. Plugins work with your kubeconfig and your permissions.
- can-i or who-can?
can-ichecks one subject,who-cansearches for all subjects for an action.
Enterprise Note
- An agreed plugin list per team, versioned (e.g. as a Krew custom index or an installation list in the team repo), instead of everyone installing whatever they find.
- Plugins that display Secrets (
view-secret) are a convenience for authorized users, not a risk in themselves. The risk is overly broadget secretspermissions, andwho-canfinds those. - For audits, RBAC evaluations belong in a regular report, not in manual work. Day 11 shows tools that deliver this as a report.