kubectl Basics
- kubectl getList resources
- kubectl describeShow details
- kubectl createCreate resource
- kubectl applyApply config
- kubectl deleteDelete resource
- kubectl editEdit resource
Pod Commands
- kubectl get podsList pods
- kubectl logsView logs
- kubectl exec -itExecute command
- kubectl port-forwardForward port
- kubectl cpCopy files
- kubectl top podsResource usage
Deployment Commands
- kubectl rollout statusRollout status
- kubectl rollout historyHistory
- kubectl rollout undoRollback
- kubectl scaleScale replicas
- kubectl set imageUpdate image
Resource Types
- pods (po)Pod
- deployments (deploy)Deployment
- services (svc)Service
- configmaps (cm)ConfigMap
- secretsSecret
- ingress (ing)Ingress
- nodes (no)Node
Deployment YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: my-app:1.0
ports:
- containerPort: 8080
Service YAML
apiVersion: v1
kind: Service
metadata:
name: my-app-svc
spec:
type: ClusterIP # ClusterIP, NodePort, LoadBalancer
selector:
app: my-app
ports:
- port: 80
targetPort: 8080
Config & Secrets
- kubectl create cmCreate ConfigMap
- kubectl create secretCreate Secret
- --from-fileFrom file
- --from-literalFrom key=value
- --from-env-fileFrom .env file
Debugging
- kubectl logs -fFollow logs
- kubectl logs --previousPrevious logs
- kubectl describe podPod events
- kubectl get eventsCluster events
- kubectl debugDebug pod
Common Commands
# Get all resources in namespace
kubectl get all -n my-namespace
# Apply all YAML files in directory
kubectl apply -f ./manifests/
# Scale deployment
kubectl scale deployment/my-app --replicas=5
# Update image
kubectl set image deployment/my-app my-app=my-app:2.0
# Port forward to local machine
kubectl port-forward pod/my-pod 8080:80
# Execute shell in pod
kubectl exec -it my-pod -- /bin/sh
Output Formats
- -o wideMore columns
- -o yamlYAML output
- -o jsonJSON output
- -o nameNames only
- --watchWatch changes
Context & Namespace
- kubectl config get-contextsList contexts
- kubectl config use-contextSwitch context
- -n namespaceSpecify namespace
- --all-namespacesAll namespaces