wrong-nginx-version.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-19
spec:
  containers:
  - name: nginx-pod
    image: nginx:1.19.19

 

kubectl apply -f wrong-nginx-version.yaml

더보기

Name:             nginx-19
Namespace:        default
Priority:         0
Service Account:  default
Node:             worker-node1/192.168.0.62
Start Time:       Thu, 15 Feb 2024 14:50:54 +0900
Labels:           <none>
Annotations:      cni.projectcalico.org/containerID: 25ff3ed7ed37842f4b43d7eb0835a1857d800bcb886a7c0cdc93a65112ffe7f7
                  cni.projectcalico.org/podIP: 10.10.180.228/32
                  cni.projectcalico.org/podIPs: 10.10.180.228/32
Status:           Pending
IP:               10.10.180.228
IPs:
  IP:  10.10.180.228
Containers:
  nginx-pod:
    Container ID:
    Image:          nginx:1.19.19
    Image ID:
    Port:           <none>
    Host Port:      <none>
    State:          Waiting
      Reason:       ImagePullBackOff
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-fhwrl (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  kube-api-access-fhwrl:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  55s                default-scheduler  Successfully assigned default/nginx-19 to worker-node1
  Normal   BackOff    25s (x2 over 53s)  kubelet            Back-off pulling image "nginx:1.19.19"
  Warning  Failed     25s (x2 over 53s)  kubelet            Error: ImagePullBackOff
  Normal   Pulling    11s (x3 over 55s)  kubelet            Pulling image "nginx:1.19.19"
  Warning  Failed     10s (x3 over 53s)  kubelet            Failed to pull image "nginx:1.19.19": rpc error: code = NotFound desc = failed to pull and unpack image "docker.io/library/nginx:1.19.19": failed to resolve reference "docker.io/library/nginx:1.19.19": docker.io/library/nginx:1.19.19: not found
  Warning  Failed     10s (x3 over 53s)  kubelet            Error: ErrImagePull


kubectl describe nginx-19 상태 조회

kubectl logs nginx-19 로그 보기

kubectl logs -f nginx-19 로그 tail

 

쿠버네티스 디버깅 프로세스 작업 순서는 다음과 같다.

 

apply -> get -> describe -> logs -> get event

 

1. yaml 파일을 이용해 오브젝트를 생성(apply)하고, 생성한 오브젝트 리스트는 get 명령어로 확인한다.

2.만약 파드가 정상적으로 생성되지 않으면 상세한 설정 정보를 describe 명령어로 확인한다.

3. 이후 어플리케이션 관련 에러는 로그 명령어(logs)로 확인하고 쿠버네티스 클러스터 관련 메시지는 이벤트 명령어(get event)로 확인한다.

 

 

파드를 연결하는 방식을 정의하는 서비스(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"

 

 

+ Recent posts