Kubernetes 기본 명령어

알파로그·2023년 7월 4일
0

Kubernetes

목록 보기
4/15

📍 명령어 자동완성 시키기

  • 설치와 설정이 완료되면 명령어 자동완성 기능이 활성화된다.
    • 만약 기능이 제대로 작동되지 않는다면 다시 로그인 하면된다.
sudo yum install bash-completion -y
source /usr/share/bash-completion/bash_completion
kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null
echo 'alias k=kubectl' >>~/.bashrc
echo 'complete -o default -F __start_kubectl k' >>~/.bashrc
source /usr/share/bash-completion/bash_completion

📍 node 조회하기

  • 기본 조회
kubectl get nodes
  • 모든 node 조회
kubectl get nodes -A
  • 상세 조회
kubectl get nodes -A -o wide

📍 pod 조회하기

  • Pod 는 Kubenetes 의 배포 최소 단위를 뜻한다.
    • docker 와 비교하면 container 쯤으로 생각할 수 있다.
kubectl get pods
  • 모든 pod 조회
kubectl get pods -A
  • 기본 세팅만 완료 후 모든 pod 를 조회하면 아래와 같이 나온다.
    • 첫번째와 두번째 status 가 pending 이기 때문에 get nodes 명령어를 입력했을 때 Status 가 NotRead 로 출력된다.
NAMESPACE     NAME                             READY   STATUS    RESTARTS   AGE
kube-system   coredns-5d78c9869d-qwc65         0/1     Pending   0          48m
kube-system   coredns-5d78c9869d-t7rfv         0/1     Pending   0          48m
kube-system   etcd-master                      1/1     Running   3          48m
kube-system   kube-apiserver-master            1/1     Running   3          48m
kube-system   kube-controller-manager-master   1/1     Running   3          48m
kube-system   kube-proxy-9q59n                 1/1     Running   0          48m
kube-system   kube-scheduler-master            1/1     Running   3          48m

📍 모든 오브젝트 한번에 확인하기

  • pod 와 node 를 한번에 확인할 수 있다.
kubectl get all
  • 마찬가지로 -o wide 속성을 추가하면 더 구제적인 상태를 확인할 수 있다.
kubectl get all -o wide

📍 Pod 삭제하기

  • pod 뿐만아니라 deployments 도 삭제가 가능하다.
kubectl delete 파드이름

✏️ pod 에 명령하기

📍 기본 명령어

  • 아래의 명령어로 특정 pod 에 명령을 할 수 있다.
kubectl exec -it 파드명 -- 명령어

📍특정 pod 의 Container 접속

kubectl exec -it 파드명 -- bash

📍 pod 띄우기

  • 보통은 아래처럼 단일 pod 로 띄우지 않고 replicaset 과 함께 deployment 로 묶어서 pod 를 띄운다.
kubectl run 파드명 --image=이미지명

📍 deployment 띄우기

  • deployment 는 replicaset 의 묶음을 뜻한다.
    • replicaset 은 pod 의 복제본을 뜻한다.
kubectl create deployment 파드명 --image=이미지명 --replicas=복제수
  • 아래의 명령으로 생성된 deployment 내부엔 replicaset 이 있고,
    그 내부에 3개의 nginx pod 가 생성된다.
# EX)
# webserver 라는 이름으로 2개의 복제본을 가진 
kubectl create deployment webserver --image=nginx --replicas=3
  • 현황을 확인하니 아래와 같이 출력되었다.
    • watch 옵션을 앞에 추가하면 실시간 현황을 확인할 수 있다.
    • pod
      • 생성된 복제 pod
    • replicaset
      • 복제 pod 를 감사는 replicaset
      • 복제 pod 3개가 정상적으로 돌아가고 있다.
      • CURRENT 는 실제로 작동중인 pod 수를 뜻한다.
        • DESIRED 를 수정하면 kubernetes 는 동기화를 위해 CURRENT 값을 수정된 DESIRED 값과 동일하게 수정시켜준다.
    • deployment
      • 마찬가지로 복제 pod 3 개가 정상적으로 작동되는걸 확인할 수 있다.
NAME                             READY   STATUS    RESTARTS   AGE
pod/webserver-566cf946c6-gndfz   1/1     Running   0          87s
pod/webserver-566cf946c6-n5mjv   1/1     Running   0          87s
pod/webserver-566cf946c6-rt9wr   1/1     Running   0          87s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   4h3m

NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/webserver   3/3     3            3           87s

NAME                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/webserver-566cf946c6   3         3         3       87s
profile
잘못된 내용 PR 환영

0개의 댓글