파드(Pod) 는 쿠버네티스에서 생성하고 관리할 수 있는 배포 가능한 가장 작은 컴퓨팅 단위이다.

  • Kubernetes Documentation: Pods


Pod의 기본

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

Pod를 생성하려면 아래와 같이 명령어를 실행한다.

kubectl apply -f https://k8s.io/examples/pods/simple-pod.yaml

아래와 같이 cat 명령어를 응용하면 yaml파일을 만들지 않고도 Pod를 생성할 수 있다.

cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
  name: http-go
spec:
  containers:
  - name: http-go
    image: gasbugs/http-go
    ports:
    - containerPort: 8080
EOF

생성된 Pod를 확인하려면 아래와 같은 명령어를 입력한다.

kubectl get pod
kubectl get pod -w
  • -w 옵션은 실시간으로 모니터링 하는 기능이다.

  • http-go이름의 Pod 상세 정보를 확인하고 싶다면 아래와 같은 명령어를 입력한다.

kubectl describe pod http-go

생성된 Pod의 웹 페이지에 바로 접근하고 싶다면, port-forward 기능을 사용한다.

kubectl port-forward http-go 8080:8080

# 예시
$ kubectl port-forward http-go 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080

# 해당 상태에서 다른 터미널에서 curl 명령어를 실행하면 Welcome! http-go 라는 글자가 출력된다.
$ curl localhost:8080
Welcome! http-go

# 그이후에 kubectl port-forward 명령어를 내린 터미널에서는 아래와 같은 로그가 출력된다.
Handling connection for 8080

# port-forward 설정을 하지 않고 Pod 안에서 바로 8080 포트를 확인하는 방법이다.
$ kubectl exec http-go -- curl 127.0.0.1:8080 -s
Welcome! http-go

kubectl 작성요령을 확인하고 싶다면 아래와 같은 명령어를 입력한다.
간단한 설명이 나오며 상세한 내용은 출력되는 url주소를 확인해야 한다.

kubectl explain pods

생성된 Pod를 삭제하고 싶다면 아래 명령어를 사용한다.

kubectl delete pod http-go

# yaml 파일을 통해 생성했다면 아래 명령어 처럼 생성과 삭제를 할 수 있다.
kubectl create -f test.yaml
kubectl delete -f test.yaml

Pod의 로그를 확인하고 싶다면 아래와 같은 명령어를 실행한다.

kubectl logs http-go

Pod에 특별히 로그를 남기는 기능이 없다면 출력되는 내용은 없다.

Pod의 Annotation은 라벨처럼 사용자가 원하는 값을 설정하기 보다는 쿠버네티스 시스템에서 필요한 정보들을 표시해 주기 위해서 사용한다.
Annotation에서 사용되는 키는 쿠버네티스 시스템이 인식할 수 있는 값들을 사용한다. 레이블과 같이 key-value 구조이다.

kubectl annotate pod http-go test11=test22

# Pod를 describe으로 봤을 때 Annotations 항목에서 확인 할 수 있다.
$ kubectl describe pod http-go
Name:             http-go
...
Annotations:      cni.projectcalico.org/containerID: 01c04c225ea3d8bc5438bf81f1ac885947774f895306af9eac828c4ebd2931b6
                  cni.projectcalico.org/podIP: 192.168.180.196/32
                  cni.projectcalico.org/podIPs: 192.168.180.196/32
                  test11: test22


Liveness, Readiness, Startup Probe

Liveness Probe

  • 컨테이너가 살아있는지 파악, 교착 상태에 빠진 컨테이너를 재시작
  • 높은 가용성

Readiness Probe

  • Pod가 준비된 상태인지 확인, 정상 서비스를 시작
  • 준비가 완전하지 않은 경우 로드밸런싱을 하지 않는다.

Startup Probe

  • 애플리케이션 가동 시간을 확보(구동시간이 오래걸리는 경우)
  • Startup Probe가 동작중에는 Liveness, Readiness 기능 비활성화 처리된다.

이러한 기능들은 Pod의 옵션으로 추가할 수 있다.

exec-liveness.yaml 예제 설명

  • 링크 - exec-liveness.yaml
  • 컨테이너에서 동작 설명
    • 구성 파일에서 Pod에 단일 컨테이너가 있음을 확인할 수 있습니다.
    • periodSeconds 필드는 kubelet이 5초마다 활성 프로브를 수행해야 함을 지정합니다.
    • initialDelaySeconds 필드는 첫 번째 프로브를 수행하기 전에 5초를 기다려야 함을 kubelet에 알려줍니다.
    • 프로브를 수행하기 위해 kubelet은 대상 컨테이너에서 cat /tmp/healthy 명령을 실행합니다.
    • 명령이 성공하면 0을 반환하고 kubelet은 컨테이너가 살아 있고 건강한 것으로 간주합니다.
    • 명령이 0이 아닌 값을 반환하면 kubelet은 컨테이너를 종료하고 다시 시작합니다.
# 해당 예제를 실행하면 아래와 같은 이벤트를 확인 할 수 있으며
Events:
  Type     Reason     Age              From               Message
  ----     ------     ----             ----               -------
  Normal   Scheduled  41s              default-scheduler  Successfully assigned default/liveness-exec to worker-node1
  Normal   Pulling    40s              kubelet            Pulling image "registry.k8s.io/busybox"
  Normal   Pulled     38s              kubelet            Successfully pulled image "registry.k8s.io/busybox" in 1.718s (1.718s including waiting)
  Normal   Created    38s              kubelet            Created container liveness
  Normal   Started    38s              kubelet            Started container liveness
  Warning  Unhealthy  0s (x2 over 5s)  kubelet            Liveness probe failed: cat: can't open '/tmp/healthy': No such file or directory
# 계속해서 RESTARTS를 하고 있다는 것을 알수 있다.
$ kubectl get pod
NAME            READY   STATUS    RESTARTS      AGE
liveness-exec   1/1     Running   1 (15s ago)   91s

http-liveness.yaml 예제 설명

  • 링크 - http-liveness.yaml
  • http 헬스 체크하는 예제
    • 컨테이너 시작한지 2~5 사이에 http 200 리턴을 한다.
    • 그 이후 부터는 http 500 리턴을 하도록 만들어져 있다.
# 해당 예제를 실행하면 아래와 같은 이벤트를 확인 할 수 있으며
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  34s                default-scheduler  Successfully assigned default/liveness-http to worker-node1
  Normal   Pulled     32s                kubelet            Successfully pulled image "registry.k8s.io/liveness" in 1.753s (1.753s including waiting)
  Normal   Pulling    13s (x2 over 34s)  kubelet            Pulling image "registry.k8s.io/liveness"
  Normal   Killing    13s                kubelet            Container liveness failed liveness probe, will be restarted
  Normal   Created    12s (x2 over 32s)  kubelet            Created container liveness
  Normal   Started    12s (x2 over 32s)  kubelet            Started container liveness
  Normal   Pulled     12s                kubelet            Successfully pulled image "registry.k8s.io/liveness" in 1.249s (1.249s including waiting)
  Warning  Unhealthy  1s (x4 over 19s)   kubelet            Liveness probe failed: HTTP probe failed with statuscode: 500
# 계속해서 RESTARTS를 하고 있다는 것을 알수 있다.
$ kubectl get pod
NAME            READY   STATUS    RESTARTS      AGE
liveness-http   1/1     Running   5 (3s ago)   2m3s