Stage 2, Part 5 of 9, kubectl Mastery & Debugging

Text

PART 5: ARCHITECTURE & STRATEGY

23. Standardizing kubectl Across Teams

The Standard Tooling

ToolPurpose
kubectlCore CLI
kubectx/kubensContext/namespace switching
k9sTUI
sternMulti-Pod logs
krewPlugin manager
kubectl-neatClean YAML output
yqYAML processing

The Standard Aliases

Distribute a common ~/.kube/aliases.sh:

bash
alias k='kubectl'
alias kg='kubectl get'
alias kgp='kubectl get pods'
alias kgpa='kubectl get pods -A'
alias kgs='kubectl get svc'
alias kgd='kubectl get deployment'
alias kd='kubectl describe'
alias kdp='kubectl describe pod'
alias kl='kubectl logs'
alias klf='kubectl logs -f'
alias ke='kubectl exec -it'
alias ka='kubectl apply -f'
alias kdel='kubectl delete'
alias kx='kubectx'
alias kn='kubens'
alias kdebug='kubectl debug -it'

The Standard Shell Prompt

Show current context and namespace in your prompt:

bash
# In ~/.bashrc
kube_ps1() {
  local ctx=$(kubectl config current-context 2>/dev/null)
  local ns=$(kubectl config view --minify --output 'jsonpath={..namespace}' 2>/dev/null)
  echo "($ctx/${ns:-default})"
}
PS1='\u@\h $(kube_ps1) \$ '

Or use kube-ps1 (a popular tool).

Dangerous Command Prevention

Guardrails:

bash
# Warn on delete
alias kdelete='read -p "Delete? (y/n) " -n 1 -r; echo; [[ $REPLY =~ ^[Yy]$ ]] && kubectl delete'

# Prevent prod context without confirmation
kubectl() {
  local ctx=$(command kubectl config current-context 2>/dev/null)
  if [[ "$ctx" == *"prod"* ]]; then
    read -p "You are in PROD. Continue? (yes/no) " ans
    [[ "$ans" != "yes" ]] && return 1
  fi
  command kubectl "$@"
}

24. kubectl in CI/CD and GitOps

CI/CD Patterns

Read-only access: CI should have limited permissions.

yaml
# CI service account
apiVersion: v1
kind: ServiceAccount
metadata:
  name: ci-readonly
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: ci-readonly
rules:
- apiGroups: [""]
  resources: ["pods", "services", "endpoints"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["apps"]
  resources: ["deployments", "statefulsets"]
  verbs: ["get", "list", "watch"]

GitOps vs Imperative CI

AspectImperative CIGitOps
Who applies?CI pipelineIn-cluster controller
Source of truthCI logsGit
Drift detectionManualAutomatic
RollbackRerun pipelineGit revert
Best forLegacyModern

GitOps tools: Argo CD, Flux.

kubectl for GitOps Debugging

bash
# Check what Argo CD has synced
kubectl get applications -n argocd

# Check specific app
kubectl describe application myapp -n argocd

# Force sync
argocd app sync myapp

# Check Flux
kubectl get kustomizations -A
kubectl get gitrepositories -A

25. Compliance & Security for kubectl Access

RBAC Best Practices

PracticeWhy
Least privilegeMinimal permissions per role
No cluster-admin for humansUse break-glass only
Namespace-scopedPrefer Role over ClusterRole
Audit accessLog all API calls
Rotate credentialsShort-lived tokens

Audit Logging

Kubernetes API server can log every request:

yaml
# /etc/kubernetes/audit-policy.yaml
apiVersion: audit.k8s.io/v1
kind: Policy
rules:
- level: RequestResponse
  resources:
  - group: ""
    resources: ["secrets"]
- level: Metadata
  resources:
  - group: ""
    resources: ["pods", "deployments"]
  omitStages:
  - RequestReceived

Emergency Access

Break-glass kubeconfig stored in a secure location:

Emergency process:

  1. Announce incident
  2. Retrieve break-glass credentials
  3. Fix the issue
  4. Rotate credentials
  5. Post-mortem

1:1 copy of the class Google Doc, code in code blocks.