PART 5: ARCHITECTURE & STRATEGY
23. Standardizing kubectl Across Teams
The Standard Tooling
| Tool | Purpose |
| kubectl | Core CLI |
| kubectx/kubens | Context/namespace switching |
| k9s | TUI |
| stern | Multi-Pod logs |
| krew | Plugin manager |
| kubectl-neat | Clean YAML output |
| yq | YAML 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
| Aspect | Imperative CI | GitOps |
| Who applies? | CI pipeline | In-cluster controller |
| Source of truth | CI logs | Git |
| Drift detection | Manual | Automatic |
| Rollback | Rerun pipeline | Git revert |
| Best for | Legacy | Modern |
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
| Practice | Why |
| Least privilege | Minimal permissions per role |
| No cluster-admin for humans | Use break-glass only |
| Namespace-scoped | Prefer Role over ClusterRole |
| Audit access | Log all API calls |
| Rotate credentials | Short-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:
- Vault
- Sealed envelope in physical safe
- Separate cloud account with MFA
Emergency process:
- Announce incident
- Retrieve break-glass credentials
- Fix the issue
- Rotate credentials
- Post-mortem
1:1 copy of the class Google Doc, code in code blocks.