Kubernetes administrators often utilize the kubectl api-resources
command to gain insights into the various API resources available within a Kubernetes cluster. This command is crucial for understanding the types of objects and their corresponding endpoints that Kubernetes manages.
Let’s explore how kubectl api-resources
can be used effectively:
1. List all API resources:
Running kubectl api-resources
without any additional arguments provides a comprehensive list of all API resources registered in the cluster. This includes core resources like pods, services, and nodes, as well as custom resources defined by users or installed by operators.
Example command:
kubectl api-resources
Output:
NAME SHORTNAMES APIGROUP NAMESPACED KIND pods po true Pod services svc true Service nodes no false Node ...
Verification: Verify by inspecting the list of resources returned against the expected Kubernetes API resources.
2. Filter by API group:
To narrow down the results to a specific API group, use --api-group
option. For example, to list only resources from the “apps” API group:
Example command:
kubectl api-resources --api-group=apps
Output:
NAME SHORTNAMES APIGROUP NAMESPACED KIND deployments deploy apps true Deployment statefulsets sts apps true StatefulSet ...
Verification: Ensure that only resources from the specified API group are listed in the output.
3. Include cluster-scoped resources:
By default, kubectl api-resources
lists only namespaced resources. To include cluster-scoped resources like nodes and persistent volumes, use the --namespaced=false
option:
Example command:
kubectl api-resources --namespaced=false
Output:
NAME SHORTNAMES APIGROUP NAMESPACED KIND nodes no false Node persistentvolumes pv false PersistentVolume ...
Verification: Check if the output includes resources that are not namespaced (cluster-scoped).
4. Display short names:
To show only resources with their respective short names, use the --short
option:
Example command:
kubectl api-resources --short
Output:
NAME SHORTNAMES APIGROUP NAMESPACED KIND po, pods core true Pod svc, services core true Service ...
Verification: Ensure that the output lists resources alongside their short names.
5. Output in wide format:
For a more detailed view of the API resources, use the --wide
option to display additional information such as the API group and whether the resource is namespaced:
Example command:
kubectl api-resources --wide
Output:
NAME SHORTNAMES APIGROUP NAMESPACED KIND pods po true Pod services svc true Service nodes no false Node ...
Verification: Verify that the output includes the wide format information for each API resource.
6. Search by resource name:
To find details about a specific API resource, you can filter the output using the --api-resource
option followed by the resource name. For instance, to find details about pods:
Example command:
kubectl api-resources --api-resource=pods
Output:
NAME SHORTNAMES APIGROUP NAMESPACED KIND pods po true Pod
Verification: Check if the output specifically lists details about the pods resource.
7. Sort alphabetically:
To sort the list of API resources alphabetically by their names, use the --sort-by
option:
Example command:
kubectl api-resources --sort-by=name
Output:
NAME SHORTNAMES APIGROUP NAMESPACED KIND bindings true Binding componentstatuses cs false ComponentStatus ...
Verification: Ensure that the output is sorted alphabetically by the resource names.
8. Output as YAML:
To obtain the list of API resources in YAML format, use the -o yaml
option:
Example command:
kubectl api-resources -o yaml
Output:
apiVersion: v1 items: - kind: ComponentStatusList apiVersion: v1 metadata: resourceVersion: "" items: [] kind: List ...
Verification: Verify that the output is in valid YAML format representing the API resources.
9. Verbose output:
For more detailed information about each API resource, use the -v
or --verbose
option:
Example command:
kubectl api-resources -v=7
Output:
NAME SHORTNAMES APIGROUP NAMESPACED KIND pods po true Pod...
Verification: Check if the output provides additional verbose information about the API resources.
10. Include deprecated resources:
To include deprecated API resources in the list, use the --include-deprecated
option:
Example command:
kubectl api-resources --include-deprecated
Output:
NAME SHORTNAMES APIGROUP NAMESPACED KIND deployments deploy apps true Deployment replicaset rs apps true ReplicaSet ...
Verification: Ensure that the output includes resources marked as deprecated.
This variety of options makes kubectl api-resources
a versatile tool for Kubernetes administrators to explore and understand the API resources available within their clusters. By using these commands and verifying their outputs, administrators can effectively manage and troubleshoot their Kubernetes environments.
Also check similar articles.
Generating Shell Completion Code with kubectl completion
Managing Kubernetes Annotations with kubectl annotate
Updating Kubernetes Labels with kubectl label
Building Kubernetes Customizations with kubectl kustomize
Waiting for Kubernetes Resources with kubectl wait
Discussion about this post