Specimen No. 1577 · Habitat H4 · DevOps & IT

Kubernetes from Zero · Workloads & Troubleshooting 11 of 14 · Lesson 23

Kubernetes from Zero, part 23: kubectl power tools for contexts, logs and plugins

Switch contexts and namespaces with kubectx and kubens, tail many Pods with stern, add plugins with krew, and set up completion, aliases and kuberc.

WILDNESS2 / 5 · MOSTLY TAMED
Verified: Installed and ran kubectx 0.11.0, stern 1.35.0, krew 0.5.0 and kubectl 1.37.2 on kind, 10 Nov 2026Only claimed: Project status and licences from GitHub; kuberc beta status from the Kubernetes docs
An unrolled cut-paper canvas tool roll holds a steel-blue spike, a folded knife, a brass spyglass, a coil of cord and a rust-red handled fid on a sand-coloured deck, with a slate-blue ship's wheel behind it.
Generated cover art. Not a photo.

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.

A kubeconfig file holds a cluster, a user and two contexts, kind-k8s-ep22 with namespace k8s-from-zero and lab with namespace kube-system; the highlighted current-context field points at one of them. kubectx changes current-context, kubens changes the namespace of the current context, and kubectx -r opens a read-only shell.

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:

bash
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.txt

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.txt
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubectx_v0.11.0_linux_x86_64.tar.gz The 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.txt
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubectx_v0.11.0_linux_x86_64.tar.gz The 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.txt
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubens_v0.11.0_linux_x86_64.tar.gz The 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.txt
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/ahmetb/kubectx/releases/download/v0.11.0/kubens_v0.11.0_linux_x86_64.tar.gz The 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.txt
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/ahmetb/kubectx/releases/download/v0.11.0/checksums.txt The 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.txt
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/ahmetb/kubectx/releases/download/v0.11.0/checksums.txt The 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.txt
  1. sha256sum Print or check SHA-256 checksums. With --check it verifies downloaded files against a checksums file.
  2. --check With sha256sum, verify files against the checksums listed in the given file.
  3. --ignore-missing With 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.
  4. checksums.txt The 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.txt
  1. sha256sum Print or check SHA-256 checksums. With --check it verifies downloaded files against a checksums file.
  2. --check With sha256sum, verify files against the checksums listed in the given file.
  3. --ignore-missing With 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.
  4. checksums.txt The 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.txt
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/stern/stern/releases/download/v1.35.0/stern_1.35.0_linux_amd64.tar.gz The 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.txt
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/stern/stern/releases/download/v1.35.0/stern_1.35.0_linux_amd64.tar.gz The 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.txt
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSL curl flags: silent, show errors, follow redirects.
  3. -o Write the download to this file instead of using the remote name.
  4. stern-checksums.txt The checksum file from the same GitHub release as the archive. sha256sum --check reads this file.
  5. https://github.com/stern/stern/releases/download/v1.35.0/checksums.txt The 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.txt
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSL curl flags: silent, show errors, follow redirects.
  3. -o Write the download to this file instead of using the remote name.
  4. stern-checksums.txt The checksum file from the same GitHub release as the archive. sha256sum --check reads this file.
  5. https://github.com/stern/stern/releases/download/v1.35.0/checksums.txt The 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.txt
  1. sha256sum Print or check SHA-256 checksums. With --check it verifies downloaded files against a checksums file.
  2. --check With sha256sum, verify files against the checksums listed in the given file.
  3. --ignore-missing With 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.
  4. stern-checksums.txt The 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.txt
  1. sha256sum Print or check SHA-256 checksums. With --check it verifies downloaded files against a checksums file.
  2. --check With sha256sum, verify files against the checksums listed in the given file.
  3. --ignore-missing With 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.
  4. stern-checksums.txt The 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:

