The Kubernetes API is at the core of Kubernetes’ declarative approach to managing containerized applications and infrastructure. It provides a standardized interface for interacting with the cluster, allowing users to create, read, update, and delete various resources. In this article, we’ll explore Kubernetes API Resources in detail. Focusing on the most commonly used resources and their significance in Kubernetes deployments here.

You can use the kubectl api-resources to find details about any resources

$ kubectl api-resources

NAME                              SHORTNAMES          APIVERSION                             NAMESPACED   KIND
bindings                                              v1                                     true         Binding
componentstatuses                 cs                  v1                                     false        ComponentStatus
configmaps                        cm                  v1                                     true         ConfigMap
endpoints                         ep                  v1                                     true         Endpoints
events                            ev                  v1                                     true         Event
limitranges                       limits              v1                                     true         LimitRange
namespaces                        ns                  v1                                     false        Namespace
nodes                             no                  v1                                     false        Node
persistentvolumeclaims            pvc                 v1                                     true         PersistentVolumeClaim
persistentvolumes                 pv                  v1                                     false        PersistentVolume
pods                              po                  v1                                     true         Pod
podtemplates                                          v1                                     true         PodTemplate
replicationcontrollers            rc                  v1                                     true         ReplicationController
resourcequotas                    quota               v1                                     true         ResourceQuota
secrets                                               v1                                     true         Secret

Use the -h flag to find more details. Here’s a breakdown of the columns in the output:

  • NAME: The name of the API resource.
  • SHORTNAMES: Short aliases for the resource.
  • APIGROUP: The API group to which the resource belongs. Some resources are in the core group (""), while others may belong to specific API groups.
  • NAMESPACED: Indicates whether the resource is namespace-scoped (true) or cluster-scoped (false).
  • KIND: The kind of Kubernetes object corresponding to the resource.

Core resources in Kubernetes:

1. Pods:

  • Pods are the smallest deployable units in the Kubernetes object model. They represent a running process on your cluster.
  • Pods encapsulate one or more containers sharing the same network namespace and storage volumes.

Here’s an example of a Pod resource:

YAML
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: my-container
      image: my-image
      ports:
        - containerPort: 80

Here are some commonly used commands to manage Kubernetes Pods:

  • kubectl get pods: This command lists all the pods in the current namespace.
  • kubectl get pods -n <namespace>: This command lists all the pods in a specific namespace.
  • kubectl describe pod <pod-name>: This command displays detailed information about a specific pod.
  • kubectl logs <pod-name>: This command retrieves the logs of a specific pod.
  • kubectl logs <pod-name> -c <container-name>: This command retrieves the logs of a specific container in a pod.
  • kubectl exec <pod-name> -c <container-name> -- <command>: This command executes a command in a specific container of a pod.
  • kubectl create -f <manifest-file.yaml>: This command creates a new pod from a YAML manifest file.
  • kubectl apply -f <manifest-file.yaml>: This command creates or updates a pod from a YAML manifest file.
  • kubectl delete -f <manifest-file.yaml>: This command deletes a pod defined in a YAML manifest file.
  • kubectl delete pod <pod-name>: This command deletes a specific pod.

2. Deployments:

  • A higher-level abstraction that manages the lifecycle of Pods, ensuring declarative updates, scaling, and rollback capabilities.
  • Deployments enable blue-green deployments, rolling updates, and canary releases for applications.

Here’s an example of a Deployment resource:

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: my-image:latest
          ports:
            - containerPort: 80

Here are some commonly used commands to manage Kubernetes Deployments:

  • kubectl get deployments: This command lists all the deployments in the current namespace.
  • kubectl get deployments -n <namespace>: This command lists all the deployments in a specific namespace.
  • kubectl describe deployment <deployment-name>: This command displays detailed information about a specific deployment.
  • kubectl rollout status deployment <deployment-name>: This command shows the status of the latest deployment.
  • kubectl rollout history deployment <deployment-name>: This command shows the rollout history of a specific deployment.
  • kubectl rollout undo deployment <deployment-name>: This command undoes the last deployment.
  • kubectl rollout undo deployment <deployment-name> --to-revision=<revision-number>: This command undoes a specific deployment to a previous revision.
  • kubectl scale deployment <deployment-name> --replicas=<number-of-replicas>: This command scales the number of replicas in a deployment.
  • kubectl edit deployment <deployment-name>: This command opens the deployment manifest in a text editor for editing.
  • kubectl delete deployment <deployment-name>: This command deletes a specific deployment.

3. Services:

  • Exposes a set of Pods as a network service, providing a stable endpoint for accessing applications running in the cluster.
  • Services support various types of load balancing, including ClusterIP, NodePort, and LoadBalancer.

Here’s an example of a Service resource:

YAML
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - port: 80
  type: LoadBalancer

Here are some common commands used to manage Services in Kubernetes:

  1. kubectl get services: This command lists all the services in the current namespace.
  2. kubectl get services -n <namespace>: This command lists all the services in a specific namespace.
  3. kubectl describe service <service-name>: This command displays detailed information about a specific service.
  4. kubectl create service clusterip <service-name> --tcp=<port>:<target-port>: This command creates a new ClusterIP service.
  5. kubectl expose deployment <deployment-name> --type=<service-type> --port=<port> --target-port=<target-port>: This command creates a new service for an existing deployment.
  6. kubectl edit service <service-name>: This command opens the service manifest in a text editor for editing.
  7. kubectl delete service <service-name>: This command deletes a specific service.

