Post

Kubernetes 명령어 레퍼런스

주요 명령어 사용법을 정리한 실전 참조 자료

Kubernetes 명령어 레퍼런스

기본 조작

클러스터 상태 확인

명령어용도
kubectl get nodes노드 목록 조회
kubectl get nodes -o wide노드 상세 정보
kubectl cluster-info클러스터 정보 요약
kubectl get pods --all-namespaces모든 Pod 조회

Pod 모니터링

명령어용도
kubectl get pods -n <namespace>네임스페이스별 Pod 조회
kubectl get pods -l app=<label>라벨별 Pod 필터링
kubectl describe pod <pod-name>Pod 상세 정보
kubectl logs <pod-name> -fPod 로그 실시간 확인
kubectl exec -it <pod-name> -- /bin/bashPod 내부 접속

Job 관리

명령어용도
kubectl get jobsJob 목록 조회
kubectl logs job/<job-name>Job 로그 확인
kubectl get pods -l job-name=<job-name>Job이 생성한 Pod 조회
kubectl delete job <job-name>Job 삭제

네트워크 관리

Service 조작

명령어용도
kubectl get svc -n <namespace>Service 목록 조회
kubectl describe svc <service-name>Service 상세 정보
kubectl get pods -l <selector>셀렉터로 Pod 조회

포트 포워딩

1
2
3
4
5
# 로컬에서 클러스터 서비스 접근
kubectl port-forward svc/<service-name> <local-port>:<service-port> -n <namespace>

# 백그라운드 실행
kubectl port-forward svc/<service-name> <local-port>:<service-port> -n <namespace> &

연결 테스트

1
2
3
4
5
# DNS 해상도 테스트 (Pod 내부에서)
kubectl exec -it <pod-name> -- nslookup <service>.<namespace>.svc.cluster.local

# 서비스 연결 테스트
curl http://localhost:<port>/health

스토리지 관리

PV/PVC 조작

명령어용도
kubectl get storageclassStorageClass 조회
kubectl get pvPersistentVolume 조회
kubectl get pvc -n <namespace>PersistentVolumeClaim 조회
kubectl describe pvc <pvc-name>PVC 상세 정보

스토리지 검증

1
2
3
4
5
6
7
8
# 데이터 확인
kubectl exec -it <pod-name> -n <namespace> -- ls -la /data

# 볼륨 마운트 확인
kubectl describe pod <pod-name> -n <namespace> | grep -A5 Mounts

# 디스크 사용량 확인
kubectl exec -it <pod-name> -n <namespace> -- df -h

Secret 관리

Secret 생성

타입명령어
일반 Secretkubectl create secret generic <name> --from-literal=key=value
Docker 인증kubectl create secret docker-registry <name> --docker-username=<user>
파일 기반kubectl create secret generic <name> --from-file=<path>

Secret 확인

명령어용도
kubectl get secretSecret 목록
kubectl get secret <name> -o yamlSecret 상세 정보
echo "<encoded>" \| base64 -dBase64 디코딩

Secret 업데이트

1
2
3
4
5
6
# 기존 Secret 삭제 후 재생성
kubectl delete secret <name> -n <namespace> --ignore-not-found
kubectl create secret generic <name> --from-literal=key=newvalue -n <namespace>

# Patch 방식 업데이트
kubectl patch secret <name> -p '{"data":{"key":"'$(echo -n "newvalue" | base64)'"}}'

리소스 정리

개별 리소스 삭제

리소스 타입명령어
Podkubectl delete pod <name>
Jobkubectl delete job <name>
Servicekubectl delete svc <name>
Secretkubectl delete secret <name>
YAML 기반kubectl delete -f <file.yaml>

대량 삭제

범위명령어
네임스페이스 전체kubectl delete namespace <name>
라벨 기반kubectl delete pods -l app=<name>
타입별 전체kubectl delete jobs --all -n <namespace>

안전한 정리 스크립트

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/bin/bash
# 단계적 리소스 정리

echo "리소스 정리 시작..."

# 1. 애플리케이션 레벨
kubectl delete deployment <name> -n <namespace>

# 2. 네트워크 레벨  
kubectl delete svc <name> -n <namespace>

# 3. 설정 레벨
kubectl delete secret <name> -n <namespace>

# 4. 스토리지 레벨
kubectl delete pvc --all -n <namespace>

# 5. 네임스페이스
kubectl delete namespace <namespace>

echo "정리 완료"

진단 및 트러블슈팅

시스템 상태 확인

확인 항목명령어
Kubernetes 설정ls -la /etc/kubernetes/
CNI 설정ls -la /etc/cni/net.d/
containerd 설정grep SystemdCgroup /etc/containerd/config.toml
커널 모듈lsmod \| grep br_netfilter

클러스터 진단

진단 항목명령어
노드 상태kubectl describe nodes
시스템 Podkubectl get pods -n kube-system
이벤트 확인kubectl get events --sort-by='.lastTimestamp' -A
리소스 사용량kubectl top nodes

문제 해결

문제 유형진단 명령어
Pod 시작 실패kubectl describe pod <name>
네트워크 문제kubectl exec -it <pod> -- nslookup kubernetes.default
스토리지 문제kubectl describe pvc <name>
Service 문제kubectl get endpoints <service-name>
This post is licensed under CC BY 4.0 by the author.