In part 22 you debugged Services, DNS, stuck objects and sick nodes, and typed a lot of -n k8s-from-zero along the way. This part is about typing less and seeing more. You will install four small open source tools and set up kubectl itself: kubectx and kubens to switch cluster and namespace, stern to follow logs from many Pods at once, krew to install kubectl plugins, plus shell completion, an alias and kuberc preferences. We tested everything on a kind cluster (kind v0.33.0, node v1.37.0) with kubectl v1.37.2. You need the cluster from part 3 and the web Deployment and Service from part 22 (selector fixed), plus a busybox client Pod that calls the Service once a second, so there are logs to read.
What you will build
By the end your shell knows where it points, completes resource names with Tab, and has three plugins on its PATH. None of these tools changes the cluster: kubectx and kubens edit your kubeconfig file, stern only reads logs, and krew only puts files in a directory you own. The versions we used are the latest releases on 10 Nov 2026: kubectx and kubens v0.11.0, stern v1.35.0 and krew v0.5.0, all Apache-2.0 licensed open source projects on GitHub.
Install with checksums
Download release files from the projects’ GitHub release pages and check each against the checksum file published with it. sha256sum --check --ignore-missing checks only the files you downloaded:
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubectx_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubens_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/checksums.txt
sha256sum --check --ignore-missing checksums.txt
curl -sSLO https://github.com/stern/stern/releases/download/v1.35.0/stern_1.35.0_linux_amd64.tar.gz
curl -sSL -o stern-checksums.txt https://github.com/stern/stern/releases/download/v1.35.0/checksums.txt
sha256sum --check --ignore-missing stern-checksums.txtExplain this command
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubectx_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubens_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/checksums.txt
sha256sum --check --ignore-missing checksums.txt
curl -sSLO https://github.com/stern/stern/releases/download/v1.35.0/stern_1.35.0_linux_amd64.tar.gz
curl -sSL -o stern-checksums.txt https://github.com/stern/stern/releases/download/v1.35.0/checksums.txt
sha256sum --check --ignore-missing stern-checksums.txtcurlA command-line HTTP client. Here it downloads a release file from GitHub.-sSLOcurl flags: silent, show errors, follow redirects, and save the file under its remote name.https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubectx_v0.11.0_linux_x86_64.tar.gzThe release asset URL. The lesson pins a version tag and verifies the download with a checksum before extracting it.
Explain this command
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubectx_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubens_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/checksums.txt
sha256sum --check --ignore-missing checksums.txt
curl -sSLO https://github.com/stern/stern/releases/download/v1.35.0/stern_1.35.0_linux_amd64.tar.gz
curl -sSL -o stern-checksums.txt https://github.com/stern/stern/releases/download/v1.35.0/checksums.txt
sha256sum --check --ignore-missing stern-checksums.txtcurlA command-line HTTP client. Here it downloads a release file from GitHub.-sSLOcurl flags: silent, show errors, follow redirects, and save the file under its remote name.https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubens_v0.11.0_linux_x86_64.tar.gzThe release asset URL. The lesson pins a version tag and verifies the download with a checksum before extracting it.
Explain this command
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubectx_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubens_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/checksums.txt
sha256sum --check --ignore-missing checksums.txt
curl -sSLO https://github.com/stern/stern/releases/download/v1.35.0/stern_1.35.0_linux_amd64.tar.gz
curl -sSL -o stern-checksums.txt https://github.com/stern/stern/releases/download/v1.35.0/checksums.txt
sha256sum --check --ignore-missing stern-checksums.txtcurlA command-line HTTP client. Here it downloads a release file from GitHub.-sSLOcurl flags: silent, show errors, follow redirects, and save the file under its remote name.https://github.com/ahmetb/kubectx/releases/download/v0.11.0/checksums.txtThe checksum file URL from the same GitHub release. The lesson pins the same version tag as the archive.
Explain this command
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubectx_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubens_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/checksums.txt
sha256sum --check --ignore-missing checksums.txt
curl -sSLO https://github.com/stern/stern/releases/download/v1.35.0/stern_1.35.0_linux_amd64.tar.gz
curl -sSL -o stern-checksums.txt https://github.com/stern/stern/releases/download/v1.35.0/checksums.txt
sha256sum --check --ignore-missing stern-checksums.txtsha256sumPrint or check SHA-256 checksums. With --check it verifies downloaded files against a checksums file.--checkWith sha256sum, verify files against the checksums listed in the given file.--ignore-missingWith sha256sum --check, skip checksum lines for files that are not in the current directory, so one upstream checksums file can be used for a single archive.checksums.txtThe checksum file from the same GitHub release as the archive. sha256sum --check reads this file.
Explain this command
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubectx_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubens_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/checksums.txt
sha256sum --check --ignore-missing checksums.txt
curl -sSLO https://github.com/stern/stern/releases/download/v1.35.0/stern_1.35.0_linux_amd64.tar.gz
curl -sSL -o stern-checksums.txt https://github.com/stern/stern/releases/download/v1.35.0/checksums.txt
sha256sum --check --ignore-missing stern-checksums.txtcurlA command-line HTTP client. Here it downloads a release file from GitHub.-sSLOcurl flags: silent, show errors, follow redirects, and save the file under its remote name.https://github.com/stern/stern/releases/download/v1.35.0/stern_1.35.0_linux_amd64.tar.gzThe release asset URL. The lesson pins a version tag and verifies the download with a checksum before extracting it.
Explain this command
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubectx_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubens_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/checksums.txt
sha256sum --check --ignore-missing checksums.txt
curl -sSLO https://github.com/stern/stern/releases/download/v1.35.0/stern_1.35.0_linux_amd64.tar.gz
curl -sSL -o stern-checksums.txt https://github.com/stern/stern/releases/download/v1.35.0/checksums.txt
sha256sum --check --ignore-missing stern-checksums.txtcurlA command-line HTTP client. Here it downloads a release file from GitHub.-sSLcurl flags: silent, show errors, follow redirects.-oWrite the download to this file instead of using the remote name.stern-checksums.txtThe checksum file from the same GitHub release as the archive. sha256sum --check reads this file.https://github.com/stern/stern/releases/download/v1.35.0/checksums.txtThe checksum file URL from the same GitHub release. The lesson pins the same version tag as the archive.
Explain this command
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubectx_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubens_v0.11.0_linux_x86_64.tar.gz
curl -sSLO https://github.com/ahmetb/kubectx/releases/download/v0.11.0/checksums.txt
sha256sum --check --ignore-missing checksums.txt
curl -sSLO https://github.com/stern/stern/releases/download/v1.35.0/stern_1.35.0_linux_amd64.tar.gz
curl -sSL -o stern-checksums.txt https://github.com/stern/stern/releases/download/v1.35.0/checksums.txt
sha256sum --check --ignore-missing stern-checksums.txtsha256sumPrint or check SHA-256 checksums. With --check it verifies downloaded files against a checksums file.--checkWith sha256sum, verify files against the checksums listed in the given file.--ignore-missingWith sha256sum --check, skip checksum lines for files that are not in the current directory, so one upstream checksums file can be used for a single archive.stern-checksums.txtThe checksum file from the same GitHub release as the archive. sha256sum --check reads this file.
kubectx_v0.11.0_linux_x86_64.tar.gz: OK
kubens_v0.11.0_linux_x86_64.tar.gz: OK
stern_1.35.0_linux_amd64.tar.gz: OK
Then unpack each binary into a directory on your PATH, for example ~/.local/bin:
mkdir -p ~/.local/bin
tar xzf kubectx_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubectx
tar xzf kubens_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubens
tar xzf stern_1.35.0_linux_amd64.tar.gz -C ~/.local/bin stern
stern --versionExplain this command
mkdir -p ~/.local/bin
tar xzf kubectx_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubectx
tar xzf kubens_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubens
tar xzf stern_1.35.0_linux_amd64.tar.gz -C ~/.local/bin stern
stern --versionmkdirCreate a directory. -p creates parent directories if they are missing and does not fail if the directory already exists.-pFor mkdir, create parent directories as needed and do not fail if the directory already exists.~/.local/binA directory on a typical user PATH, used here so the installed binaries can be run without sudo.
Explain this command
mkdir -p ~/.local/bin
tar xzf kubectx_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubectx
tar xzf kubens_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubens
tar xzf stern_1.35.0_linux_amd64.tar.gz -C ~/.local/bin stern
stern --versiontarCreate or extract tape archives. xzf extracts a gzip-compressed archive.xzftar flags: extract, filter through gzip, and use the following file as the archive.kubectx_v0.11.0_linux_x86_64.tar.gzThe downloaded release archive to extract.-CWith tar, change to this directory before extracting, so the binary lands in ~/.local/bin.~/.local/binA directory on a typical user PATH, used here so the installed binaries can be run without sudo.kubectxThe member to extract from the archive. Passing the binary name avoids unpacking extra files such as licenses into ~/.local/bin.
Explain this command
mkdir -p ~/.local/bin
tar xzf kubectx_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubectx
tar xzf kubens_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubens
tar xzf stern_1.35.0_linux_amd64.tar.gz -C ~/.local/bin stern
stern --versiontarCreate or extract tape archives. xzf extracts a gzip-compressed archive.xzftar flags: extract, filter through gzip, and use the following file as the archive.kubens_v0.11.0_linux_x86_64.tar.gzThe downloaded release archive to extract.-CWith tar, change to this directory before extracting, so the binary lands in ~/.local/bin.~/.local/binA directory on a typical user PATH, used here so the installed binaries can be run without sudo.kubensThe member to extract from the archive. Passing the binary name avoids unpacking extra files such as licenses into ~/.local/bin.
Explain this command
mkdir -p ~/.local/bin
tar xzf kubectx_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubectx
tar xzf kubens_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubens
tar xzf stern_1.35.0_linux_amd64.tar.gz -C ~/.local/bin stern
stern --versiontarCreate or extract tape archives. xzf extracts a gzip-compressed archive.xzftar flags: extract, filter through gzip, and use the following file as the archive.stern_1.35.0_linux_amd64.tar.gzThe downloaded release archive to extract.-CWith tar, change to this directory before extracting, so the binary lands in ~/.local/bin.~/.local/binA directory on a typical user PATH, used here so the installed binaries can be run without sudo.sternThe member to extract from the archive. Passing the binary name avoids unpacking extra files such as licenses into ~/.local/bin.
Explain this command
mkdir -p ~/.local/bin
tar xzf kubectx_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubectx
tar xzf kubens_v0.11.0_linux_x86_64.tar.gz -C ~/.local/bin kubens
tar xzf stern_1.35.0_linux_amd64.tar.gz -C ~/.local/bin stern
stern --versionsternTail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.--versionPrint the version of this command.
version: 1.35.0
commit: 37fcd4cfc29f61dcd1cdfdb68de8a75e8b33c5fe
built at: 2026-10-22T13:52:12Z
On our shared lab box we unpacked into a private directory instead of ~/.local/bin; the commands are otherwise the same. The kubectx and stern READMEs also list Homebrew (brew install kubectx stern) for macOS and Linux; pick one install method and stick to it.
Try it: switch contexts and namespaces
- A context (part 3) is a named triple of cluster, user and default namespace in your kubeconfig. Add a second context for the same kind cluster with a different namespace, so you have something to switch between:
kubectl config set-context lab-system --cluster=kind-k8s-ep22 --user=kind-k8s-ep22 --namespace=kube-system
kubectx
kubectx -cExplain this command
kubectl config set-context lab-system --cluster=kind-k8s-ep22 --user=kind-k8s-ep22 --namespace=kube-system
kubectx
kubectx -ckubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.configThe command group for kubeconfig entries: contexts, clusters and users.set-contextSet a context entry in kubeconfig. The docs say that if the name already exists, new fields merge on top of the existing values for those fields.lab-systemThe context name the lesson creates for kube-system work.--cluster=kind-k8s-ep22The kubeconfig cluster name to set on this context, here kind-k8s-ep22.--user=kind-k8s-ep22The kubeconfig user name to set on this context, here kind-k8s-ep22.--namespace=kube-systemThe namespace name. This flag sets it to kube-system.
Explain this command
kubectl config set-context lab-system --cluster=kind-k8s-ep22 --user=kind-k8s-ep22 --namespace=kube-system
kubectx
kubectx -ckubectxSwitch, rename or list kubectl contexts. kubectx edits your kubeconfig; it does not change the cluster. -r starts a read-only shell, and -c prints the current context.
Explain this command
kubectl config set-context lab-system --cluster=kind-k8s-ep22 --user=kind-k8s-ep22 --namespace=kube-system
kubectx
kubectx -ckubectxSwitch, rename or list kubectl contexts. kubectx edits your kubeconfig; it does not change the cluster. -r starts a read-only shell, and -c prints the current context.-cPrint the current context name and exit. kubectx does not switch context when you pass -c.
Context "lab-system" created.
kind-k8s-ep22
lab-system
kind-k8s-ep22
- Replace
kind-k8s-ep22with your cluster’s names (kubectl config get-contextsshows them). Switch, look, and switch back with-, the same idea ascd -:
kubectx lab-system
kubectl get pods | head -4
kubectx -Explain this command
kubectx lab-system
kubectl get pods | head -4
kubectx -kubectxSwitch, rename or list kubectl contexts. kubectx edits your kubeconfig; it does not change the cluster. -r starts a read-only shell, and -c prints the current context.lab-systemThe kubeconfig context to switch to. kubectx writes this name into the current-context field; it does not change the cluster.
Explain this command
kubectx lab-system
kubectl get pods | head -4
kubectx -kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podsThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.|A shell pipe. The output of the command on the left becomes the input of the command on the right.headShell. Print the first lines of its input.-4How many lines from the start of the input head should print: 4.
Explain this command
kubectx lab-system
kubectl get pods | head -4
kubectx -kubectxSwitch, rename or list kubectl contexts. kubectx edits your kubeconfig; it does not change the cluster. -r starts a read-only shell, and -c prints the current context.-Switch back to the previous context. kubectx stores that name when you last switched.
✔ Switched to context "lab-system".
NAME READY STATUS RESTARTS AGE
coredns-559f6c778d-9lqkm 1/1 Running 0 2m49s
coredns-559f6c778d-w4n47 1/1 Running 0 9m39s
etcd-k8s-ep22-control-plane 1/1 Running 0 12m
✔ Switched to context "kind-k8s-ep22".
- Now set the namespace for the current context, so you can stop typing
-n:
kubens k8s-from-zero
kubectl get pods
kubens -cExplain this command
kubens k8s-from-zero
kubectl get pods
kubens -ckubensSwitch or list namespaces for the current context. kubens edits the namespace field in your kubeconfig.k8s-from-zeroThe namespace to set on the current context. kubens writes this into the context's namespace field in your kubeconfig.
Explain this command
kubens k8s-from-zero
kubectl get pods
kubens -ckubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podsThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
Explain this command
kubens k8s-from-zero
kubectl get pods
kubens -ckubensSwitch or list namespaces for the current context. kubens edits the namespace field in your kubeconfig.-cPrint the current namespace for this context and exit.
✔ Active namespace is "k8s-from-zero"
NAME READY STATUS RESTARTS AGE
noshell 1/1 Running 0 60s
web-86f79b74d9-cjsf2 1/1 Running 0 2m49s
web-86f79b74d9-plcl5 1/1 Running 0 2m49s
k8s-from-zero
- Rename a context with a long cloud-generated name to something short, and look at the file kubectl reads:
kubectx lab=lab-system
kubectl config get-contextsExplain this command
kubectx lab=lab-system
kubectl config get-contextskubectxSwitch, rename or list kubectl contexts. kubectx edits your kubeconfig; it does not change the cluster. -r starts a read-only shell, and -c prints the current context.lab=lab-systemRename a context for shorter typing, written as alias=context. After this, kubectx lab selects that context.
Explain this command
kubectx lab=lab-system
kubectl config get-contextskubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.configThe command group for kubeconfig entries: contexts, clusters and users.get-contextsList the contexts in your kubeconfig, with a star on the current one.
✔ Context lab-system renamed to lab.
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* kind-k8s-ep22 kind-k8s-ep22 kind-k8s-ep22 k8s-from-zero
lab kind-k8s-ep22 kind-k8s-ep22 kube-system
Both tools only edit the kubeconfig that kubectl uses (the KUBECONFIG variable, or ~/.kube/config). They also keep a little state of their own: on our box kubectx wrote the previous context to ~/.kube/kubectx and kubens wrote one file per context under ~/.kube/kubens/, which is how - knows where to go back.
- The most useful flag for a beginner is
-r. It starts a new shell in which kubectl may read from the context but write requests are refused:
kubectx -r kind-k8s-ep22
kubectl get pods
kubectl delete pod noshell
exitExplain this command
kubectx -r kind-k8s-ep22
kubectl get pods
kubectl delete pod noshell
exitkubectxSwitch, rename or list kubectl contexts. kubectx edits your kubeconfig; it does not change the cluster. -r starts a read-only shell, and -c prints the current context.-rStart a subshell in which kubectl cannot write. The kubectx help describes this as a read-only session. exit leaves the subshell.kind-k8s-ep22The kubeconfig context to switch to. kubectx writes this name into the current-context field; it does not change the cluster.
Explain this command
kubectx -r kind-k8s-ep22
kubectl get pods
kubectl delete pod noshell
exitkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podsThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
Explain this command
kubectx -r kind-k8s-ep22
kubectl get pods
kubectl delete pod noshell
exitkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.deleteDelete resources by file names, stdin, resources and names, or by resources and label selector.podThe resource type.noshellThe noshell Pod from part 22. The lesson tries to delete it to show that the delete is refused or asks first.
Explain this command
kubectx -r kind-k8s-ep22
kubectl get pods
kubectl delete pod noshell
exitexitLeave the current shell. After kubectx -r, this returns you to the writable shell you started from.
...
NAME READY STATUS RESTARTS AGE
noshell 1/1 Running 0 80s
web-86f79b74d9-cjsf2 1/1 Running 0 3m9s
web-86f79b74d9-plcl5 1/1 Running 0 3m9s
Error from server (MethodNotAllowed): [kubectx] readonly mode: DELETE requests are not allowed
exit
[READONLY SHELL EXITED] kubectl context is now kind-k8s-ep22.
The first line, trimmed here, announced the read-only shell and told us to type exit to leave. The [kubectx] tag in the error shows that kubectx blocked the write inside that shell. Your user’s permissions on the cluster did not change, so a normal shell can still delete. Treat -r as a seat belt for looking around a production context, and use RBAC (part 20) for real limits.
Follow logs from many Pods with stern
kubectl logs -f follows one Pod. A Deployment has several, and a rollout replaces them while you watch. stern takes a regular expression (or -l with a label selector) and follows every matching container, adding new Pods as they appear (stern on GitHub).
If you do not have a client from part 22 any more, start one that calls the Service every second:
kubectl run client --image=busybox:1.37 -- sh -c 'while true; do wget -qO- -T 2 http://web >/dev/null; sleep 1; done'Explain this command
kubectl run client --image=busybox:1.37 -- sh -c 'while true; do wget -qO- -T 2 http://web >/dev/null; sleep 1; done'kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.runCreate and run a particular image in a pod. The run docs say this is intended for simple development and debug use.clientThe Pod name passed to kubectl run.--image=busybox:1.37The container image, set here to busybox:1.37.--Ends kubectl's flags. The exec examples say to use two dashes so the command's own flags are read as that command's flags.shA shell. The command that follows runs inside it.-cAn argument of sh. It tells sh to run the command string that follows.'while true; do wget -qO- -T 2 http://web >/dev/null; sleep 1; done'The command the shell runs in the client Pod: a loop that requests http://web every second, so later stern commands have log traffic to show.
pod/client created
Read the last ten seconds of logs from both web Pods. We cut each line to 150 characters and kept the first eight:
timeout 6 stern web --since 10s --no-follow --color never | cut -c1-150 | head -8Explain this command
timeout 6 stern web --since 10s --no-follow --color never | cut -c1-150 | head -8timeoutRun a command and stop it after the given number of seconds.6Stop stern after 6 seconds at most, so the command cannot hang.sternTail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.webThe Pod name or regular expression stern matches. Here it follows Pods whose names contain web.--sinceWith stern, only show logs newer than this duration.10sThe duration passed to --since. stern keeps only logs newer than this.--no-followWith stern, print matching logs and exit instead of continuing to follow.--colorWhether stern colours its output. never disables colour, which keeps the lab log readable.neverThe value passed to --color so stern does not colour its output, which keeps the lab log readable.|A shell pipe. The output of the command on the left becomes the input of the command on the right.cutShell. Print only part of each input line.-c1-150Keep characters 1 to 150 of each line, so long log lines stay readable.|A shell pipe. The output of the command on the left becomes the input of the command on the right.headShell. Print the first lines of its input.-8How many lines from the start of the input head should print.
+ web-86f79b74d9-plcl5 › web
+ web-86f79b74d9-cjsf2 › web
- web-86f79b74d9-cjsf2 › web
- web-86f79b74d9-plcl5 › web
web-86f79b74d9-cjsf2 web 10.244.1.11 - - [10/Nov/2026:21:36:43 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
web-86f79b74d9-cjsf2 web 10.244.1.11 - - [10/Nov/2026:21:36:44 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
web-86f79b74d9-cjsf2 web 10.244.1.11 - - [10/Nov/2026:21:36:46 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
web-86f79b74d9-cjsf2 web 10.244.1.11 - - [10/Nov/2026:21:36:47 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
web-86f79b74d9-plcl5 web 10.244.1.11 - - [10/Nov/2026:21:36:42 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
web-86f79b74d9-plcl5 web 10.244.1.11 - - [10/Nov/2026:21:36:45 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
With --no-follow, stern prints what is there and exits; the lines come grouped per Pod. Now follow only new lines for five seconds:
timeout 5 stern web --tail 0 --color never 2>/dev/null | cut -c1-110; echo exit=$?Explain this command
timeout 5 stern web --tail 0 --color never 2>/dev/null | cut -c1-110; echo exit=$?timeoutRun a command and stop it after the given number of seconds.5How many seconds timeout lets stern run before it stops it.sternTail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.webThe Pod name or regular expression stern matches. Here it follows Pods whose names contain web.--tailHow many lines from the end of each container's logs to show.0Print no existing log lines; only lines that appear after stern starts are shown.--colorWhether stern colours its output. never disables colour, which keeps the lab log readable.neverThe value passed to --color so stern does not colour its output, which keeps the lab log readable.2>/dev/nullA shell redirect. Standard error is discarded so the output stays short.|A shell pipe. The output of the command on the left becomes the input of the command on the right.cutShell. Print only part of each input line.-c1-110Keep characters 1 to 110 of each line.
Explain this command
timeout 5 stern web --tail 0 --color never 2>/dev/null | cut -c1-110; echo exit=$?echoShell. Print its arguments. When it is piped into another command, those arguments become that command's input.exit=$?The text to print. $? is the exit status of the command before it; timeout returns 124 when it had to stop the command, and 0 here.
web-86f79b74d9-cjsf2 web 10.244.1.11 - - [10/Nov/2026:21:36:55 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
web-86f79b74d9-cjsf2 web 10.244.1.11 - - [10/Nov/2026:21:36:56 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
web-86f79b74d9-plcl5 web 10.244.1.11 - - [10/Nov/2026:21:36:57 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
web-86f79b74d9-cjsf2 web 10.244.1.11 - - [10/Nov/2026:21:36:58 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
exit=0
Each line starts with the Pod and container name, so you can see the Service spreading requests. Lines that start with + and - mark a stream that stern opened or closed; they go to standard error, which is why 2>/dev/null hides them.
Now the part kubectl logs cannot do: restart the Deployment while stern runs and watch it pick up the new Pods. We kept only the + and - lines:
kubectl rollout restart deployment/web >/dev/null
timeout 25 stern web --tail 0 --color never --container-state running 2>&1 | grep -E '^[+-]'Explain this command
kubectl rollout restart deployment/web >/dev/null
timeout 25 stern web --tail 0 --color never --container-state running 2>&1 | grep -E '^[+-]'kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.rolloutThe command group for rollout actions.restartTrigger a rolling restart of the named resource. The rollout docs say this is for a Deployment, DaemonSet or StatefulSet.deployment/webThe resource written as type/name: deployment/web.>/dev/nullA shell redirect. Standard output is discarded so the lab log stays short.
Explain this command
kubectl rollout restart deployment/web >/dev/null
timeout 25 stern web --tail 0 --color never --container-state running 2>&1 | grep -E '^[+-]'timeoutRun a command and stop it after the given number of seconds.25Let stern run for 25 seconds, long enough for the rollout to replace both Pods.sternTail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.webThe Pod name or regular expression stern matches. Here it follows Pods whose names contain web.--tailHow many lines from the end of each container's logs to show.0Print no existing log lines; only lines that appear after stern starts are shown.--colorWhether stern colours its output. never disables colour, which keeps the lab log readable.neverThe value passed to --color so stern does not colour its output, which keeps the lab log readable.--container-stateWith stern, only follow containers in this state, such as running, waiting or terminated.runningThe container state passed to --container-state. Only running containers are followed.2>&1A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.|A shell pipe. The output of the command on the left becomes the input of the command on the right.grepShell. Prints input lines that match a pattern.-ETell grep to treat the pattern as an extended regular expression.'^[+-]'The pattern grep keeps. stern marks a new matching Pod with + and a Pod that has gone with -, so this line shows only those changes.
+ web-86f79b74d9-plcl5 › web
+ web-86f79b74d9-cjsf2 › web
+ web-854c556f4-5qx6w › web
+ web-854c556f4-2mnfl › web
- web-86f79b74d9-cjsf2 › web
- web-86f79b74d9-plcl5 › web
Two more flags worth knowing: -l selects by label instead of name, and -o raw prints the bare log line, handy when you pipe JSON logs into another tool:
timeout 6 stern -l app.kubernetes.io/name=web --tail 1 --no-follow --color never -o raw | cut -c1-120Explain this command
timeout 6 stern -l app.kubernetes.io/name=web --tail 1 --no-follow --color never -o raw | cut -c1-120timeoutRun a command and stop it after the given number of seconds.6Stop stern after 6 seconds at most, so the command cannot hang.sternTail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.-lShort form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.app.kubernetes.io/name=webThe label selector passed to -l. It keeps only the objects whose labels match this query.--tailHow many lines from the end of each container's logs to show.1Print this many existing log lines from the end of each matching container, then stop or follow depending on the other flags.--no-followWith stern, print matching logs and exit instead of continuing to follow.--colorWhether stern colours its output. never disables colour, which keeps the lab log readable.neverThe value passed to --color so stern does not colour its output, which keeps the lab log readable.-oShort form of --output. stern formats include default, raw, json and extjson. raw prints only the log line.rawA stern output format that prints only the log line, without a Pod or container prefix.|A shell pipe. The output of the command on the left becomes the input of the command on the right.cutShell. Print only part of each input line.-c1-120Keep characters 1 to 120 of each line.
+ web-86f79b74d9-plcl5 › web
+ web-86f79b74d9-cjsf2 › web
- web-86f79b74d9-cjsf2 › web
- web-86f79b74d9-plcl5 › web
10.244.1.11 - - [10/Nov/2026:21:36:47 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
10.244.1.11 - - [10/Nov/2026:21:36:45 +0000] "GET / HTTP/1.1" 200 896 "-" "Wget" "-"
Plugins: any kubectl-name file on your PATH
kubectl has a simple plugin mechanism: when you type kubectl here, it looks for an executable called kubectl-here on your PATH and runs it.
Save this as kubectl-here:
#!/bin/sh
# kubectl here: print the current context and its namespace.
ctx=$(kubectl config current-context)
ns=$(kubectl config view --minify -o jsonpath='{..namespace}')
echo "context: $ctx"
echo "namespace: ${ns:-default}"Explain this command
#!/bin/sh
# kubectl here: print the current context and its namespace.
ctx=$(kubectl config current-context)
ns=$(kubectl config view --minify -o jsonpath='{..namespace}')
echo "context: $ctx"
echo "namespace: ${ns:-default}"#!/bin/shThe shebang. This file is a POSIX shell script.ctx=$(kubectl config current-context)Capture the current kubeconfig context name in ctx, using kubectl config current-context.ns=$(kubectl config view --minify -o jsonpath='{..namespace}')Capture the current context's namespace in ns, using a minified kubeconfig and a jsonpath query. The script later prints default if this is empty.echoShell. Print its arguments. When it is piped into another command, those arguments become that command's input."context: $ctx"The string echo prints, including the value captured earlier in the script.echoShell. Print its arguments. When it is piped into another command, those arguments become that command's input."namespace: ${ns:-default}"The string echo prints, including the value captured earlier in the script.
chmod +x kubectl-here
cp kubectl-here ~/.local/bin/
kubectl here
kubectl plugin list 2>&1 | grep hereExplain this command
chmod +x kubectl-here
cp kubectl-here ~/.local/bin/
kubectl here
kubectl plugin list 2>&1 | grep herechmodChange file permissions. +x makes the file executable, which kubectl requires for a plugin.+xThe chmod mode that adds execute permission, required so kubectl can run a plugin.kubectl-hereThe plugin script from this lesson. kubectl finds it because it is named kubectl-here and sits on PATH.
Explain this command
chmod +x kubectl-here
cp kubectl-here ~/.local/bin/
kubectl here
kubectl plugin list 2>&1 | grep herecpShell. Copy a file to a new path.kubectl-hereThe plugin script from this lesson. kubectl finds it because it is named kubectl-here and sits on PATH.~/.local/bin/A directory on a typical user PATH, used here so the installed binaries can be run without sudo.
Explain this command
chmod +x kubectl-here
cp kubectl-here ~/.local/bin/
kubectl here
kubectl plugin list 2>&1 | grep herekubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.hereThe here plugin from this lesson, an executable named kubectl-here on PATH. It prints the current context and namespace.
Explain this command
chmod +x kubectl-here
cp kubectl-here ~/.local/bin/
kubectl here
kubectl plugin list 2>&1 | grep herekubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.pluginThe command group for kubectl plugins. plugin list searches PATH for executables named kubectl-*.listList plugin executables found on PATH. The plugin docs say kubectl looks for executables named kubectl- followed by the plugin name.2>&1A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.|A shell pipe. The output of the command on the left becomes the input of the command on the right.grepShell. Prints input lines that match a pattern.hereThe pattern grep keeps. The lesson checks that plugin list can see kubectl-here.
context: kind-k8s-ep22
namespace: k8s-from-zero
/tmp/k8s22/bin/kubectl-here
The last line shows our private lab directory; yours will show ~/.local/bin. Watch the file name: a dash in it separates subcommands. Our first version was called kubectl-whoami-ns, and kubectl whoami-ns failed with error: unknown command "whoami-ns" for "kubectl", while kubectl whoami ns worked. The docs explain that an underscore in the file name (kubectl-whoami_ns) gives you a dash in the command.
krew is a plugin manager for kubectl, maintained under kubernetes-sigs, with a community index of plugins. Download and verify it like the others, then let it install itself (Installing Krew; it needs git):
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz.sha256
echo "$(cat krew-linux_amd64.tar.gz.sha256) krew-linux_amd64.tar.gz" | sha256sum --check
tar xzf krew-linux_amd64.tar.gz ./krew-linux_amd64
./krew-linux_amd64 install krew 2>&1 | tail -12
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
kubectl krew version | head -4Explain this command
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz.sha256
echo "$(cat krew-linux_amd64.tar.gz.sha256) krew-linux_amd64.tar.gz" | sha256sum --check
tar xzf krew-linux_amd64.tar.gz ./krew-linux_amd64
./krew-linux_amd64 install krew 2>&1 | tail -12
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
kubectl krew version | head -4curlA command-line HTTP client. Here it downloads a release file from GitHub.-sSLOcurl flags: silent, show errors, follow redirects, and save the file under its remote name.https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gzThe release asset URL. The lesson pins a version tag and verifies the download with a checksum before extracting it.
Explain this command
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz.sha256
echo "$(cat krew-linux_amd64.tar.gz.sha256) krew-linux_amd64.tar.gz" | sha256sum --check
tar xzf krew-linux_amd64.tar.gz ./krew-linux_amd64
./krew-linux_amd64 install krew 2>&1 | tail -12
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
kubectl krew version | head -4curlA command-line HTTP client. Here it downloads a release file from GitHub.-sSLOcurl flags: silent, show errors, follow redirects, and save the file under its remote name.https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz.sha256The checksum file URL from the same GitHub release. The lesson pins the same version tag as the archive.
Explain this command
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz.sha256
echo "$(cat krew-linux_amd64.tar.gz.sha256) krew-linux_amd64.tar.gz" | sha256sum --check
tar xzf krew-linux_amd64.tar.gz ./krew-linux_amd64
./krew-linux_amd64 install krew 2>&1 | tail -12
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
kubectl krew version | head -4echoShell. Print its arguments. When it is piped into another command, those arguments become that command's input."$(cat krew-linux_amd64.tar.gz.sha256) krew-linux_amd64.tar.gz"A checksum line built from the separate .sha256 file, in the format sha256sum --check expects: the hash, two spaces, and the archive name.|A shell pipe. The output of the command on the left becomes the input of the command on the right.sha256sumPrint or check SHA-256 checksums. With --check it verifies downloaded files against a checksums file.--checkWith sha256sum, verify files against the checksums listed in the given file.
Explain this command
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz.sha256
echo "$(cat krew-linux_amd64.tar.gz.sha256) krew-linux_amd64.tar.gz" | sha256sum --check
tar xzf krew-linux_amd64.tar.gz ./krew-linux_amd64
./krew-linux_amd64 install krew 2>&1 | tail -12
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
kubectl krew version | head -4tarCreate or extract tape archives. xzf extracts a gzip-compressed archive.xzftar flags: extract, filter through gzip, and use the following file as the archive.krew-linux_amd64.tar.gzThe downloaded release archive to extract../krew-linux_amd64The Krew installer binary extracted from the archive. Running it with install krew puts kubectl-krew in ~/.krew/bin.
Explain this command
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz.sha256
echo "$(cat krew-linux_amd64.tar.gz.sha256) krew-linux_amd64.tar.gz" | sha256sum --check
tar xzf krew-linux_amd64.tar.gz ./krew-linux_amd64
./krew-linux_amd64 install krew 2>&1 | tail -12
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
kubectl krew version | head -4./krew-linux_amd64The Krew installer binary extracted from the archive. Running it with install krew puts kubectl-krew in ~/.krew/bin.installThe Krew installer subcommand. install krew writes the kubectl-krew plugin into ~/.krew/bin.krewThe argument to the installer, meaning install the krew plugin itself.2>&1A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.|A shell pipe. The output of the command on the left becomes the input of the command on the right.tailShell. Print the last lines of its input.-12How many lines from the end of the input tail should print.
Explain this command
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz.sha256
echo "$(cat krew-linux_amd64.tar.gz.sha256) krew-linux_amd64.tar.gz" | sha256sum --check
tar xzf krew-linux_amd64.tar.gz ./krew-linux_amd64
./krew-linux_amd64 install krew 2>&1 | tail -12
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
kubectl krew version | head -4exportShell. Set an environment variable in this shell so later commands in the session can see it.PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"Put the Krew bin directory first on PATH so kubectl can find plugins that krew installed.
Explain this command
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz
curl -sSLO https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz.sha256
echo "$(cat krew-linux_amd64.tar.gz.sha256) krew-linux_amd64.tar.gz" | sha256sum --check
tar xzf krew-linux_amd64.tar.gz ./krew-linux_amd64
./krew-linux_amd64 install krew 2>&1 | tail -12
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
kubectl krew version | head -4kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.krewThe kubectl plugin manager. kubectl krew install adds a plugin from the community index. The Kubernetes plugin docs warn that Krew plugins are not audited for security.versionPrint the Krew client version and the paths it is using.|A shell pipe. The output of the command on the left becomes the input of the command on the right.headShell. Print the first lines of its input.-4How many lines from the start of the input head should print: 4.
krew-linux_amd64.tar.gz: OK
...
OPTION VALUE
GitTag v0.5.0
GitCommit 8a4a6ff
IndexURI https://github.com/kubernetes-sigs/krew-index.git
Put the export PATH line in your ~/.bashrc so it survives a new shell. Install two plugins from the index, tree (shows ownership chains) and neat (removes the noise from -o yaml output):
kubectl krew install tree neat 2>&1 | grep -v '^\s*|' | tail -8
kubectl krew listExplain this command
kubectl krew install tree neat 2>&1 | grep -v '^\s*|' | tail -8
kubectl krew listkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.krewThe kubectl plugin manager. kubectl krew install adds a plugin from the community index. The Kubernetes plugin docs warn that Krew plugins are not audited for security.installInstall the named plugin or plugins from the Krew index into ~/.krew/bin. The Kubernetes plugin docs warn that Krew plugins are not audited for security.treeA community plugin name on the Krew index. The lesson installs tree.neatA community plugin name on the Krew index. The lesson installs neat.2>&1A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.|A shell pipe. The output of the command on the left becomes the input of the command on the right.grepShell. Prints input lines that match a pattern.-vWith grep, print the lines that do not match the pattern.'^\s*|'The pattern grep -v drops: lines that start with a pipe character, the frame krew draws around its install message.|A shell pipe. The output of the command on the left becomes the input of the command on the right.tailShell. Print the last lines of its input.-8How many lines from the end of the input tail should print.
Explain this command
kubectl krew install tree neat 2>&1 | grep -v '^\s*|' | tail -8
kubectl krew listkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.krewThe kubectl plugin manager. kubectl krew install adds a plugin from the community index. The Kubernetes plugin docs warn that Krew plugins are not audited for security.listList the plugins krew has installed in this account.
...
Installed plugin: neat
...
WARNING: You installed plugin "neat" from the krew-index plugin repository.
These plugins are not audited for security by the Krew maintainers.
Run them at your own risk.
krew
neat
tree
Take that warning seriously: a plugin runs with your credentials. kubectl krew info <name> shows where the archive comes from and its checksum, so you can look at the project first. Then use them:
kubectl tree deployment web
kubectl get pod client -o yaml | wc -l
kubectl get pod client -o yaml | kubectl neat | wc -lExplain this command
kubectl tree deployment web
kubectl get pod client -o yaml | wc -l
kubectl get pod client -o yaml | kubectl neat | wc -lkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.treeThe tree plugin. It prints the ownership tree of a resource, such as the ReplicaSets and Pods a Deployment owns.deploymentThe resource type.webThe Deployment name. tree prints the objects this Deployment owns.
Explain this command
kubectl tree deployment web
kubectl get pod client -o yaml | wc -l
kubectl get pod client -o yaml | kubectl neat | wc -lkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.clientThe client Pod started earlier in this lesson.-oShort form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.yamlThe output format. This command prints yaml.|A shell pipe. The output of the command on the left becomes the input of the command on the right.wcShell. Count lines, words or bytes. With -l it prints the number of lines.-lWith wc, print the number of lines. The lesson uses this to show how much YAML kubectl neat removed.
Explain this command
kubectl tree deployment web
kubectl get pod client -o yaml | wc -l
kubectl get pod client -o yaml | kubectl neat | wc -lkubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.getDisplay one or many resources. kubectl get prints a table of the most important information about the resources you name.podThe resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.clientThe client Pod started earlier in this lesson.-oShort form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.yamlThe output format. This command prints yaml.|A shell pipe. The output of the command on the left becomes the input of the command on the right.kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.neatThe neat plugin. It strips generated fields from the YAML or JSON that kubectl printed, which is why the line count drops.|A shell pipe. The output of the command on the left becomes the input of the command on the right.wcShell. Count lines, words or bytes. With -l it prints the number of lines.-lWith wc, print the number of lines. The lesson uses this to show how much YAML kubectl neat removed.
NAMESPACE NAME READY REASON STATUS AGE
k8s-from-zero Deployment/web - - 12m
k8s-from-zero ├─ReplicaSet/web-854c556f4 - - 46s
k8s-from-zero │ ├─Pod/web-854c556f4-2mnfl True Current 46s
k8s-from-zero │ └─Pod/web-854c556f4-5qx6w True Current 46s
k8s-from-zero └─ReplicaSet/web-86f79b74d9 - - 12m
129
48
tree drew the ownership you met in part 7, including the old ReplicaSet kept for rollbacks. neat cut the Pod’s YAML from 129 lines to 48 by dropping status and server-set fields, which makes it a good start for a manifest you want to keep.
Completion, an alias and kuberc
kubectl can generate its own shell completion script. For bash it needs the bash-completion package first (install guide): sudo apt-get install bash-completion on Debian and Ubuntu, then open a new shell and check that type _init_completion prints a function. Then add these lines to ~/.bashrc, the same lines the quick reference gives for the alias:
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl kExplain this command
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl ksourceShell. Run the commands in the given file or process substitution in the current shell, so completion functions take effect.<(kubectl completion bash)A process substitution. bash runs kubectl completion bash and source reads that generated script in the current shell.
Explain this command
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl kaliasShell. Define a short name for a command. alias k=kubectl lets you type k in this shell.k=kubectlThe alias assignment: k becomes kubectl in this shell.
Explain this command
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl kcompleteThe bash builtin that registers completion. This line reuses kubectl's completion function for the alias k.-oFor the bash complete builtin, this option controls how completion is attempted. default uses bash's default filename completion as a fallback.defaultA complete option value. default lets bash fall back to its usual completion after kubectl's function runs.-FTell complete to call this function when generating completions.__start_kubectlThe completion function kubectl generates for bash. complete registers it for the alias k.kThe alias that should receive the same completion as kubectl.
In a new shell, k get po followed by Tab completed to k get pod in our test, k logs web- followed by Tab completed to the common prefix k logs web-854c556f4-, and k get pods -n kube-sy followed by Tab completed to kube-system. Completion asks the API server for live names, so it works for Pods that appeared a second ago.
The newer way to shorten commands is kuberc, a preferences file for kubectl that holds aliases and default flags and no cluster details or credentials. It is beta since Kubernetes v1.34 and lives at ~/.kube/kuberc, or wherever the KUBERC variable or --kuberc flag points (kuberc). Save this as kuberc.yaml:
apiVersion: kubectl.config.k8s.io/v1beta1
kind: Preference
aliases:
- name: pods
command: get
prependArgs:
- pods
options:
- name: output
default: wide
defaults:
- command: delete
options:
- name: interactive
default: "true"
The alias kubectl pods becomes kubectl get pods -o wide, and every kubectl delete asks before it deletes:
KUBERC=$PWD/kuberc.yaml kubectl pods
echo n | KUBERC=$PWD/kuberc.yaml kubectl delete pod noshell
KUBERC=off kubectl pods 2>&1 | head -3Explain this command
KUBERC=$PWD/kuberc.yaml kubectl pods
echo n | KUBERC=$PWD/kuberc.yaml kubectl delete pod noshell
KUBERC=off kubectl pods 2>&1 | head -3KUBERC=$PWD/kuberc.yamlPoint kubectl at this kuberc file for this command only, instead of $HOME/.kube/kuberc. The lesson uses the file in the current directory.kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.podsThe pods alias defined in the lesson's kuberc file. It expands to get pods, and in this lesson also defaults to wide output. Without a matching kuberc, kubectl would reject pods as an unknown command.
Explain this command
KUBERC=$PWD/kuberc.yaml kubectl pods
echo n | KUBERC=$PWD/kuberc.yaml kubectl delete pod noshell
KUBERC=off kubectl pods 2>&1 | head -3echoShell. Print its arguments. When it is piped into another command, those arguments become that command's input.nThe answer sent to kubectl delete when kuberc has made --interactive the default: n means do not delete.|A shell pipe. The output of the command on the left becomes the input of the command on the right.KUBERC=$PWD/kuberc.yamlPoint kubectl at this kuberc file for this command only, instead of $HOME/.kube/kuberc. The lesson uses the file in the current directory.kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.deleteDelete resources by file names, stdin, resources and names, or by resources and label selector.podThe resource type.noshellThe noshell Pod from part 22. The lesson tries to delete it to show that the delete is refused or asks first.
Explain this command
KUBERC=$PWD/kuberc.yaml kubectl pods
echo n | KUBERC=$PWD/kuberc.yaml kubectl delete pod noshell
KUBERC=off kubectl pods 2>&1 | head -3KUBERC=offDisable kuberc for this command. The kuberc docs say you can set KUBERC=off, or export KUBECTL_KUBERC=false, to turn the feature off.kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.podsThe pods alias defined in the lesson's kuberc file. It expands to get pods, and in this lesson also defaults to wide output. Without a matching kuberc, kubectl would reject pods as an unknown command.2>&1A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.|A shell pipe. The output of the command on the left becomes the input of the command on the right.headShell. Print the first lines of its input.-3How many lines from the start of the input head should print: 3.
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
client 1/1 Running 0 84s 10.244.1.11 k8s-ep22-worker <none> <none>
noshell 1/1 Running 0 3m 10.244.1.10 k8s-ep22-worker <none> <none>
web-854c556f4-2mnfl 1/1 Running 0 66s 10.244.1.13 k8s-ep22-worker <none> <none>
web-854c556f4-5qx6w 1/1 Running 0 66s 10.244.1.12 k8s-ep22-worker <none> <none>
You are about to delete the following 1 resource(s):
pod/noshell
Do you want to continue? (y/N): deletion is cancelled
error: unknown command "pods" for "kubectl"
...
KUBERC=off turns the file off, which is what scripts should do so that your personal defaults never change their behaviour. A flag you type yourself takes precedence over a kuberc default; in our test kubectl delete configmap scratch --interactive=false deleted without asking, with the kuberc file still active.
kubectl explain is the manual you already have
When you forget a field name, kubectl explain reads the schema from your own cluster, so it matches your version exactly. --recursive prints the whole tree under a field:
kubectl explain deployment.spec.strategy --recursive
kubectl explain deployment.spec.strategy.rollingUpdate.maxSurge | sed -n '1,12p'Explain this command
kubectl explain deployment.spec.strategy --recursive
kubectl explain deployment.spec.strategy.rollingUpdate.maxSurge | sed -n '1,12p'kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.explainDescribe the fields of an API resource, using the OpenAPI schema from the cluster. The explain docs show --recursive and --max-depth.deployment.spec.strategyThe resource and field path passed to kubectl explain, written as TYPE.FIELDNAME[.FIELDNAME]. The explain docs use this form.--recursiveWith kubectl explain, print nested fields as well as the top-level ones. Use --max-depth to cap how deep that goes.
Explain this command
kubectl explain deployment.spec.strategy --recursive
kubectl explain deployment.spec.strategy.rollingUpdate.maxSurge | sed -n '1,12p'kubectlThe Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.explainDescribe the fields of an API resource, using the OpenAPI schema from the cluster. The explain docs show --recursive and --max-depth.deployment.spec.strategy.rollingUpdate.maxSurgeThe resource and field path passed to kubectl explain, written as TYPE.FIELDNAME[.FIELDNAME]. The explain docs use this form.|A shell pipe. The output of the command on the left becomes the input of the command on the right.sedThe stream editor. With -n and a line range it prints only those lines.-nWith sed, print nothing unless a command asks for it.'1,12p'Print lines 1 to 12 of the input, the start of the explain output.
GROUP: apps
KIND: Deployment
VERSION: v1
FIELD: strategy <DeploymentStrategy>
...
FIELDS:
rollingUpdate <RollingUpdateDeployment>
maxSurge <IntOrString>
maxUnavailable <IntOrString>
type <string>
enum: Recreate, RollingUpdate
...
DESCRIPTION:
The maximum number of pods that can be scheduled above the desired number of
pods. Value can be an absolute number (ex: 5) or a percentage of desired
pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number is
calculated from percentage by rounding up. Defaults to 25%. Example: when
...
That is the same 25% default you met with rolling updates in part 7, straight from the API server. kubectl explain pods --recursive --max-depth=2 limits the depth when the full tree is too long.
When it goes wrong
bash: _get_comp_words_by_ref: command not foundon Tab. bash-completion is missing; we hit exactly this on our lab box. Install the package and open a new shell.unknown command "whoami-ns"for your own plugin. Dashes in the file name split subcommands. Use an underscore in the file name, or a name without dashes.- A plugin is listed but does not run.
kubectl plugin listprints a warning for files that are not executable or that shadow another plugin. Runchmod +xand remove the duplicate. - kubectx changed the wrong file. It edits whatever
KUBECONFIGpoints to. If you keep one kubeconfig per cluster, checkecho $KUBECONFIGbefore you switch. - stern shows nothing. Check the namespace (stern uses the context’s namespace, or
-n, or-A), and remember that the query is a regular expression:webalso matcheswebhook.
Cheat sheet
| Task | Command or field | Note |
|---|---|---|
| List contexts | kubectx |
current one with -c |
| Switch context | kubectx lab |
kubectx - goes back |
| Rename a context | kubectx short=long-generated-name |
edits kubeconfig |
| Read-only shell | kubectx -r <context> |
writes refused on your side only |
| Set namespace | kubens k8s-from-zero |
for the current context |
| Show namespace | kubens -c |
|
| Tail all web Pods | stern web |
regex on Pod names |
| Tail by label | stern -l app.kubernetes.io/name=web |
add -A for all namespaces |
| Only new lines | stern web --tail 0 |
--since 10s for recent ones |
| Bare log lines | stern web -o raw |
for piping |
| Your own plugin | kubectl-<name> executable on PATH |
kubectl plugin list |
| Install a plugin | kubectl krew install tree |
read kubectl krew info first |
| Ownership tree | kubectl tree deployment web |
krew plugin |
| Clean YAML | kubectl get pod x -o yaml | kubectl neat |
krew plugin |
| Completion | source <(kubectl completion bash) |
needs bash-completion |
| Alias with completion | alias k=kubectl; complete -o default -F __start_kubectl k |
in ~/.bashrc |
| kuberc file | ~/.kube/kuberc or KUBERC=path |
KUBERC=off in scripts |
| Field docs | kubectl explain deployment.spec.strategy --recursive |
from your cluster’s schema |
Recap
- kubectx and kubens change two fields in your kubeconfig, and
kubectx -rgives you a read-only shell for looking around. - stern follows every Pod that matches a name or label, and picks up new Pods during a rollout.
- Any executable named
kubectl-<name>on your PATH is a plugin; krew installs community plugins and warns that they are not audited. - Completion, an alias and kuberc cut typing;
KUBERC=offkeeps scripts predictable. kubectl explain --recursiveanswers field questions from your own cluster.
Command explanations and quoted passages in this lesson come from or adapt the kubectl reference and the Kubernetes documentation, © The Kubernetes Authors, licensed under CC BY 4.0. We shortened and edited some passages. Kubernetes® is a registered trademark of The Linux Foundation. aitamer.news is not affiliated with or endorsed by The Linux Foundation or the CNCF.

The Campfire
CommentsThanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.
Add a comment