becool

20210826 (목) liveness, restartPolicy, replication controller 본문

kubernetes

20210826 (목) liveness, restartPolicy, replication controller

gusalstm 2021. 8. 26. 17:30
반응형

 

 Label 

   Kubernetes Cluster 에서 Object를 식별하여 속성을 지정할 때 사용

 - Pod 목록 확인 (레이블)

   kubectl get pods ---show-labels

 - 레이블 지정, 변경 옵션 (명령어)

   kubectl label OBJECT OBJECT_NAME ( --overwrite ) 

 - 레이블로 검색

   kubectl get pods --show-labels -l 'KEYWORD'

 - 레이블 삭제 

   kubectl label node NODENAME KEY_NAME -

   vagrant@kube-control1:~$ kubectl label nodes kube-node1 node-   
      node/kube-node1 labeled    →  node라는 키값을 갖는 label을 kube-node1에서 삭제

vagrant@kube-control1:~/work/20210826$ kubectl get pods --show-labels -l 'app'
NAME         READY   STATUS    RESTARTS   AGE   LABELS
nginx-pod1   1/1     Running   0          21m   app=web-public,run=nginx-pod1
nginx-pod2   1/1     Running   0          20m   app=internal-app,run=nginx-pod2,tier=frontend
test-web     1/1     Running   0          10m   app=internal-app,tier=frontend
vagrant@kube-control1:~/work/20210826$ kubectl get pods --show-labels -l '!tier'
NAME         READY   STATUS    RESTARTS   AGE   LABELS
nginx-pod1   1/1     Running   0          21m   app=web-public,run=nginx-pod1
vagrant@kube-control1:~/work/20210826$ kubectl get pods --show-labels -l 'app in(internal-app,web-public)'
NAME         READY   STATUS    RESTARTS   AGE   LABELS
nginx-pod1   1/1     Running   0          23m   app=web-public,run=nginx-pod1
nginx-pod2   1/1     Running   0          23m   app=internal-app,run=nginx-pod2,tier=frontend
test-web     1/1     Running   0          13m   app=internal-app,tier=frontend

 

 Annotation

   오브젝트에 비식별 메타데이터를 지정함. 검색할 수 없으며, describe pod나 -o yaml 형태시 출력됨 (-l 옵션 불가)

 - Annotation 지정(명령어)

   kubectl annotate pod POD_NAME ANNOTATION 

   kubectl annotate pods nginx-pod1 maintainer="Hong gildong"

 

 Namespace

   Kubernetes Cluster를 논리적으로 나눠주는 논리적 파티션

   각각의 격리된 공간에 같은 pod(object)를 생성하고 관리할 수 있게 됨

 - Namespace (명령어)

   kubectl get pods -n NAMESPACE : namespace지정하여 pod 목록 확인

   kubectl create namespace NAMESPACE : 생성

   kubectl create -f [YAML] -n NAMESPACE : manifestfile 실행 시 속할 namespace 지정

 

반응형

   

 Pod의 Life Cycle 및 Probe

 

 Pod의 Life Cycle : Pending → Running → Succeed or Failed

 Pod의 실행 단계

   Pending

     pod가 클러스터에서 승인되었으나 실행 단계가 되지 않은 상태

     pod가 스케쥴링 되기 이전의 상태

   Running

     pod가 노드에 할당되어 실행

     하나이상의 컨테이너가 실행되어야 함

   Succeed : pod 내 모든 container(application)이 실행 후 정상 종료된 상태

   Failed : 모든 container가 종료된 상태. 하나 이상의 컨테이너가 비정상적으로 종료된 상태

   Unknown : 알 수 없는 상태

 

 Container의 상태

   Waiting : 컨테이너가 Running 상태나 Terminated 상태가 아닌 상태

   Running : 컨테이너가 문제없이 실행 중인 상태

   Terminated : 컨테이너의 실행이 완료된 상태

 

 Container 재시작 정책 (restartPolicy)

   kubectl describe pod POD 시, container status에 따라 restart를 실행할건지를 결정

   Always : (Default) 종료/실패시 항상 재시작 

   Onfailure : 실패시 재시작

   Never : 재시작하지 않음

##### restartPolicy onfailure 예제 #####

vagrant@kube-control1:~/work/20210826$ cat test-pod-restart-onfailure.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod-restart-onfailure
spec:
  restartPolicy: OnFailure
  containers:
  - name: test-restart-onfailure
    image: titiiscat/busybox:uploadtest
    command: ["sh"]
    args: ["-c", "sleep 30 && exit 1"]  → exit code 1번을 출력함으로써 정상적으로 종료되지 않은 것 처럼 보임.
