본문 바로가기
Compute/kubernetis

[CKA] 02. kubectl get 명령어 사용 문제 | kubectl get pods -o name | kubectl get pods -o yaml | jsonpath

by 조청유곽 2025. 1. 11.
반응형

01. 두 개의 nginx 이미지를 사용하는 pod를 생성하고, 각 pod에 label을 추가한다. 

pod 생성을 완료한 후 레이블을 확인한다. 

 

[Solve]

[root@k8s-master ~]# vi 2-test.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx1
  labels:
    env: prod
spec:
  containers:
  - name: nginx1
    image: nginx:1.14.2
    ports:
    - containerPort: 80

---

apiVersion: v1
kind: Pod
metadata:
  name: nginx2
  labels:
    env: dev
spec:
  containers:
  - name: nginx2
    image: nginx:1.14.2
    ports:
    - containerPort: 80

[root@k8s-master ~]# kubectl apply -f 2-test.yaml 
pod/nginx1 created
pod/nginx2 created
[root@k8s-master ~]# kubectl get pods -l env=pod
No resources found in default namespace.
[root@k8s-master ~]# kubectl get pods
NAME       READY   STATUS             RESTARTS      AGE
frontend   0/1     CrashLoopBackOff   7 (46s ago)   11m
nginx1     1/1     Running            0             39s
nginx2     1/1     Running            0             39s
[root@k8s-master ~]# kubectl get pods --show-labels 
NAME       READY   STATUS             RESTARTS      AGE   LABELS
frontend   0/1     CrashLoopBackOff   7 (83s ago)   12m   <none>
nginx1     1/1     Running            0             76s   env=prod
nginx2     1/1     Running            0             76s   env=dev
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get pods -l env=prod
NAME     READY   STATUS    RESTARTS   AGE
nginx1   1/1     Running   0          2m56s
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get pods -l env=dev
NAME     READY   STATUS    RESTARTS   AGE
nginx2   1/1     Running   0          3m22s
[root@k8s-master ~]#

 


02. nginx pod를 생성하고, 다양한 세부 옵션(verbosity)을 이용하여 pod를 조회

 

[solve]

[root@k8s-master ~]# vi 3-test.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14.2
    ports:
    - containerPort: 80

