Asterrr's Handbook

The API and kubectl

API groups and versions, object structure, declarative vs imperative management, everyday kubectl, RBAC basics, and which tool builds which kind of cluster.

Exam tasks: 1.2 (Administration: the Kubernetes API, kubectl, access control basics, cluster tooling)

The decision: how do you change the cluster (declare a file or issue a command), which API version and group does an object live in, and which tool fits the cluster you need?

Everything is an API call

  • kubectl is just a client. It reads a kubeconfig (default ~/.kube/config) that lists clusters, users (credentials) and contexts (a cluster + user + default namespace).
  • Every request passes authentication, then authorization, then admission control, before the object is stored. A rejected request fails at one of these three stages.

API groups and versions

GroupapiVersion you writeURL pathExamples
core (legacy, no name)v1/api/v1Pod, Service, ConfigMap, Secret, Namespace, Node
appsapps/v1/apis/apps/v1Deployment, StatefulSet, DaemonSet, ReplicaSet
batchbatch/v1/apis/batch/v1Job, CronJob
networking.k8s.ionetworking.k8s.io/v1/apis/networking.k8s.io/v1Ingress, NetworkPolicy
rbac.authorization.k8s.iorbac.authorization.k8s.io/v1/apis/rbac.authorization.k8s.io/v1Role, RoleBinding
autoscalingautoscaling/v2/apis/autoscaling/v2HorizontalPodAutoscaler
  • Versions mature as alpha (v1alpha1: off by default, may vanish), beta (v1beta1: well tested; new beta APIs are also off by default), and stable (v1: kept for the long term).
  • Deprecated versions are removed after a published grace period, so old manifests can stop applying after an upgrade. kubectl api-resources and kubectl api-versions show what the server supports now.

Legacy: use apps/v1, autoscaling/v2, networking.k8s.io/v1 instead

Older examples use extensions/v1beta1 or apps/v1beta2 for Deployments, autoscaling/v2beta2 for the HPA, and extensions/v1beta1 for Ingress. All of these have been removed; manifests using them are rejected.

Anatomy of an object

apiVersion: apps/v1          # group/version
kind: Deployment             # type
metadata:                    # identity: name, namespace, labels, annotations
  name: quotes-api
  namespace: shop
  labels:
    app: quotes-api
spec:                        # desired state: you write this
  replicas: 2
  selector:
    matchLabels:
      app: quotes-api
  template:
    metadata:
      labels:
        app: quotes-api
    spec:
      containers:
        - name: api
          image: registry.example.com/quotes-api:2.4.1
# status:                    # observed state: controllers write this
  • spec is what you want; status is what the system reports. Controllers work to make status match spec.
  • kubectl explain deployment.spec.strategy documents any field straight from the server.

Declarative vs imperative

StyleExampleGood for
Imperative commandkubectl create deployment quotes-api --image=nginx --replicas=2Quick one-offs, generating YAML
Imperative object configkubectl create -f quotes.yaml, kubectl replace -fFiles, but you choose the operation
Declarativekubectl apply -f manifests/Version-controlled config, GitOps, repeatable changes
  • With apply, you state the end result and Kubernetes works out create vs update. Re-running it is safe (idempotent), which is why CI/CD and GitOps tools use it.
  • kubectl create fails if the object already exists; kubectl apply updates it.
  • --dry-run=client -o yaml turns any imperative command into a starter manifest.

Exam signal

"Desired state stored in files in Git and applied repeatedly" is declarative management with kubectl apply. "Run a command that performs a specific action now" is imperative.

kubectl you should recognise

CommandDoes
kubectl get pods -n shop -o wideList, with node and IP
kubectl describe pod quotes-api-7d9fDetails plus recent events
kubectl logs quotes-api-7d9f -c apiContainer logs (--previous for the last crash)
kubectl exec -it quotes-api-7d9f -- shShell inside a running container
kubectl apply -f file.yaml / kubectl delete -f file.yamlDeclare / remove
kubectl scale deployment quotes-api --replicas=5Change replica count
kubectl rollout status / undo deployment/quotes-apiFollow or roll back a rollout
kubectl config use-context stagingSwitch cluster or user
kubectl auth can-i delete pods -n shopCheck your own permissions

RBAC in one table

ObjectScopeRole
RoleOne namespaceLists allowed verbs (get, list, create...) on resources
ClusterRoleCluster-wideSame, plus cluster-scoped resources such as nodes
RoleBindingOne namespaceGrants a Role or ClusterRole to subjects in that namespace
ClusterRoleBindingCluster-wideGrants a ClusterRole everywhere
  • Subjects are users, groups or ServiceAccounts. Kubernetes has no User object: humans come from certificates or an identity provider, while ServiceAccounts are real objects for workloads.
  • RBAC is additive only. There are no deny rules; anything not granted is refused.
  • Hands-on depth: CKA RBAC.

ClusterRole means cluster-wide access

A ClusterRole bound with a RoleBinding grants its permissions only inside that RoleBinding's namespace. The binding decides the scope. This is the usual way to reuse one ClusterRole such as view across many namespaces.

Tools for building clusters

ToolWhat it isPick it when
kubeadmOfficial tool that bootstraps a conformant cluster on machines you provide (kubeadm init, kubeadm join)You run your own nodes and want upstream Kubernetes
minikubeLocal single-node (or small) cluster in a VM or containerLearning and local development
kindKubernetes in Docker: nodes are containersFast, disposable clusters for CI and testing
k3sLightweight certified distribution in one binaryEdge, IoT, small servers
Managed (EKS, GKE, AKS)The provider runs the control planeProduction without operating the control plane

kubeadm, kubelet, kubectl

Three different binaries. kubeadm sets up the cluster, kubelet runs on each node as the node agent, and kubectl is the client you type commands into. A question about "installing a cluster" never answers with kubectl.

Scenarios

Scenario
Which apiVersion should a manifest use for a Deployment on a current Kubernetes cluster?
Scenario
A team at Quillstone keeps all manifests in Git, and a pipeline applies them after every merge. Some objects already exist and some are new. Which command fits this workflow?
Scenario
An auditor wants read-only access to Pods in the payments namespace only. An existing ClusterRole named pod-reader grants get and list on pods. What is the narrowest way to grant this?

Further reading

On this page