본문 바로가기

카테고리 없음

Practice Test - Advanced Kubectl Commands

Get the list of nodes in JSON format and store it in a file at /opt/outputs/nodes.json

kubectl get nodes -o json > /opt/outputs/nodes.json

 

Get the details of the node node01 in json format and store it in the file /opt/outputs/node01.json

kubectl get nodes node01 -o json > /opt/outputs/node01.json

 

ㅁ Use JSON PATH query to fetch node names and store them in /opt/outputs/node_names.txt

Remember the file should only have node names

kubectl get nodes -o=jsonpath='{.items[*].metadata.name}' > /opt/outputs/node_names.txt

 

ㅁ Use JSON PATH query to retrieve the osImages of all the nodes and store it in a file /opt/outputs/nodes_os.txt

The osImages are under the nodeInfo section under status of each node.

kubectl get node -o=jsonpath='{.items[*].status.nodeInfo.osImage}' > /opt/outputs/nodes_os.txt

 

ㅁ A kube-config file is present at /root/my-kube-config. Get the user names from it and store it in a file /opt/outputs/users.txt

Use the command kubectl config view --kubeconfig=/root/my-kube-config to view the custom kube-config

kubectl config view --kubeconfig=/root/my-kube-config -o=jsonpath='{.users[*].name}' > /opt/outputs/users.txt

 

A set of Persistent Volumes are available. Sort them based on their capacity and store the result in the file /opt/outputs/storage-capacity-sorted.txt

 

그냥 jsonpath로 데이터를 추출하는 경우는

kubectl get pv -o=jsonpath='{.items[*].spec.capacity.storage}'

sort하는 경우는

kubectl get pv --sort-by=.spec.capacity.storage

최종 답은

kubectl get pv --sort-by=.spec.capacity.storage > /opt/outputs/storage-capacity-sorted.txt
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv-log-4   40Mi       RWX            Retain           Available                                   34m
pv-log-1   100Mi      RWX            Retain           Available                                   34m
pv-log-2   200Mi      RWX            Retain           Available                                   34m
pv-log-3   300Mi      RWX            Retain           Available                                   34m

ㅁ That was good, but we don't need all the extra details. Retrieve just the first 2 columns of output and store it in /opt/outputs/pv-and-capacity-sorted.txt

The columns should be named NAME and CAPACITY. Use the custom-columns option. And remember it should still be sorted as in the previous question.

kubectl get pv --sort-by=.spec.capacity.storage -o=custom-columns=NAME:.metadata.name,CAPACITY:.spec.capacity.storag
NAME       CAPACITY
pv-log-4   40Mi
pv-log-1   100Mi
pv-log-2   200Mi
pv-log-3   300Mi

 

Use a JSON PATH query to identify the context configured for the aws-user in the my-kube-config context file and store the result in /opt/outputs/aws-context-name.

kubectl config view --kubeconfig=my-kube-config -o jsonpath="{.contexts[?(@.context.user=='aws-user')].name}" > /opt/outputs/aws-context-name