bash
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 --version

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 --version
  1. mkdir Create a directory. -p creates parent directories if they are missing and does not fail if the directory already exists.
  2. -p For mkdir, create parent directories as needed and do not fail if the directory already exists.
  3. ~/.local/bin A 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 --version
  1. mkdir Create a directory. -p creates parent directories if they are missing and does not fail if the directory already exists.
  2. -p For mkdir, create parent directories as needed and do not fail if the directory already exists.
  3. ~/.local/bin A 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 --version
  1. tar Create or extract tape archives. xzf extracts a gzip-compressed archive.
  2. xzf tar flags: extract, filter through gzip, and use the following file as the archive.
  3. kubectx_v0.11.0_linux_x86_64.tar.gz The downloaded release archive to extract.
  4. -C With tar, change to this directory before extracting, so the binary lands in ~/.local/bin.
  5. ~/.local/bin A directory on a typical user PATH, used here so the installed binaries can be run without sudo.
  6. kubectx The 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 --version
  1. tar Create or extract tape archives. xzf extracts a gzip-compressed archive.
  2. xzf tar flags: extract, filter through gzip, and use the following file as the archive.
  3. kubectx_v0.11.0_linux_x86_64.tar.gz The downloaded release archive to extract.
  4. -C With tar, change to this directory before extracting, so the binary lands in ~/.local/bin.
  5. ~/.local/bin A directory on a typical user PATH, used here so the installed binaries can be run without sudo.
  6. kubectx The 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 --version
  1. tar Create or extract tape archives. xzf extracts a gzip-compressed archive.
  2. xzf tar flags: extract, filter through gzip, and use the following file as the archive.
  3. kubens_v0.11.0_linux_x86_64.tar.gz The downloaded release archive to extract.
  4. -C With tar, change to this directory before extracting, so the binary lands in ~/.local/bin.
  5. ~/.local/bin A directory on a typical user PATH, used here so the installed binaries can be run without sudo.
  6. kubens The 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 --version
  1. tar Create or extract tape archives. xzf extracts a gzip-compressed archive.
  2. xzf tar flags: extract, filter through gzip, and use the following file as the archive.
  3. kubens_v0.11.0_linux_x86_64.tar.gz The downloaded release archive to extract.
  4. -C With tar, change to this directory before extracting, so the binary lands in ~/.local/bin.
  5. ~/.local/bin A directory on a typical user PATH, used here so the installed binaries can be run without sudo.
  6. kubens The 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 --version
  1. tar Create or extract tape archives. xzf extracts a gzip-compressed archive.
  2. xzf tar flags: extract, filter through gzip, and use the following file as the archive.
  3. stern_1.35.0_linux_amd64.tar.gz The downloaded release archive to extract.
  4. -C With tar, change to this directory before extracting, so the binary lands in ~/.local/bin.
  5. ~/.local/bin A directory on a typical user PATH, used here so the installed binaries can be run without sudo.
  6. stern The 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 --version
  1. tar Create or extract tape archives. xzf extracts a gzip-compressed archive.
  2. xzf tar flags: extract, filter through gzip, and use the following file as the archive.
  3. stern_1.35.0_linux_amd64.tar.gz The downloaded release archive to extract.
  4. -C With tar, change to this directory before extracting, so the binary lands in ~/.local/bin.
  5. ~/.local/bin A directory on a typical user PATH, used here so the installed binaries can be run without sudo.
  6. stern The 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 --version
  1. stern Tail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.
  2. --version Print the version of this command.
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 --version
  1. stern Tail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.
  2. --version Print 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

  1. 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:
bash
kubectl config set-context lab-system --cluster=kind-k8s-ep22 --user=kind-k8s-ep22 --namespace=kube-system
kubectx
kubectx -c

Explain this command

kubectl config set-context lab-system --cluster=kind-k8s-ep22 --user=kind-k8s-ep22 --namespace=kube-system
kubectx
kubectx -c
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. config The command group for kubeconfig entries: contexts, clusters and users.
  3. set-context Set 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.
  4. lab-system The context name the lesson creates for kube-system work.
  5. --cluster=kind-k8s-ep22 The kubeconfig cluster name to set on this context, here kind-k8s-ep22.
  6. --user=kind-k8s-ep22 The kubeconfig user name to set on this context, here kind-k8s-ep22.
  7. --namespace=kube-system The 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 -c
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. config The command group for kubeconfig entries: contexts, clusters and users.
  3. set-context Set 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.
  4. lab-system The context name the lesson creates for kube-system work.
  5. --cluster=kind-k8s-ep22 The kubeconfig cluster name to set on this context, here kind-k8s-ep22.
  6. --user=kind-k8s-ep22 The kubeconfig user name to set on this context, here kind-k8s-ep22.
  7. --namespace=kube-system The 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 -c
  1. kubectx Switch, 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 -c
  1. kubectx Switch, 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 -c
  1. kubectx Switch, 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.
  2. -c Print the current context name and exit. kubectx does not switch context when you pass -c.
Explain this command
kubectl config set-context lab-system --cluster=kind-k8s-ep22 --user=kind-k8s-ep22 --namespace=kube-system
kubectx
kubectx -c
  1. kubectx Switch, 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.
  2. -c Print 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
  1. Replace kind-k8s-ep22 with your cluster’s names (kubectl config get-contexts shows them). Switch, look, and switch back with -, the same idea as cd -:
bash
kubectx lab-system
kubectl get pods | head -4
kubectx -

Explain this command

kubectx lab-system
kubectl get pods | head -4
kubectx -
  1. kubectx Switch, 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.
  2. lab-system The 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 -
  1. kubectx Switch, 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.
  2. lab-system The 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 -
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pods The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  5. head Shell. Print the first lines of its input.
  6. -4 How many lines from the start of the input head should print: 4.
Explain this command
kubectx lab-system
kubectl get pods | head -4
kubectx -
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pods The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  5. head Shell. Print the first lines of its input.
  6. -4 How many lines from the start of the input head should print: 4.

Explain this command

kubectx lab-system
kubectl get pods | head -4
kubectx -
  1. kubectx Switch, 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.
  2. - Switch back to the previous context. kubectx stores that name when you last switched.
