123DevOps··9 min read

The Kubernetes API and objects in practice: groups, versions, metadata, server-side apply, and finalizers

How the Kubernetes API is organized, the anatomy of an object (apiVersion, kind, metadata, spec, and status), labels, ownerReferences, declarative apply with server-side apply and field managers, direct calls with get --raw and kubectl proxy, finalizers, and CRDs.

Kubernetes

Everything in Kubernetes goes through the API. kubectl, the controllers, the scheduler, the kubelet, and Argo CD don’t talk to each other: they all read and write objects in the kube-apiserver, which stores state in etcd. Understanding how that API is organized, and how an object is put together, explains a lot of everyday “mysteries”, like an apply that complains about a conflict, a namespace that never finishes deleting, or a Pod that comes back on its own. Every command below was run on a kind cluster with Kubernetes 1.37, and the outputs were only shortened.

API groups and versions

Resources are organized into API groups, and each group has one or more versions. The “core” group is legacy and has no name, so it shows up as just v1. The others use the group/version format:

$ kubectl api-versions
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apps/v1
autoscaling/v1
autoscaling/v2
batch/v1
discovery.k8s.io/v1
networking.k8s.io/v1
rbac.authorization.k8s.io/v1
...
v1

To see which resources exist, whether they are namespaced, and which kind to use in YAML:

$ kubectl api-resources --api-group=apps
NAME                  SHORTNAMES   APIVERSION   NAMESPACED   KIND
controllerrevisions                apps/v1      true         ControllerRevision
daemonsets            ds           apps/v1      true         DaemonSet
deployments           deploy       apps/v1      true         Deployment
replicasets           rs           apps/v1      true         ReplicaSet
statefulsets          sts          apps/v1      true         StatefulSet

$ kubectl api-resources --namespaced=false | head -4
NAME                SHORTNAMES   APIVERSION   NAMESPACED   KIND
componentstatuses   cs           v1           false        ComponentStatus
namespaces          ns           v1           false        Namespace
nodes               no           v1           false        Node

Versions signal maturity: v1alpha1 (experimental, usually disabled by default), v1beta1 (pre-stable), and v1 (stable). When a version is removed, old manifests stop working, so check the versions before every cluster upgrade.

Group Example kinds
core (v1) Pod, Service, ConfigMap, Secret, Namespace, Node
apps/v1 Deployment, StatefulSet, DaemonSet, ReplicaSet
batch/v1 Job, CronJob
networking.k8s.io/v1 Ingress, NetworkPolicy
discovery.k8s.io/v1 EndpointSlice
rbac.authorization.k8s.io/v1 Role, ClusterRole, RoleBinding
apiextensions.k8s.io/v1 CustomResourceDefinition

kubectl explain is the API documentation inside your terminal, read from the server itself (so it always matches your cluster’s version):

$ kubectl explain deployment.spec.strategy
GROUP:      apps
KIND:       Deployment
VERSION:    v1

FIELD: strategy <DeploymentStrategy>
...
  type	<string>
  enum: Recreate, RollingUpdate
    Type of deployment. Can be "Recreate" or "RollingUpdate". Default is
    RollingUpdate.

Use --recursive to see a type’s whole field tree.

Anatomy of an object

Every persisted object has the same five-part structure. Here is a real Deployment after it was applied (excerpt):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: b-api-lab
  uid: 0fd88581-502f-4734-9bf7-56b9eb402d23
  resourceVersion: "2060"
  generation: 1
  creationTimestamp: "2026-11-25T04:36:39Z"
  labels:
    app.kubernetes.io/name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app.kubernetes.io/name: web
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
  template:
    ...
status:
  availableReplicas: 2
  observedGeneration: 1
  conditions:
  - type: Available
    status: "True"
    reason: MinimumReplicasAvailable
Field Who writes it What it’s for
apiVersion + kind you Which type and which schema version
metadata you and the server Name, namespace, labels, annotations; the server fills in uid, resourceVersion, generation
spec you The desired state
status controllers The observed state; don’t edit it by hand

Two details help a lot when troubleshooting. resourceVersion changes on every write and is used for optimistic concurrency control. generation only goes up when the spec changes, and the controller records in status.observedGeneration which generation it has already processed: if the two don’t match, the change hasn’t been reconciled yet. Notice also that the server filled in defaults you never wrote, like the RollingUpdate strategy with 25%.

Labels, annotations, namespaces, and ownerReferences