[root@k8s-master ~]# kubectl apply -f 3-test.yaml 
pod/nginx created
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get pods nginx --v=7
I1226 23:30:00.148153   56297 loader.go:373] Config loaded from file:  /root/.kube/config
I1226 23:30:00.156782   56297 round_trippers.go:463] GET https://192.168.56.30:6443/api/v1/namespaces/default/pods/nginx
I1226 23:30:00.156800   56297 round_trippers.go:469] Request Headers:
I1226 23:30:00.156808   56297 round_trippers.go:473]     Accept: application/json;as=Table;v=v1;g=meta.k8s.io,application/json;as=Table;v=v1beta1;g=meta.k8s.io,application/json
I1226 23:30:00.156815   56297 round_trippers.go:473]     User-Agent: kubectl/v1.27.2 (linux/amd64) kubernetes/7f6f68f
I1226 23:30:00.165943   56297 round_trippers.go:574] Response Status: 200 OK in 9 milliseconds
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          112s
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get pods nginx --v=8
I1226 23:30:08.456517   56382 loader.go:373] Config loaded from file:  /root/.kube/config
I1226 23:30:08.463324   56382 round_trippers.go:463] GET https://192.168.56.30:6443/api/v1/namespaces/default/pods/nginx
I1226 23:30:08.463340   56382 round_trippers.go:469] Request Headers:
I1226 23:30:08.463347   56382 round_trippers.go:473]     Accept: application/json;as=Table;v=v1;g=meta.k8s.io,application/json;as=Table;v=v1beta1;g=meta.k8s.io,application/json
I1226 23:30:08.463354   56382 round_trippers.go:473]     User-Agent: kubectl/v1.27.2 (linux/amd64) kubernetes/7f6f68f
I1226 23:30:08.473071   56382 round_trippers.go:574] Response Status: 200 OK in 9 milliseconds
I1226 23:30:08.473093   56382 round_trippers.go:577] Response Headers:
I1226 23:30:08.473101   56382 round_trippers.go:580]     Date: Thu, 26 Dec 2024 14:30:08 GMT
I1226 23:30:08.473110   56382 round_trippers.go:580]     Audit-Id: 0ee7c8c1-451e-4a40-89b3-81eb2ed88bf8
I1226 23:30:08.473115   56382 round_trippers.go:580]     Cache-Control: no-cache, private
I1226 23:30:08.473125   56382 round_trippers.go:580]     Content-Type: application/json
I1226 23:30:08.473164   56382 round_trippers.go:580]     X-Kubernetes-Pf-Flowschema-Uid: c2c841d7-6631-4244-8c69-ce28f913005d
I1226 23:30:08.473173   56382 round_trippers.go:580]     X-Kubernetes-Pf-Prioritylevel-Uid: 5a9746b8-4334-46a8-b4a6-e5acabecd257
I1226 23:30:08.473319   56382 request.go:1188] Response Body: {"kind":"Table","apiVersion":"meta.k8s.io/v1","metadata":{"resourceVersion":"6798"},"columnDefinitions":[{"name":"Name","type":"string","format":"name","description":"Name must be unique within a namespace. Is required when creating resources, although some resources may allow a client to request the generation of an appropriate name automatically. Name is primarily intended for creation idempotence and configuration definition. Cannot be updated. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#names","priority":0},{"name":"Ready","type":"string","format":"","description":"The aggregate readiness state of this pod for accepting traffic.","priority":0},{"name":"Status","type":"string","format":"","description":"The aggregate status of the containers in this pod.","priority":0},{"name":"Restarts","type":"string","format":"","description":"The number of times the containers in this pod have been restarted and when the last container in this pod has restarted.","priority":0},{"n [truncated 4445 chars]
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          2m
[root@k8s-master ~]# 
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get pods nginx --v=9
I1226 23:30:12.242609   56389 loader.go:373] Config loaded from file:  /root/.kube/config
I1226 23:30:12.251061   56389 round_trippers.go:466] curl -v -XGET  -H "User-Agent: kubectl/v1.27.2 (linux/amd64) kubernetes/7f6f68f" -H "Accept: application/json;as=Table;v=v1;g=meta.k8s.io,application/json;as=Table;v=v1beta1;g=meta.k8s.io,application/json" 'https://192.168.56.30:6443/api/v1/namespaces/default/pods/nginx'
I1226 23:30:12.251775   56389 round_trippers.go:510] HTTP Trace: Dial to tcp:192.168.56.30:6443 succeed
I1226 23:30:12.260707   56389 round_trippers.go:553] GET https://192.168.56.30:6443/api/v1/namespaces/default/pods/nginx 200 OK in 9 milliseconds
I1226 23:30:12.260748   56389 round_trippers.go:570] HTTP Statistics: DNSLookup 0 ms Dial 0 ms TLSHandshake 5 ms ServerProcessing 2 ms Duration 9 ms
I1226 23:30:12.260757   56389 round_trippers.go:577] Response Headers:
I1226 23:30:12.260766   56389 round_trippers.go:580]     Cache-Control: no-cache, private
I1226 23:30:12.260776   56389 round_trippers.go:580]     Content-Type: application/json
I1226 23:30:12.260781   56389 round_trippers.go:580]     X-Kubernetes-Pf-Flowschema-Uid: c2c841d7-6631-4244-8c69-ce28f913005d
I1226 23:30:12.260792   56389 round_trippers.go:580]     X-Kubernetes-Pf-Prioritylevel-Uid: 5a9746b8-4334-46a8-b4a6-e5acabecd257
I1226 23:30:12.260816   56389 round_trippers.go:580]     Date: Thu, 26 Dec 2024 14:30:12 GMT
I1226 23:30:12.260821   56389 round_trippers.go:580]     Audit-Id: b5c3177a-6400-4b40-9969-0150951b0829
I1226 23:30:12.260999   56389 request.go:1188] Response Body: {"kind":"Table","apiVersion":"meta.k8s.io/v1","metadata":{"resourceVersion":"6798"},"columnDefinitions":[{"name":"Name","type":"string","format":"name","description":"Name must be unique within a namespace. Is required when creating resources, although some resources may allow a client to request the generation of an appropriate name automatically. Name is primarily intended for creation idempotence and configuration definition. Cannot be updated. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names#names","priority":0},{"name":"Ready","type":"string","format":"","description":"The aggregate readiness state of this pod for accepting traffic.","priority":0},{"name":"Status","type":"string","format":"","description":"The aggregate status of the containers in this pod.","priority":0},{"name":"Restarts","type":"string","format":"","description":"The number of times the containers in this pod have been restarted and when the last container in this pod has restarted.","priority":0},{"name":"Age","type":"string","format":"","description":"CreationTimestamp is a timestamp representing the server time when this object was created. It is not guaranteed to be set in happens-before order across separate operations. Clients may not set this value. It is represented in RFC3339 form and is in UTC.\n\nPopulated by the system. Read-only. Null for lists. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata","priority":0},{"name":"IP","type":"string","format":"","description":"IP address allocated to the pod. Routable at least within the cluster. Empty if not yet allocated.","priority":1},{"name":"Node","type":"string","format":"","description":"NodeName is a request to schedule this pod onto a specific node. If it is non-empty, the scheduler simply schedules this pod onto that node, assuming that it fits resource requirements.","priority":1},{"name":"Nominated Node","type":"string","format":"","description":"nominatedNodeName is set only when this pod preempts other pods on the node, but it cannot be scheduled right away as preemption victims receive their graceful termination periods. This field does not guarantee that the pod will be scheduled on this node. Scheduler may decide to place the pod elsewhere if other nodes become available sooner. Scheduler may also decide to give the resources on this node to a higher priority pod that is created after preemption. As a result, this field may be different than PodSpec.nodeName when the pod is scheduled.","priority":1},{"name":"Readiness Gates","type":"string","format":"","description":"If specified, all readiness gates will be evaluated for pod readiness. A pod is ready when all its containers are ready AND all conditions specified in the readiness gates have status equal to \"True\" More info: https://git.k8s.io/enhancements/keps/sig-network/580-pod-readiness-gates","priority":1}],"rows":[{"cells":["nginx","1/1","Running","0","2m4s","20.96.36.69","k8s-node1","\u003cnone\u003e","\u003cnone\u003e"],"object":{"kind":"PartialObjectMetadata","apiVersion":"meta.k8s.io/v1","metadata":{"name":"nginx","namespace":"default","uid":"127c6479-803e-43c5-9680-319707a6cf8b","resourceVersion":"6798","creationTimestamp":"2024-12-26T14:28:08Z","annotations":{"cni.projectcalico.org/containerID":"c135cf60414be79d4003377f06a871cc04303025d3e0604b1f30395061142c34","cni.projectcalico.org/podIP":"20.96.36.69/32","cni.projectcalico.org/podIPs":"20.96.36.69/32","kubectl.kubernetes.io/last-applied-configuration":"{\"apiVersion\":\"v1\",\"kind\":\"Pod\",\"metadata\":{\"annotations\":{},\"name\":\"nginx\",\"namespace\":\"default\"},\"spec\":{\"containers\":[{\"image\":\"nginx:1.14.2\",\"name\":\"nginx\",\"ports\":[{\"containerPort\":80}]}]}}\n"},"managedFields":[{"manager":"calico","operation":"Update","apiVersion":"v1","time":"2024-12-26T14:28:08Z","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:annotations":{"f:cni.projectcalico.org/containerID":{},"f:cni.projectcalico.org/podIP":{},"f:cni.projectcalico.org/podIPs":{}}}},"subresource":"status"},{"manager":"kubectl-client-side-apply","operation":"Update","apiVersion":"v1","time":"2024-12-26T14:28:08Z","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:annotations":{".":{},"f:kubectl.kubernetes.io/last-applied-configuration":{}}},"f:spec":{"f:containers":{"k:{\"name\":\"nginx\"}":{".":{},"f:image":{},"f:imagePullPolicy":{},"f:name":{},"f:ports":{".":{},"k:{\"containerPort\":80,\"protocol\":\"TCP\"}":{".":{},"f:containerPort":{},"f:protocol":{}}},"f:resources":{},"f:terminationMessagePath":{},"f:terminationMessagePolicy":{}}},"f:dnsPolicy":{},"f:enableServiceLinks":{},"f:restartPolicy":{},"f:schedulerName":{},"f:securityContext":{},"f:terminationGracePeriodSeconds":{}}}},{"manager":"kubelet","operation":"Update","apiVersion":"v1","time":"2024-12-26T14:28:17Z","fieldsType":"FieldsV1","fieldsV1":{"f:status":{"f:conditions":{"k:{\"type\":\"ContainersReady\"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}},"k:{\"type\":\"Initialized\"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}},"k:{\"type\":\"Ready\"}":{".":{},"f:lastProbeTime":{},"f:lastTransitionTime":{},"f:status":{},"f:type":{}}},"f:containerStatuses":{},"f:hostIP":{},"f:phase":{},"f:podIP":{},"f:podIPs":{".":{},"k:{\"ip\":\"20.96.36.69\"}":{".":{},"f:ip":{}}},"f:startTime":{}}},"subresource":"status"}]}}}]}
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          2m4s
[root@k8s-master ~]#

 

 

