본문 바로가기
INFRA/DevOps

[K8s] Init container & Infra container

by BTC_프로틴 2022. 9. 14.

Init container란?

  • main container를 실행하는데 필요한 초기화 및 환경 구성
  • app container 실행 전에 미리 동작시킬 container
  • init container가 모두 실행된 후에 app container를 실행
  • Init Containers 참고

[ Hands - on ] : init container

  • init container 2개가 실행되어야 main container가 실행되는 yaml 파일 생성
vi init-container-exam.yaml
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 create -f init-container-exam.yaml
kubectl get pods -o wide --watch
NAME            READY   STATUS             RESTARTS        AGE   IP               NODE                NOMINATED NODE   READINESS GATES
myapp-pod       0/1     Init:0/2           0               0s    <none>           node2.example.com   <none>           <none>
  • init container 중 init-myservice를 실행하기 위한 container yaml파일 생성
vi init-container-exam-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: mydb
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9377
kubectl get pods -o wide --watch
NAME        READY   STATUS    RESTARTS   AGE   IP               NODE                NOMINATED NODE   READINESS GATES
myapp-pod   1/1     Running   0          12m   192.168.221.16   node2.example.com   <none>           <none>
  • 남은 init container init-mydb를 실행하기 위한 container yaml 파일 생성
vi init-container-exam-db.yaml
apiVersion: v1
kind: Service
metadata:
  name: mydb
spec:
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9377
kubectl get pods -o wide --watch
NAME        READY   STATUS    RESTARTS   AGE   IP               NODE                NOMINATED NODE   READINESS GATES
myapp-pod   1/1     Running   0          12m   192.168.221.16   node2.example.com   <none>           <none>

Infra container(pause) 란?

  • pod의 환경(IP, hostname 등)을 생성 및 관리하는 container
  • pod 생성 및 삭제 시 생성/삭제

Static pod란?

  • kubelet daemon에 의해서 동작되는 pod
  • API server 없이 특정 node에 있는 kubelet daemon에 의해 직접 관리
  • /etc/kubernetes/manifests/ 디렉토리에 있는 k8s yaml 파일을 저장 시 적용
  • staticPodPath 수정 시 반드시 systemctl restart kubelet 을 통해 kubelet 재시작
  • /var/lib/kubelet/config.yaml를 통해 staticPodPath 확인 가능
cat /var/lib/kubelet/config.yaml
apiVersion: kubelet.config.k8s.io/v1beta1
authentication:
  anonymous:
    enabled: false
  webhook:
    cacheTTL: 0s
    enabled: true
  x509:
    clientCAFile: /etc/kubernetes/pki/ca.crt
authorization:
  mode: Webhook
  webhook:
    cacheAuthorizedTTL: 0s
    cacheUnauthorizedTTL: 0s
cgroupDriver: systemd
clusterDNS:
- 10.96.0.10
clusterDomain: cluster.local
cpuManagerReconcilePeriod: 0s
evictionPressureTransitionPeriod: 0s
fileCheckFrequency: 0s
healthzBindAddress: 127.0.0.1
healthzPort: 10248
httpCheckFrequency: 0s
imageMinimumGCAge: 0s
kind: KubeletConfiguration
logging:
  flushFrequency: 0
  options:
    json:
      infoBufferSize: "0"
  verbosity: 0
memorySwap: {}
nodeStatusReportFrequency: 0s
nodeStatusUpdateFrequency: 0s
resolvConf: /run/systemd/resolve/resolv.conf
rotateCertificates: true
runtimeRequestTimeout: 0s
shutdownGracePeriod: 0s
shutdownGracePeriodCriticalPods: 0s
staticPodPath: /etc/kubernetes/manifests      #확인 가능
streamingConnectionIdleTimeout: 0s
syncFrequency: 0s
volumeStatsAggPeriod: 0s
  • control-plane의 /etc/kubernetes/manifests/ 디렉토리 확인
    • control-plane에 기존 것 외에 새로운 yaml 파일을 추가하면 worker node들 중 하나에 배치
ls /etc/kubernetes/manifests/
etcd.yaml  kube-apiserver.yaml  kube-controller-manager.yaml  kube-scheduler.yaml

 

참고 :

[따배쿠] 5-3, 4. Kubernetes Pod - init container & infra container

[따배쿠] 5-5 static Pod(feat. kubelet daemon)

댓글