3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kubectl custom-columns, label-columns, field-selector, sort-byを試してみた - その1

Posted at

試したこと

元々kubectl get podsでこれを表示したくて

  • .spec.containers[*].resources.requests.memory
  • .spec.containers[*].resources.limits.memory

時間があったのでcustom-columns, label-columns, field-selector, sort-byも試してみました。表示できるスペックのまとまっているサイトを見つけることができなかったのでリスト化することに。

以下の#71612でも説明ありますが、現在のcustom-columnsは使いにくい。
kubectl get pods + custom-columnではなくcustom-columnsしか表示されません。

あと検索すると-o=を使っている人と-o+spaceの例があるのですがオフィシャルはスペースでした。

気になる/kind feature

kubectl - Introduce "custom-columns" variant to add additional columns to output #71612
https://github.com/kubernetes/kubernetes/issues/71612

References

https://kubernetes.io/docs/reference/kubectl/overview/#custom-columns
https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/
https://kubernetes.io/docs/concepts/cluster-administration/manage-deployment/
https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
https://kubernetes.io/docs/reference/kubectl/overview/#sorting-list-objects

使い方

表示したいものを探すのには
kubectl get <pod, node, svc etc> -o json
kubectl get <pod, node, svc etc> -o yaml
https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands

こういうものも見つけました
https://gist.github.com/so0k/42313dbb3b547a0f51a547bb968696ba

基本

kubectl get pods -o custom-columns='<表示したいもの>'
Example - Pod
CONTAINERS:.spec.containers[].name
M_LIMITS:.spec.containers[].resources.limits.memory
M_REQUESTS:.spec.containers[].resources.requests.memory
NAME:.metadata.name
NAMESPACE:.metadata.namespace
NODE_IP:.status.hostIP
POD_IP:.status.podIP
PORTS:.spec.template.spec.containers[].ports.containerPort
RESTART_POLICY:.spec.restartPolicy
RESTARTS:.status.containerStatuses[].restartCount
RSRC:.metadata.resourceVersion
STATUS:.status.phase

サンプル

custom-coulmns

-o custom-columns=<spec>

Pods
デフォルト表示のREADYとAGEもcustom-columnsでは表示できず。

kubectl get pods -o='custom-columns=NAME:.metadata.name,CONTAINERS:.spec.containers[].name,Images:.spec.containers[].image'

kubectl get pods -o custom-columns='NAME:.metadata.name,STATUS:.status.phase, RESTARTS:.status.containerStatuses[].restartCount,M_REQUESTS:.spec.containers[].resources.requests.memory,M_LIMITS:.spec.containers[].resources.limits.memory,NODE_IP:.status.hostIP,POD_IP:.status.podIP'

Nodes
AZがわかると便利だと思う。 ポイント".metadata.labels."kubernetes.io/role"のようなものはエスケープする。

kubectl get nodes -o custom-columns='NAME:metadata.name,ZONE:metadata.labels.failure-domain\.beta\.kubernetes\.io/zone'

Use a separate file

-o custom-columns-file=<filename>

NAME	STATUS	M_REQUESTS	M_LIMITS				
.metadata.name	.status.phase	.spec.containers[*].resources.requests.memory	.spec.containers[*].resources.limits.memory

label-columns

-L or --label-columns <spec>

kubectl get nodes --label-columns failure-domain.beta.kubernetes.io/zone

field-selector

--field-selector <spec>

kubectl get pods --field-selector=status.phase=Running,spec.restartPolicy=Always

sort-by

kubectl get pods -o wide --sort-by=.spec.nodeName

deployment おまけ

deploy.spec.template.spec.containers.ports.containerPort

Jsonpathの使い方

次にまとめようと思います

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?