verbosity란? 

https://kubernetes.io/docs/reference/kubectl/quick-reference/


03.  특정 namespace의 pod를 찾아서 한 줄에 한개씩 이름만 출력하고 이것을 파일로 저장한다. 

[precondition]

: 특정 namespace와 해당 namespace에 속한 pod를 먼저 생성한다. 

[root@k8s-master ~]# kubectl create namespace development
namespace/development created
[root@k8s-master ~]# vi 11-test.yaml
[root@k8s-master ~]# kubectl apply -f 11-test.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: icandor 
  namespace: development
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
deployment.apps/icandor created
[root@k8s-master ~]# kubectl get pods -n development
NAME                      READY   STATUS    RESTARTS   AGE
icandor-cbdccf466-5mtg7   1/1     Running   0          50s
icandor-cbdccf466-m5wqf   1/1     Running   0          50s
icandor-cbdccf466-swdwb   1/1     Running   0          50s

 

[solve]

: 파일을 생성할 위치는 미리 만들어둔다. 

[root@k8s-master ~]# kubectl get pods -n development -o name
pod/icandor-cbdccf466-5mtg7
pod/icandor-cbdccf466-m5wqf
pod/icandor-cbdccf466-swdwb
[root@k8s-master ~]# mkdir /opt/KUCC00302
[root@k8s-master ~]# kubectl get pods -n development -o name > /opt/KUCC00302/kucc00302.txt
[root@k8s-master ~]# cat /opt/KUCC00302/kucc00302.txt 
pod/icandor-cbdccf466-5mtg7
pod/icandor-cbdccf466-m5wqf
pod/icandor-cbdccf466-swdwb
[root@k8s-master ~]#

 


