# Setup and productivitybash Explain Copy
alias k = kubectl
source <( kubectl completion bash ) && complete -o default -F __start_kubectl k
export do = " --dry-run=client -o yaml " # k run x --image=nginx $do
kubectl config get-contexts
kubectl config use-context < ct x >
kubectl config set-context --current --namespace= < ns >
kubectl api-resources # kinds, short names, namespaced or not
kubectl explain deploy.spec.strategy --recursive # Create, apply, deleteTask Command Apply file or dir kubectl apply -f x.yaml / -f dir/ / -k overlay/Preview change kubectl diff -f x.yamlServer-side validate kubectl apply -f x.yaml --dry-run=serverRun a Pod kubectl run web --image=nginx:1.27 --port=80Create Deployment kubectl create deploy web --image=nginx:1.27 --replicas=3Expose kubectl expose deploy web --port=80 --target-port=8080Delete kubectl delete -f x.yaml / kubectl delete pod x --grace-period=0 --forceReplace stuck object kubectl replace --force -f x.yaml
# Inspect and queryTask Command List with detail kubectl get pods -o wide -AWatch kubectl get pods -wLabels kubectl get pods --show-labels -l app=web,tier!=dbField select kubectl get pods --field-selector status.phase=Pending,spec.nodeName=n1JSONPath kubectl get pod x -o jsonpath='{.status.podIP}'Custom columns kubectl get pods -o custom-columns=N:.metadata.name,NODE:.spec.nodeNameSort kubectl get pods --sort-by=.status.startTimeEvents kubectl get events --sort-by=.lastTimestamp -ADescribe kubectl describe pod xRaw object kubectl get pod x -o yamlResource usage kubectl top pods --containers / kubectl top nodes
# Debuggingbash Explain Copy
kubectl logs x # add -c <container> -f --tail=100 --since=10m
kubectl logs x --previous # last crashed container
kubectl logs -l app=web --all-containers --prefix
kubectl exec -it x -- sh
kubectl port-forward svc/web 8080:80
kubectl debug -it x --image=busybox:1.36 --target=app # ephemeral container
kubectl debug node/n1 -it --image=ubuntu # host filesystem at /host
kubectl run tmp --rm -it --restart=Never --image=nicolaka/netshoot -- bash
kubectl cp x:/path/file ./file # Pod status cheat tableStatus Meaning First look PendingNot scheduled or volume or image pending describe pod events: Insufficient cpu, taints, unbound PVCImagePullBackOff / ErrImagePullCannot pull image Tag typo, registry auth, imagePullSecrets CrashLoopBackOffContainer keeps exiting logs --previous, exit code, probesOOMKilled (exit 137)Memory limit exceeded Raise limit, find leak CreateContainerConfigErrorMissing ConfigMap, Secret or key describe podInit:Error / Init:CrashLoopBackOffInit container failing logs -c <init>EvictedNode pressure describe node, ephemeral storage, memoryTerminating (stuck)Finalizer or node down get pod -o yaml finalizers, kubelet stateRunning but 0/1Readiness failing Probe path and port, describe
Exit codes: 0 ok, 1 app error, 126 not executable, 127 command not found, 137 SIGKILL (OOM or kill), 139 segfault, 143 SIGTERM.
# Workloadsbash Explain Copy
kubectl scale deploy web --replicas=5
kubectl set image deploy/web nginx=nginx:1.27.1
kubectl set resources deploy/web --requests=cpu=100m,memory=128Mi --limits=memory=256Mi
kubectl rollout status deploy/web
kubectl rollout history deploy/web --revision=2
kubectl rollout undo deploy/web --to-revision=1
kubectl rollout restart deploy/web
kubectl rollout pause deploy/web # then resume
kubectl autoscale deploy web --min=2 --max=10 --cpu-percent=60
kubectl create job once --image=busybox -- date
kubectl create job manual --from=cronjob/nightly # Deployment skeletonyaml Copy
apiVersion : apps/v1
kind : Deployment
metadata : { name : web , labels : { app : web } }
spec :
replicas : 3
revisionHistoryLimit : 5
strategy :
type : RollingUpdate
rollingUpdate : { maxSurge : 25% , maxUnavailable : 0 }
selector : { matchLabels : { app : web } }
template :
metadata : { labels : { app : web } }
spec :
terminationGracePeriodSeconds : 30
containers :
- name : web
image : nginx:1.27
ports : [{ containerPort : 80 , name : http }]
resources :
requests : { cpu : 100m , memory : 128Mi }
limits : { memory : 256Mi }
startupProbe : { httpGet : { path : /healthz , port : http }, failureThreshold : 30 , periodSeconds : 2 }
livenessProbe : { httpGet : { path : /healthz , port : http }, periodSeconds : 10 }
readinessProbe : { httpGet : { path : /ready , port : http }, periodSeconds : 5 }
lifecycle :
preStop : { exec : { command : [ " sleep " , " 5 " ] } }
securityContext :
runAsNonRoot : true
allowPrivilegeEscalation : false
readOnlyRootFilesystem : true
capabilities : { drop : [ " ALL " ] }
seccompProfile : { type : RuntimeDefault } # Services, Ingress, DNSType Use ClusterIPInternal virtual IP (default) NodePortPort 30000-32767 on every node LoadBalancerCloud load balancer ExternalNameCNAME to an external host clusterIP: NoneHeadless: DNS returns Pod IPs, used by StatefulSets
text Copy
<svc>.<ns>.svc.cluster.local Service
<pod-name>.<headless-svc>.<ns>.svc... StatefulSet Podyaml Copy
apiVersion : networking.k8s.io/v1
kind : Ingress
metadata :
name : web
annotations : { nginx.ingress.kubernetes.io/proxy-body-size : " 20m " }
spec :
ingressClassName : nginx
tls : [{ hosts : [ app.example.com ], secretName : app-tls }]
rules :
- host : app.example.com
http :
paths :
- { path : / , pathType : Prefix , backend : { service : { name : web , port : { number : 80 } } } } bash Explain Copy
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl create secret tls app-tls --cert=tls.crt --key=tls.key
kubectl run dns --rm -it --restart=Never --image=busybox:1.36 -- nslookup web.default.svc.cluster.local # Config and secretsbash Explain Copy
kubectl create cm app --from-literal=K=V --from-file=app.conf
kubectl create secret generic db --from-literal=password= ' s3cr3t '
kubectl create secret docker-registry reg --docker-server=ghcr.io --docker-username=u --docker-password= $TOKEN
kubectl get secret db -o jsonpath= ' {.data.password} ' | base64 -d
kubectl set env deploy/web --from=configmap/app yaml Copy
envFrom :
- configMapRef : { name : app }
env :
- name : DB_PASSWORD
valueFrom : { secretKeyRef : { name : db , key : password } }
volumeMounts : [{ name : cfg , mountPath : /etc/app , readOnly : true }]
volumes : [{ name : cfg , configMap : { name : app } }] # Storagebash Explain Copy
kubectl get sc,pv,pvc
kubectl patch pvc data -p ' {"spec":{"resources":{"requests":{"storage":"20Gi"}}}} ' # needs allowVolumeExpansion
kubectl patch sc standard -p ' {"metadata":{"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}} ' Access mode Meaning ReadWriteOnceOne node read-write ReadWriteOncePodOne Pod read-write ReadOnlyManyMany nodes read-only ReadWriteManyMany nodes read-write (EFS, NFS, CephFS)
Reclaim policy: Delete removes the volume with the PVC, Retain keeps it.
# Schedulingbash Explain Copy
kubectl label node n1 disk=ssd
kubectl taint node n1 dedicated=gpu:NoSchedule # remove with dedicated=gpu:NoSchedule-
kubectl cordon n1 && kubectl drain n1 --ignore-daemonsets --delete-emptydir-data
kubectl uncordon n1 yaml Copy
nodeSelector : { disk : ssd }
tolerations : [{ key : dedicated , operator : Equal , value : gpu , effect : NoSchedule }]
affinity :
podAntiAffinity :
requiredDuringSchedulingIgnoredDuringExecution :
- labelSelector : { matchLabels : { app : web } }
topologyKey : kubernetes.io/hostname
topologySpreadConstraints :
- maxSkew : 1
topologyKey : topology.kubernetes.io/zone
whenUnsatisfiable : DoNotSchedule
labelSelector : { matchLabels : { app : web } } QoS Condition Evicted Guaranteed requests = limits for all containers (CPU and memory) last Burstable some request or limit set middle BestEffort none set first
# RBACbash Explain Copy
kubectl create role r --verb=get,list,watch --resource=pods -n ns
kubectl create rolebinding rb --role=r --serviceaccount=ns:sa -n ns
kubectl create clusterrole cr --verb=get,list --resource=nodes
kubectl create clusterrolebinding crb --clusterrole=cr --user=alice
kubectl auth can-i delete pods -n ns --as=system:serviceaccount:ns:sa
kubectl auth can-i --list -n ns --as=alice
kubectl create token sa --duration=1h -n ns
kubectl get rolebindings,clusterrolebindings -A -o wide | grep < subjec t > # NetworkPolicyyaml Copy
# deny all in and out for the namespace
apiVersion : networking.k8s.io/v1
kind : NetworkPolicy
metadata : { name : default-deny }
spec :
podSelector : {}
policyTypes : [ Ingress , Egress ]
---
# allow api -> db:5432
apiVersion : networking.k8s.io/v1
kind : NetworkPolicy
metadata : { name : db-from-api }
spec :
podSelector : { matchLabels : { tier : db } }
policyTypes : [ Ingress ]
ingress :
- from : [{ podSelector : { matchLabels : { tier : api } } }]
ports : [{ protocol : TCP , port : 5432 }] Remember: always allow DNS (UDP and TCP 53 to kube-dns) when you deny egress. Policies are additive; there is no deny rule.
# Autoscalingyaml Copy
apiVersion : autoscaling/v2
kind : HorizontalPodAutoscaler
metadata : { name : web }
spec :
scaleTargetRef : { apiVersion : apps/v1 , kind : Deployment , name : web }
minReplicas : 2
maxReplicas : 10
metrics :
- type : Resource
resource : { name : cpu , target : { type : Utilization , averageUtilization : 60 } }
behavior :
scaleDown : { stabilizationWindowSeconds : 300 } Formula: desired = ceil(current * currentMetric / targetMetric). Utilization is relative to the request.
# Cluster and node operationsbash Explain Copy
kubectl get nodes -o wide
kubectl describe node n1 | sed -n ' /Allocated resources/,/Events/p '
kubectl get componentstatuses 2> /dev/null ; kubectl get --raw= ' /readyz?verbose '
kubectl cluster-info dump | head
kubectl get pods -n kube-system
kubeadm certs check-expiration # on a kubeadm control plane node
ETCDCTL_API = 3 etcdctl snapshot save /backup/etcd.db \
--endpoints=https://127.0.0.1:2379 \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/server.key On the node (containerd): crictl ps, crictl images, crictl logs <id>, journalctl -u kubelet -f.
# Kustomize and Helmbash Explain Copy
kubectl kustomize overlays/prod | kubectl apply -f -
helm repo add bitnami https://charts.bitnami.com/bitnami && helm repo update
helm upgrade --install web ./chart -n prod --create-namespace -f values-prod.yaml --atomic --timeout 5m
helm template web ./chart -f values.yaml | kubectl apply --dry-run=server -f -
helm list -A && helm history web -n prod && helm rollback web 1 -n prod
helm get values web -n prod # kind and minikubebash Explain Copy
kind create cluster --name dev --config kind.yaml
kind load docker-image app:1.0 --name dev
kind delete cluster --name dev
minikube start --nodes 3 --cpus 4 --memory 6g
minikube addons enable ingress metrics-server
minikube service web --url
minikube delete # Top 15 interview one-liners
Pod: the smallest deployable unit, one or more containers sharing network and storage.
Deployment vs StatefulSet: Deployment pods are interchangeable; StatefulSet pods have stable names, ordered rollout and their own PVC.
Service: a stable virtual IP and DNS name in front of Pods, backed by EndpointSlices of ready Pods only.
Ingress vs LoadBalancer: a LoadBalancer Service exposes one service at L4 per load balancer; Ingress routes many hosts and paths at L7 through one entry point.
Liveness vs readiness: liveness restarts the container, readiness removes it from traffic, startup delays both until the app is up.
Requests vs limits: requests are for scheduling and guaranteed share, limits cap usage; CPU over limit throttles, memory over limit kills.
ConfigMap vs Secret: both are key-value config; Secrets are base64, not encrypted, unless encryption at rest or an external store is used.
What happens on kubectl apply: authn, authz, admission, write to etcd; controllers create ReplicaSet and Pods, scheduler binds, kubelet runs them.
CrashLoopBackOff: the container starts and exits repeatedly with exponential backoff up to five minutes; read logs --previous and the exit code.
Rolling update control: maxSurge and maxUnavailable, gated by readiness; rollback is kubectl rollout undo.
RBAC: deny by default; Role plus RoleBinding in a namespace, ClusterRole plus ClusterRoleBinding cluster-wide; verify with auth can-i.
NetworkPolicy: allow-list only; once a Pod is selected, anything not allowed is denied, and your CNI must enforce it.
HPA: scales replicas on metrics, needs metrics-server and CPU requests, and uses ceil(current * metric / target).
Taint vs affinity: a taint repels Pods unless tolerated; affinity attracts Pods to nodes. Use both to dedicate a node pool.
Zero-downtime deploy: readiness probe, maxUnavailable: 0, a preStop sleep, adequate terminationGracePeriodSeconds, and a PodDisruptionBudget.