CKA 30제 03: Static Pod 생성

주영·2023년 10월 17일
0

CKA 30제

목록 보기
3/7
post-thumbnail

본 게시물은 [따배씨] 03. Static Pod 생성하기 영상을 참고하여 작성한 글입니다.

kubernetes 공식 문서 : [Kubernetes docs] Create static Pods

이론

< Pod 동작 방식 >

  1. master에게 kubectl 명령어를 통해 nginx pod 1개 생성 요청
  2. 요청이 master의 API로 전달됨
  3. API는 요청을 받으면 etcd에 저장되어 있는 클러스터의 상태 정보를 꺼내서 scheduler에게 보내고, 적합한 노드 정보를 요청 (어느 노드에 pod를 띄우는 것이 적합한지)
  4. scheduler는 시스템과 클러스터 상태를 확인하고 가장 적합한 노드(ex. node1)를 선택해서 API에게 전달
  5. API는 node1에서 실행되고 있는 kubelet에게 nginx pod 실행을 요청
  6. node1의 kubelet은 컨테이너 엔진에게 pod(컨테이너) 실행 요청 (각 노드에는 컨테이너 엔진이 있음)
  7. 컨테이너 엔진은 registry라고 하는 허브에서 nginx 이미지를 다운로드 받아서 실행하고, 실행된 상태 정보를 kubelet에게 전달
  8. kubelet은 그 정보를 API에게 전달
  9. API는 현재 node1에서 nginx pod가 실행됐다는 것을 etcd에 기록

< Static Pod 동작 방식 >

  • API에게 kubectl 명령으로 요청하는 방식이 아니고, API의 도움을 받지 않고 동작한다.
  • 각 노드의 kubelet에게 요청하고 kubelet에 의해 실행된다.
  • static pod 위치 정보
    • /var/lib/kubelet/config.yaml
      • kubelet 데몬이 동작될 때 kubelet의 configuration 파일
      • 해당 파일을 기반으로 kubelet 데몬이 동작함
      • kubelet이 어떻게 동작해야 할지 구성 정보가 담겨져있음
        [root@master ~]# 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/ssl/ca.crt
        authorization:
          mode: Webhook
          webhook:
            cacheAuthorizedTTL: 0s
            cacheUnauthorizedTTL: 0s
        cgroupDriver: systemd
        clusterDNS: 
          - 10.96.0.3
        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
        rotateCertificates: true
        runtimeRequestTimeout: 0s
        shutdownGracePeriod: 0s
        shutdownGracePeriodCriticalPods: 0s
        staticPodPath: /etc/kubernetes/manifests   // static pod를 배치시킬 위치 정보. 기본값
        streamingConnectionIdleTimeout: 0s
        syncFrequency: 0s
        volumeStatsAggPeriod: 0s
      • static pod 위치 기본값: /etc/kubernetes/manifests
  • static pod 생성
    • kubelet이 사용하는 config 파일에 정의되어 있는 static pod 위치에 yaml 파일을 저장하면 kubelet은 해당 yaml 파일을 기준으로 pod를 실행한다.
    • static pod 생성은 root 권한이 필요하다.
    • ex) node1에 static pod를 만들고 싶으면 node1의 /etc/kubernetes/manifests 에 pod.yaml 파일을 만든다.
  • static pod 삭제
    • kubelet이 사용하는 config 파일에 정의되어 있는 static pod 위치에 있는 yaml 파일을 삭제하면 kubelet이 pod를 삭제한다.

< master components >

  • master component들도 static pod로 동작
    • etcd, kube-apiserver, kube-controller-manager, kube-scheduler
  • static pod로 만들어져 있기 때문에 master의 kubelet 데몬이 start될 때 실행해주게 됨
// static pod로 떠있는 master component들
[root@master ~]# k get po -n kube-system  -owide
NAME                                       READY   STATUS    RESTARTS   AGE   IP               NODE     NOMINATED NODE   READINESS GATES
etcd-master                                1/1     Running   0          15d   xxx.xxx.xxx.xx   master   <none>           <none>
kube-apiserver-master                      1/1     Running   0          15d   xxx.xxx.xxx.xx   master   <none>           <none>
kube-controller-manager-master             1/1     Running   0          15d   xxx.xxx.xxx.xx   master   <none>           <none>
kube-scheduler-master                      1/1     Running   0          15d   xxx.xxx.xxx.xx   master   <none>           <none>

[root@master1 manifests]# pwd
/etc/kubernetes/manifests

[root@master1 manifests]# ls
etcd.yaml  kube-apiserver.yaml  kube-controller-manager.yaml  kube-scheduler.yaml

문제

Configure kubelet hosting to start a pod on the node

  • TASK:
    • Node: hk8s-w1
    • pod Name: web
    • image: nginx

풀이

1. pod 템플릿 생성

$ kubectl run web --image=nginx --dry-run=client -o yaml

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: web
  name: web
spec:
  containers:
  - image: nginx
    name: web
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

2. node 접속

$ ssh hk8s-w1

3. root 권한 (static pod 생성은 root 권한이 필요함)

$ sudo -i

4. static pod 위치 정보 확인 - kubelet 데몬이 사용하는 config 파일 확인

# 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/ssl/ca.crt
authorization:
  mode: Webhook
  webhook:
    cacheAuthorizedTTL: 0s
    cacheUnauthorizedTTL: 0s
cgroupDriver: systemd
clusterDNS:
- 10.96.0.3
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
rotateCertificates: true
runtimeRequestTimeout: 0s
shutdownGracePeriod: 0s
shutdownGracePeriodCriticalPods: 0s
staticPodPath: /etc/kubernetes/manifests   // static Pod 경로 확인
streamingConnectionIdleTimeout: 0s
syncFrequency: 0s
volumeStatsAggPeriod: 0s

5. static pod 생성

// 해당 위치에 yaml 파일을 생성하면 바로 static pod가 생성됨
# cd /etc/kubernetes/manifests

# cat > web-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: web
spec:
  containers:
  - image: nginx
    name: web
(Ctrl+d)

6. static pod 실행중인지 확인

// root 권한
# exit

// hk8s-w1 서버
$ exit

$ kubectl get pods
NAME          READY   STATUS    RESTARTS   AGE
web-hk8s-w1   1/1     Running   0          50s

0개의 댓글