관리 메뉴

피터의 개발이야기

[kubernetes] Pod 기본 명령어 본문

Kubernetes/기초공부

[kubernetes] Pod 기본 명령어

기록하는 백앤드개발자 2024. 1. 15. 12:09
반응형

[kubernetes] 쿠버네티스 목차

ㅁ 들어가며

 kubectl 명령어 중 Core에 해당하는 Pod에 관한 명령어 정리

 

ㅁ Pod 관련 명령어

# nginx pod 생성
kubectl run nginx --image=nginx

# pod 조회
kubectl get po
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          28s

 

 

Pod의 Node 위치는?

$ kubectl get po -o wide
NAME            READY   STATUS    RESTARTS   AGE     IP           NODE           NOMINATED NODE   READINESS GATES
nginx           1/1     Running   0          7m37s   10.42.0.9    controlplane   <none>           <none>
newpods-ld4lk   1/1     Running   0          7m3s    10.42.0.11   controlplane   <none>           <none>
newpods-q7cs7   1/1     Running   0          7m3s    10.42.0.10   controlplane   <none>           <none>
newpods-gstr5   1/1     Running   0          7m3s    10.42.0.12   controlplane   <none>           <none>

 

Pod의 상세정보 확인

$ kubectl describe pod webapp 
Name:             webapp
Namespace:        default
Priority:         0
Service Account:  default
Node:             controlplane/192.4.205.6
Start Time:       Mon, 15 Jan 2024 02:39:51 +0000
Labels:           <none>
Annotations:      <none>
Status:           Pending
IP:               10.42.0.13
IPs:
  IP:  10.42.0.13
Containers:
  nginx:
    Container ID:   containerd://e23c37da0d1b98cb4f6e3668013103f3717b52ed609ed2c76b97595e5fc2d940
    Image:          nginx
    Image ID:       docker.io/library/nginx@sha256:4c0fdaa8b6341bfdeca5f18f7837462c80cff90527ee35ef185571e1c327beac
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 15 Jan 2024 02:39:52 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-jvpqq (ro)
  agentx:
    Container ID:   
    Image:          agentx
    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-jvpqq (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  kube-api-access-jvpqq:
    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  90s                default-scheduler  Successfully assigned default/webapp to controlplane
  Normal   Pulling    89s                kubelet            Pulling image "nginx"
  Normal   Pulled     89s                kubelet            Successfully pulled image "nginx" in 173.763446ms (173.790028ms including waiting)
  Normal   Created    89s                kubelet            Created container nginx
  Normal   Started    89s                kubelet            Started container nginx
  Normal   Pulling    45s (x3 over 89s)  kubelet            Pulling image "agentx"
  Warning  Failed     44s (x3 over 88s)  kubelet            Failed to pull image "agentx": rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/library/agentx:latest": failed to resolve reference "docker.io/library/agentx:latest": pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
  Warning  Failed     44s (x3 over 88s)  kubelet            Error: ErrImagePull
  Normal   BackOff    9s (x6 over 88s)   kubelet            Back-off pulling image "agentx"
  Warning  Failed     9s (x6 over 88s)   kubelet            Error: ImagePullBackOff

 

Pod의 Container 정보만 출력

$ k get po webapp -o json | jq .spec.containers
[
  {
    "image": "nginx",
    "imagePullPolicy": "Always",
    "name": "nginx",
    "resources": {},
    "terminationMessagePath": "/dev/termination-log",
    "terminationMessagePolicy": "File",
    "volumeMounts": [
      {
        "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
        "name": "kube-api-access-jvpqq",
        "readOnly": true
      }
    ]
  },
  {
    "image": "agentx",
    "imagePullPolicy": "Always",
    "name": "agentx",
    "resources": {},
    "terminationMessagePath": "/dev/termination-log",
    "terminationMessagePolicy": "File",
    "volumeMounts": [
      {
        "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
        "name": "kube-api-access-jvpqq",
        "readOnly": true
      }
    ]
  }
]

 

Pod의 상태 확인 및 원인분석

$ k describe po webapp 
Name:             webapp
Namespace:        default
Priority:         0
Service Account:  default
Node:             controlplane/192.4.205.6
Start Time:       Mon, 15 Jan 2024 02:39:51 +0000
Labels:           <none>
Annotations:      <none>
Status:           Pending
IP:               10.42.0.13

~~~ 생략 ~~~

agentx:
    Container ID:   
    Image:          agentx
    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-jvpqq (ro)

~~~ 생략 ~~~

Events:
  Type     Reason     Age                     From               Message
  ----     ------     ----                    ----               -------
~~~ 생략 ~~~
  Normal   Pulling    7m13s (x3 over 7m57s)   kubelet            Pulling image "agentx"
  Warning  Failed     7m12s (x3 over 7m56s)   kubelet            Failed to pull image "agentx": rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/library/agentx:latest": failed to resolve reference "docker.io/library/agentx:latest": pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
  Warning  Failed     7m12s (x3 over 7m56s)   kubelet            Error: ErrImagePull
  Warning  Failed     6m37s (x6 over 7m56s)   kubelet            Error: ImagePullBackOff
  Normal   BackOff    2m50s (x22 over 7m56s)  kubelet            Back-off pulling image "agentx"

 ㄴ 컨테이너가 image를 가져오지 못해 waiting 상태이고 Events에도 이미지 관련 Warning이 확인됨.

 

 ㄴ pod 목록 조회 시 컨테이너 하나가 실패하여 2개중 1개만 Ready 상태임을 알 수 있다.

 

Pod 삭제

# Pod 삭제
$ kubectl delete pod webapp 
pod "webapp" deleted

 

샘플 Pod 생성 yaml 만들기

ㄴ 수정을 위해image를 잘못되게 만듬.

# dry run으로 샘플 yaml 가져오기
$ kubectl run redis --image=redis123 --dry-run=client -o yaml > pod.yaml

# pod.yaml 확인
$ cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: redis
  name: redis
spec:
  containers:
  - image: redis123
    name: redis
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

# yaml파일로 pod 생성
$ k apply -f pod.yaml 
pod/redis created

# 생성확인
$ k get po redis
NAME    READY   STATUS         RESTARTS   AGE
redis   0/1     ErrImagePull   0          64s

 

Pod의 image 수정 방법

# pod edit
$ k edit po redis
~~~ 생략 ~~~
spec:
  containers:
  - image: redis    <=== 수정
    imagePullPolicy: Always
    name: redis
    ~~~ 생략 ~~~
    
# 수정확인
$ k get po redis
NAME    READY   STATUS    RESTARTS   AGE
redis   1/1     Running   0          2m57s
반응형
Comments