Kubernetes: Viewing and Finding Resources Cheat Sheet

List all namespaces in the cluster

kubectl get namespaces

List all services in the namespace

kubectl get services

List all pods in all namespaces

kubectl get pods --all-namespaces

List all pods in the namespace, with more details

kubectl get pods -o wide

List a particular replication controller

kubectl get rc <rc-name>

List a particular RC

kubectl get replicationcontroller <rc-name>

List a particular node with verbose output

kubectl describe nodes <node-name>

List a particular pod with verbose output

kubectl describe pods <pod-name>
kubectl describe pods/<pod-name> # Equivalent to previous

Lists pods created by using common prefix

kubectl describe pods <rc-name>

List services sorted by name

kubectl get services --sort-by=.metadata.name

List pods sorted by restart count

kubectl get pods --sort-by=.status.containerStatuses[0].restartCount

Get the version label of all pods with label app=cassandra

kubectl get pods --selector=app=cassandra rc -o 'jsonpath={.items[*].metadata.labels.version}'

Get ExternalIPs of all nodes

kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="ExternalIP")].address}'

List names of pods that belong to Particular RC

# "jq" command useful for transformations that are too complex for jsonpath
sel=$(./kubectl get rc <rc-name> --output=json | jq -j '.spec.selector | to_entries | .[] | "\(.key)=\(.value),"')
sel=${sel%?} # Remove trailing comma
pods=$(kubectl get pods --selector=$sel --output=jsonpath={.items..metadata.name})`

Check which nodes are ready

kubectl get nodes -o jsonpath='{range .items[*]}{@.metadata.name}:{range @.status.conditions[*]}{@.type}={@.status};{end}{end}'| tr ';' "\n"  | grep "Ready=True"

Leave a Reply

Your email address will not be published. Required fields are marked *