04. persistent volume의 capacity로 sorting한다. 해당 정보를 특정 파일로 저장한다. 

 

[precondition]

capacity가 다른 3개의 persistent volume을 생성

[root@k8s-master ~]# vi 13-test.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003-01
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /tmp
    server: 172.17.0.2

---

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003-02
spec:
  capacity:
    storage: 2Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /tmp
    server: 172.17.0.2

--- 

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003-03
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /tmp
    server: 172.17.0.2


[root@k8s-master ~]# kubectl apply -f 13-test.yaml 
persistentvolume/pv0003-01 created
persistentvolume/pv0003-02 created
persistentvolume/pv0003-03 created
[root@k8s-master ~]#

 

 

[solve]

[root@k8s-master ~]# kubectl get pv --sort-by=.spec.capacity.storage
NAME        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv0003-02   2Gi        RWO            Recycle          Available           slow                    5m44s
pv0003-01   5Gi        RWO            Recycle          Available           slow                    5m44s
pv0003-03   10Gi       RWO            Recycle          Available           slow                    5m44s
[root@k8s-master ~]# mkdir /opt/KUCC00102
[root@k8s-master ~]# kubectl get pv --sort-by=.spec.capacity.storage > /opt/KUCC00102/volume_list
[root@k8s-master ~]# cat /opt/KUCC00102/volume_list 
NAME        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv0003-02   2Gi        RWO            Recycle          Available           slow                    7m45s
pv0003-01   5Gi        RWO            Recycle          Available           slow                    7m45s
pv0003-03   10Gi       RWO            Recycle          Available           slow                    7m45s
[root@k8s-master ~]#

05. describe 명령어를 사용하지 말고 pod의 image 버전을 확인한다.

 

[solve]

