Kubernetes
API resources
subresources
Kubernetes documentation
cloud-native tools

Where can I get a list of Kubernetes API resources and subresources?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Kubernetes is a powerful orchestration tool for containerized applications, and at its core is a rich and complex set of API resources. Understanding these resources and how to interact with them is crucial for managing and orchestrating applications efficiently. If you're looking to find detailed information on Kubernetes API resources and subresources, this article will guide you through the process, offering technical insights and practical examples.

Understanding Kubernetes API Resources and Subresources

Kubernetes resources are objects within the Kubernetes ecosystem that represent your cluster's state. Examples include Pods, Services, Deployments, and ConfigMaps. Each resource may have one or more API endpoints where they can be managed or queried.

Subresources provide additional API functionality for resources, such as scaling, status checking, or executing commands within a container. They often appear as API endpoints nested under resource URLs.

Retrieving a List of Kubernetes API Resources

To view all available API resources, the kubectl api-resources command is indispensable. This command lists all the resource names, along with their associated API groups and version information.

bash
kubectl api-resources

Example Output:

 
1NAME                              SHORTNAMES   APIGROUP      NAMESPACED   KIND
2pods                              po                        true         Pod
3services                          svc                       true         Service
4deployments                       deploy       apps          true         Deployment
5replicasets                       rs           apps          true         ReplicaSet
6...

This command provides essential metadata about each resource. The columns denote:

  • NAME: The name of the resource.
  • SHORTNAMES: Possible abbreviations for use in kubectl commands.
  • APIGROUP: Indicates the API group the resource belongs to.
  • NAMESPACED: A boolean indicating if the resource is namespaced.
  • KIND: Represents the kind of API object.

Accessing Kubernetes API Resources and Subresources Programmatically

The Kubernetes API server exposes these resources and subresources via HTTP endpoints. Accessing these involves understanding the URL structure, which generally follows the format:

 
https://<k8s-server>/api/v1/<resource>/<name>/<subresource>

Here's a breakdown:

  • <k8s-server>: The URL of the Kubernetes API server.
  • <resource>: The particular resource type (e.g., pods).
  • <name>: The object name or identifier.
  • <subresource>: Any subresource you’re accessing (e.g., log).

Example API Call to Fetch Pod Logs:

To fetch logs for a specific pod, /log as a subresource is appended to the pod's URL.

bash
curl -k https://<k8s-server>/api/v1/namespaces/default/pods/nginx-xyz/log

Discovering APIs Programmatically

Programmatically discovering available resources can be accomplished by querying the /api and /apis endpoints of the Kubernetes API server.

List Core Resources:

bash
curl -k https://<k8s-server>/api

List Custom API Groups:

bash
curl -k https://<k8s-server>/apis

These commands return JSON data listing available API versions. Further querying each version yields detailed resources and subresource information.

Table of Key Kubernetes API Concepts

ConceptDescription
ResourcesPrimary object types in Kubernetes (e.g., Pods, Services, Deployments).
SubresourcesExtensions or additional operations on a resource (e.g., logs, exec).
Namespaced ResourceIndicates if a resource exists within a namespace scope.
API GroupLogical grouping of resources, allowing API separation and versioning.
Endpoint URLThe specific URL structure through which APIs are accessed.

Additional Topics

Role of API Versions Kubernetes supports API versioning, allowing APIs to independently evolve. This ensures backward compatibility and smooth transitions between API updates. Resources are typically versioned as v1, v1beta1, etc.

Interacting via Client Libraries Kubernetes client libraries in languages such as Go, Python, and Java can interact with API resources, providing a programmatically friendly interface to the Kubernetes core.

python
1from kubernetes import client, config
2
3config.load_kube_config()
4v1 = client.CoreV1Api()
5print("Listing pods with their IPs:")
6for i in v1.list_pod_for_all_namespaces(watch=False).items:
7    print(f"{i.status.pod_ip}\t{i.metadata.namespace}\t{i.metadata.name}")

Security and Authentication When accessing the Kubernetes API programmatically or via a CLI like kubectl, authentication and permissions play a crucial role. RBAC (Role-Based Access Control) is often employed to secure API requests.

Conclusion

Understanding Kubernetes API resources and subresources is vital for operating and managing Kubernetes clusters. Whether using kubectl, directly interfacing with API endpoints, or leveraging client libraries, this knowledge is foundational. With the accessible commands and methods provided here, accessing and exploring these resources can be seamlessly integrated into your Kubernetes workflows.


Course illustration
Course illustration

All Rights Reserved.