Skip to content

kubectl

Install kubectl | Linux

Docs Here

Install kubectl - Latest release

Bash
echo "Downloading the latest release"
echo ""
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
echo "Installing latest release"
echo ""
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
echo "Move to ~/.local/bin"
echo ""
chmod +x kubectl
mkdir -p ~/.local/bin
mv ./kubectl ~/.local/bin/kubectl
# and then append (or prepend) ~/.local/bin to $PATH
echo "Version:"
kubectl version --client

Enable kubectl autocomplete (bash)

Bash
echo 'source <(kubectl completion bash)' >>~/.bashrc
  • updated:
Bash
kubectl completion bash > kubectl &&\
sudo mv kubectl /etc/bash_completion.d/ &&\
source ~/.bashrc

Enable kubectl autocomplete (zsh)

Bash
echo "source <(kubectl completion zsh)" >> ~/.zshrc

Nice Tricks

List all images being used

Bash
kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c

Also nice one

Bash
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' |\
sort

List Everything - Just names (except roles, secrets ..)

Bash
kubectl get all -A --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'

All pods (on all namespaces)

Bash
kubectl get pod -A --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}'

Change some other namespace to be default

Bash
kubectl config set-context --current --namespace=argocd