Skip to content
LF Cert Trainer

Lesson 79 · Section 7: Security

Lesson 79: API groups: how the Kubernetes API is organised

Why the API is split into the core group and named groups, how apiVersion and kind become a URL, how to explore the groups with kubectl and curl, and why RBAC rules fail when the group is wrong. · 15 min read · 3 min video

1/10API groups
0:00 / 3:22

Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.

Why API groups exist

Every kubectl command, every controller and every kubelet talks to one HTTP API. In the first versions of Kubernetes that API was a single flat list: Pods, Services, nodes and everything else sat side by side under /api/v1. That stopped working as the project grew. A new feature could not be tried out without touching the one shared version, one team's change could hold up everybody else's, and there was no way for an add-on to bring types of its own.

API groups solved this by splitting the API into independent parts. Each group collects related resources, has its own versions, and can be switched on or off without affecting the others.

You need this in the exam for three things: writing the correct apiVersion in a manifest, writing apiGroups in Roles and ClusterRoles (the next lessons), and reading the errors that appear when either one is wrong.

What the API looks like

Ask the API server for its root and it lists every path it serves:

kubectl get --raw / | head -n 12
{
  "paths": [
    "/.well-known/openid-configuration",
    "/api",
    "/api/v1",
    "/apis",
    "/apis/",
    "/apis/admissionregistration.k8s.io",
    "/apis/admissionregistration.k8s.io/v1",
    "/apis/apiextensions.k8s.io",
    "/apis/apiextensions.k8s.io/v1",
    "/apis/apps",
The paths of the API serverkube-apiserverport 6443/apicore group/apisnamed groupsServer paths/version /healthzv1pods, services, nodesapps, batch, ...apps/v1: deployments/metrics /logs/livez /readyz
The paths of the API server. Resources live under two roots: /api for the core group and /apis for every named group. The other paths are about the server itself, not about objects in the cluster.

Parts:

  • kube-apiserver (port 6443)
  • /api (core group)
  • /apis (named groups)
  • Server paths (/version /healthz)
  • v1 (pods, services, nodes)
  • apps, batch, ... (apps/v1: deployments)
  • /metrics /logs (/livez /readyz)

Connections:

  • kube-apiserver to /api
  • kube-apiserver to /apis
  • kube-apiserver to Server paths
  • /api to v1
  • /apis to apps, batch, ...
  • Server paths to /metrics /logs

Only two of these roots hold cluster objects:

RootCalledContains
/apiThe core group (also "legacy" group)The original types: Pods, Services, nodes, namespaces, ConfigMaps, Secrets, PersistentVolumes and more
/apisThe named groupsEverything added later, one sub-path per group: /apis/apps, /apis/batch, /apis/networking.k8s.io and so on

The other paths are about the server, not about your workloads: /version reports the Kubernetes version, /healthz, /livez and /readyz report health, /metrics exposes Prometheus metrics, and /openapi/v3 serves the schemas that kubectl explain reads.

The core group

The core group has no name. That is why its manifests say apiVersion: v1 with nothing in front, and why an RBAC rule refers to it as the empty string "". Its resources are the ones you have used since the first lesson:

pods            services         nodes              namespaces
configmaps      secrets          serviceaccounts    endpoints
events          persistentvolumes                   persistentvolumeclaims
replicationcontrollers           resourcequotas     limitranges

New types are no longer added to the core group. It stays for compatibility, which is where the name "legacy" comes from.

The named groups

Everything else lives in a named group. These are the ones a cluster administrator meets most often:

GroupapiVersionResources you will use
appsapps/v1Deployments, ReplicaSets, StatefulSets, DaemonSets
batchbatch/v1Jobs, CronJobs
networking.k8s.ionetworking.k8s.io/v1NetworkPolicies, Ingresses, IngressClasses
rbac.authorization.k8s.iorbac.authorization.k8s.io/v1Roles, ClusterRoles and their bindings
storage.k8s.iostorage.k8s.io/v1StorageClasses, VolumeAttachments, CSIDrivers
certificates.k8s.iocertificates.k8s.io/v1CertificateSigningRequests
autoscalingautoscaling/v2HorizontalPodAutoscalers
policypolicy/v1PodDisruptionBudgets
scheduling.k8s.ioscheduling.k8s.io/v1PriorityClasses
apiextensions.k8s.ioapiextensions.k8s.io/v1CustomResourceDefinitions
gateway.networking.k8s.iogateway.networking.k8s.io/v1Gateways, HTTPRoutes (installed as custom resources)

Older groups have short names such as apps and batch. Newer ones are named like domains and end in .k8s.io. Groups that come from add-ons use the add-on's own domain, for example cert-manager.io.

Resources and verbs

Inside a group and version sit the resources, and on each resource you can perform verbs. These two words return in every RBAC rule:

VerbHTTP requestExample with kubectl
getGET on one objectkubectl get deploy web
listGET on the collectionkubectl get deploy
watchGET with ?watch=truekubectl get deploy -w
createPOSTkubectl create -f web.yaml
updatePUTkubectl replace -f web.yaml
patchPATCHkubectl patch, kubectl apply, kubectl scale
deleteDELETE on one objectkubectl delete deploy web
deletecollectionDELETE on the collectionkubectl delete deploy --all

How it works

From apiVersion and kind to a URL

A manifest never contains a URL. It contains an apiVersion and a kind, and kubectl turns the two into a path.

From apiVersion and kind to a URLapplyPOSTapplyPOSTDeploymentapiVersion: apps/v1kubectlkind → resource/apis/apps/v1…/shop/deploymentsPodapiVersion: v1kubectlkind → resource/api/v1…/shop/pods
From apiVersion and kind to a URL. apiVersion names the group and the version, kind names the type. kubectl looks up the resource name for that kind and builds the path. The core group has no name, so its apiVersion is only v1.

Parts:

  • Deployment (apiVersion: apps/v1)
  • kubectl (kind → resource)
  • /apis/apps/v1 (…/shop/deployments)
  • Pod (apiVersion: v1)
  • kubectl (kind → resource)
  • /api/v1 (…/shop/pods)

Connections:

  • Deployment to kubectl: apply
  • kubectl to /apis/apps/v1: POST
  • Pod to kubectl: apply
  • kubectl to /api/v1: POST

The paths follow a fixed pattern:

WhatPath
Core group, namespaced/api/v1/namespaces/<namespace>/<resource>/<name>
Core group, cluster-wide/api/v1/<resource>/<name>
Named group, namespaced/apis/<group>/<version>/namespaces/<namespace>/<resource>/<name>
Named group, cluster-wide/apis/<group>/<version>/<resource>/<name>

So these pairs are the same request:

kubectl get pods -n shop
kubectl get --raw /api/v1/namespaces/shop/pods

kubectl get deployment web -n shop
kubectl get --raw /apis/apps/v1/namespaces/shop/deployments/web

kubectl get nodes
kubectl get --raw /api/v1/nodes

kubectl get clusterroles
kubectl get --raw /apis/rbac.authorization.k8s.io/v1/clusterroles

Two details in those paths are easy to miss. The path uses the resource name, which is plural and lower case (deployments), not the kind from the manifest (Deployment). And some resources have subresources behind one more path segment, such as pods/log and deployments/scale:

kubectl get --raw /api/v1/namespaces/shop/pods/db/log        # what kubectl logs db reads

To watch kubectl build these paths, raise its verbosity. Level 6 prints each URL it calls:

kubectl get deploy -n shop -v=6 2>&1 | grep GET
GET https://controlplane:6443/apis/apps/v1/namespaces/shop/deployments?limit=500 200 OK

Discovery: how kubectl knows the groups

kubectl has no built-in list of resources. It asks the server. The answers to /api and /apis describe every group, every version and every resource in it, with its kind, its short names, its verbs and whether it is namespaced. This is called discovery, and it is what these commands print:

kubectl api-resources          # every resource, with its group and version
kubectl api-versions           # every group/version the server offers

Discovery is why a CustomResourceDefinition you install becomes usable with kubectl get a moment later, without upgrading kubectl. The results are cached under ~/.kube/cache/discovery for a few minutes, which is why a brand new custom resource can be "not found" on the first attempt and found on the second.

Versions inside a group

Each group is versioned on its own. A version name tells you how stable it is:

VersionExampleMeaning
Alphav1alpha1Experimental. Off by default, may change or disappear without notice
Betav1beta1Well tested, but can still change. New beta APIs are off by default
Stablev1, v2Generally available. Stays for many releases

A group can serve several versions at once, for example autoscaling/v1 and autoscaling/v2. They are different views of the same stored objects: an HPA created as autoscaling/v2 can be read as autoscaling/v1, and the API server converts between them. One version per group is the preferred one, which kubectl uses when you do not say otherwise.

Versions are switched on and off with a flag of the API server, set in its static Pod manifest /etc/kubernetes/manifests/kube-apiserver.yaml:

- --runtime-config=batch/v1=false                       # switch a group/version off
- --runtime-config=api/all=true                         # switch everything on, alphas included
- --runtime-config=admissionregistration.k8s.io/v1beta1 # switch one version on (=true is implied)

Several entries are separated by commas in a single flag. The kubelet restarts the API server when you save the file, and after that kubectl api-versions shows the difference.

Under the hood · Where a group can come from

One /apis listing hides three different sources:

  • Built-in groups are compiled into kube-apiserver: apps, batch, rbac.authorization.k8s.io and the rest.
  • CustomResourceDefinitions add groups without any extra server. When you install a CRD for the group gateway.networking.k8s.io, the API server starts serving /apis/gateway.networking.k8s.io/v1 itself and stores the objects in etcd. The Custom Resource Definition lesson covers this.
  • Aggregated APIs are served by another program. The API server forwards a whole group to it. The metrics server is the usual example: /apis/metrics.k8s.io is answered by the metrics-server Pod.

kubectl get apiservices shows every group and version with its source. Local means built-in or CRD; a Service name means aggregated:

NAME                           SERVICE                      AVAILABLE   AGE
v1.                            Local                        True        41d
v1.apps                        Local                        True        41d
v1.batch                       Local                        True        41d
v1beta1.metrics.k8s.io         kube-system/metrics-server   True        41d

The first line, v1. with nothing after the dot, is the core group. If AVAILABLE is False for an aggregated group, commands that depend on it fail: a broken v1beta1.metrics.k8s.io is the reason behind kubectl top answering Metrics API not available.

Why groups matter for access

Authorization decides on the attributes of a request: who is asking, which verb, which API group, which resource, which namespace. An RBAC rule matches only when its group matches too.

A rule only matches inside its API grouplistno matchallowedjanekubectl get deployRequestapps / deploymentsapiGroups: [""]deployments: none hereapiGroups: ["apps"]deployments: allowed
A rule only matches inside its API group. Authorization compares the group of the request with the apiGroups of each rule. A rule for deployments in the core group matches nothing, because no deployments exist there.

Parts:

  • jane (kubectl get deploy)
  • Request (apps / deployments)
  • apiGroups: [""] (deployments: none here)
  • apiGroups: ["apps"] (deployments: allowed)

Connections:

  • jane to Request: list
  • Request to apiGroups: [""]: no match
  • Request to apiGroups: ["apps"]: allowed
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: app-reader
  namespace: shop
rules:
  - apiGroups: [""]              # core group: the empty string
    resources: ["pods", "services"]
    verbs: ["get", "list", "watch"]
  - apiGroups: ["apps"]          # group name only, no version
    resources: ["deployments"]
    verbs: ["get", "list", "watch"]

Three rules of thumb cover almost every mistake:

  • The core group is "". Not "core", not "v1".
  • A named group is written without its version: "apps", not "apps/v1".
  • A rule that lists deployments under apiGroups: [""] is accepted by the API server and then matches nothing. There is no warning, because RBAC does not check that the resource exists in that group.

The error tells you what is missing. Read it word by word:

Error from server (Forbidden): deployments.apps is forbidden: User "jane" cannot list resource "deployments"
in API group "apps" in the namespace "shop"

Verb list, resource deployments, group apps, namespace shop: exactly the four values the rule needs. For a core resource the message says in API group "".

Using it

Example · Find the group, version and scope of any resource

A task mentions "a NetworkPolicy" and you need its apiVersion:

kubectl api-resources | grep -i networkpol
networkpolicies   netpol   networking.k8s.io/v1   true   NetworkPolicy

The columns are name, short name, apiVersion, namespaced, kind. One line answers four questions: the manifest starts with apiVersion: networking.k8s.io/v1, the short name is netpol, it lives in a namespace, and an RBAC rule would use apiGroups: ["networking.k8s.io"] with resources: ["networkpolicies"].

Useful filters:

kubectl api-resources --api-group=apps          # one named group
kubectl api-resources --api-group=""            # only the core group
kubectl api-resources --namespaced=false        # cluster-wide resources
kubectl api-resources --verbs=list -o name      # names only, for scripts
kubectl api-resources -o wide                   # adds the verbs each resource supports

Example · Explore the API with kubectl proxy and curl

kubectl get --raw is the quickest way to read a path. To use curl, or to give a browser or script access, start a local proxy. It listens on your machine only and adds the credentials from your kubeconfig:

kubectl proxy --port=8001 &
curl -s localhost:8001/version
curl -s localhost:8001/apis | grep '"name"'
curl -s localhost:8001/apis/apps/v1 | grep '"name"'
      "name": "controllerrevisions",
      "name": "daemonsets",
      "name": "daemonsets/status",
      "name": "deployments",
      "name": "deployments/scale",
      "name": "deployments/status",

The names with a slash are the subresources. Stop the proxy with kill %1 when you are done.

Without the proxy you must authenticate yourself. Certificates work, and so does a ServiceAccount token:

curl https://controlplane:6443/apis --cacert /etc/kubernetes/pki/ca.crt \
  --cert admin.crt --key admin.key

TOKEN=$(kubectl create token default)
curl -s --cacert /etc/kubernetes/pki/ca.crt -H "Authorization: Bearer $TOKEN" \
  https://controlplane:6443/apis

With no credentials at all, a kubeadm cluster still answers /version and the health paths, and refuses the rest with 403 Forbidden for the user system:anonymous.

Example · Give a user read access to Deployments and Pods

Jane must list Pods and Deployments in the namespace shop. Let kubectl write the rules, one command per rule, and read what it produced:

kubectl create role app-reader -n shop --verb=get,list,watch \
  --resource=pods,deployments --dry-run=client -o yaml
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs: [get, list, watch]
- apiGroups:
  - apps
  resources:
  - deployments
  verbs: [get, list, watch]

kubectl looked up each resource and split them into one rule per group. Create the Role and its binding, then test as Jane:

kubectl create role app-reader -n shop --verb=get,list,watch --resource=pods,deployments
kubectl create rolebinding app-reader -n shop --role=app-reader --user=jane
kubectl auth can-i list deployments -n shop --as jane        # yes
kubectl auth can-i list deployments.apps -n shop --as jane   # yes, the same question, group spelled out
kubectl auth can-i list jobs -n shop --as jane               # no: batch is another group

Example · Fix a Role that was written with the wrong group

Jane has a Role but kubectl get deploy -n shop still answers Forbidden. Look at the rule:

kubectl get role app-reader -n shop -o yaml | grep -A6 rules
rules:
- apiGroups:
  - ""
  resources:
  - deployments
  verbs: [get, list]

The resource is right and the group is wrong: Deployments are in apps. Confirm, then correct the Role:

kubectl api-resources | grep -w deployments     # apps/v1
kubectl edit role app-reader -n shop            # change "" to apps
kubectl auth can-i list deployments -n shop --as jane

Tricks

Common mistakes

  • Writing apiVersion: v1 for a Deployment, or apps/v1 for a Pod. The error is no matches for kind "Deployment" in version "v1". Core types use v1; everything else needs its group in front.
  • Putting the version into an RBAC rule. apiGroups: ["apps/v1"] is accepted and matches nothing. Rules take the group name only.
  • Using "core" or leaving apiGroups out for Pods. The core group is the empty string, and the field is required for resource rules.
  • Listing resources from two groups under one group. apiGroups: [""] with resources: ["pods", "deployments"] grants access to Pods only.
  • Using the kind instead of the resource. Rules and paths take deployments, not Deployment.
  • Calling the API with curl and no credentials. Everything except /version and the health paths answers 403. Use kubectl proxy, kubectl get --raw, a client certificate or a token.

Recap

  • The API is split into the core group at /api/v1 (apiVersion: v1) and named groups at /apis/<group>/<version> (apiVersion: <group>/<version>).
  • A resource is addressed by group, version and resource name; kubectl builds the URL from apiVersion and kind using discovery.
  • kubectl api-resources, kubectl api-versions and kubectl explain tell you the group of anything.
  • In RBAC rules the core group is "" and named groups are written without a version. A wrong group fails silently until someone gets Forbidden.
  • kubectl proxy and kubectl get --raw let you read the API paths with your own credentials.

Official docs

Quiz: check what you learned

5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.

Sign in to take the quiz and keep your score.

Practise: try it on a real cluster

A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.

  • Find the group, version and scope of built-in and custom resources, then repair a Role that was written with the wrong API group.

    Investigate · Level 2 · standard · target 15 min

Discussion

Learners ask and answer questions here. Sign in to read and join the discussion.