multi-container POD 생성하기

multi-container를 정의한 yaml 파일로 pod를 생성한다.

- file명 : multipod.yaml

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

아래 명령어를 수행하여 multipod를 생성하고 조회합니다.

## multipod 생성
kubeclt create -f multipod.yaml

## pod 조회
kubectl get pods -o wide
NAME                      READY   STATUS    RESTARTS      AGE     IP          NODE     NOMINATED NODE   READINESS GATES
multipod                  2/2     Running   0             19m     10.44.0.4   node-1   <none>           <none>

READY 상태가 2/2 로 표시되는데 이는 2개의 컨테이너가 running 상태임을 나타냅니다.

 

생성된 centos-container 에 접속한 후 웹페이지를 조회합니다.

$ kubectl exec multipod -it -c centos-container -- /bin/bash

## centos-container 에 접속한 상태
[root@multipod /]# curl http://localhost:80

## 실행결과
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
......

## centos-container ps로 프로세스 확인
[root@multipod /]# ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 13:17 ?        00:00:00 sleep 10000
root          26       0  0 13:43 pts/0    00:00:00 /bin/bash
root          41      26  0 13:44 pts/0    00:00:00 ps -ef
=> nginx 프로세스는 보이지 않음

[root@multipod /]# curl localhost:80
=> nginx 페이지를 불러옴

## centos-container 에서 빠져나옴
[root@multipod /]# exit

위에서 확인할 수 있듯이 POD의 컨테이너는 동일한 IP를 사용하고 있는 것을 알 수 있습니다.

 

컨테이너의 log를 조회합니다.

kubectl logs multipod -c nginx-container

 

[참고 : deployment 삭제]

deployment 생성했다면 pods를 먼저 삭제하면 다시 실행되기 때문에 deployment 를 삭제해야 한다. 

systanx : kubectl delete deployment <deployment_name>
예시) deployment 이름이 deploy_app 인 경우 deployment 삭제 명령어
kubectl delete deployment deploy_app

 

 

 

POD 동작 flow

pod 생성 과정을 보기 위해 아래와 같이 --watch option으로 모니터링을 합니다.

kubectl create 명령어를 수행하면 pod 생성 과정을 볼 수 있습니다.

$ kubectl get pods -o wide --watch

NAME        READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
nginx-pod   0/1     Pending   0          0s    <none>   <none>   <none>           <none>
nginx-pod   0/1     Pending   0          0s    <none>   node-2   <none>           <none>
nginx-pod   0/1     ContainerCreating   0          0s    <none>   node-2   <none>           <none>
nginx-pod   1/1     Running             0          3s    10.36.0.1   node-2   <none>           <none>

새로운 창에서 pod를 생성하면서 watch 옵션으로 수행중인 명령창을 확인합니다.

$ kubectl create -f nginx.yaml

nginx.yaml 파일 내용

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

 

Pod 관리하기

- 동작중인 pod 정보 보기

kubectl get pod -o wide

kubectl describe pod <pod_name> -o wide

 

- 동작중인 pod 수정

kubectl edit pod <pod_name>

 

- 동작중인 pod 삭제

kubectl delete pod <pod_name>

kubectl delete pod --all

 

 

LivenessProbe 로 self-healing pod 만들기

- kubelet로 컨테이너 진단하기

pod가 계속 실행할 수 있음을 보장

pod의 spec에 정의

 

pod definition livenessProbe definition
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
  - image: nginx
    name: nginx-container
    ports:
    - containerPort: 80
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers:
  - image: nginx
    name: nginx-container
    ports:
    - containerPort: 80
    livenessProbe:
      httpGet:
        path: /
        port: 80
      initialDelaySeconds: 15 
      periodSeconds: 20
      timeoutSeconds: 1
      successThreshold: 1
      failureThreshold: 3

LivenessProbe  메커니즘

httpGet probe: 지정한 IP주소, port, path에 HTTP GET 요청을 보내 해당 컨테이너가 응답하는지를 확인합니다. 반환코드가 200이 아닌 값이 나오면 오류이고 컨테이너를 다시 시작합니다.

    livenessProbe:
      httpGet:
        path: /
        port: 80

tcpSocket probe:  지정된 포트에 TCP연결을 시도. 연결되지 않으면 컨테이너를 다시 시작합니다.

    livenessProbe:
      tcpSocket:
        port: 22

exec probe : exec 명령을 전달하고 명령의 종료코드가 0이 아니면 컨테이너를 다시 시작합니다.

    livenessProbe:
      exec:
        command:
        - ls
        - /filesystem

livenessProve 매개변수를 설정하지 않을 경우에는 default 값을 사용합니다. 아래 명령어를 수행한 우 describe 를 하면 Liveness 정보를 확인할 수 있습니다.

kubectl describe pod <pod-name>

## 예제
$ kubectl describe pod nginx-pod-liveness | grep -i liveness
Name:         nginx-pod-liveness
    Liveness:       http-get http://:80/ delay=0s timeout=1s period=10s #success=1 #failure=3
  Normal  Scheduled  44s   default-scheduler  Successfully assigned default/nginx-pod-liveness to node-2

 

 

 

livenessProve 매개변수

      initialDelaySeconds: pod 실행 후 delay 할 시간 
      periodSeconds: health check 반복 수행 시간
      timeoutSeconds: health check 응답을 기다리는 시간
      successThreshold: 1
      failureThreshold: 3

 

 

 

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

 

init container : 초기화 컨테이너

앱 컨테이너 실행전에 미리 동작시킬 컨테이너

main 컨테이너가 실행되기 전에 사전 작업이 필요한 경우 사용

초기화 컨테이너가 모두 실행된 후에 앱 컨테이너 실행

 

 

main container : init 컨테이너에 의존

 

예를 들어 pod에 여러개의 컨테이너가 있을 경우, init 컨테이너가 성공해야 main 컨테이너가 실행됨

 

사용 중인 초기화 컨테이너

쿠버네티스 1.5에 대한 다음의 yaml 파일은 두 개의 초기화 컨테이너를 포함한 간단한 파드에 대한 개요를 보여준다. 첫 번째는 myservice 를 기다리고 두 번째는 mydb 를 기다린다. 두 컨테이너들이 완료되면, 파드가 시작될 것이다.

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 myapp.yaml

myapp.yaml 파일 내용

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

init container를 실행하기 위해 myapp.yaml 파일로 myservice 서비스를 생성합니다.

kubectl create -f myservice.yaml

 

mydb.yaml 파일 내용

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

init container를 실행하기 위해 mydb.yaml 파일로 mydb 서비스를 생성합니다.

kubectl create -f mydb.yaml

위 명령어를 실행 후 pod 상태를 조회해 보면, init 컨테이너가 실행되면서 pod가 running 상태로 변경된 것을 확인할 수 있습니다.

kubectl get pods myapp-pod -o wide --watch

infra container(pause)

pod를 생성하면 기본적으로 생성되는 컨테이너입니다.

pause 컨테이너를 확인하기 위해 pod를 하나 생성합니다.

$ kubectl run  webserver --image=nginx

컨테이너 정보를 확인합니다.

 

 

+ Recent posts