vagrant@kube-control1:~/work/20210826$ kubectl describe pod test-pod-restart-onfailure
    State:          Running
      Started:      Thu, 26 Aug 2021 04:06:24 +0000
    Last State:     Terminated
      Reason:       Error
      Exit Code:    1
      Started:      Thu, 26 Aug 2021 04:05:39 +0000
      Finished:     Thu, 26 Aug 2021 04:06:09 +0000
    Ready:          True
    Restart Count:  2


##### 비교 : exit 0으로 출력값을 true로 했을 때 #####
vagrant@kube-control1:~/work/20210826$ kubectl describe pod test-pod-restart-onfailure
    Command:
      sh
    Args:
      -c
      sleep 30 && exit 0
    State:          Terminated
      Reason:       Completed  → 정상종료 확인
      Exit Code:    0
      Started:      Thu, 26 Aug 2021 04:10:14 +0000
      Finished:     Thu, 26 Aug 2021 04:10:44 +0000
    Ready:          False
    Restart Count:  0

   

 

 Container Probe

   컨테이너의 동작 상태를 감시

   Kubelet이 Probe Handler를 호출하여 컨테이너 상태를 감시

 

 Probe Handler의 Probe 방식

   HTTPGetAction : HTTP GET 메서드로 특정 경로의 웹 리소스 요청

   TCPSocketAction : TCP Socket통신을 사용하여 특정 Port로 연결을 시도

   ExecAction : 컨테이너 내에 있는 특정 실행바이너리를 실행하여 정상적으로 종료가 되는지 여부 확인

 

 Probe Handler의 상태 종류 :   Success    Failure    Unknown  

 

 컨테이너 프로브 종류 Container Probe Types

   livenessProbe : 컨테이너 애플리케이션이 정상 동작 중인지를 확인

      → 진단에 실패하게 되면 재시작 정책을 적용함

   readinessProbe : 컨테이너 애플리케이션이 요청을 처리할 준비가되었는지를 확인

      → 진단에 실패하게 되면 endpoint controller가 pod의 ip주소를 endpoint 에서 제거함  

   startupProbe : 컨테이너 애플리케이션이 시작되었는지를 확인 (가장먼저 실행되어야하는 Probe)

      → startupProbe가 선언된 경우 startupProbe가 통과하기 전까지는 다른 Probe는 활성화하지 않음. 

 

728x90

 

 Controller 

   파드(Pod)를 올바르게 동작시키기 위해 특정 상태를 보장하는 역할을 수행하는 오브젝트

   - Replication Controller : Pod가 특정 개수만큼 복제되어 동작하는 것을 보장하는 컨트롤러

     구성에 필요한 요소 : Pod를 지정하기 위한 Label Selector

         새로운 pod의 복제본을 생성하기 위한 pod의 Template

         실행시킬 pod 복제본 갯수

     Replication Controller의 장점 :

         원하는 pod의 복제본 갯수만큼 실행되지 않았을 경우 Template 이용하여 pod를 생성

         노드에 장애 발생 시 장애가 발생한 노드에서 실행중이던 pod를 다른 노드에서 실행되도록 복제본 생성

         수동/자동으로 pod를 수평적 스케일링이 가능함

         label로 관리시, relabel이 되면 label에 맞는 또다른 pod를 생성 (template에 정의한 replicas 숫자만큼 유지)

  

   - Replicaset : 

   - Daemonset : 

 

 

 

vagrant@kube-control1:~/work/20210826$ kubectl get replicationcontroller
NAME       DESIRED   CURRENT   READY   AGE
myapp-rc   3         3         3       24s
vagrant@kube-control1:~/work/20210826$ kubectl describe replicationcontroller
Name:         myapp-rc
Namespace:    default
Selector:     app=myapp-rc
Labels:       app=myapp-rc
Annotations:  <none>
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=myapp-rc
  Containers:
   myapp:
    Image:        go-myweb:latest
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                    Message
  ----    ------            ----  ----                    -------
  Normal  SuccessfulCreate  32s   replication-controller  Created pod: myapp-rc-r8jqj
  Normal  SuccessfulCreate  32s   replication-controller  Created pod: myapp-rc-44dmt
  Normal  SuccessfulCreate  32s   replication-controller  Created pod: myapp-rc-pm88w
  
vagrant@kube-control1:~/work/20210826$ kubectl scale replicationcontrollers myapp-rc --replicas 4
replicationcontroller/myapp-rc scaled
vagrant@kube-control1:~/work/20210826$ kubectl get pods --show-labels
NAME             READY   STATUS    RESTARTS   AGE   LABELS
myapp-rc-44dmt   1/1     Running   0          52m   app=myapp-rc
myapp-rc-4w5cx   1/1     Running   0          14m   app=myapp-rc
myapp-rc-bhglh   1/1     Running   0          10s   app=myapp-rc
myapp-rc-pm88w   1/1     Running   0          52m   app=myapp-rc

 

 

728x90
Comments