본문 바로가기
Container & Orchestration

자주 쓰는 kubectl 명령어

by abstract.jiin 2023. 8. 17.

자주 쓰는 kubectl  명령어 

 

get 

kubectl get pod nginx -o yaml

 

run, create, apply 

kubectl run nginx --image=nginx -o yaml --dry-run=client
kubectl create deploy nginx --image=-nginx

kubectl apply -f  [yaml경로]

 

 

delete

kubectl delete [yaml경로]

 

yaml 로 설치한 구성 전부 삭제할 경우, 해당 yaml 이 속한 디렉토리로 delete 명령 날리면 전부 함께 삭제 된다.

마찬가지로 실행 순서 상관없이 디렉토리 내 모든 yaml 기동을 하려면, 디렉토리경로를 입력해서 apply 해도 가능하다.

서비스 지정해서, 이름으로 내릴 수도 있다. 

 

kubectl delete pod [pod이름]

kubectl delete service [service 이름]

exec

해당 pod에 직접 접속하기

kubectl exec [pod 이름]  -it -- /bin/bash

 

kubectl exec deploy-hostpath-55bf9cdff5-xtgvb -it -- /bin/bash

 

https://jamesdefabia.github.io/docs/user-guide/kubectl/kubectl_exec/

  -c, --container="": Container name. If omitted, the first container in the pod will be chosen
  -p, --pod="": Pod name
  -i, --stdin[=false]: Pass stdin to the container
  -t, --tty[=false]: Stdin is a TTY

 

 

 

 

scale 

kubectl scale deployment nginx --replicas=3 

 

edit


event 

 

kubectl get event 
kubectl get event -n kube-system


describe 


kubectl describe pod niginx


logs

kubectl logs nginx(container 로그 표시됨)

 

예약 단축어

pod,pods > po 
deployment, deployments > deploy
nodes > no
namespaces > ns
services >  svc 
endpoint > ep

 

예시  : k get po

dry run
-o yaml

--dry-run=client   -o yaml

--dry-run=client -o yaml



watch 의w 

 

kubectl get pod -w 

실시간 pod 상태 추적

 

watch "kubectl describe po {관찰할 pod name}| tail"

watch "kubectl describe po liveness-exec | tail"

실시간 pod describe 상태 추적

 

 

label 

# kubectl label {object} {pod name} {label key}={labelvelue}  

kubectl label pod nginx input=test

 

지정된 label 전체 보기

kubectl get po --show-labels

 

 

label이 input=test 로 지정된 pod 보기 

#kebectl get pod -l {label key}={label value}

kubectl get po -l input=test

 

 

dry run 진행시,

run 으로 dry run 하면 label 이 자동으로 run = {pod name} 로 배정

create 으로 dry run 하면 label 이 자동으로 app =  {pod name} 으로 배정됨 

 

응용)

이렇게도 확인할 수 있다.!

run으로 기동한 pod 검색하기

k get po -l run

 

label 의 제거

label key 값에 (-)

#kebectl label pod {pod name} {label key}-

kubectl label pod nginx input-