Installation

Linux

Binaries

# Download binaries
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
 
# Download checksum
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
 
# Validate checksum
echo "$(cat kubectl.sha256)  kubectl" | sha256sum --check
 
# Install then remove installer
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl && rm kubectl

APT

While it is possible with to install via apt,
You would need to update the package index if you ever choose to upgrade.

Snap

Using snap

snap install kubectl --classic
kubectl version --client

Windows

WinGet

Install winget

winget install -e --id Kubernetes.kubectl
winget upgrade -e --id Kubernetes.kubectl

Chocolatey

Install chocolatey Run powershell as admin

choco install kubernetes-cli -y
choco upgrade kubernetes-cli -y

Usage

# List all pods (Verbose)
kubectl get pods --all-namespaces --output wide
# List all pods (Concise)
kubectl get pods -A -o wide
# List all services
kubectl get service --all-namespaces
# Create namespace
kubectl create namespace <namespace-name>
# Get pod state
kubectl describe pods <pod-id>
# Get pod logs
kubectl logs <pod-id>
# Login to the pod's terminal
kubectl exec -it -n <namespace> <pod-id> -- /bin/bash
# Map container port to local port
kubectl port-forward <pod-id> <local-port>:<container-port>

Appendix