본문 바로가기
Kubernetes/활용

yaml 파일을 이용한 쿠버네티스 오브젝트 관리

by lumination 2024. 2. 8.

 

파드를 연결하는 방식을 정의하는 서비스(service)

개별 어플리케이션의 환경변수 설정을 정의하는 컨피그맵(configMap)

자원을 많이 사용해서 동일한 노드를 사용하는 다른 파드에 영향을 끼치지 않도록 하는 리소스 리미트(limits)/리퀘스트(requests)

 

위와 같은 오브젝트들은 대부분 코드로 구현한다.

 

 

1. 쿠버네티스 리소스를 가독성이 뛰어난 yaml 파일 형태로 export하는 kube-neat 플러그인을 설치합니다.

k run busybox --image=busybox

pod/busybox created

 

k get po

NAME      READY   STATUS      RESTARTS     AGE
busybox   0/1     Completed   1 (4s ago)   6s

 

k get pod busybox -o yaml

apiVersion: v1
kind: Pod
......
      - configMap:
          items:
          - key: ca.crt
            path: ca.crt
          name: kube-root-ca.crt
      - downwardAPI:
          items:
          - fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
            path: namespace
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2024-02-08T05:46:25Z"
    status: "True"
    type: Initialized
  .....
  hostIP: 192.168.0.62
  phase: Running
  podIP: 10.10.180.226
  podIPs:
  - ip: 10.10.180.226
  qosClass: BestEffort
  startTime: "2024-02-08T05:46:25Z"

 

-o yaml은 status까지 붙어서 보기 힘들다.

 

#kube-neat 플러그인 설치

kubectl krew install neat

 

#neat 추출

k get pod busybox -o yaml|k neat

apiVersion: v1
kind: Pod
metadata:
  annotations:
    cni.projectcalico.org/containerID: de9c8cb31863b0717ed859de586fd5c876bbfeb5c0d3b23070b289c11b1edc37
    cni.projectcalico.org/podIP: 10.10.180.226/32
    cni.projectcalico.org/podIPs: 10.10.180.226/32
  labels:
    run: busybox
  name: busybox
  namespace: default
spec:
  containers:
  - image: busybox
    name: busybox
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-x2wzs
      readOnly: true
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  serviceAccountName: default
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: kube-api-access-x2wzs
    projected:
      sources:
      - serviceAccountToken:
          expirationSeconds: 3607
          path: token
      - configMap:
          items:
          - key: ca.crt
            path: ca.crt
          name: kube-root-ca.crt
      - downwardAPI:
          items:
          - fieldRef:
              fieldPath: metadata.namespace
            path: namespace

 

2. 명령어를 사용해 busybox 파드를 실행합니다. busybox 파드를 yaml 파일 형태로 출력하고, Yaml 파일을 vs code등의 편집기를 이용해 command 옵션과 resource limits/requests 옵션을 추가합니다. 수정된 파일로 busybox 파드를 재배포합니다.

 

spec 하위에 입력

command:

- "/bin/sh"

- "-c"

- "sleep inf"

apiVersion: v1
kind: Pod
metadata:
  annotations:
    cni.projectcalico.org/containerID: de9c8cb31863b0717ed859de586fd5c876bbfeb5c0d3b23070b289c11b1edc37
    cni.projectcalico.org/podIP: 10.10.180.226/32
    cni.projectcalico.org/podIPs: 10.10.180.226/32
  labels:
    run: busybox
  name: busybox
  namespace: default
spec:
  containers:
  - image: busybox
    name: busybox
    command:
    - "/bin/sh"
    - "-c"
    - "sleep inf"
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-x2wzs
      readOnly: true
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  serviceAccountName: default
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: kube-api-access-x2wzs
    projected:
      sources:
      - serviceAccountToken:
          expirationSeconds: 3607
          path: token
      - configMap:
          items:
          - key: ca.crt
            path: ca.crt
          name: kube-root-ca.crt
      - downwardAPI:
          items:
          - fieldRef:
              fieldPath: metadata.namespace
            path: namespace

 

해당 파일을 저장 kubectl get pod busybox -o yaml | kubectl neat > busybox_pod.yaml

kubectl apply -f busybox_pod.yaml로 실행한다.

 kubectl get pod -o wide
NAME      READY   STATUS    RESTARTS   AGE     IP              NODE           NOMINATED NODE   READINESS GATES
busybox   1/1     Running   0          4m44s   10.10.180.227   worker-node1   <none>           <none>

busybox가 sleep inf 무한을 걸어놔서 꺼지지 않는다.

 

 

3. 쿠버네티스 환경에서 필요한 yaml 파일의 기본 문법을 정리합니다.

spec:
  containers:
  - image: busybox
    name: busybox
    resources:
      limits:
        memory: 128Mi
      requests:
        memory: 64Mi
    command:
    - "/bin/sh"
    - "-c"
    - "sleep inf"