Labels and selectors

Labels are key/value pairs that identify the object and are used to select groups of objects. Services, Deployments, and NetworkPolicies find their Pods through labels. Prefer the recommended app.kubernetes.io/* keys.

$ kubectl -n b-api-lab label deploy web tier=frontend
deployment.apps/web labeled
$ kubectl get deploy -n b-api-lab -l 'tier=frontend'
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    2/2     2            2           5s
$ kubectl get cm -A -l 'app.kubernetes.io/part-of=checkout,env in (dev,stg)'
NAMESPACE   NAME         DATA   AGE
b-api-lab   app-config   2      5s

Annotations

Annotations hold non-identifying metadata: service owner, runbook link, settings read by tools. You can’t select objects by annotation.

kubectl -n b-api-lab annotate deploy web example.com/runbook="https://wiki.example.com/web"

Namespaces

Namespaces split the cluster into scopes for names, RBAC, quotas, and network policies. Every cluster starts with default, kube-system, kube-public, and kube-node-lease. Resources such as Nodes, PersistentVolumes, and ClusterRoles don’t belong to any namespace (NAMESPACED false in api-resources).

ownerReferences and garbage collection

When a controller creates an object, it writes an ownerReference pointing to the owner. That’s how the garbage collector knows what to delete in cascade:

$ kubectl -n b-api-lab get pod web-69977c9c79-jccd6 -o jsonpath='{.metadata.ownerReferences}' | jq
[
  {
    "apiVersion": "apps/v1",
    "blockOwnerDeletion": true,
    "controller": true,
    "kind": "ReplicaSet",
    "name": "web-69977c9c79",
    "uid": "13aedf7c-89ff-4c5c-9184-1f6ed427f3e4"
  }
]

The Pod belongs to the ReplicaSet, which belongs to the Deployment. Deleting the Deployment deletes the rest. With kubectl delete --cascade=orphan the dependents are left behind, and --cascade=foreground waits for the children to go away before removing the owner.

Imperative vs. declarative

The imperative style (kubectl create, scale, set image) is great for testing and for generating YAML:

kubectl create deployment web --image=nginx:1.29-alpine --replicas=2 \
  -n b-api-lab --dry-run=client -o yaml > deploy.yaml

In production, the standard is declarative: the manifest lives in Git and is applied with kubectl apply (or by a GitOps tool like Argo CD).

kubectl apply and kubectl diff

Before applying, see what will change. kubectl diff compares the manifest with the live object through a server-side dry run, and it exits with code 1 when there are differences, which is handy in pipelines:

$ kubectl diff -f deploy.yaml; echo "exit=$?"
@@ -17,7 +17,7 @@
   uid: 0fd88581-502f-4734-9bf7-56b9eb402d23
 spec:
   progressDeadlineSeconds: 600
-  replicas: 2
+  replicas: 3
   revisionHistoryLimit: 10
exit=1

Classic apply runs on the client and stores the last applied configuration in the kubectl.kubernetes.io/last-applied-configuration annotation.

Server-side apply and field managers

With --server-side, the merge is done by the apiserver, which records which manager owns each field in metadata.managedFields. If another manager already owns a field and you try to change it, you get an explicit conflict instead of a silent overwrite. In the lab, moving the Deployment from client-side apply to a field manager called platform produced exactly that:

$ kubectl apply --server-side --field-manager=platform -f deploy.yaml
error: Apply failed with 1 conflict: conflict with "kubectl-client-side-apply" using apps/v1: .spec.replicas

$ kubectl apply --server-side --field-manager=platform --force-conflicts -f deploy.yaml
deployment.apps/web serverside-applied

$ kubectl -n b-api-lab scale deploy web --replicas=4
$ kubectl -n b-api-lab get deploy web --show-managed-fields -o json \
    | jq -r '.metadata.managedFields[] | "\(.manager)\t\(.operation)\t\(.subresource // "")"'
platform                 Apply
kubectl                  Update   scale
kubectl-annotate         Update
kubectl-label            Update
kube-controller-manager  Update   status

$ kubectl apply --server-side --field-manager=platform -f deploy.yaml
error: Apply failed with 1 conflict: conflict with "kubectl" with subresource "scale" using apps/v1: .spec.replicas

That second conflict is the classic HPA case: when another agent controls replicas, the right move is to remove the field from the manifest. Once that was done, the apply went through and the 4 replicas were kept. Use --force-conflicts only when you really want to take ownership of the field.

Talking to the API directly: get –raw and kubectl proxy

kubectl get --raw makes an HTTP request authenticated with your kubeconfig and returns the raw JSON:

$ kubectl get --raw /version | jq -c '{major,minor,gitVersion}'
{"major":"1","minor":"37","gitVersion":"v1.37.0"}

$ kubectl get --raw '/readyz?verbose' | tail -2
[+]shutdown ok
readyz check passed

$ kubectl get --raw /apis/apps/v1/namespaces/b-api-lab/deployments/web \
    | jq -c '{kind, name: .metadata.name, replicas: .spec.replicas}'
{"kind":"Deployment","name":"web","replicas":4}

Paths follow a predictable pattern: /api/v1/... for the core group and /apis/<group>/<version>/namespaces/<ns>/<resource>/<name> for everything else. kubectl proxy opens an already-authenticated local proxy, so any HTTP client works:

$ kubectl proxy --port=8011 &
Starting to serve on 127.0.0.1:8011

$ curl -s http://127.0.0.1:8011/api/v1/namespaces/b-api-lab/configmaps/app-config | jq -c .data
{"FEATURE_X":"false","LOG_LEVEL":"info"}

$ curl -s -X PATCH -H 'Content-Type: application/merge-patch+json' \
    -d '{"data":{"LOG_LEVEL":"debug"}}' \
    http://127.0.0.1:8011/api/v1/namespaces/b-api-lab/configmaps/app-config | jq -c .data
{"FEATURE_X":"false","LOG_LEVEL":"debug"}

The proxy inherits all of your permissions; keep it on 127.0.0.1 only and stop it when you’re done. To understand how these requests are authenticated and authorized, see Kubernetes authentication and RBAC.

Finalizers

A finalizer is a key in metadata.finalizers that blocks the final removal of an object until some controller does its cleanup and removes the key. When it receives the delete, the apiserver only sets deletionTimestamp:

$ kubectl -n b-api-lab patch configmap protected --type=merge \
    -p '{"metadata":{"finalizers":["example.com/external-cleanup"]}}'
$ kubectl -n b-api-lab delete configmap protected --wait=false
$ kubectl -n b-api-lab get configmap protected \
    -o jsonpath='{.metadata.deletionTimestamp}  {.metadata.finalizers}'
2026-11-25T04:37:10Z  ["example.com/external-cleanup"]

$ kubectl -n b-api-lab patch configmap protected --type=json \
    -p '[{"op":"remove","path":"/metadata/finalizers"}]'
$ kubectl -n b-api-lab get configmap protected
Error from server (NotFound): configmaps "protected" not found

This is why a namespace gets stuck in Terminating: the Namespace itself has the kubernetes finalizer in spec.finalizers (check it with kubectl get ns default -o kyaml, the KYAML output that became stable in 1.37), and PVCs use kubernetes.io/pvc-protection. Removing a finalizer by hand skips the cleanup it was guarding; first find out which controller should have removed it and why it didn’t.

CRDs: extending the API

A CustomResourceDefinition adds a new type to the API, with an OpenAPI schema validated by the server. The manifest below was validated with kubectl apply --dry-run=server in the lab:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: backups.platform.example.com
spec:
  group: platform.example.com
  scope: Namespaced
  names: { plural: backups, singular: backup, kind: Backup }
  versions:
    - name: v1alpha1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              required: [schedule]
              properties:
                schedule: { type: string }
                retentionDays: { type: integer, minimum: 1 }

Once created, kubectl get backups works like any native resource, and the type shows up in api-resources. The CRD only defines the data; a controller provides the behavior (the “operator” pattern). For the path from a kubectl apply to a running Pod, see how Kubernetes works, and for a complete deployment, deploy and scale nginx.

Checklist

  • Check kubectl api-versions and api-resources before every cluster upgrade
  • Use kubectl explain instead of guessing YAML fields
  • Standardize on app.kubernetes.io/* labels and use annotations for tool metadata
  • Run kubectl diff in the pipeline before any apply
  • Adopt server-side apply with one --field-manager per tool
  • Remove replicas from the manifest when an HPA controls scaling
  • Investigate the responsible controller before removing a finalizer by hand

Once you understand spec, status, and managedFields, you read the cluster as it really works: a database of objects with owners, versions, and controllers reconciling all the time.

Get updates on new posts and stay in the loop!