How can I list the taints on Kubernetes nodes?

前端 未结 11 1534
别那么骄傲
别那么骄傲 2021-01-30 08:47

The docs are great about explaining how to set a taint on a node, or remove one. And I can use kubectl describe node to get a verbose description of one node, inclu

相关标签:
11条回答
  • 2021-01-30 09:18

    The simplest way to do this without using any extra tools such as JQ is to use the custom-columns output option.

    $ kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints --no-headers 
    
    

    Output:

    master-11   [map[effect:PreferNoSchedule key:node-role.kubernetes.io/master]]
    master-12   [map[effect:PreferNoSchedule key:node-role.kubernetes.io/master]]
    master-13   [map[effect:PreferNoSchedule key:node-role.kubernetes.io/master]]
    

    With something like Taints where it is a map or list and you want it to look clean for parsing with some other tool you can clean them up using you can use something similar to the answer by Edwin Tai but with a little extra smarts to extract the keys.

    kubectl get nodes -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.taints[*].key}{"\n"}{end}' 
    

    Output:

    master-11   node-role.kubernetes.io/master
    master-12   node-role.kubernetes.io/master
    master-13   node-role.kubernetes.io/master
    worker-21   thegoldfish.org/storage thegoldfish.org/compute
    worker-22   thegoldfish.org/storage thegoldfish.org/compute
    worker-23   thegoldfish.org/compute
    worker-24   thegoldfish.org/storage thegoldfish.org/compute
    

    Extra examples:

    Using this method you can easily create custom outputs

    Quick overview of nodes:

    kubectl get nodes -o custom-columns=NAME:.metadata.name,ARCH:.status.nodeInfo.architecture,KERNEL:.status.nodeInfo.kernelVersion,KUBLET:.status.nodeInfo.kubeletVersion,CPU:.status.capacity.cpu,RAM:.status.capacity.memory
    

    Output:

    NAME        ARCH    KERNEL                       KUBLET    CPU   RAM
    master-11   amd64   3.10.0-1062.9.1.el7.x86_64   v1.17.0   6     7910096Ki
    master-12   amd64   3.10.0-1062.9.1.el7.x86_64   v1.17.0   6     7910096Ki
    master-13   amd64   3.10.0-1062.9.1.el7.x86_64   v1.17.0   6     7910096Ki
    

    Overview of pods and where to find them sorted by creation time:

    kubectl get pods -A -o custom-columns=NAMESPACE:.metadata.namespace,NAME:.metadata.name,NODE:.spec.nodeName,HOSTIP:.status.hostIP,PHASE:.status.phase,START_TIME:.metadata.creationTimestamp --sort-by=.metadata.creationTimestamp
    

    Output:

    NAMESPACE              NAME                                                  NODE        HOSTIP            PHASE       START_TIME
    kube-system            kube-proxy-rhmrz                                      master-11   192.168.121.108   Running     2019-12-26T14:22:03Z
    kube-system            coredns-6955765f44-777v9                              master-11   192.168.121.108   Running     2019-12-26T14:22:03Z
    kube-system            coredns-6955765f44-w7rch                              master-11   192.168.121.108   Running     2019-12-26T14:22:03Z
    kube-system            kube-scheduler-master-11                              master-11   192.168.121.108   Running     2019-12-26T14:22:05Z
    kube-system            kube-controller-manager-master-11                     master-11   192.168.121.108   Running     2019-12-26T14:22:05Z
    kube-system            etcd-master-11                                        master-11   192.168.121.108   Running     2019-12-26T14:22:05Z
    kube-system            kube-apiserver-master-11                              master-11   192.168.121.108   Running     2019-12-26T14:22:05Z
    kube-system            calico-node-sxls8                                     master-11   192.168.121.108   Running     2019-12-26T14:55:41Z
    kube-system            calico-kube-controllers-6d85fdfbd8-dnpn4              master-11   192.168.121.108   Running     2019-12-26T14:55:41Z
    kubernetes-dashboard   dashboard-metrics-scraper-76585494d8-jx9cg            master-11   192.168.121.108   Running     2019-12-26T16:10:16Z
    kubernetes-dashboard   kubernetes-dashboard-5996555fd8-5z5p2                 master-11   192.168.121.108   Running     2019-12-26T16:10:16Z
    

    The documentation for this is https://kubernetes.io/docs/reference/kubectl/overview/#custom-columns

    0 讨论(0)
  • 2021-01-30 09:19
    kubectl get nodes -o json | jq '.items[].spec'
    

    which will give the complete spec with node name, or:

    kubectl get nodes -o json | jq '.items[].spec.taints'
    

    will produce the list of the taints per each node

    0 讨论(0)
  • 2021-01-30 09:22

    I was looking to get the list of nodes that have a specific Taint. I only found this SO answer, so if anybody is looking for this answer, here is the solution:

    kubectl get nodes -o go-template='{{range $item := .items}}{{with $nodename := $item.metadata.name}}{{range $taint := $item.spec.taints}}{{if and (eq $taint.key "node-role.kubernetes.io/master") (eq $taint.effect "NoSchedule")}}{{printf "%s\n" $nodename}}{{end}}{{end}}{{end}}{{end}}'
    

    On my cluster, the output is:

    preprod-master
    preprod-proxy
    
    0 讨论(0)
  • 2021-01-30 09:22

    PowerShell:\> kubectl describe nodes | findstr "Taint Hostname"

    or

    Bash# kubectl describe nodes | egrep -hi "Taint|Hostname"

    This command is mad easy to remember

    Output looks like this:

    Taints:             <none>  
      Hostname:   aks-agentpool-30208295-0  
    Taints:             <none>    
      Hostname:  aks-agentpool-30208295-1
    ...
    
    0 讨论(0)
  • 2021-01-30 09:25
    kubectl describe nodes [node_name] | grep 'Taints'
    
    kubectl get nodes -o json | jq '.items[].spec.taints' 
    

    --> this last step will require jq installed ( sudo apt install jq)

    0 讨论(0)
提交回复
热议问题