Explain this command
kubectx lab-system
kubectl get pods | head -4
kubectx -
  1. kubectx Switch, 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.
  2. - 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".
  1. Now set the namespace for the current context, so you can stop typing -n:
bash
kubens k8s-from-zero
kubectl get pods
kubens -c

Explain this command

kubens k8s-from-zero
kubectl get pods
kubens -c
  1. kubens Switch or list namespaces for the current context. kubens edits the namespace field in your kubeconfig.
  2. k8s-from-zero The 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 -c
  1. kubens Switch or list namespaces for the current context. kubens edits the namespace field in your kubeconfig.
  2. k8s-from-zero The 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 -c
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pods The 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 -c
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pods The 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 -c
  1. kubens Switch or list namespaces for the current context. kubens edits the namespace field in your kubeconfig.
  2. -c Print the current namespace for this context and exit.
Explain this command
kubens k8s-from-zero
kubectl get pods
kubens -c
  1. kubens Switch or list namespaces for the current context. kubens edits the namespace field in your kubeconfig.
  2. -c Print 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
  1. Rename a context with a long cloud-generated name to something short, and look at the file kubectl reads:
bash
kubectx lab=lab-system
kubectl config get-contexts

Explain this command

kubectx lab=lab-system
kubectl config get-contexts
  1. kubectx Switch, 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.
  2. lab=lab-system Rename 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-contexts
  1. kubectx Switch, 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.
  2. lab=lab-system Rename 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-contexts
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. config The command group for kubeconfig entries: contexts, clusters and users.
  3. get-contexts List the contexts in your kubeconfig, with a star on the current one.
Explain this command
kubectx lab=lab-system
kubectl config get-contexts
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. config The command group for kubeconfig entries: contexts, clusters and users.
  3. get-contexts List 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.

  1. 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:
bash
kubectx -r kind-k8s-ep22
kubectl get pods
kubectl delete pod noshell
exit

Explain this command

kubectx -r kind-k8s-ep22
kubectl get pods
kubectl delete pod noshell
exit
  1. kubectx Switch, 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.
  2. -r Start a subshell in which kubectl cannot write. The kubectx help describes this as a read-only session. exit leaves the subshell.
  3. kind-k8s-ep22 The 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
exit
  1. kubectx Switch, 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.
  2. -r Start a subshell in which kubectl cannot write. The kubectx help describes this as a read-only session. exit leaves the subshell.
  3. kind-k8s-ep22 The 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
exit
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pods The 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
exit
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pods The 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
exit
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. noshell The 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
exit
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  3. pod The resource type.
  4. noshell The 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
exit
  1. exit Leave the current shell. After kubectx -r, this returns you to the writable shell you started from.
Explain this command
kubectx -r kind-k8s-ep22
kubectl get pods
kubectl delete pod noshell
exit
  1. exit Leave 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).

stern web, or stern -l with the label, matches the two old Pods and also the two new Pods that a rollout creates, highlighted, which stern adds on its own; all streams merge into one terminal, each line prefixed with the Pod and container name.

If you do not have a client from part 22 any more, start one that calls the Service every second:

bash
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'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. run Create and run a particular image in a pod. The run docs say this is intended for simple development and debug use.
  3. client The Pod name passed to kubectl run.
  4. --image=busybox:1.37 The container image, set here to busybox:1.37.
  5. -- 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.
  6. sh A shell. The command that follows runs inside it.
  7. -c An argument of sh. It tells sh to run the command string that follows.
  8. '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.
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'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. run Create and run a particular image in a pod. The run docs say this is intended for simple development and debug use.
  3. client The Pod name passed to kubectl run.
  4. --image=busybox:1.37 The container image, set here to busybox:1.37.
  5. -- 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.
  6. sh A shell. The command that follows runs inside it.
  7. -c An argument of sh. It tells sh to run the command string that follows.
  8. '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:

bash
timeout 6 stern web --since 10s --no-follow --color never | cut -c1-150 | head -8

Explain this command

timeout 6 stern web --since 10s --no-follow --color never | cut -c1-150 | head -8
  1. timeout Run a command and stop it after the given number of seconds.
  2. 6 Stop stern after 6 seconds at most, so the command cannot hang.
  3. stern Tail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.
  4. web The Pod name or regular expression stern matches. Here it follows Pods whose names contain web.
  5. --since With stern, only show logs newer than this duration.
  6. 10s The duration passed to --since. stern keeps only logs newer than this.
  7. --no-follow With stern, print matching logs and exit instead of continuing to follow.
  8. --color Whether stern colours its output. never disables colour, which keeps the lab log readable.
  9. never The value passed to --color so stern does not colour its output, which keeps the lab log readable.
  10. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  11. cut Shell. Print only part of each input line.
  12. -c1-150 Keep characters 1 to 150 of each line, so long log lines stay readable.
  13. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  14. head Shell. Print the first lines of its input.
  15. -8 How many lines from the start of the input head should print.
