List only the examples from Kubernetes command help sections


Run the kubernetes command with the help flag and grep to list only the examples.

Example 1 (kubectl get):

[root@master01 ~]# kubectl get -h | grep '# ' -A2
  # List all pods in ps output format.
  kubectl get pods
  # List all pods in ps output format with more information (such as node name).
  kubectl get pods -o wide
  # List a single replication controller with specified NAME in ps output format.
  kubectl get replicationcontroller web
  # List deployments in JSON output format, in the "v1" version of the "apps" API group:
  kubectl get deployments.v1.apps -o json
  # List a single pod in JSON output format.
  kubectl get -o json pod web-pod-13je7
  # List a pod identified by type and name specified in "pod.yaml" in JSON output format.
  kubectl get -f pod.yaml -o json
  # List resources from a directory with kustomization.yaml - e.g. dir/kustomization.yaml.
  kubectl get -k dir/
  # Return only the phase value of the specified pod.
  kubectl get -o template pod/web-pod-13je7 --template={{.status.phase}}
  # List resource information in custom columns.
  kubectl get pod test-pod -o custom-columns=CONTAINER:.spec.containers[0].name,IMAGE:.spec.containers[0].image
  # List all replication controllers and services together in ps output format.
  kubectl get rc,services
  # List one or more resources by their type and names.
  kubectl get rc/web service/frontend pods/web-pod-13je7

Example 2 (kubectl create):

[root@master01 ~]# kubectl create -h | grep '# ' -A2
  # Create a pod using the data in pod.json.
  kubectl create -f ./pod.json
  # Create a pod based on the JSON passed into stdin.
  cat pod.json | kubectl create -f -
  # Edit the data in docker-registry.yaml in JSON then create the resource using the edited data.
  kubectl create -f docker-registry.yaml --edit -o json

Example 3 (kubectl scale):

[root@master01 ~]# kubectl scale -h | grep '# ' -A2
  # Scale a replicaset named 'foo' to 3.
  kubectl scale --replicas=3 rs/foo
  # Scale a resource identified by type and name specified in "foo.yaml" to 3.
  kubectl scale --replicas=3 -f foo.yaml
  # If the deployment named mysql's current size is 2, scale mysql to 3.
  kubectl scale --current-replicas=2 --replicas=3 deployment/mysql
  # Scale multiple replication controllers.
  kubectl scale --replicas=5 rc/foo rc/bar rc/baz
  # Scale statefulset named 'web' to 3.
  kubectl scale --replicas=3 statefulset/web


Create an alias to quickly list the examples for a specific kubernetes command.

1. Create bash alias:

# Create function in .bashrc to accept a kubernetes command
echo "kex() { kubectl "$1" -h | grep '# ' -A2 ; }" >> ~/.bashrc

# Source .bashrc to make it available in the current session
source ~/.bashrc

# Use the function as an alias to return kubernetes commands (Eg. kubernetes "get"):
kex get


Quickly and easily list kubernetes command examples.