4. ConfigMaps:

  • Stores configuration data in key-value pairs, which can be consumed by Pods as environment variables or mounted as volumes.
  • ConfigMaps enables decoupling configuration from application code, facilitating easier configuration management.

Here’s an example of a ConfigMap resource:

YAML
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-configmap
data:
  my-key: my-value

Here are some common commands used to manage ConfigMaps in Kubernetes:

  1. kubectl create configmap <configmap-name> --from-literal=<key>=<value>: This command creates a new ConfigMap with a single key-value pair.
  2. kubectl create configmap <configmap-name> --from-file=<path>: This command creates a new ConfigMap from a file or directory.
  3. kubectl get configmaps: This command lists all the ConfigMaps in the current namespace.
  4. kubectl get configmaps -n <namespace>: This command lists all the ConfigMaps in a specific namespace.
  5. kubectl describe configmap <configmap-name>: This command displays detailed information about a specific ConfigMap.
  6. kubectl edit configmap <configmap-name>: This command opens the ConfigMap manifest in a text editor for editing.
  7. kubectl delete configmap <configmap-name>: This command deletes a specific ConfigMap.

5. Secrets:

  • Stores sensitive data, such as passwords, API tokens, and certificates, securely within the cluster.
  • Secrets are base64-encoded by default and can be mounted into Pods as files or exposed as environment variables

Here is an example:

YAML
apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  my-key: <I_am_BASE_64_ENCODED>
  1. kubectl create secret generic <secret-name> --from-literal=<key>=<value>: This command creates a new Secret with a single key-value pair.
  2. kubectl create secret generic <secret-name> --from-file=<path>: This command creates a new Secret from a file or directory.
  3. kubectl get secrets: This command lists all the Secrets in the current namespace.
  4. kubectl get secrets -n <namespace>: This command lists all the Secrets in a specific namespace.
  5. kubectl describe secret <secret-name>: This command displays detailed information about a specific Secret.
  6. kubectl edit secret <secret-name>: This command opens the Secret manifest in a text editor for editing.
  7. kubectl delete secret <secret-name>: This command deletes a specific Secret.
kubectl create secret generic my-secret --from-file=my-secret.txt

6. Persistent Volumes:

  • PVs represent durable storage volumes provisioned by the cluster’s storage backend.
  • Persistent Volumes allow you to manage storage in your cluster. They enable you to request storage resources and use them with your Pods.

Here’s an example of a Persistent Volume resource:

YAML
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: my-storage-class
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /path/to/nfs/share
    server: my-nfs-server

Here are some common commands used to manage PVs in Kubernetes:

  1. kubectl get pv: This command lists all the PVs in the cluster.
  2. kubectl get pv -n <namespace>: This command lists all the PVs in a specific namespace.
  3. kubectl describe pv <pv-name>: This command displays detailed information about a specific PV.
  4. kubectl create pv <pv-name> --volume-mode=Filesystem --capacity=storageCapacity --access-modes=accessMode --persistent-volume-reclaim-policy=reclaimPolicy --storage-class-name=storageClassName --mount-options=mountOptions --host-path=hostPath: This command creates a new PV with the specified parameters.
  5. kubectl edit pv <pv-name>: This command opens the PV manifest in a text editor for editing.
  6. kubectl delete pv <pv-name>: This command deletes a specific PV.

7. Persistent Volume Claims

  • Persistent Volume Claims allow you to request storage resources from your cluster.
  • They enable you to request a specific amount of storage and access mode for your Pods.
  • PVCs are requests for storage resources from PVs, allowing Pods to claim and use persistent storage.

Here’s an example of a Persistent Volume Claim resource:

YAML
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  storageClassName: my-storage-class
  • kubectl apply -f pvc.yaml: To create a PVC
  • kubectl describe pvc <pvc-name>: To view the details of a PVC
  • kubectl get pvc: To view the status of a PVC
  • kubectl delete -f pvc.yaml: To delete a PVC
  • kubectl get events -w: To view the events associated with a PVC
  • kubectl describe pv <pv-name>: To view the PVCs bound to a specific PV
  • kubectl get pv --show-labels: To view the PVs that match a specific PVC
  • kubectl wait --for=condition=bound pvc/<pvc-name>: To wait for a PVC to be bound
  • kubectl get pvc <pvc-name> -o yaml: To view the YAML manifest of a PVC
  • kubectl edit pvc <pvc-name>: To edit the YAML manifest of a PVC

Custom Resources:

  1. CustomResourceDefinitions (CRDs):
    • Extend the Kubernetes API with custom types of resources tailored to specific use cases and domain requirements.
    • CRDs enable users to define and manage their own custom resources and controllers within the cluster.
    • Example: kubectl get crd
  2. Custom Resources:
    • Instances of custom resource types defined by CRDs, representing domain-specific objects within the cluster.
    • Custom resources enable users to extend Kubernetes with their own application-specific resources and controllers.
    • Example: kubectl get <custom-resource>

Conclusion:

Kubernetes API Resources form the foundation of Kubernetes’ declarative management model, providing a standardized interface for interacting with the cluster. By understanding the most commonly used resources and their functionalities, users can effectively manage applications and infrastructure within Kubernetes environments, ensuring scalability, reliability, and efficiency in their deployments.

By |Last Updated: April 16th, 2024|Categories: Kubernetes|