클라우드/쿠버네티스

[쿠버네티스] 쿠버네티스 3: 쿠버네티스 pod 명령어 모음

KyuminKim 2024. 11. 27. 00:03

이번 시간에는 pod 관련 명령어를 알아보자!


pod 생성

pod 생성의 3가지 방법

 

(1) run

$ kubectl run webserver --image nginx:1.14 --port 80
$ kubectl run test --image nginx:latest --port -dry-run=client
--image (이미지명:태그)         : pod 이미지 지정
--port (포트번호)                    : 포트번호 지정
--env  (“=“)                     환경변수 지정
--dry-run (server, client, none) : 실행은 않고 해당 오브젝트 생성 파일(yaml) 추출
--restart Never                       : 컨테이너 프로세스가 종료되어도 프로세스 재시작 x
   
* 기본적으로 쿠버네티스는, pod에서 실행되는 컨테이너 프로세스가 종료된다면 항상 재시작하여
항상 컨테이너가 동작되도록  (restart policy참고)
 
 

(2) create

$ kubectl create -f pod-example.yaml
# pod-example.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-example
  labels:
    app: nginx
spec:
    containers:
    - name: nginx
      image: nginx:latest
      ports:
      - containerPort: 80
        protocol: TCP

 

(3) apply

$ kubectl apply -f pod-example2.yaml
# pod-example2.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-example2
  labels:
    app: nginx
spec:
    containers:
    - name: nginx
      image: nginx:latest
      ports:
      - containerPort: 80
        protocol: TCP

 

 

🙋‍♀️pod yaml

 


pod 조회

생성한 pod들을  눈에 보자!
list 형태로 확인할  있으며, 생성  날짜, pod  컨테이너 개수 지표 확인 가능
$ kubectl get pod -o wide                  # 상세한 정보 조회
$ kubectl get pod -o json                   # json 형식으로 출력
$ kubectl get pod --show-labels           # pod의 label 조회

 


pod 상세조회

$ kubectl logs (pod이름)
$ kubectl logs (pod이름) -c (컨테이너이름)       # 컨테이너를 지정하여 로그 조회

 


pod 진입

pod라는 컨테이너 묶음을 생성했다!
pod 속에 있는 컨테이너에게 명령어를 전달하거나, 
pod 속에 있는 컨테이너 프로그램에 직접 접근하여 명령어를 실행하고 싶을 
pod에 진입하여 해결할 수 있다
$ kubetl exec -it pod-example -- /bin/bash
$ kubetl exec -it pod-example -c contianer1  -- /bin/bash       # 컨테이너 명시 가능

kubectl exec

$ kubectl attach pod-example -it      # 컨테이너와 상호작용

kubectl attach

 


pod 수정

기존의 pod의 특징(이름, label, 컨테이너 이미지, …) 수정하자

$ kubectl run test --image nginx        # 명령형으로 생성한 경우 (‘test’ pod)
$ kubectl replace -f pod.yaml           # ‘test’ pod 교체
$ kubectl run test2 --image nginx        # 명령형으로 생성한 경우
$ kubectl edit pod test2                 # 편집기를 이용해 리소스 정의 편집

 


pod 삭제

이름, label 선택, 리소스 선택  조건을 가진 pod 삭제 가능하다

$ kubectl delete pod mypod 
$ kubectl delete -f ./pod.yaml                      # pod.yaml 파일로 생성한 pod 삭제
$ kubectl delete pod -l name=myLabel 		        # 특정 label을 가진 pod 삭제
$ kubectl delete pod -n my-ns  --all                # 특정 namespace 속 pod 삭제