POD?

  • Container를 표현하는 k8s API의 최소단위
  • k8s가 Pod라는 단위로 컨테이너를 묶어서 관리
    • Pod와 Container 1대 다관계

 

 

 

POD의 생명주기

https://kubernetes.io/ko/docs/concepts/workloads/pods/pod-lifecycle/

 

파드 라이프사이클

이 페이지에서는 파드의 라이프사이클을 설명한다. 파드는 정의된 라이프사이클을 따른다. Pending 단계에서 시작해서, 기본 컨테이너 중 적어도 하나 이상이 OK로 시작하면 Running 단계를 통과하

kubernetes.io

  • Pending
    • K8s 시스템에 Pod를 생성하는 중인 상태
  • Running
    • 파드 안 컨테이너가 실행중인 상태
  • Succeeded
    • Pod 안의 모든 컨테이너가 정상 실행 종료된 상태
  • Failed
    • Pod 안의 모든 컨테이너 중 정상적으로 실행 종료되지 않은 컨테이너가 있는 상태
  • Unknown
    • Pod의 상태를 확인할 수 없는 상태

 

 

 

현재 동작중인 Pod 조회

pod 조회

  • # kubectl get pods
  • # kubectl get pod mypod -o wide
  • kubectl get pod mypod -o yaml
  • kubectl get pod mypod -o json

 

 

 

pod 생성(CLI명령어)

pod 생성 및 테스트

pod 생성

  • # kubectl run webserver2 --image=nginx:1.14 --port 80
    • nginx의 기본 listen port는 80

pod 조회

  • # kubectl get pods
  • # kubectl get pods -o wide
  • # kubectl get pod webserver -o yaml > test.yaml
    • yaml형식으로 test.yaml파일로 output
  • # kubectl get pod webserver -o json > test.json
    • json형식으로 test.json파일로 output
  • # kubectl get pods --all-namespaces
    • 모든 namespace에 동작중인 pod 조회

pod의 로그 출력

  • # kubectl logs webserver

테스트

  • # curl 172.16.132.2

 

 

 

POD생성(yaml) & 명령어 실행가능 상태 확인

yaml파일을 이용하여 pod 생성

  • # kuctl create -f ./nginx.yaml
  • # kubectl apply -f ./nginx.yaml

 

실행가능 상태만 확인

  • # kubectl run webserver --image=nginx --port 80 --dry-run=client

지정된 yaml파일로 출력하여 확인

  • # kubectl run webserver --image=nginx --port 80 --dry-run=client -o yaml

지정된 yaml파일에 출력하여 확인

  • # kubectl run webserver --image=nginx --port 80 --dry-run=client -o yaml > websv_create.yaml

yaml파일을 이용하여 pod 생성

  • # kubectl apply -f ./websv_create.yaml
  • # kubectl create -f ./websv_create.yaml

 

 

 

port-forwarding 테스트

port forwarding 테스트

  • # kubectl port-forward webserver 80:80

 

 

 

Static Pod?

  • kubu-apiserver를 통하지 않고 kublet데몬에 의해서 직접 실행하는 pod
  • kube-apiserver를 통해서 pod를 edit할 수 없음
  • 용도
    • 주로 시스템 pod(kube-apiserver, etcd)를 살행하는 용도로 많이 사용
  • static pod 디렉토리 구성
    • /var/lib/kubelet/config.yaml

staticPodPath의 위치

 

static pod path에 yaml파일 생성

  • static pod를 생성할 node에서 staticPodPath에 yaml파일을 저장하면 kubelet daemon에서 pod를 생성

 

  • static pod path 변경
    • # systemctl restart kubelet
      • kubelet daemon 재시작

 

  • static pod path에서 yaml파일삭제
    • 해당 yaml로 실행된 pod가 삭제됨

 

  • master node에서 구동하는 시스템 pod들은 static pod 형태로 구동됨을 확인 할 수 있다

 

 

 

POD에 CPU/Memory 메모리 자원 할당

  • Resource limits
    • pod가 사용할 수 있는  최대 리소스 양을 제한
  • Resource requests
    • pod를 실행하기 위한 최소 리소스 양 요청
  • 메모리를 초과해서 사용되는 pod는 OOM kill되며 재스케줄링 됨

  • 리소스 request, limit 설정하여 pod 생성

 

 

 

