쿠버네티스 명령어
쿠버네티스 명령어
참고자료
- Kubernetes Official Documentation - 공식 문서
- kubectl Cheat Sheet - 공식 치트시트
- CKA Curriculum - 시험 범위
- Practice Exercises - 연습 문제
1. kubectl 기본 설정 및 컨텍스트
1.1 클러스터 접근 및 기본 정보
1
2
3
4
5
6
7
8
9
10
11
12
# 클러스터 정보 확인
kubectl cluster-info
kubectl version --short
# 노드 상태 확인
kubectl get nodes
kubectl get nodes -o wide
# API 리소스 확인
kubectl api-resources
kubectl api-resources --namespaced=true
kubectl api-resources --namespaced=false
1.2 컨텍스트 및 네임스페이스 관리
1
2
3
4
5
6
7
8
9
10
11
12
13
# 컨텍스트 관리
kubectl config view
kubectl config current-context
kubectl config get-contexts
kubectl config use-context <context-name>
# 네임스페이스 관리
kubectl get namespaces
kubectl create namespace <namespace-name>
kubectl config set-context --current --namespace=<namespace-name>
# 기본 네임스페이스 설정 (CKA 시험에서 중요)
kubectl config set-context --current --namespace=production
2. 파드(Pod) 관리
2.1 파드 생성 및 기본 조작
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 파드 생성 (명령형)
kubectl run nginx --image=nginx
kubectl run nginx --image=nginx --port=80
kubectl run nginx --image=nginx --labels="app=web,env=prod"
# 파드 조회
kubectl get pods
kubectl get pods -o wide
kubectl get pods --show-labels
kubectl get pods -l app=web
kubectl get pods --all-namespaces
# 파드 상세 정보
kubectl describe pod nginx
kubectl get pod nginx -o yaml
kubectl get pod nginx -o json
2.2 파드 실행 및 디버깅
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 파드 내부 접근
kubectl exec -it nginx -- bash
kubectl exec -it nginx -- /bin/sh
kubectl exec nginx -- ls -la
# 멀티 컨테이너 파드
kubectl exec -it nginx -c container-name -- bash
# 파드 로그 확인
kubectl logs nginx
kubectl logs nginx --previous
kubectl logs nginx --tail=50
kubectl logs -f nginx
# 파일 복사
kubectl cp nginx:/etc/nginx/nginx.conf ./nginx.conf
kubectl cp ./config.txt nginx:/app/config.txt
2.3 임시 파드 및 디버깅
1
2
3
4
5
6
7
8
9
# 일회성 디버깅 파드
kubectl run tmp --image=busybox --rm -it --restart=Never -- sh
kubectl run tmp --image=nginx --rm -it --restart=Never -- /bin/bash
# 네트워크 디버깅
kubectl run net-debug --image=nicolaka/netshoot --rm -it --restart=Never
# DNS 테스트
kubectl run dns-test --image=busybox --rm -it --restart=Never -- nslookup kubernetes.default
3. 디플로이먼트(Deployment) 관리
3.1 디플로이먼트 생성 및 관리
1
2
3
4
5
6
7
8
9
10
11
12
# 디플로이먼트 생성
kubectl create deployment nginx --image=nginx
kubectl create deployment nginx --image=nginx --replicas=3
# 디플로이먼트 조회
kubectl get deployments
kubectl get deploy
kubectl describe deployment nginx
# 스케일링
kubectl scale deployment nginx --replicas=5
kubectl scale deploy nginx --replicas=2
3.2 롤아웃 관리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 이미지 업데이트
kubectl set image deployment/nginx nginx=nginx:1.21
kubectl set image deploy nginx nginx=nginx:1.21
# 롤아웃 상태 확인
kubectl rollout status deployment/nginx
kubectl rollout history deployment/nginx
# 롤백
kubectl rollout undo deployment/nginx
kubectl rollout undo deployment/nginx --to-revision=2
# 롤아웃 제어
kubectl rollout pause deployment/nginx
kubectl rollout resume deployment/nginx
kubectl rollout restart deployment/nginx
4. 서비스(Service) 관리
4.1 서비스 생성 및 노출
1
2
3
4
5
6
7
8
9
10
11
12
# 디플로이먼트 노출
kubectl expose deployment nginx --port=80 --type=ClusterIP
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl expose deployment nginx --port=80 --type=LoadBalancer
# 파드 직접 노출
kubectl expose pod nginx --port=80 --target-port=80
# 서비스 확인
kubectl get services
kubectl get svc
kubectl describe service nginx
4.2 서비스 디스커버리 및 테스트
1
2
3
4
5
6
7
8
9
10
11
# 엔드포인트 확인
kubectl get endpoints
kubectl get ep
# 서비스 테스트
kubectl run test --image=busybox --rm -it --restart=Never -- wget -qO- nginx:80
# 포트 포워딩
kubectl port-forward svc/nginx 8080:80
kubectl port-forward pod/nginx 8080:80
kubectl port-forward deployment/nginx 8080:80
5. 레플리카셋(ReplicaSet) 관리
1
2
3
4
5
6
7
# 레플리카셋 확인
kubectl get replicasets
kubectl get rs
kubectl describe rs nginx-rs
# 레플리카셋 스케일링
kubectl scale rs nginx-rs --replicas=4
6. 네임스페이스 및 리소스 관리
6.1 네임스페이스 조작
1
2
3
4
5
6
7
8
# 네임스페이스 생성/삭제
kubectl create namespace production
kubectl delete namespace production
# 네임스페이스별 리소스 확인
kubectl get all -n kube-system
kubectl get pods -n production
kubectl get pods --all-namespaces
6.2 리소스 쿼터 및 제한
1
2
3
4
5
6
7
# 리소스 쿼터 확인
kubectl get resourcequota
kubectl describe resourcequota
# 리밋 레인지 확인
kubectl get limitrange
kubectl describe limitrange
7. ConfigMap 및 Secret
7.1 ConfigMap 관리
1
2
3
4
5
6
7
8
9
10
# ConfigMap 생성
kubectl create configmap app-config --from-literal=key1=value1 --from-literal=key2=value2
kubectl create configmap app-config --from-file=config.properties
kubectl create configmap app-config --from-file=./config-dir/
# ConfigMap 확인
kubectl get configmaps
kubectl get cm
kubectl describe configmap app-config
kubectl get configmap app-config -o yaml
7.2 Secret 관리
1
2
3
4
5
6
7
8
9
10
11
# Secret 생성
kubectl create secret generic db-secret --from-literal=username=admin --from-literal=password=secret123
kubectl create secret docker-registry myregcred --docker-server=registry.com --docker-username=user --docker-password=pass
# Secret 확인
kubectl get secrets
kubectl describe secret db-secret
kubectl get secret db-secret -o yaml
# Secret 값 확인 (base64 디코딩)
kubectl get secret db-secret -o jsonpath='{.data.password}' | base64 -d
8. 볼륨 및 스토리지
8.1 PersistentVolume 및 PersistentVolumeClaim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# PV 및 PVC 확인
kubectl get persistentvolumes
kubectl get pv
kubectl get persistentvolumeclaims
kubectl get pvc
# 상세 정보
kubectl describe pv pv-name
kubectl describe pvc pvc-name
# 스토리지 클래스 확인
kubectl get storageclass
kubectl get sc
kubectl describe sc standard
9. 노드 관리
9.1 노드 상태 및 관리
1
2
3
4
5
6
7
8
9
10
11
12
# 노드 상세 정보
kubectl describe node worker-node-1
kubectl get nodes -o wide
# 노드 라벨 관리
kubectl label nodes worker-node-1 disktype=ssd
kubectl get nodes --show-labels
# 노드 스케줄링 제어
kubectl cordon worker-node-1 # 스케줄링 비활성화
kubectl uncordon worker-node-1 # 스케줄링 활성화
kubectl drain worker-node-1 # 파드 퇴거 후 스케줄링 비활성화
9.2 테인트 및 톨러레이션
1
2
3
4
5
6
7
8
9
# 테인트 설정
kubectl taint nodes worker-node-1 key1=value1:NoSchedule
kubectl taint nodes worker-node-1 key1=value1:NoExecute
# 테인트 제거
kubectl taint nodes worker-node-1 key1=value1:NoSchedule-
# 노드 테인트 확인
kubectl describe node worker-node-1 | grep -i taint
10. 클러스터 정보 및 이벤트
10.1 클러스터 컴포넌트 확인
1
2
3
4
5
6
7
8
9
# 시스템 파드 확인
kubectl get pods -n kube-system
kubectl get componentstatus
kubectl get cs
# 이벤트 확인
kubectl get events
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl get events --field-selector=type=Warning
10.2 리소스 사용량 모니터링
1
2
3
4
5
6
# 리소스 사용량 확인 (Metrics Server 필요)
kubectl top nodes
kubectl top pods
kubectl top pods --all-namespaces
kubectl top pods --sort-by=cpu
kubectl top pods --sort-by=memory
11. YAML 생성 및 Dry-run
11.1 YAML 템플릿 생성
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 파드 YAML 생성
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
# 디플로이먼트 YAML 생성
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > deployment.yaml
# 서비스 YAML 생성
kubectl expose deployment nginx --port=80 --dry-run=client -o yaml > service.yaml
# ConfigMap YAML 생성
kubectl create configmap app-config --from-literal=key=value --dry-run=client -o yaml > configmap.yaml
# Secret YAML 생성
kubectl create secret generic db-secret --from-literal=password=secret --dry-run=client -o yaml > secret.yaml
11.2 리소스 적용 및 관리
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 리소스 적용
kubectl apply -f pod.yaml
kubectl apply -f . # 현재 디렉토리 모든 YAML
kubectl apply -f deployment/ # 디렉토리 내 모든 YAML
kubectl apply -f https://raw.githubusercontent.com/kubernetes/examples/master/guestbook/all-in-one/guestbook-all-in-one.yaml
# 변경사항 확인
kubectl diff -f deployment.yaml
# 리소스 삭제
kubectl delete -f pod.yaml
kubectl delete pod nginx
kubectl delete deployment nginx
kubectl delete service nginx
12. 라벨 및 셀렉터
12.1 라벨 관리
1
2
3
4
5
6
7
8
9
10
11
# 라벨 추가/수정
kubectl label pods nginx app=web
kubectl label pods nginx version=v1.0
kubectl label nodes worker-node-1 disktype=ssd
# 라벨 제거
kubectl label pods nginx app-
kubectl label nodes worker-node-1 disktype-
# 라벨 덮어쓰기
kubectl label pods nginx app=api --overwrite
12.2 셀렉터를 이용한 조회
1
2
3
4
5
6
7
8
9
# 라벨 셀렉터
kubectl get pods -l app=web
kubectl get pods -l app=web,version=v1.0
kubectl get pods -l 'app in (web,api)'
kubectl get pods -l app!=web
# 필드 셀렉터
kubectl get pods --field-selector=status.phase=Running
kubectl get pods --field-selector=spec.nodeName=worker-node-1
13. 자주 사용하는 단축 명령어
13.1 별칭 설정 (bashrc/zshrc에 추가)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
alias k='kubectl'
alias kg='kubectl get'
alias kd='kubectl describe'
alias kdel='kubectl delete'
alias kaf='kubectl apply -f'
alias kdf='kubectl delete -f'
# 네임스페이스 별칭
alias kgp='kubectl get pods'
alias kgs='kubectl get svc'
alias kgd='kubectl get deploy'
alias kgn='kubectl get nodes'
# 시스템 네임스페이스
alias ksys='kubectl -n kube-system'
13.2 유용한 bash 함수
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 파드 shell 접근
kshell() {
kubectl exec -it $1 -- /bin/bash
}
# 파드 로그 실시간 확인
klogs() {
kubectl logs -f $1
}
# 네임스페이스 변경
kns() {
kubectl config set-context --current --namespace=$1
}
14. CKA 시험 핵심 명령어 체크리스트
14.1 반드시 외워야 할 기본 명령어
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 파드 관리
kubectl run nginx --image=nginx
kubectl get pods -o wide
kubectl describe pod nginx
kubectl logs nginx
kubectl exec -it nginx -- bash
kubectl delete pod nginx
# 디플로이먼트 관리
kubectl create deployment nginx --image=nginx --replicas=3
kubectl scale deployment nginx --replicas=5
kubectl set image deployment/nginx nginx=nginx:1.21
kubectl rollout undo deployment/nginx
# 서비스 관리
kubectl expose deployment nginx --port=80 --type=ClusterIP
kubectl get svc
kubectl describe svc nginx
# 네임스페이스 관리
kubectl create namespace production
kubectl config set-context --current --namespace=production
# 노드 관리
kubectl get nodes
kubectl cordon node-1
kubectl drain node-1 --ignore-daemonsets
kubectl uncordon node-1
# 리소스 생성 템플릿
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > deploy.yaml
14.2 문제 해결 필수 명령어
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 상태 확인
kubectl get events --sort-by=.metadata.creationTimestamp
kubectl describe pod <pod-name>
kubectl logs <pod-name> --previous
# 리소스 사용량
kubectl top nodes
kubectl top pods
# 네트워크 디버깅
kubectl run test --image=busybox --rm -it --restart=Never -- sh
kubectl exec -it <pod> -- nslookup kubernetes.default
# 설정 확인
kubectl config view
kubectl get componentstatus
This post is licensed under CC BY 4.0 by the author.