๐Ÿ“‚ Container & K8S/Kubernetes

[K8S] kubectl ๋ช…๋ น์–ด ์ •๋ฆฌ

dhyuck 2023. 3. 14. 21:50
๋ฐ˜์‘ํ˜•
  • kubectl ๊ตฌ๋ฌธ

      kubectl [COMMAND] [TYPE] [NAME] [FLAGS]
      COMMAND : ๋ฆฌ์†Œ์Šค์— ์ˆ˜ํ–‰ํ•˜๋Š” ๋™์ž‘์„ ์ง€์ •(create, get, describe, delete ๋“ฑ)
      TYPE : ๋ฆฌ์†Œ์Šค ํƒ€์ž…์„ ์ง€์ •(node, pod, service)
      NAME : ๋ฆฌ์†Œ์Šค ์ด๋ฆ„์„ ์ง€์ •
      FLAGS : ๋ถ€๊ฐ€์ ์„ ์„ค์ •ํ•  ์˜ต์…˜(--helo, --dry-run, -o yaml ๋“ฑ)
  • kubectl ์•ฝ์–ด(shortname)

      ns -> namespace
      sa -> serviceaccount
      no -> node
      po -> pod
      rs -> replicaset
      deploy -> deployment
      sts -> statefulset
      ds -> daemonsets
      svc -> service
      ing -> ingress
      pv -> persistentvolume
      pvc -> persistentvolumeclaim
  • kubectl ๋ช…๋ น์–ด

      kubectl api-resources          # ์ „์ฒด ์˜ค๋ธŒ์ ํŠธ ์ข…๋ฅ˜ ํ™•์ธ, ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ API ๋ฆฌ์†Œ์Šค๋ฅผ ๋‚˜์—ด
      kubectl apply -f k8s-app.yaml
    
      # kubectl์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ context๋กœ ์„ค์ •
      kubectl config current-context # ํ˜„์žฌ ํด๋Ÿฌ์Šคํ„ฐ ํ™•์ธ
      kubeclt config use-context k8s # ํด๋Ÿฌ์Šคํ„ฐ ๋ณ€๊ฒฝ
      kubectl config get-contexts
      kubectl cluster-info           # ํด๋Ÿฌ์Šคํ„ฐ ์ƒํƒœ ํ™•์ธ
      kubectl create deployment webserver --image=nginx:1.14 --replicas=2 --dry-run=client -o yaml
      kubectl create namespace my-namespace
      kubectl create serviceaccount my-sa --namespace=my-namespace
      kubectl create role my-role --verb=watch,list --resource=pods -n=my-namespace
      kubectl create rolebinding my-rolebinding --role=my-role --serviceaccount=my-namespace:my-sa -n=my-namespace
      kubectl create clusterrole my-cluster-role --verb=create --resouce=secret  # cluster role์€ ์ „์ฒด ๋„ค์ž„์ŠคํŽ˜์ด์Šค๋ฅผ ๋Œ€์ƒ
      kubectl create clusterrolebinding my-cluster-rolebinding --clusterrole=my-cluster-role --serviceaccount=my-namespace:my-sa
      kubectl create deploy my-deploy --image=tomcat --port=8080 --replicas=2
      kubectl delete -f k8s-app.yaml
    
      kubectl describe pod/my-nginx
      kubectl describe deploy/my-deploy
      kubectl exec -it my-nginx -- bash
    
      kubectl expose deploy my-deploy --type=ClusterIP --port=80 --target-port=8080
      kubectl expose pod my-pod --type=ClusterIP --port=80
    
      kubectl run my-nginx --image=nginx --dry-run=client -o yaml
      kubectl run my-nginx --image=nginx --labels=app=web
      kubectl get all                    # default ๋„ค์ž„์ŠคํŽ˜์ด์Šค์— ์žˆ๋Š” ๋ชจ๋“  ๋ฆฌ์†Œ์Šค ํ™•์ธ
      kubectl get pod --show-labels      # ๋ชจ๋“  ๋ผ๋ฒจ ์ •๋ณด ํ™•์ธ [--show-labels]
      kubectl get pod <NAME> -L gpu      # ๋ผ๋ฒจ ์ •๋ณด ํ™•์ธ [-L]
      kubectl get pod <NAME> -l gpu=true # ๋ผ๋ฒจ์„ ์ด์šฉํ•œ ์กฐ๊ฑด ํ•„ํ„ฐ๋ง [-l]
      kubectl get pod --sort-by=.metadata.creationTimestamp  # ๋ชจ๋“  ํŒŒ๋“œ๋“ค์„ ์ƒ์„ฑ์‹œ๊ฐ„์œผ๋กœ ์ •๋ ฌ
      kubectl get pod -A
      kubectl get endpoints
      kubectl logs my-nginx
      kubectl logs app nginx    # app ํŒŒ๋“œ์˜ nginx ์ปจํ…Œ์ด๋„ˆ์˜ ๋กœ๊ทธ ํ™•์ธ
      kubectl logs app tomcat   # ๋ฉ€ํ‹ฐ์ปจํ…Œ์ด๋„ˆ ํ™˜๊ฒฝ์—์„œ ์ปจํ…Œ์ด๋„ˆ ์ง€์ •
      kubectl label pod/my-nginx run=myginx        # pod์— ๋ ˆ์ด๋ธ” ์ถ”๊ฐ€
      kubectl label pod/my-nginx run-              # pod์— ๋ ˆ์ด๋ธ” ์‚ญ์ œ
      kubectl label node k8s-worker1 gpu=true      # ํ‚ค๊ฐ€ gpu์ด๊ณ , ๊ฐ’์ด true์ธ ๋ ˆ์ด๋ธ” ์ถ”๊ฐ€
      kubectl label node k8s-worker1 gpu-          # key๊ฐ€ gpu์ธ label ์ œ๊ฑฐ
      kubectl label ns dev stage=dev
    
      kubectl scale rs <RS_NAME> --replicas <NUMBER>         # replicasets์˜ ์Šค์ผ€์ผ ์กฐ์ •
      kubectl scale deploy <DEPLOY_NAME> --replicas <NUMBER> 
      kubectl scale sts <STS_NAME> --replicas <NUMBER> --namespace <NAMESPACE_NAME>
      kubectl set image my-deploy nginx=nginx:1.11.13-alpine  # my-deploy์˜ nginx ์ด๋ฏธ์ง€๋ฅผ ๋กค๋ง ์—…๋ฐ์ดํŠธ
    
      kubectl rollout history deploy/hyuckang-deploy               # ๋กค๋ง์—…๋ฐ์ดํŠธ ํžˆ์Šคํ† ๋ฆฌ ํ™•์ธ
      kubectl rollout history deploy/hyuckang-deploy --revision=1  # revision 1 ํžˆ์Šคํ† ๋ฆฌ ์ƒ์„ธ ํ™•์ธ
      kubectl rollout undo deploy/hyuckang-deploy                  # ๋ฐ”๋กœ ์ „ ๋ฒ„์ „์œผ๋กœ ๋กค๋ฐฑ
      kubectl rollout undo deploy/hyuckang-deploy --to-revision=2  # ํŠน์ • ๋ฒ„์ „์œผ๋กœ ๋กค๋ฐฑ
      kubectl cordon k8s-worker01
      kubectl uncordon k8s-worker01
      kubectl drain k8s-worker02 --ignore-daemonsets
    
      kubectl top pods
      kubeclt top pods --containers
      kubectl top pods --sort-by=cpu  # Pod๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” CPU๋กœ ์ •๋ ฌ
      kubectl top nodes --sort-by=memory
๋ฐ˜์‘ํ˜•

'๐Ÿ“‚ Container & K8S > Kubernetes' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[K8S] Karpenter์™€ Cluster AutoScaler(CA)  (0) 2024.03.11