Docker & Kubernetes

kubernetes - 명령어 정리 및 사용법

홍덕화 2019. 10. 3. 22:16

사용자가 쿠버네티스를 사용할때는 명령어를 입력하고 이 명령어를 입력받은 apiserver가 해당 명령어를 수행한다.

 

기본적인 명령어 형식은 kubectl [COMMAND] [TYPE] {NAME] {FLAGS] 이다.



- COMMAND : create, get, describe 와 같은 operation 종류
- TYPE : pods, nodes 등의 타입 ( https://kubernetes.io/docs/reference/kubectl/overview/#resource-types 참고 )
- NAME : 특정 리소스의 이름 선언
- FLAGS : 추가적인 옵션 선언

 

자주 사용하는 command : get, describe, edit, create, delete, top

get : 리소스 리스트를 출력할 때 사용
describe : 리소스와 name에 해당하는 자세한 정보 출력( IP, 작업내용 등)
edit : Pod, Service, PVC 등 resource에 설정을 변경할 때 사용
create : resource를 생성할 때 사용

apply : resource를 적용할 때 사용
delete : resource를 제거할 때 사용
top : node의 cpu, memory 사용량 확인


kubectl describe node {node명} -> 해당 node pod 사용량 확인

 

자주 사용하는 resource : pods, nodes, services(svc), storageclass(sc), pvc(영구볼륨클레임), pv(영구볼륨)

 

 

탭 키로 명령어 자동완성하는 방법

 

source <(kubectl completion bash)

echo 'source <(kubectl completion bash)' >>~/.bashrc

 

여러 리소스에 한번에 명령 내리기

1. 똑같은 operation을 여러 같은 type의 리소스에 명령하는 경우

-> kubectl get pods pods1 pods2

2. 똑같은 operation을 다른 type의 리소스에 명령하는 경우

-> kubectl get pods/pods1 replicationcontroller/rc1

azure cli에서 cluster 설정 

az aks get-credentials --resource-group ( 리소스 그룹 명 ) --name ( 클러스터 명 )

  
모든 namespace의 pods 정보 확인 

-> kubectl get pods -o wide --all-namespaces

특정 namespace의 pods 정보 확인

-> kubectl get pods -o wide -n [namespace 명]

특정 node에 배포된 pod 정보 출력

-> kubectl get pods --field-selector=spec.nodeName= 

특정 노드의 정보 출력

-> kubectl describe nodes 

특정 파드의 정보 출력

-> kubectl describe pods

example.yaml 파일이름의 서비스를 생성

-> kubectl create -f example.yaml 

특정 replicaset이 제어하는 pod들의 정보 출력

-> kubectl describe pods 

특정 을 가진 pod의 첫번째 container에 bash shell 실행

-> kubectl exec -it  /bin/bash

특정 이름을 가진 pod의 로그 조회

-> kubectl logs 

 

특정 pod의 컨테이너에 접근

 

kubectl -n [네임스페이스명] exec -it [pod이름] /bin/bash