[root@k8s-master ~]# kubectl get pods -o=jsonpath='{range .items[*]}{.spec.containers..image}{"\t"}{"\n"}{end}'
gcr.io/google-samples/hello-app:2.0	
busybox:1.28	
alpine:latest	
nginx:1.14.2 redis memcached	
nginx:1.14.2	
nginx:1.14.2	
nginx:1.14.2	
nginx:1.14.2	
nginx:1.14.2	
nginx:1.14.2	
nginx:1.14.2	
nginx:1.14.2	
nginx:1.14.2	
redis	
[root@k8s-master ~]#

06. pod name과 start time을 특정 파일로 저장한다.

 

[solve]

[root@k8s-master ~]# kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
env-pod	2024-12-26T14:48:30Z
foo	2024-12-26T15:55:24Z
frontend	2024-12-26T14:05:48Z
kucc8	2024-12-26T18:16:53Z
nginx	2024-12-26T16:26:23Z
nginx-deployment-cbdccf466-4k8t8	2024-12-26T18:41:38Z
nginx-deployment-cbdccf466-6nqjr	2024-12-26T16:37:50Z
nginx-deployment-cbdccf466-bjtj5	2024-12-26T18:41:38Z
nginx-deployment-cbdccf466-nsp68	2024-12-26T16:43:48Z
nginx-deployment-cbdccf466-wf8gk	2024-12-26T16:37:55Z
nginx-deployment-cbdccf466-xx9hr	2024-12-26T18:41:33Z
nginx1	2024-12-26T14:16:59Z
nginx2	2024-12-26T14:16:59Z
non-persistent-redis	2024-12-26T16:19:09Z
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}' > /opt/pod-status
[root@k8s-master ~]# cat /opt/pod-status 
env-pod	2024-12-26T14:48:30Z
foo	2024-12-26T15:55:24Z
frontend	2024-12-26T14:05:48Z
kucc8	2024-12-26T18:16:53Z
nginx	2024-12-26T16:26:23Z
nginx-deployment-cbdccf466-4k8t8	2024-12-26T18:41:38Z
nginx-deployment-cbdccf466-6nqjr	2024-12-26T16:37:50Z
nginx-deployment-cbdccf466-bjtj5	2024-12-26T18:41:38Z
nginx-deployment-cbdccf466-nsp68	2024-12-26T16:43:48Z
nginx-deployment-cbdccf466-wf8gk	2024-12-26T16:37:55Z
nginx-deployment-cbdccf466-xx9hr	2024-12-26T18:41:33Z
nginx1	2024-12-26T14:16:59Z
nginx2	2024-12-26T14:16:59Z
non-persistent-redis	2024-12-26T16:19:09Z
[root@k8s-master ~]#

07. 특정 pod의 ip address를 확인한다.

 

[solve]

[root@k8s-master ~]# kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}'
env-pod	20.96.169.133
foo	20.96.36.72
frontend	20.96.169.130
kucc8	20.96.36.79
nginx	20.96.36.74
nginx-deployment-cbdccf466-4k8t8	20.96.169.139
nginx-deployment-cbdccf466-6nqjr	20.96.36.75
nginx-deployment-cbdccf466-bjtj5	20.96.169.140
nginx-deployment-cbdccf466-nsp68	20.96.36.76
nginx-deployment-cbdccf466-wf8gk	20.96.169.137
nginx-deployment-cbdccf466-xx9hr	20.96.36.80
nginx1	20.96.169.132
nginx2	20.96.169.131
non-persistent-redis	20.96.36.73
[root@k8s-master ~]# kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.podIP}{"\n"}{end}'| grep kucc8
kucc8	20.96.36.79
[root@k8s-master ~]#

08. pod를 이름순으로 정렬한다. 

 

[solve]

[root@k8s-master ~]# kubectl get pods --sort-by=.metadata.name
NAME                               READY   STATUS             RESTARTS       AGE
env-pod                            1/1     Running            0              5h17m
foo                                1/1     Running            4 (10m ago)    4h11m
frontend                           0/1     CrashLoopBackOff   75 (53s ago)   6h
kucc8                              3/3     Running            0              109m
nginx                              1/1     Running            0              3h40m
nginx1                             1/1     Running            0              5h49m
nginx2                             1/1     Running            0              5h49m
nginx-deployment-cbdccf466-4k8t8   1/1     Running            0              85m
nginx-deployment-cbdccf466-6nqjr   1/1     Running            0              3h28m
nginx-deployment-cbdccf466-bjtj5   1/1     Running            0              85m
nginx-deployment-cbdccf466-nsp68   1/1     Running            0              3h22m
nginx-deployment-cbdccf466-wf8gk   1/1     Running            0              3h28m
nginx-deployment-cbdccf466-xx9hr   1/1     Running            0              85m
non-persistent-redis               1/1     Running            0              3h47m
[root@k8s-master ~]#