Pod 환경변수 설정

  • 외부에서 현재 pod의 환경 변수 조회
    • # kubectl exec {POD_NAME} -- env

 

  • 환경변수 설정

 

 

 

Multi Container?

  • Pod 1개에 다수 컨테이너 구조
  • 모두 노드 하나 안에서 실행
  • pod내 컨테이너들이 자원을 공유

kubernetes.io/ko/docs/tasks/access-application-cluster/communicate-containers-same-pod-shared-volume/

 

공유 볼륨을 이용하여 동일한 파드의 컨테이너 간에 통신하기

이 페이지에서는 동일한 파드에서 실행 중인 두 개의 컨테이너 간에 통신할 때에, 어떻게 볼륨을 이용하는지 살펴본다. 컨테이너 간에 프로세스 네임스페이스 공유하기를 통해 통신할 수 있는

kubernetes.io

 

apiVersion: v1
kind: Pod
metadata:
  name: multipod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    ports:
    - containerPort: 80
  - name: centos-container
    image: centos:7
    command:
    - sleep
    - "100000"

multiContainer.yaml 파일

 

  • # kubectl describe pods multi-container
    • nginx-container, centos-container 2 container가 1개의 pod에서 running중임을 확인
  • # kubectl exec -it multi-container -c nginx-container -it -- /bin/bash
    • multi-container pod의 nginx-container container로 shell 접속

 

redis-server process 구동확인

  • # apt-get update && apt-get install -y procps

 

멀티컨테이너 로그 출력

  • # kubectl logs multi-container -c nginx-container
  • # kubectl delete pod --all
    • 현재 namespace의 모든 pod 삭제

 

 

 

init Container?

  • 메인 컨테이너가 구동하기전에 미리 동작시킬 컨테이너
  • 메인 컨테이너가 실행되기 전에 사전작업이 필요한 경우 사용
  • init 컨테이너를 포함한 pod는 init 컨테이너에 설정한 조건이 성공해야 메인 컨테이너를 구동시킬 수 있다.
  • init 컨테이너는 readinessProbe를 지원하지 않는다.
    • Pod가 모두 준비되기 전에 실행한 후 종료되는 컨테이너이므로(실제 서비스를 위한 컨테이너가 아니므로)

https://kubernetes.io/ko/docs/concepts/workloads/pods/init-containers/

 

초기화 컨테이너

이 페이지는 초기화 컨테이너에 대한 개요를 제공한다. 초기화 컨테이너는 파드의 앱 컨테이너들이 실행되기 전에 실행되는 특수한 컨테이너이며, 앱 이미지에는 없는 유틸리티 또는 설정 스크

kubernetes.io

 

init Container 테스트

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
spec:
  containers:
  - name: myapp-container
    image: busybox:1.28
    command: ['sh', '-c', 'echo The app is running! && sleep 3600']
  initContainers:
  - name: init-myservice
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for myservice; sleep 2; done"]
  - name: init-mydb
    image: busybox:1.28
    command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]

  • # kubectl apply -f ./init_container.yaml
  • init Container가 성공해야 메인컨테이너가 구동 할 수 있다.
    • init Container가 전부 성공할때까지 대기

 

---
apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
---
apiVersion: v1
kind: Service
metadata:
  name: mydb
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9377

  • # kubectl apply -f ./service.yaml
  • 각 init 컨테이너가 대기하는 2개의 service를 생성
    • 메인 컨테이너가 Running 상태로 변함을 알 수 있다.

 

 

 

infra Container(Pause)?

  • pod당 1개 생성되는 컨테이너
  • PID 1로 설정되며 다른 컨테이너의 부모 컨테이너 역할
  • 역할
    • Pod에 대한 인프라(IP, hostname등) 네트워크 리소스를 관리하고 생성

 

infra Container 테스트

  • work node에서 컨테이너 조회
    • pod가 1개 생성될때 1개의 pause 컨테이너가 같이 생성됨을 확인 할 수 있다
  • pod가 삭제될때 해당 pod의 pause 컨테이너도 같이 삭제됨을 확인 할 수 있다

'클라우드 > K8s' 카테고리의 다른 글

K8s namespaces  (0) 2022.03.17
K8s Pod 구성패턴  (0) 2022.03.17
K8s 동작원리  (0) 2022.03.17
Single Master K8s 설치 및 기본 명령어  (0) 2022.03.17
Kubernetes?  (0) 2022.03.17

+ Recent posts