Kubernetes json path
Use json path to filter data.
#kubectl get nodes
#kubectl get nodes -o wide
1. Identify the kubectl command
2. Familiarize with json output
#kubectl get nodes -o json
#kubectl get pods -o json
3. Form the json path query
.items[0].spec.containers.[0].image
4. Use the json path query with kubectl command
#kubectl get nodes -o jsonpath='{.items[0].spec.containers.[0].image}'
Combine two query. And separate by {"\n"} or {"\t"}
#kubectl get nodes -o jsonpath='{.items[*].metadata.name}{"\n"}{.items[*].status.capacity.cpu}'
Result:
master node01
4 4
Format the display
Start loop --> '{range .item[*]}
Operation --> {.metadata.name}{"\t"}{.status.capacity.cpu}{"\n"}
End loop --> {end}'
#kubectl get nodes -o jsonpath='{range .item[*]}{.metadata.name}{"\t"}{.status.capacity.cpu}{"\n"}{end}'
Result
master 4
node01 4
Format the display with custom columns
#kubectl get nodes -o custom-columns=<column name>:<json path>
#kubectl get nodes -o custom-columns=NODE:.metadata.name, CPU:.status.capacity.cpu
Result
NODE CPU
master 4
node01 4
Sort the result
--sort-by=<item>
#kubectl get nodes --sort-by=.metadata.name
#kubectl get pod --sort-by=.status.capacity.cpu
Example: get name, capacity and order by capacity
#kubectl get pv -o custom-columns=NAME:.metadata.name,CAPACITY:.spec.capacity.storage --sort-by=.spec.capacity.storage
Read config file
#kubectl config view --kubeconfig=<config file>
Check config file with condition
kubectl config view --kubeconfig=my-kube-config -o jsonpath="{.contexts[?(@.context.user=='aws-user')].context}"
Comments
Post a Comment