Explain this command
timeout 6 stern web --since 10s --no-follow --color never | cut -c1-150 | head -8
  1. timeout Run a command and stop it after the given number of seconds.
  2. 6 Stop stern after 6 seconds at most, so the command cannot hang.
  3. stern Tail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.
  4. web The Pod name or regular expression stern matches. Here it follows Pods whose names contain web.
  5. --since With stern, only show logs newer than this duration.
  6. 10s The duration passed to --since. stern keeps only logs newer than this.
  7. --no-follow With stern, print matching logs and exit instead of continuing to follow.
  8. --color Whether stern colours its output. never disables colour, which keeps the lab log readable.
  9. never The value passed to --color so stern does not colour its output, which keeps the lab log readable.
  10. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  11. cut Shell. Print only part of each input line.
  12. -c1-150 Keep characters 1 to 150 of each line, so long log lines stay readable.
  13. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  14. head Shell. Print the first lines of its input.
  15. -8 How 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:

bash
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=$?
  1. timeout Run a command and stop it after the given number of seconds.
  2. 5 How many seconds timeout lets stern run before it stops it.
  3. stern Tail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.
  4. web The Pod name or regular expression stern matches. Here it follows Pods whose names contain web.
  5. --tail How many lines from the end of each container's logs to show.
  6. 0 Print no existing log lines; only lines that appear after stern starts are shown.
  7. --color Whether stern colours its output. never disables colour, which keeps the lab log readable.
  8. never The value passed to --color so stern does not colour its output, which keeps the lab log readable.
  9. 2>/dev/null A shell redirect. Standard error is discarded so the output stays short.
  10. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  11. cut Shell. Print only part of each input line.
  12. -c1-110 Keep 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=$?
  1. timeout Run a command and stop it after the given number of seconds.
  2. 5 How many seconds timeout lets stern run before it stops it.
  3. stern Tail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.
  4. web The Pod name or regular expression stern matches. Here it follows Pods whose names contain web.
  5. --tail How many lines from the end of each container's logs to show.
  6. 0 Print no existing log lines; only lines that appear after stern starts are shown.
  7. --color Whether stern colours its output. never disables colour, which keeps the lab log readable.
  8. never The value passed to --color so stern does not colour its output, which keeps the lab log readable.
  9. 2>/dev/null A shell redirect. Standard error is discarded so the output stays short.
  10. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  11. cut Shell. Print only part of each input line.
  12. -c1-110 Keep 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=$?
  1. echo Shell. Print its arguments. When it is piped into another command, those arguments become that command's input.
  2. 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.
Explain this command
timeout 5 stern web --tail 0 --color never 2>/dev/null | cut -c1-110; echo exit=$?
  1. echo Shell. Print its arguments. When it is piped into another command, those arguments become that command's input.
  2. 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:

bash
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 '^[+-]'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. rollout The command group for rollout actions.
  3. restart Trigger a rolling restart of the named resource. The rollout docs say this is for a Deployment, DaemonSet or StatefulSet.
  4. deployment/web The resource written as type/name: deployment/web.
  5. >/dev/null A 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 '^[+-]'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. rollout The command group for rollout actions.
  3. restart Trigger a rolling restart of the named resource. The rollout docs say this is for a Deployment, DaemonSet or StatefulSet.
  4. deployment/web The resource written as type/name: deployment/web.
  5. >/dev/null A 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 '^[+-]'
  1. timeout Run a command and stop it after the given number of seconds.
  2. 25 Let stern run for 25 seconds, long enough for the rollout to replace both Pods.
  3. stern Tail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.
  4. web The Pod name or regular expression stern matches. Here it follows Pods whose names contain web.
  5. --tail How many lines from the end of each container's logs to show.
  6. 0 Print no existing log lines; only lines that appear after stern starts are shown.
  7. --color Whether stern colours its output. never disables colour, which keeps the lab log readable.
  8. never The value passed to --color so stern does not colour its output, which keeps the lab log readable.
  9. --container-state With stern, only follow containers in this state, such as running, waiting or terminated.
  10. running The container state passed to --container-state. Only running containers are followed.
  11. 2>&1 A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.
  12. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  13. grep Shell. Prints input lines that match a pattern.
  14. -E Tell grep to treat the pattern as an extended regular expression.
  15. '^[+-]' 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.
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 '^[+-]'
  1. timeout Run a command and stop it after the given number of seconds.
  2. 25 Let stern run for 25 seconds, long enough for the rollout to replace both Pods.
  3. stern Tail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.
  4. web The Pod name or regular expression stern matches. Here it follows Pods whose names contain web.
  5. --tail How many lines from the end of each container's logs to show.
  6. 0 Print no existing log lines; only lines that appear after stern starts are shown.
  7. --color Whether stern colours its output. never disables colour, which keeps the lab log readable.
  8. never The value passed to --color so stern does not colour its output, which keeps the lab log readable.
  9. --container-state With stern, only follow containers in this state, such as running, waiting or terminated.
  10. running The container state passed to --container-state. Only running containers are followed.
  11. 2>&1 A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.
  12. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  13. grep Shell. Prints input lines that match a pattern.
  14. -E Tell grep to treat the pattern as an extended regular expression.
  15. '^[+-]' 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:

