쿠버네티스 / kubectl 치트시트
검색과 인쇄가 가능한 kubectl 참고자료입니다: context, 파드, 디플로이먼트, 서비스, 설정과 시크릿, 스케일링, 문제 해결까지. 무료입니다.
kubectl basics
13kubectl get pods
kubectl get pods -o wide
kubectl get all
kubectl describe pod my-pod
kubectl logs my-pod
kubectl logs -f my-pod
kubectl logs my-pod -c sidecar
kubectl logs my-pod --previous
kubectl exec -it my-pod -- sh
kubectl exec my-pod -- env
kubectl get pod my-pod -o yaml
kubectl explain deployment.spec
kubectl api-resources
Contexts & namespaces
11kubectl config get-contexts
kubectl config current-context
kubectl config use-context prod
kubectl get namespaces
kubectl get pods -n kube-system
kubectl get pods -A
kubectl config set-context --current --namespace=dev
kubectl create namespace staging
kubectl delete namespace staging
kubectl config view --minify
export KUBECONFIG=~/.kube/other-config
Creating & applying resources
12kubectl apply -f app.yaml
kubectl apply -f ./manifests/
kubectl apply -k ./overlays/prod
kubectl delete -f app.yaml
kubectl create deployment web --image=nginx
kubectl run tmp --image=busybox -it --rm -- sh
--dry-run=client -o yaml
kubectl diff -f app.yaml
kubectl edit deployment web
kubectl patch deploy web -p '{"spec":{"replicas":3}}'
kubectl label pod my-pod env=prod
kubectl annotate pod my-pod note="checked"
Pods & debugging
13kubectl get pods -w
kubectl get pods -l app=web
kubectl get events --sort-by=.lastTimestamp
kubectl top pods
kubectl port-forward pod/my-pod 8080:80
kubectl cp my-pod:/var/log/app.log ./app.log
kubectl debug my-pod -it --image=busybox
kubectl debug node/my-node -it --image=busybox
kubectl delete pod my-pod
kubectl delete pod my-pod --grace-period=0 --force
kubectl get pod my-pod -o jsonpath='{.status.phase}'
CrashLoopBackOff
ImagePullBackOff
Deployments & rollouts
12kubectl get deployments
kubectl set image deploy/web web=nginx:1.27
kubectl rollout status deploy/web
kubectl rollout history deploy/web
kubectl rollout undo deploy/web
kubectl rollout undo deploy/web --to-revision=2
kubectl rollout restart deploy/web
kubectl rollout pause deploy/web
kubectl rollout resume deploy/web
kubectl scale deploy/web --replicas=5
maxSurge: 1, maxUnavailable: 0
kubectl get rs
Services & networking
12kubectl get svc
kubectl expose deploy/web --port=80 --target-port=8080
type: ClusterIP
type: NodePort
type: LoadBalancer
kubectl get endpoints web
kubectl port-forward svc/web 8080:80
kubectl get ingress
web.default.svc.cluster.local
kubectl run curl --image=curlimages/curl -it --rm -- sh
kubectl get networkpolicies
sessionAffinity: ClientIP
Config & secrets
11kubectl create configmap app-cfg --from-literal=ENV=prod
kubectl create configmap app-cfg --from-file=config.ini
kubectl create secret generic db --from-literal=pass=s3cret
kubectl get secret db -o jsonpath='{.data.pass}' | base64 -d
envFrom:
- configMapRef:
name: app-cfg
env:
- name: DB_PASS
valueFrom:
secretKeyRef:
name: db
key: pass
volumes:
- name: cfg
configMap:
name: app-cfg
kubectl create secret tls web-tls --cert=tls.crt --key=tls.key
kubectl create secret docker-registry regcred --docker-server=...
imagePullSecrets: [{name: regcred}]
kubectl rollout restart deploy/web
Scaling & autoscaling
11kubectl scale deploy/web --replicas=10
kubectl autoscale deploy/web --min=2 --max=10 --cpu-percent=70
kubectl get hpa
resources:
requests:
cpu: 100m
memory: 128Mi
resources:
limits:
cpu: 500m
memory: 256Mi
cpu: 100m
kubectl top pods --sort-by=memory
OOMKilled (exit code 137)
kubectl get pdb
minAvailable: 1
kubectl scale deploy/web --replicas=0
Nodes & cluster admin
12kubectl get nodes
kubectl describe node my-node
kubectl top nodes
kubectl cordon my-node
kubectl drain my-node --ignore-daemonsets
kubectl uncordon my-node
kubectl taint nodes my-node key=value:NoSchedule
nodeSelector: { disktype: ssd }
kubectl cluster-info
kubectl version
kubectl auth can-i delete pods
kubectl get componentstatuses
YAML manifest skeletons
9apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 80
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web
port:
number: 80
apiVersion: v1
kind: ConfigMap
metadata:
name: app-cfg
data:
APP_ENV: production
livenessProbe:
httpGet:
path: /healthz
port: 80
initialDelaySeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 80
apiVersion: batch/v1
kind: CronJob
metadata:
name: cleanup
spec:
schedule: "0 3 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: job
image: busybox
command: ["sh", "-c", "echo run"]
restartPolicy: OnFailure
securityContext:
runAsNonRoot: true
readOnlyRootFilesystem: true
# kubectl create deploy web --image=nginx --dry-run=client -o yaml
Troubleshooting one-liners
13kubectl get pods -A | grep -v Running
kubectl get events -A --sort-by=.lastTimestamp | tail -20
kubectl describe pod my-pod | grep -A5 Events
kubectl logs -l app=web --tail=50
kubectl get pods --field-selector=status.phase=Failed
kubectl delete pods --field-selector=status.phase=Succeeded
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'
kubectl get deploy web -o jsonpath='{.spec.template.spec.containers[0].image}'
kubectl rollout status deploy/web --timeout=120s
kubectl exec -it my-pod -- nslookup web
kubectl get pod my-pod -o yaml | grep -A5 lastState
kubectl api-resources --verbs=list -o name | xargs -n1 kubectl get -n dev
watch kubectl get pods
“:q”와 일치하는 항목이 없습니다.
쿠버네티스 / kubectl 치트시트 소개
이 쿠버네티스 치트시트는 kubectl을 검색 가능한 한 페이지에 담았습니다: kubectl 기초, context와 네임스페이스, 리소스 생성과 적용, 파드와 디버깅, 디플로이먼트와 롤아웃, 서비스와 네트워킹, 설정과 시크릿, 스케일링과 오토스케일링, 노드와 클러스터 관리, YAML 매니페스트 기본 골격, 그리고 문제 해결용 한 줄 명령어까지 다룹니다.
위기 상황에서 실행하게 되는 명령어를 중심으로 구성되어 있습니다: 크래시 루프에 빠진 파드를 describe하기, 이전 컨테이너의 로그를 tail하기, 실행 중인 컨테이너에 exec로 접속하기, 디플로이먼트 롤백하기, 서비스 포트포워딩하기 등을 kubectl --help에만 맡기지 않고 플래그까지 자세히 풀어서 보여줍니다.
이 그룹의 다른 치트시트와 마찬가지로 무료이며 클라이언트 측에서 동작합니다: 검색창으로 실시간으로 행을 필터링하고, 고정된 목차로 섹션을 이동하고, 클릭 한 번으로 명령어를 복사하고, 터미널 옆에서 참고할 수 있도록 페이지를 인쇄할 수 있습니다.
쿠버네티스 / kubectl 치트시트 사용 방법
- 시트를 열고 kubectl 기초부터 문제 해결용 한 줄 명령어까지 섹션을 검토하세요.
- logs, rollout, port-forward 같은 키워드를 검색해 모든 행을 실시간으로 필터링하세요.
- Deployment, Service, ConfigMap의 시작 예제가 필요할 때는 YAML 매니페스트 기본 골격 섹션으로 이동하세요.
- 명령어나 복사 아이콘을 클릭해 터미널에 바로 복사하세요.
- 전체 kubectl 참고자료를 종이로 보려면 인쇄를 사용하세요.