# Advanced: inside the cluster, CRDs and many clusters

Under the hood: what the control plane and nodes run, how EKS splits the work, a CustomResourceDefinition with validation, what operators add, and when to run more than one cluster.

Course: [Kubernetes & Helm](https://devops.rajeev.pro/courses/kubernetes), lesson 15. About 30 minutes, intermediate. Source: https://devops.rajeev.pro/courses/kubernetes/15-advanced-topics

## What you will learn

- Name the control plane components and check their health
- Explain what runs on every worker node and why a node fits only so many Pods
- Create a CustomResourceDefinition with validation and explain what an operator adds
- Decide when to use more than one cluster, and how to manage them

> **Lab status.** **Verified lab:** control plane health, node capacity, the system DaemonSets, and a CRD with schema validation (including a real ordering error), on the course's EKS cluster. The output shown is real. <br />**Code walkthrough:** writing an operator, and multi-cluster tooling (explained). <br />**Cloud lab:** the control plane checks show EKS specifics; CRDs work the same on kind.

You've used Kubernetes for fourteen lessons. This one opens it up: what the machines actually run, how you add
your own resource types, and what changes when one cluster isn't enough.

## The control plane

The control plane is the cluster's brain. Its parts:

| Component                    | Job                                                                                     |
| ---------------------------- | --------------------------------------------------------------------------------------- |
| **kube-apiserver**           | The front door. Every `kubectl` command and every controller talks only to it.          |
| **etcd**                     | The database holding every object's desired and current state.                          |
| **kube-scheduler**           | Picks a node for each new Pod (lesson 11).                                              |
| **kube-controller-manager**  | Runs the loops that make reality match the spec: Deployments, ReplicaSets, nodes, jobs. |
| **cloud-controller-manager** | Talks to the cloud: nodes, load balancers, routes.                                      |

> **Figure.** The official Kubernetes components diagram. A Kubernetes cluster contains a control plane with the API server in the middle, connected to etcd, the scheduler, the controller manager and the optional cloud controller manager, which talks to the cloud provider API. Two nodes each run a kubelet and kube-proxy, and both connect only to the API server.

On EKS, AWS runs all of this across several availability zones, and you never see the machines:

```text
$ aws eks describe-cluster --name academy --query "cluster.{version:version,platform:platformVersion,status:status,endpointPublic:resourcesVpcConfig.endpointPublicAccess}" --output table
| endpointPublic  | platform  | status  |  version  |
|  True           |  eks.7    |  ACTIVE |  1.37     |
$ kubectl get --raw='/readyz?verbose'
[+]ping ok
[+]log ok
[+]etcd ok
[+]etcd-readiness ok
[+]kms-providers ok
[+]informer-sync ok
...
[+]shutdown ok
readyz check passed
```

`/readyz` is the API server reporting its own health, including its connection to etcd. That's the extent of
your view into a managed control plane, and the point of paying $0.10 an hour for it: upgrades, backups of etcd
and high availability are AWS's job. On a self-managed cluster (kubeadm), these run as Pods on your control
plane nodes and backing up etcd is your job.

This lab's endpoint was public, reachable from the internet, protected by IAM authentication. Production
clusters usually make it private or restrict it to known IP ranges.

## Worker nodes

Every worker node runs the **kubelet** (starts containers and reports status), a **container runtime**
(containerd), and **kube-proxy** (Service networking, lesson 5). On EKS, a few more agents run on every node as
DaemonSets:

```text
$ kubectl get daemonsets -n kube-system
NAME                     DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR              AGE
aws-node                 3         3         3       3            3           <none>                     61m
ebs-csi-node             3         3         3       3            3           kubernetes.io/os=linux     58m
ebs-csi-node-windows     0         0         0       0            0           kubernetes.io/os=windows   58m
eks-pod-identity-agent   3         3         3       3            3           <none>                     61m
kube-proxy               3         3         3       3            3           <none>                     58m
```

`aws-node` is the VPC CNI, which gives every Pod a real VPC IP address; `ebs-csi-node` mounts EBS disks
(lesson 12); `eks-pod-identity-agent` hands Pods their IAM credentials. Three nodes, three of each: a DaemonSet
puts one Pod on every node that matches, including new ones the autoscaler adds.

Put the two halves together and you can follow one `kubectl apply` through the whole cluster:

> **Figure.** kubectl apply, step by step. 1: kubectl sends a Deployment to kube-apiserver, the front door, which authenticates and validates it. 2: it is stored in etcd. 3: the controller manager sees it and creates a ReplicaSet, which creates Pods. 4: the scheduler picks a node for each Pod. 5: on that node, ip-10-42-0-115 or ip-10-42-28-149, the kubelet sees the Pod and containerd starts its containers. 6: the kubelet reports status back to the API server, and kube-proxy programs Service rules. The control plane is run by AWS on EKS; each worker node also runs the EKS DaemonSets aws-node, ebs-csi-node and eks-pod-identity-agent. Scheduler and controllers never talk to each other or to etcd; they watch the API server.

What a node can hold:

```text
$ kubectl describe node ip-10-42-0-115.ap-south-1.compute.internal
Capacity:
  cpu:                2
  memory:             3927880Ki
  pods:               17
Allocatable:
  cpu:                1930m
  memory:             3372872Ki
  pods:               17
System Info:
  Container Runtime Version:  containerd://2.3.7+unknown
  Kubelet Version:            v1.37.1-eks-6e7947a
```

(trimmed). **Allocatable** is capacity minus what's reserved for the kubelet and the OS: 70m of CPU and about
540Mi of memory here. The scheduler only hands out Allocatable, which is why the 600m Pods in lesson 10 fit three
per node, not more. And `pods: 17`: with the VPC CNI each Pod takes an IP from the node's network interfaces,
and a t3.medium has room for 17. Small instances run out of Pod slots before they run out of CPU, a common
surprise on EKS. (Prefix delegation on the VPC CNI raises the limit.)

## CRDs and operators

The API has about 88 built-in and add-on resource types (`kubectl api-resources | wc -l` printed 88, header
included). A **CustomResourceDefinition** adds your own. This one defines a `Website`:

```yaml
# 15-crd.yaml (CRD part)
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: websites.academy.example.com
spec:
  group: academy.example.com
  scope: Namespaced
  names:
    plural: websites
    singular: website
    kind: Website
    shortNames: ['ws']
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              required: ['domain', 'replicas']
              properties:
                domain:
                  type: string
                replicas:
                  type: integer
                  minimum: 1
                  maximum: 10
```

The same file also held a `Website` named `shop`. Applying both at once:

```text
$ kubectl apply -f 15-crd.yaml
customresourcedefinition.apiextensions.k8s.io/websites.academy.example.com created
error: resource mapping not found for name: "shop" namespace: "" from "15-crd.yaml": no matches for kind "Website" in version "academy.example.com/v1"
ensure CRDs are installed first
$ kubectl get crd websites.academy.example.com
NAME                           SCOPE        VERSIONS      CREATED AT
websites.academy.example.com   Namespaced   v1(storage)   2026-11-09T06:49:50Z
```

A real gotcha: the CRD was created, but the API server hadn't started serving the new kind when kubectl sent
the `Website` a moment later. Apply CRDs first, wait (`kubectl wait --for=condition=Established crd/...`), then
apply the resources. Helm keeps CRDs in a separate `crds/` folder and Argo CD has sync waves for the same reason.

The schema is enforced by the API server like any built-in type:

```text
$ kubectl apply -f bad-ws.yaml     # replicas: 50
The Website "too-big" is invalid: spec.replicas: Invalid value: 50: spec.replicas in body should be less than or equal to 10
```

The cluster already had CRDs from add-ons, such as the load balancer controller's
`targetgroupbindings.elbv2.k8s.aws`. That's the pattern: a CRD alone only **stores** objects. An **operator**
is a controller, code running in the cluster, that watches those objects and makes the world match them. The
load balancer controller watches Ingresses and TargetGroupBindings and creates ALBs. A database operator
(CloudNativePG, for example) watches a `Cluster` object and creates StatefulSets, backups and failover. Operators
are usually written in Go with Kubebuilder or Operator SDK; you'll use far more of them than you write.

> **Figure.** Animation of a reconcile loop. Left: the Website shop stored through the API server, domain shop.example.com, its replicas edited from 2 to 3. Middle: an operator gets a watch event, compares spec with reality, want 3 have 2, and acts by scaling. Right: the Deployment shop gains a third Pod, replicas 3. The operator then sees want 3, have 3, and waits for the next change. The course CRD has no operator; this shows what one would add.

## More than one cluster

One cluster is simplest. Teams add more when they need:

- **Isolation of environments**: separate clusters for dev, staging and production, so a bad change in dev
  can't touch production. This is the most common reason.
- **Blast radius and upgrades**: a broken cluster upgrade takes down one cluster, not everything.
- **Regions**: serve users from nearby, or survive a region outage.
- **Hard tenancy**: teams or customers who must not share nodes or a control plane.

> **Figure.** An example multi-cluster layout. A Git repository with an Argo CD ApplicationSet deploys one app definition to four clusters, each with its own values file: dev and staging in ap-south-1, prod-mumbai in ap-south-1 serving users, and prod-hyderabad in ap-south-2 to survive a region outage. A Terraform module creates all four identically. On the left, the laptop kubeconfig has one context per cluster, with prod-mumbai current, and a warning to check kubectl config current-context before deleting anything.

The cost is more to run: upgrades, add-ons and policies times N. The tools that make it manageable are the ones
from this course: Terraform modules to create identical clusters (lesson 3), Helm charts and GitOps (Argo CD
`ApplicationSet` deploys one app to many clusters), and kubeconfig **contexts** to switch between them
(`kubectl config get-contexts`, `kubectl config use-context`). Always check which context you're in before a
destructive command; `kubectl` happily deletes in production if that's where it points.

> **Where to go next.** You now have the whole path: containers, a cluster, workloads, networking, config, resources, security, troubleshooting, autoscaling, scheduling, storage, Helm and releases. Build the capstone from these pieces on kind, or on EKS if you're ready to pay for a few hours, and destroy it when you're done.

## Mission: Look inside and extend the API

Record the control plane's readyz result, one node's Capacity and Allocatable, and the kube-system DaemonSets. Then apply the Website CRD on its own, wait for it, create a valid Website and try one with replicas 50.

- You need: 15-crd.yaml
- You'll have: ~/advanced.txt

```sh
$ kubectl get --raw='/readyz?verbose' | tail -1 > ~/advanced.txt
$ kubectl describe node $(kubectl get nodes -o name | head -1 | cut -d/ -f2) | sed -n '/^Capacity:/,/^System Info:/p' >> ~/advanced.txt
$ kubectl get daemonsets -n kube-system >> ~/advanced.txt
$ kubectl apply -f 15-crd.yaml 2>&1 | tee -a ~/advanced.txt
$ kubectl wait --for=condition=Established crd/websites.academy.example.com
$ kubectl apply -f 15-crd.yaml && kubectl get websites >> ~/advanced.txt
$ kubectl create -f - <<'EOF' 2>> ~/advanced.txt
apiVersion: academy.example.com/v1
kind: Website
metadata: { name: too-big }
spec: { domain: big.example.com, replicas: 50 }
EOF
$ kubectl delete crd websites.academy.example.com
```

The first apply should create the CRD and fail on the Website; the second should succeed. Deleting the CRD
deletes every Website with it.

## Try it

- Run `kubectl get --raw='/readyz?verbose'` and find the etcd check.
- Compare Capacity and Allocatable in `kubectl describe node`.
- List the DaemonSets in kube-system and count their Pods.
- Apply the CRD, then a Website with replicas 50, and read the error.
- Do the mini mission and keep `~/advanced.txt`.

## Quiz

1. On EKS, who runs etcd and the API server?
   - You, on the worker nodes
   - **AWS, in the managed control plane; you see only the API endpoint** (answer)
   - Nobody, EKS doesn't use etcd
   Why: You can check health through /readyz, but you can't see or SSH to those machines.

2. You apply a file with a CRD and a resource of that new kind together, and the resource fails with "ensure CRDs are installed first". Why?
   - The CRD is invalid
   - **The API server hadn't registered the new kind yet when the second object was sent; apply the CRD first, wait, then the resources** (answer)
   - Custom resources can't be applied with kubectl
   Why: That exact error happened in the lab. Tools like Helm install CRDs in a separate step for this reason.

3. A CRD alone stores Website objects. What does an operator add?
   - Nothing
   - **A controller that watches those objects and creates or changes real resources to match them** (answer)
   - A database
   Why: CRD = new noun in the API; operator = the code that acts on it, like the Deployment controller does for Deployments.

## Resources

- [Kubernetes components](https://kubernetes.io/docs/concepts/overview/components/) (Kubernetes)
- [Custom resources](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/) (Kubernetes)
- [Operator pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/) (Kubernetes)
- [Amazon EKS architecture](https://docs.aws.amazon.com/eks/latest/userguide/eks-architecture.html) (AWS Docs)
- [EKS best practices guide](https://docs.aws.amazon.com/eks/latest/best-practices/introduction.html) (AWS Docs)