09. pod 리스트를 custom columns을 이용하여 출력한다. 

 

[solve]

[root@k8s-master ~]# kubectl get pods -o=custom-columns="POD_NAME:.metadata.name,POD_STATUS:.status.phase"
POD_NAME                           POD_STATUS
env-pod                            Running
foo                                Running
frontend                           Running
kucc8                              Running
nginx                              Running
nginx-deployment-cbdccf466-4k8t8   Running
nginx-deployment-cbdccf466-6nqjr   Running
nginx-deployment-cbdccf466-bjtj5   Running
nginx-deployment-cbdccf466-nsp68   Running
nginx-deployment-cbdccf466-wf8gk   Running
nginx-deployment-cbdccf466-xx9hr   Running
nginx1                             Running
nginx2                             Running
non-persistent-redis               Running
[root@k8s-master ~]#

10. pod 리스트를 생성한 timestamp로 정렬해서 출력한다.

 

[solve]

[root@k8s-master ~]# kubectl get pods --sort-by=.metadata.creationTimestamp
NAME                               READY   STATUS             RESTARTS        AGE
frontend                           0/1     CrashLoopBackOff   83 (109s ago)   6h42m
nginx2                             1/1     Running            0               6h31m
nginx1                             1/1     Running            0               6h31m
env-pod                            1/1     Running            0               6h
foo                                1/1     Running            4 (53m ago)     4h53m
non-persistent-redis               1/1     Running            0               4h29m
nginx                              1/1     Running            0               4h22m
nginx-deployment-cbdccf466-wf8gk   1/1     Running            0               4h11m
nginx-deployment-cbdccf466-6nqjr   1/1     Running            0               4h11m
nginx-deployment-cbdccf466-nsp68   1/1     Running            0               4h5m
kucc8                              3/3     Running            0               151m
nginx-deployment-cbdccf466-bjtj5   1/1     Running            0               127m
nginx-deployment-cbdccf466-xx9hr   1/1     Running            0               127m
nginx-deployment-cbdccf466-4k8t8   1/1     Running            0               127m
[root@k8s-master ~]#

11. 특정 조건의 node의 수를 파일로 저장한다. 

조건 1 : ready 상태의 node 

조건 2 : taint, noschedule 설정이 없을 것 

 

[solve]

[root@k8s-master ~]# kubectl describe nodes | grep -i taints
Taints:             node-role.kubernetes.io/control-plane:NoSchedule
Taints:             <none>
Taints:             <none>
[root@k8s-master ~]# kubectl describe nodes | grep -i taints | grep -i noschedule
Taints:             node-role.kubernetes.io/control-plane:NoSchedule
[root@k8s-master ~]# 
[root@k8s-master ~]# echo 2 > /opt/KUSC00402/kusc0042.txt
[root@k8s-master ~]# cat /opt/KUSC00402/kusc0042.txt
2
[root@k8s-master ~]# kubectl get nodes
NAME         STATUS   ROLES           AGE     VERSION
k8s-master   Ready    control-plane   7h18m   v1.27.2
k8s-node1    Ready    <none>          7h1m    v1.27.2
k8s-node2    Ready    <none>          7h1m    v1.27.2
[root@k8s-master ~]#

12. 모든 namespace의 pod를 출력하고 특정 파일로 작성한다. 

 

[solve]