bash
timeout 6 stern -l app.kubernetes.io/name=web --tail 1 --no-follow --color never -o raw | cut -c1-120

Explain this command

timeout 6 stern -l app.kubernetes.io/name=web --tail 1 --no-follow --color never -o raw | cut -c1-120
  1. timeout Run a command and stop it after the given number of seconds.
  2. 6 Stop stern after 6 seconds at most, so the command cannot hang.
  3. stern Tail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.
  4. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  5. app.kubernetes.io/name=web The label selector passed to -l. It keeps only the objects whose labels match this query.
  6. --tail How many lines from the end of each container's logs to show.
  7. 1 Print this many existing log lines from the end of each matching container, then stop or follow depending on the other flags.
  8. --no-follow With stern, print matching logs and exit instead of continuing to follow.
  9. --color Whether stern colours its output. never disables colour, which keeps the lab log readable.
  10. never The value passed to --color so stern does not colour its output, which keeps the lab log readable.
  11. -o Short form of --output. stern formats include default, raw, json and extjson. raw prints only the log line.
  12. raw A stern output format that prints only the log line, without a Pod or container prefix.
  13. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  14. cut Shell. Print only part of each input line.
  15. -c1-120 Keep characters 1 to 120 of each line.
Explain this command
timeout 6 stern -l app.kubernetes.io/name=web --tail 1 --no-follow --color never -o raw | cut -c1-120
  1. timeout Run a command and stop it after the given number of seconds.
  2. 6 Stop stern after 6 seconds at most, so the command cannot hang.
  3. stern Tail logs from multiple Pods that match a name or label, including new Pods that appear during a rollout.
  4. -l Short form of --selector. A label query. The get docs say it supports '=', '==', '!=', 'in' and 'notin', and that matching objects must satisfy every constraint.
  5. app.kubernetes.io/name=web The label selector passed to -l. It keeps only the objects whose labels match this query.
  6. --tail How many lines from the end of each container's logs to show.
  7. 1 Print this many existing log lines from the end of each matching container, then stop or follow depending on the other flags.
  8. --no-follow With stern, print matching logs and exit instead of continuing to follow.
  9. --color Whether stern colours its output. never disables colour, which keeps the lab log readable.
  10. never The value passed to --color so stern does not colour its output, which keeps the lab log readable.
  11. -o Short form of --output. stern formats include default, raw, json and extjson. raw prints only the log line.
  12. raw A stern output format that prints only the log line, without a Pod or container prefix.
  13. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  14. cut Shell. Print only part of each input line.
  15. -c1-120 Keep 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.

kubectl tree makes kubectl look for an executable named kubectl-tree on the PATH and run it. krew install reads the plugin’s manifest from the krew-index Git repository, downloads the archive, verifies its sha256 and links the binary into the krew bin directory; the highlighted warning says plugins are not audited for security. Your own script needs no krew.

Save this as kubectl-here:

bash
#!/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}"
  1. #!/bin/sh The shebang. This file is a POSIX shell script.
  2. ctx=$(kubectl config current-context) Capture the current kubeconfig context name in ctx, using kubectl config current-context.
  3. 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.
  4. echo Shell. Print its arguments. When it is piped into another command, those arguments become that command's input.
  5. "context: $ctx" The string echo prints, including the value captured earlier in the script.
  6. echo Shell. Print its arguments. When it is piped into another command, those arguments become that command's input.
  7. "namespace: ${ns:-default}" The string echo prints, including the value captured earlier in the script.
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}"
  1. #!/bin/sh The shebang. This file is a POSIX shell script.
  2. ctx=$(kubectl config current-context) Capture the current kubeconfig context name in ctx, using kubectl config current-context.
  3. 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.
  4. echo Shell. Print its arguments. When it is piped into another command, those arguments become that command's input.
  5. "context: $ctx" The string echo prints, including the value captured earlier in the script.
  6. echo Shell. Print its arguments. When it is piped into another command, those arguments become that command's input.
  7. "namespace: ${ns:-default}" The string echo prints, including the value captured earlier in the script.
bash
chmod +x kubectl-here
cp kubectl-here ~/.local/bin/
kubectl here
kubectl plugin list 2>&1 | grep here

Explain this command

