본문 바로가기

클라우드/Kubernetes

[k8s] Kubectl 명령어

context

# context 목록 확인
kubectl config get-contexts

# context 변경
kubectl config use-context <context-name>

# 현재 context 확인
kubectl config current-context

pod 전체 확인

# pod 전체 목록 확인
kubectl get pod -A

# pod 전체 목록 IP까지 확인
kubectl get pods -A -o wide --all-namespaces
# 결과
NAMESPACE       NAME      READY   STATUS    RESTARTS      AGE     IP           NODE               NOMINATED NODE   READINESS GATES
test            test-0    1/1     Running   0             146d    1.1.1.1      host-10-0-1-1      <none>           <none>

 

특정 namespace의 pod 확인

# namespace가 test인 pod만 확인
kubectl -n test get pod

# pod IP까지 확인
kubectl -n test get pods -o wide

# kubectl 이후면 순서 상관 없음
kubectl get pod -n test

pod 상세 확인

# node의 자세한 상태와 설정 확인
kubectl describe pod <name>
kubectl describe pod <name> -n <namespace>

pod 로그 확인

# 현재 컨테이너의 로그
kubectl logs <pod name> -n <namespace>

# 이전의 컨테이너 로그. 컨테이너가 재시작 되었을때 사용. 없을 수도 있음
kubectl logs <pod name> -n <namespace> --previous

pod 접속

# pod 접속
kubectl -n <namespace> exec -it <name> -- bash

# 예시
kubectl -n was exec -it test-XXXXX -- bash

pod 내 application 로그 확인

* pod가 어떤 종류인가에 따라서 확인방법이 상이함.

아래는 하나의 예시일 분.

# pod 접속 후
# 목록 확인
ls

# 로그 확인. -f 붙이면 실시간 로그 확인
tail -f application.log

deployment 재시작 (리소스 재시작)

# 재시작
kubectl rollout restart deployment <deployment name> -n <namespace>

node 확인

# 모든 node 목록 확인
kubectl get node

# node의 자세한 상태와 설정 확인
kubectl describe node <name>

service 확인

# 모든 service 목록 확인
$ kubectl get service
# 결과
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.1.0.1    <none>        443/TCP   239d

# 특정 namespace의 service 목록 확인
kubectl get service -n <namespace>
# 결과
NAME              TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
test2-svc         LoadBalancer   10.11.11.111    k8s-~~~        80:32616/TCP   227d
test1-svc         LoadBalancer   10.22.22.222    k8s-~~~        80:31803/TCP   227d
redis-svc         ClusterIP      10.33.33.333    <none>         6379/TCP       227d

SC(StorageClass), PV(Persistent Volume), PVC(Persistent Volume Claim) 확인

# StorageClass 목록 조회
kubectl get sc

# pv 목록 조회
kubectl get pv

# pvc 목록 조회
kubectl get pvc

# 한번에 조회 가능
kubectl get pv,pvc