[root@k8s-master ~]# kubectl get pods -A
NAMESPACE              NAME                                         READY   STATUS             RESTARTS        AGE
calico-apiserver       calico-apiserver-574c754579-2kfx6            1/1     Running            0               32m
calico-apiserver       calico-apiserver-574c754579-t5fdm            1/1     Running            0               32m
calico-system          calico-kube-controllers-bf6cb6475-bhbm9      1/1     Running            0               32m
calico-system          calico-node-6pfjk                            1/1     Running            0               7h22m
calico-system          calico-node-pf4s2                            1/1     Running            0               7h6m
calico-system          calico-node-tzkh8                            1/1     Running            0               7h6m
calico-system          calico-typha-6cb494cb8d-5h2tz                1/1     Running            0               7h6m
calico-system          calico-typha-6cb494cb8d-sjr8l                1/1     Running            0               29m
calico-system          csi-node-driver-4mfcs                        2/2     Running            0               7h6m
calico-system          csi-node-driver-969dd                        2/2     Running            0               7h6m
calico-system          csi-node-driver-gfpv7                        2/2     Running            0               7h22m
default                env-pod                                      1/1     Running            0               6h15m
default                foo                                          1/1     Running            5 (8m10s ago)   5h8m
default                frontend                                     0/1     CrashLoopBackOff   86 (89s ago)    6h57m
default                kucc8                                        3/3     Running            0               167m
default                nginx                                        1/1     Running            0               4h37m
default                nginx-deployment-cbdccf466-4k8t8             1/1     Running            0               142m
default                nginx-deployment-cbdccf466-6nqjr             1/1     Running            0               4h26m
default                nginx-deployment-cbdccf466-bjtj5             1/1     Running            0               142m
default                nginx-deployment-cbdccf466-nsp68             1/1     Running            0               4h20m
default                nginx-deployment-cbdccf466-wf8gk             1/1     Running            0               4h26m
default                nginx-deployment-cbdccf466-xx9hr             1/1     Running            0               142m
default                nginx1                                       1/1     Running            0               6h46m
default                nginx2                                       1/1     Running            0               6h46m
default                non-persistent-redis                         1/1     Running            0               4h44m
development            icandor-cbdccf466-5mtg7                      1/1     Running            0               4h13m
development            icandor-cbdccf466-m5wqf                      1/1     Running            0               4h13m
development            icandor-cbdccf466-swdwb                      1/1     Running            0               4h13m
kube-system            coredns-5d78c9869d-8fpgb                     1/1     Running            0               32m
kube-system            coredns-5d78c9869d-ns9qz                     1/1     Running            0               32m
kube-system            etcd-k8s-master                              1/1     Running            0               7h23m
kube-system            kube-apiserver-k8s-master                    1/1     Running            0               7h23m
kube-system            kube-controller-manager-k8s-master           1/1     Running            0               7h23m
kube-system            kube-proxy-f7z9m                             1/1     Running            0               7h6m
kube-system            kube-proxy-jv7xk                             1/1     Running            0               7h6m
kube-system            kube-proxy-xhtzx                             1/1     Running            0               7h23m
kube-system            kube-scheduler-k8s-master                    1/1     Running            0               7h23m
kube-system            metrics-server-7db4fb59f9-rkw72              1/1     Running            0               5h39m
kubernetes-dashboard   dashboard-metrics-scraper-5cb4f4bb9c-9wnc5   1/1     Running            0               5h39m
kubernetes-dashboard   kubernetes-dashboard-6bc7c98694-7zrqz        1/1     Running            0               5h39m
tigera-operator        tigera-operator-84cf9b6dbb-brtdt             1/1     Running            0               32m
[root@k8s-master ~]# 
[root@k8s-master ~]# 
[root@k8s-master ~]# 
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl get pods -A > /opt/pods-list.yaml
[root@k8s-master ~]# cat /opt/pods-list.yaml 
NAMESPACE              NAME                                         READY   STATUS             RESTARTS         AGE
calico-apiserver       calico-apiserver-574c754579-2kfx6            1/1     Running            0                33m
calico-apiserver       calico-apiserver-574c754579-t5fdm            1/1     Running            0                33m
calico-system          calico-kube-controllers-bf6cb6475-bhbm9      1/1     Running            0                33m
calico-system          calico-node-6pfjk                            1/1     Running            0                7h23m
calico-system          calico-node-pf4s2                            1/1     Running            0                7h7m
calico-system          calico-node-tzkh8                            1/1     Running            0                7h7m
calico-system          calico-typha-6cb494cb8d-5h2tz                1/1     Running            0                7h7m
calico-system          calico-typha-6cb494cb8d-sjr8l                1/1     Running            0                30m
calico-system          csi-node-driver-4mfcs                        2/2     Running            0                7h7m
calico-system          csi-node-driver-969dd                        2/2     Running            0                7h7m
calico-system          csi-node-driver-gfpv7                        2/2     Running            0                7h23m
default                env-pod                                      1/1     Running            0                6h16m
default                foo                                          1/1     Running            5 (9m10s ago)    5h9m
default                frontend                                     0/1     CrashLoopBackOff   86 (2m29s ago)   6h58m
default                kucc8                                        3/3     Running            0                168m
default                nginx                                        1/1     Running            0                4h38m
default                nginx-deployment-cbdccf466-4k8t8             1/1     Running            0                143m
default                nginx-deployment-cbdccf466-6nqjr             1/1     Running            0                4h27m
default                nginx-deployment-cbdccf466-bjtj5             1/1     Running            0                143m
default                nginx-deployment-cbdccf466-nsp68             1/1     Running            0                4h21m
default                nginx-deployment-cbdccf466-wf8gk             1/1     Running            0                4h27m
default                nginx-deployment-cbdccf466-xx9hr             1/1     Running            0                143m
default                nginx1                                       1/1     Running            0                6h47m
default                nginx2                                       1/1     Running            0                6h47m
default                non-persistent-redis                         1/1     Running            0                4h45m
development            icandor-cbdccf466-5mtg7                      1/1     Running            0                4h14m
development            icandor-cbdccf466-m5wqf                      1/1     Running            0                4h14m
development            icandor-cbdccf466-swdwb                      1/1     Running            0                4h14m
kube-system            coredns-5d78c9869d-8fpgb                     1/1     Running            0                33m
kube-system            coredns-5d78c9869d-ns9qz                     1/1     Running            0                33m
kube-system            etcd-k8s-master                              1/1     Running            0                7h24m
kube-system            kube-apiserver-k8s-master                    1/1     Running            0                7h24m
kube-system            kube-controller-manager-k8s-master           1/1     Running            0                7h24m
kube-system            kube-proxy-f7z9m                             1/1     Running            0                7h7m
kube-system            kube-proxy-jv7xk                             1/1     Running            0                7h7m
kube-system            kube-proxy-xhtzx                             1/1     Running            0                7h24m
kube-system            kube-scheduler-k8s-master                    1/1     Running            0                7h24m
kube-system            metrics-server-7db4fb59f9-rkw72              1/1     Running            0                5h40m
kubernetes-dashboard   dashboard-metrics-scraper-5cb4f4bb9c-9wnc5   1/1     Running            0                5h40m
kubernetes-dashboard   kubernetes-dashboard-6bc7c98694-7zrqz        1/1     Running            0                5h40m
tigera-operator        tigera-operator-84cf9b6dbb-brtdt             1/1     Running            0                33m
[root@k8s-master ~]#

