Skip to content

Kubectl Switch Contexts =link= Guide

Here’s a complete story focused on using kubectl to switch contexts, told from the perspective of a platform engineer named Alex. The Wrong Context

kubectl get pods -n payment-system The list appeared. Pods were running. That was odd. He checked the logs of the oldest one: kubectl switch contexts

# Show k8s context in prompt export PS1='\[\e[33m\](k8s: $(kubectl config current-context 2>/dev/null))\[\e[0m\] \w \$ ' He also created an alias he’d never forget: Here’s a complete story focused on using kubectl

He looked at his terminal prompt. His shell was configured to show the current Kubernetes context in green. But today, it was… red . That was odd

alias kctx='kubectl config get-contexts -o name | fzf | xargs kubectl config use-context' Finally, he wrote a quick script for the team’s shared tooling: kubectl-warning.sh . It would flash a bright red warning if the context was anything other than prod-us-east and the --namespace flag was missing or set to default .

kubectl logs payment-api-f9k3l-8hj2s -n payment-system --previous There it was. A connection pool exhaustion error to the Redis cache. The new deployment had increased the default pool size, and the production Redis max connections was too low.

The College of St. Scholastica

Nondiscrimination Notice: The College of St. Scholastica is committed to fostering an inclusive and respectful environment free from discrimination and harassment. We prohibit discrimination based on race, color, national origin and other protected statuses. View full statement.