When you support large Kubernetes clusters, you need efficient methods to list pods, nodes, and deployments when you are troubleshooting issues. Kubectl has a number of built-in methods to do this. You can use jsonpath, selectors and sort-by statements to return the exact data you need. In addition, you can use the kubectl “-l” option to list objects that contain (or don’t contain) a label. To illustrate this, lets assign the label “env=staging” to a node:
$ kubectl label node test-worker env=staging
node/test-worker labeled
Once that label is assigned, you can list all nodes with that label by passing it as an argument to the list option:
$ kubectl get nodes -l env=staging
NAME STATUS ROLES AGE VERSION
test-worker Ready <none> 2m15s v1.17.0
This gets even more useful when you add in logic operations:
$ kubectl get nodes -l 'env!=staging'
NAME STATUS ROLES AGE VERSION
test-control-plane Ready master 10m v1.17.0
test-control-plane2 Ready master 10m v1.17.0
test-worker2 Ready <none> 9m31s v1.17.0
test-worker3 Ready <none> 9m41s v1.17.0
The example above will list all nodes that don’t contain the label ‘env=staging’. Super useful stuff!