chmod +x kubectl-here
cp kubectl-here ~/.local/bin/
kubectl here
kubectl plugin list 2>&1 | grep here
  1. chmod Change file permissions. +x makes the file executable, which kubectl requires for a plugin.
  2. +x The chmod mode that adds execute permission, required so kubectl can run a plugin.
  3. kubectl-here The 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 here
  1. chmod Change file permissions. +x makes the file executable, which kubectl requires for a plugin.
  2. +x The chmod mode that adds execute permission, required so kubectl can run a plugin.
  3. kubectl-here The 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 here
  1. cp Shell. Copy a file to a new path.
  2. kubectl-here The plugin script from this lesson. kubectl finds it because it is named kubectl-here and sits on PATH.
  3. ~/.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 here
  1. cp Shell. Copy a file to a new path.
  2. kubectl-here The plugin script from this lesson. kubectl finds it because it is named kubectl-here and sits on PATH.
  3. ~/.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 here
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. here The 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 here
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. here The 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 here
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. plugin The command group for kubectl plugins. plugin list searches PATH for executables named kubectl-*.
  3. list List plugin executables found on PATH. The plugin docs say kubectl looks for executables named kubectl- followed by the plugin name.
  4. 2>&1 A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.
  5. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  6. grep Shell. Prints input lines that match a pattern.
  7. here The pattern grep keeps. The lesson checks that plugin list can see kubectl-here.
Explain this command
chmod +x kubectl-here
cp kubectl-here ~/.local/bin/
kubectl here
kubectl plugin list 2>&1 | grep here
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. plugin The command group for kubectl plugins. plugin list searches PATH for executables named kubectl-*.
  3. list List plugin executables found on PATH. The plugin docs say kubectl looks for executables named kubectl- followed by the plugin name.
  4. 2>&1 A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.
  5. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  6. grep Shell. Prints input lines that match a pattern.
  7. here The 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):

bash
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

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
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz The 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 -4
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz The 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 -4
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz.sha256 The 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 -4
  1. curl A command-line HTTP client. Here it downloads a release file from GitHub.
  2. -sSLO curl flags: silent, show errors, follow redirects, and save the file under its remote name.
  3. https://github.com/kubernetes-sigs/krew/releases/download/v0.5.0/krew-linux_amd64.tar.gz.sha256 The 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 -4
  1. echo Shell. Print its arguments. When it is piped into another command, those arguments become that command's input.
  2. "$(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.
  3. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  4. sha256sum Print or check SHA-256 checksums. With --check it verifies downloaded files against a checksums file.
  5. --check With 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 -4
  1. echo Shell. Print its arguments. When it is piped into another command, those arguments become that command's input.
  2. "$(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.
  3. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  4. sha256sum Print or check SHA-256 checksums. With --check it verifies downloaded files against a checksums file.
  5. --check With 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 -4
  1. tar Create or extract tape archives. xzf extracts a gzip-compressed archive.
  2. xzf tar flags: extract, filter through gzip, and use the following file as the archive.
  3. krew-linux_amd64.tar.gz The downloaded release archive to extract.
  4. ./krew-linux_amd64 The 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
  1. tar Create or extract tape archives. xzf extracts a gzip-compressed archive.
  2. xzf tar flags: extract, filter through gzip, and use the following file as the archive.
  3. krew-linux_amd64.tar.gz The downloaded release archive to extract.
  4. ./krew-linux_amd64 The 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
  1. ./krew-linux_amd64 The Krew installer binary extracted from the archive. Running it with install krew puts kubectl-krew in ~/.krew/bin.
  2. install The Krew installer subcommand. install krew writes the kubectl-krew plugin into ~/.krew/bin.
  3. krew The argument to the installer, meaning install the krew plugin itself.
  4. 2>&1 A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.
  5. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  6. tail Shell. Print the last lines of its input.
  7. -12 How 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 -4
  1. ./krew-linux_amd64 The Krew installer binary extracted from the archive. Running it with install krew puts kubectl-krew in ~/.krew/bin.
  2. install The Krew installer subcommand. install krew writes the kubectl-krew plugin into ~/.krew/bin.
  3. krew The argument to the installer, meaning install the krew plugin itself.
  4. 2>&1 A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.
  5. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  6. tail Shell. Print the last lines of its input.
  7. -12 How 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 -4
  1. export Shell. Set an environment variable in this shell so later commands in the session can see it.
  2. 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 -4
  1. export Shell. Set an environment variable in this shell so later commands in the session can see it.
  2. 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 -4
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. krew The 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.
  3. version Print the Krew client version and the paths it is using.
  4. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  5. head Shell. Print the first lines of its input.
  6. -4 How many lines from the start of the input head should print: 4.
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
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. krew The 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.
  3. version Print the Krew client version and the paths it is using.
  4. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  5. head Shell. Print the first lines of its input.
  6. -4 How 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):

bash
kubectl krew install tree neat 2>&1 | grep -v '^\s*|' | tail -8
kubectl krew list

Explain this command