13. pod list를 jsonpath 형식으로 name, namespace가 보여지도록 확인한다. 

 

[solve]

[root@k8s-master ~]# kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.metadata.namespace}{"\n"}{end}'
dnsutils	default
env-pod	default
foo	default
frontend	default
kucc8	default
nginx	default
nginx-deployment-cbdccf466-4k8t8	default
nginx-deployment-cbdccf466-6nqjr	default
nginx-deployment-cbdccf466-bjtj5	default
nginx-deployment-cbdccf466-nsp68	default
nginx-deployment-cbdccf466-wf8gk	default
nginx-deployment-cbdccf466-xx9hr	default
nginx1	default
nginx2	default
non-persistent-redis	default
[root@k8s-master ~]#

 


14. 특정 pod의 이미지 버전을 확인한다. jsonpath를 이용한다. 

 

[solve]

[root@k8s-master ~]# kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers..image}{"\n"}{end}' | grep -i nginx-deployment
nginx-deployment-cbdccf466-hg9bw	nginx:1.14.2
nginx-deployment-cbdccf466-kpt85	nginx:1.14.2
nginx-deployment-cbdccf466-ldggc	nginx:1.14.2
nginx-deployment-cbdccf466-rswc7	nginx:1.14.2
nginx-deployment-cbdccf466-t8zb8	nginx:1.14.2
nginx-deployment-cbdccf466-xphn5	nginx:1.14.2
[root@k8s-master ~]#

 

반응형