Skip to content

Kubernetes 치트 시트

Kubernetes(쿠버네티스, 흔히 K8s로 약칭됨. 8은 “ubernete"의 8글자를 대체함)는 클라우드 플랫폼의 여러 호스트에서 컨테이너화된 애플리케이션을 관리하기 위한 오픈 소스 시스템입니다. Kubernetes의 목표는 컨테이너화된 애플리케이션 배포를 단순하고 효율적(강력)으로 만드는 것입니다. Kubernetes는 애플리케이션 배포, 스케줄링, 업데이트 및 유지 관리를 위한 메커니즘을 제공합니다.

리소스 정보 확인

노드 (Nodes)

리소스 이름: nodes, 약어: no

$ kubectl get no          # 모든 노드 표시
$ kubectl get no -o wide  # 모든 노드에 대한 상세 정보 표시
$ kubectl describe no     # 노드 상세 정보 표시
$ kubectl get no -o yaml  # 노드 상세 정보를 YAML 형식으로 표시
$ kubectl get node --selector=[label_name] # 특정 라벨로 노드 필터링
$ kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'
# jsonpath 표현식으로 정의된 필드 정보 출력
$ kubectl top node [node_name] # 노드 사용량(CPU/메모리/스토리지) 표시

포드 (Pods)

리소스 이름: pods, 약어: po

$ kubectl get po 				# 모든 포드 표시
$ kubectl get po -o wide
$ kubectl describe po
$ kubectl get po --show-labels 	# 포드 라벨 확인
$ kubectl get po -l app=nginx
$ kubectl get po -o yaml
$ kubectl get pod [pod_name] -o yaml --export
$ kubectl get pod [pod_name] -o yaml --export > nameoffile.yaml 
# 포드 정보를 YAML 파일로 내보내기
$ kubectl get pods --field-selector status.phase=Running 		
# 필드 선택기를 사용하여 포드 정보 필터링

네임스페이스 (Namespaces)

리소스 이름: namespaces, 약어: ns

$ kubectl get ns
$ kubectl get ns -o yaml
$ kubectl describe ns

디플로이먼트 (Deployments)

리소스 이름: deployments, 약어: deploy

$ kubectl get deploy
$ kubectl describe deploy
$ kubectl get deploy -o wide 
$ kubectl get deploy -o yaml

서비스 (Services)

리소스 이름: services, 약어: svc

$ kubectl get svc
$ kubectl describe svc
$ kubectl get svc -o wide 
$ kubectl get svc -o yaml
$ kubectl get svc --show-labels

데몬셋 (DaemonSets)

리소스 이름: daemonsets, 약어: ds

$ kubectl get ds
$ kubectl describe ds --all-namespaces
$ kubectl describe ds [daemonset_name] -n [namespace_name]
$ kubectl get ds [ds_name] -n [ns_name] -o yaml

이벤트 (Events)

리소스 이름: events, 약어: ev

$ kubectl get events 
$ kubectl get events -n kube-system
$ kubectl get events -w

로그 (Logs)

$ kubectl logs [pod_name]
$ kubectl logs --since=1h [pod_name]
$ kubectl logs --tail=20 [pod_name]
$ kubectl logs -f -c [container_name] [pod_name]
$ kubectl logs [pod_name] > pod.log

서비스 계정 (Service Accounts)

리소스 이름: serviceaccounts, 약어: sa

$ kubectl get sa
$ kubectl get sa -o yaml
$ kubectl get serviceaccounts default -o yaml >./sa.yaml
$ kubectl replace serviceaccount default -f ./sa.yaml

레플리카셋 (ReplicaSets)

리소스 이름: replicasets, 약어: rs

$ kubectl get rs
$ kubectl describe rs
$ kubectl get rs -o wide 
$ kubectl get rs -o yaml

역할 (Roles)

$ kubectl get roles --all-namespaces
$ kubectl get roles --all-namespaces -o yaml

시크릿 (Secrets)

$ kubectl get secrets
$ kubectl get secrets --all-namespaces 
$ kubectl get secrets -o yaml

컨피그맵 (ConfigMaps)

리소스 이름: configmaps, 약어: cm

$ kubectl get cm
$ kubectl get cm --all-namespaces
$ kubectl get cm --all-namespaces -o yaml

인그레스 (Ingresses)

리소스 이름: ingresses, 약어: ing

$ kubectl get ing
$ kubectl get ing --all-namespaces

영구 볼륨 (PersistentVolumes)

리소스 이름: persistentvolumes, 약어: pv

$ kubectl get pv 
$ kubectl describe pv

영구 볼륨 클레임 (PersistentVolumeClaims)

리소스 이름: persistentvolumeclaims, 약어: pvc

$ kubectl get pvc
$ kubectl describe pvc

스토리지 클래스 (StorageClasses)

리소스 이름: storageclasses, 약어: sc

$ kubectl get sc
$ kubectl get sc -o yaml

다중 리소스

$ kubectl get svc, po
$ kubectl get deploy, no
$ kubectl get all
$ kubectl get all --all-namespaces

리소스 속성 변경

테인트 (Taints)

$ kubectl taint [node_name] [taint_name]

라벨 (Labels)

$ kubectl label [node_name] disktype=ssd 
$ kubectl label [pod_name] env=prod

코든 / 언코든 (Cordon / Uncordon)

$ kubectl cordon [node_name]   # 노드 유지 관리(스케줄링 중지)
$ kubectl uncordon [node_name] # 노드 스케줄링 허용

노드 비우기 (Draining Nodes)

$ kubectl drain [node_name]    # 노드 비우기

노드 / 포드

$ kubectl delete node [node_name] 
$ kubectl delete pod [pod_name]
$ kubectl edit node [node_name]
$ kubectl edit pod [pod_name]

디플로이먼트 / 네임스페이스

$ kubectl edit deploy [deploy_name]
$ kubectl delete deploy [deploy_name]
$ kubectl expose deploy [deploy_name] --port=80 --type=NodePort
$ kubectl scale deploy [deploy_name] --replicas=5
$ kubectl delete ns
$ kubectl edit ns [ns_name]

서비스

$ kubectl edit svc [svc_name]
$ kubectl delete svc [svc_name]

데몬셋

$ kubectl edit ds [ds_name] -n kube-system 
$ kubectl delete ds [ds_name]

서비스 계정

$ kubectl edit sa [sa_name]
$ kubectl delete sa [sa_name]

어노테이션 (Annotations)

$ kubectl annotate po [pod_name] [annotation]
$ kubectl annotate no [node_name]

리소스 추가

포드 생성

$ kubectl create -f [name_of_file] 
$ kubectl apply -f [name_of_file]
$ kubectl run [pod_name] --image=nginx --restart=Never
$ kubectl run [pod_name] --generator=run-pod/v1 --image=nginx
$ kubectl run [pod_name] --image=nginx --restart=Never

서비스 생성

$ kubectl create svc nodeport [svc_name] --tcp=8080:80

디플로이먼트 생성

$ kubectl create -f [name_of_file] 
$ kubectl apply -f [name_of_file]
$ kubectl create deploy [deploy_name] --image=nginx

컨테이너 상호작용

$ kubectl run [pod_name] --image=busybox --rm -it --restart=Never -- sh

YAML 파일 출력

$ kubectl create deploy [deploy_name] --image=nginx --dry-run -o yaml > deploy.yaml
$ kubectl get po [pod_name] -o yaml --export > pod.yaml

도움말 확인

$ kubectl -h
$ kubectl create -h
$ kubectl run -h
$ kubectl explain deploy.spec

요청

API 호출

$ kubectl get --raw /apis/metrics.k8s.io/

클러스터 정보

$ kubectl config
$ kubectl cluster-info
$ kubectl get componentstatus