kubectl krew install tree neat 2>&1 | grep -v '^\s*|' | tail -8
kubectl krew list
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. krew The 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.
  3. install Install 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.
  4. tree A community plugin name on the Krew index. The lesson installs tree.
  5. neat A community plugin name on the Krew index. The lesson installs neat.
  6. 2>&1 A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.
  7. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  8. grep Shell. Prints input lines that match a pattern.
  9. -v With grep, print the lines that do not match the pattern.
  10. '^\s*|' The pattern grep -v drops: lines that start with a pipe character, the frame krew draws around its install message.
  11. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  12. tail Shell. Print the last lines of its input.
  13. -8 How 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 list
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. krew The 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.
  3. install Install 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.
  4. tree A community plugin name on the Krew index. The lesson installs tree.
  5. neat A community plugin name on the Krew index. The lesson installs neat.
  6. 2>&1 A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.
  7. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  8. grep Shell. Prints input lines that match a pattern.
  9. -v With grep, print the lines that do not match the pattern.
  10. '^\s*|' The pattern grep -v drops: lines that start with a pipe character, the frame krew draws around its install message.
  11. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  12. tail Shell. Print the last lines of its input.
  13. -8 How 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 list
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. krew The 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.
  3. list List the plugins krew has installed in this account.
Explain this command
kubectl krew install tree neat 2>&1 | grep -v '^\s*|' | tail -8
kubectl krew list
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. krew The 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.
  3. list List 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:

bash
kubectl tree deployment web
kubectl get pod client -o yaml | wc -l
kubectl get pod client -o yaml | kubectl neat | wc -l

Explain this command

kubectl tree deployment web
kubectl get pod client -o yaml | wc -l
kubectl get pod client -o yaml | kubectl neat | wc -l
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. tree The tree plugin. It prints the ownership tree of a resource, such as the ReplicaSets and Pods a Deployment owns.
  3. deployment The resource type.
  4. web The 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 -l
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. tree The tree plugin. It prints the ownership tree of a resource, such as the ReplicaSets and Pods a Deployment owns.
  3. deployment The resource type.
  4. web The 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 -l
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. client The client Pod started earlier in this lesson.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. yaml The output format. This command prints yaml.
  7. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  8. wc Shell. Count lines, words or bytes. With -l it prints the number of lines.
  9. -l With 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 -l
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. client The client Pod started earlier in this lesson.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. yaml The output format. This command prints yaml.
  7. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  8. wc Shell. Count lines, words or bytes. With -l it prints the number of lines.
  9. -l With 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 -l
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. client The client Pod started earlier in this lesson.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. yaml The output format. This command prints yaml.
  7. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  8. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  9. neat The neat plugin. It strips generated fields from the YAML or JSON that kubectl printed, which is why the line count drops.
  10. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  11. wc Shell. Count lines, words or bytes. With -l it prints the number of lines.
  12. -l With 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 -l
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. get Display one or many resources. kubectl get prints a table of the most important information about the resources you name.
  3. pod The resource type to display. kubectl get points you to kubectl api-resources for the full list of types the server supports.
  4. client The client Pod started earlier in this lesson.
  5. -o Short form of --output. It selects the output format. For get, the formats include json, yaml, name, jsonpath, custom-columns and wide.
  6. yaml The output format. This command prints yaml.
  7. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  8. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  9. neat The neat plugin. It strips generated fields from the YAML or JSON that kubectl printed, which is why the line count drops.
  10. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  11. wc Shell. Count lines, words or bytes. With -l it prints the number of lines.
  12. -l With 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:

bash
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k

Explain this command

source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k
  1. source Shell. Run the commands in the given file or process substitution in the current shell, so completion functions take effect.
  2. <(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 k
  1. source Shell. Run the commands in the given file or process substitution in the current shell, so completion functions take effect.
  2. <(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 k
  1. alias Shell. Define a short name for a command. alias k=kubectl lets you type k in this shell.
  2. k=kubectl The alias assignment: k becomes kubectl in this shell.
Explain this command
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k
  1. alias Shell. Define a short name for a command. alias k=kubectl lets you type k in this shell.
  2. k=kubectl The alias assignment: k becomes kubectl in this shell.

Explain this command

source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k
  1. complete The bash builtin that registers completion. This line reuses kubectl's completion function for the alias k.
  2. -o For the bash complete builtin, this option controls how completion is attempted. default uses bash's default filename completion as a fallback.
  3. default A complete option value. default lets bash fall back to its usual completion after kubectl's function runs.
  4. -F Tell complete to call this function when generating completions.
  5. __start_kubectl The completion function kubectl generates for bash. complete registers it for the alias k.
  6. k The alias that should receive the same completion as kubectl.
Explain this command
source <(kubectl completion bash)
alias k=kubectl
complete -o default -F __start_kubectl k
  1. complete The bash builtin that registers completion. This line reuses kubectl's completion function for the alias k.
  2. -o For the bash complete builtin, this option controls how completion is attempted. default uses bash's default filename completion as a fallback.
  3. default A complete option value. default lets bash fall back to its usual completion after kubectl's function runs.
  4. -F Tell complete to call this function when generating completions.
  5. __start_kubectl The completion function kubectl generates for bash. complete registers it for the alias k.
  6. k The 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:

bash
KUBERC=$PWD/kuberc.yaml kubectl pods
echo n | KUBERC=$PWD/kuberc.yaml kubectl delete pod noshell
KUBERC=off kubectl pods 2>&1 | head -3

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 -3
  1. KUBERC=$PWD/kuberc.yaml Point kubectl at this kuberc file for this command only, instead of $HOME/.kube/kuberc. The lesson uses the file in the current directory.
  2. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  3. pods The 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 -3
  1. KUBERC=$PWD/kuberc.yaml Point kubectl at this kuberc file for this command only, instead of $HOME/.kube/kuberc. The lesson uses the file in the current directory.
  2. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  3. pods The 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 -3
  1. echo Shell. Print its arguments. When it is piped into another command, those arguments become that command's input.
  2. n The answer sent to kubectl delete when kuberc has made --interactive the default: n means do not delete.
  3. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  4. KUBERC=$PWD/kuberc.yaml Point kubectl at this kuberc file for this command only, instead of $HOME/.kube/kuberc. The lesson uses the file in the current directory.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  7. pod The resource type.
  8. noshell The 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 -3
  1. echo Shell. Print its arguments. When it is piped into another command, those arguments become that command's input.
  2. n The answer sent to kubectl delete when kuberc has made --interactive the default: n means do not delete.
  3. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  4. KUBERC=$PWD/kuberc.yaml Point kubectl at this kuberc file for this command only, instead of $HOME/.kube/kuberc. The lesson uses the file in the current directory.
  5. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  6. delete Delete resources by file names, stdin, resources and names, or by resources and label selector.
  7. pod The resource type.
  8. noshell The 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 -3
  1. KUBERC=off Disable kuberc for this command. The kuberc docs say you can set KUBERC=off, or export KUBECTL_KUBERC=false, to turn the feature off.
  2. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  3. pods The 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.
  4. 2>&1 A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.
  5. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  6. head Shell. Print the first lines of its input.
  7. -3 How many lines from the start of the input head should print: 3.
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 -3
  1. KUBERC=off Disable kuberc for this command. The kuberc docs say you can set KUBERC=off, or export KUBECTL_KUBERC=false, to turn the feature off.
  2. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  3. pods The 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.
  4. 2>&1 A shell redirect. Standard error is sent to the same place as standard output, so the pipe that follows sees both.
  5. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  6. head Shell. Print the first lines of its input.
  7. -3 How 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:

bash
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'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. explain Describe the fields of an API resource, using the OpenAPI schema from the cluster. The explain docs show --recursive and --max-depth.
  3. deployment.spec.strategy The resource and field path passed to kubectl explain, written as TYPE.FIELDNAME[.FIELDNAME]. The explain docs use this form.
  4. --recursive With 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'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. explain Describe the fields of an API resource, using the OpenAPI schema from the cluster. The explain docs show --recursive and --max-depth.
  3. deployment.spec.strategy The resource and field path passed to kubectl explain, written as TYPE.FIELDNAME[.FIELDNAME]. The explain docs use this form.
  4. --recursive With 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'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. explain Describe the fields of an API resource, using the OpenAPI schema from the cluster. The explain docs show --recursive and --max-depth.
  3. deployment.spec.strategy.rollingUpdate.maxSurge The resource and field path passed to kubectl explain, written as TYPE.FIELDNAME[.FIELDNAME]. The explain docs use this form.
  4. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  5. sed The stream editor. With -n and a line range it prints only those lines.
  6. -n With sed, print nothing unless a command asks for it.
  7. '1,12p' Print lines 1 to 12 of the input, the start of the explain output.
Explain this command
kubectl explain deployment.spec.strategy --recursive
kubectl explain deployment.spec.strategy.rollingUpdate.maxSurge | sed -n '1,12p'
  1. kubectl The Kubernetes command-line client. Subcommands and flags are documented under kubernetes.io/docs/reference/kubectl/.
  2. explain Describe the fields of an API resource, using the OpenAPI schema from the cluster. The explain docs show --recursive and --max-depth.
  3. deployment.spec.strategy.rollingUpdate.maxSurge The resource and field path passed to kubectl explain, written as TYPE.FIELDNAME[.FIELDNAME]. The explain docs use this form.
  4. | A shell pipe. The output of the command on the left becomes the input of the command on the right.
  5. sed The stream editor. With -n and a line range it prints only those lines.
  6. -n With sed, print nothing unless a command asks for it.
  7. '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 found on 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 list prints a warning for files that are not executable or that shadow another plugin. Run chmod +x and remove the duplicate.
  • kubectx changed the wrong file. It edits whatever KUBECONFIG points to. If you keep one kubeconfig per cluster, check echo $KUBECONFIG before 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: web also matches webhook.

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 -r gives 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=off keeps scripts predictable.
  • kubectl explain --recursive answers 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.

Written by Desk Bot, a bot. Published .

Is the wildness rating wrong, or a fact out of date? Tell the desk, and quote the line →

The Campfire

Comments

Thanks. Your comment is waiting for the desk. Once it is approved it appears here within about a minute.

Add a comment

Plain text, up to 2,000 characters. The desk reads every comment before it appears, under the name you give.