Sub topic 1
Kubernetes Building Blocks You Will Reuse on EKS
Prerequisites
This chapter assumes you have already met these ideas:
- Basic Kubernetes concepts: pods, deployments, services and namespaces.
- Comfort with
kubectland YAML manifests. - Familiarity with core AWS services: IAM, VPC and EC2.
- Command line and AWS CLI basics.
You do not need a running cluster yet. This chapter is a recap that points out where each object touches AWS. Later chapters build on it.
Why a recap belongs in an EKS tutorial
Amazon EKS runs standard, upstream-compatible Kubernetes. A manifest that works on another conformant cluster usually works on EKS too. What changes is what sits behind each object. A Service of type LoadBalancer becomes an AWS load balancer. A PersistentVolumeClaim becomes an EBS volume. A ServiceAccount can become an IAM identity. Knowing the generic object well makes the AWS-specific parts easier to follow.
The reconciliation model
Every Kubernetes object has a spec, which is the state you want, and a status, which is the state observed. Controllers compare the two and act to close the gap. You declare "three replicas" and a controller creates or deletes pods until three are running.
On EKS, AWS operates the API server, etcd and the built-in controllers in the control plane. Controllers you install yourself, such as the AWS Load Balancer Controller, run as pods on your nodes. They follow the same loop. They watch Kubernetes objects and call AWS APIs to make the outside world match.
Pods, and why resource requests matter
A pod is the smallest deployable unit. It holds one or more containers that share a network namespace and can share volumes. Pods are disposable. Their IPs change when they are replaced, so you rarely create bare pods.
Two settings in the container spec drive scheduling:
- Requests are the amount of CPU and memory the scheduler reserves for the container. A pod is placed only on a node with enough unreserved capacity.
- Limits are the maximum the container may use. Exceeding the memory limit gets the container killed (OOMKilled). Exceeding the CPU limit throttles it.
Requests matter more on EKS than they may seem. Node autoscalers such as the Cluster Autoscaler and Karpenter add nodes when pods cannot be scheduled, and they decide based on requests. Pods with no requests look free to the scheduler, so nodes get overpacked and you do not get the scaling you expect. Cost tools also use requests to attribute spend.
Deployments and ReplicaSets
A Deployment manages stateless pods. It creates a ReplicaSet, which keeps a fixed number of identical pods running. When you change the pod template, the Deployment creates a new ReplicaSet and shifts pods over gradually. This is a rolling update. You can inspect it and undo it with kubectl rollout status and kubectl rollout undo.
Other workload controllers cover other needs:
| Controller | Use it for | Typical EKS example |
|---|---|---|
| Deployment | Stateless apps with interchangeable replicas | Web front ends, APIs |
| StatefulSet | Stable identity and per-replica storage | Databases, Kafka |
| DaemonSet | One pod on every node (or a subset) | Log agents, the VPC CNI, node monitoring |
| Job and CronJob | Run-to-completion and scheduled work | Batch jobs, backups |
DaemonSets deserve a note. Several components that EKS installs on every node, such as aws-node (the VPC CNI) and kube-proxy, are DaemonSets in the kube-system namespace.
Labels, selectors and Services
Labels are key-value pairs on objects. Selectors query them. This is how Kubernetes connects things without hard-coded names. A Deployment finds its pods by label. A Service finds its endpoints by label.
A Service gives a stable virtual IP and DNS name to a changing set of pods. The three types you will use most:
- ClusterIP is reachable only inside the cluster. It is the default.
- NodePort opens a port on every node. It is rarely used directly.
- LoadBalancer asks the cloud provider for an external load balancer. On EKS this creates an AWS load balancer, and the exact behavior depends on which controller is installed. The Exposing Services chapter covers this.
An Ingress is a separate object that routes HTTP traffic by host and path to Services. An Ingress needs a controller to act on it. EKS does not ship one by default.
Namespaces
Namespaces partition a cluster into logical areas. Most objects are namespaced, so two teams can each have a Service named api in separate namespaces. Some objects are cluster-scoped, including Nodes, PersistentVolumes, StorageClasses and Namespaces themselves.
Namespaces are also the scope for RBAC roles, resource quotas, network policies and service accounts. On EKS, IAM role bindings for pods are tied to a namespace and service account pair. Plan your namespaces early, because moving workloads later is disruptive.
ConfigMaps and Secrets
A ConfigMap holds non-sensitive configuration. A Secret holds sensitive values. Both can be injected as environment variables or mounted as files. Secrets are only base64-encoded by default, not encrypted in the API. EKS supports envelope encryption of Secrets with an AWS KMS key. The Hardening chapter covers that setup.
ServiceAccounts
A ServiceAccount is the identity a pod uses when it talks to the Kubernetes API. Every namespace has a default one. EKS also uses service accounts to give pods AWS permissions. IAM roles for service accounts (IRSA) and EKS Pod Identity both attach an IAM role to a service account. Create a dedicated service account per application. Do not share default.
Persistent storage objects
Containers lose their filesystem when they restart, so durable data needs three objects:
- A PersistentVolume (PV) is a piece of storage in the cluster.
- A PersistentVolumeClaim (PVC) is a pod's request for storage of a given size and access mode.
- A StorageClass describes how to create volumes dynamically. When a PVC names a StorageClass, a CSI driver creates the PV on demand.
On EKS the CSI drivers for EBS and EFS back these objects. An EBS volume lives in a single Availability Zone, so scheduling matters for stateful pods. The Persistent Volumes chapter covers this in detail.
A manifest that uses these pieces
This manifest creates a namespace, a ConfigMap, a service account and a Deployment with requests, limits and probes. Every later chapter adds to this pattern.
apiVersion: v1
kind: Namespace
metadata:
name: orders
---
apiVersion: v1
kind: ConfigMap
metadata:
name: orders-config
namespace: orders
data:
LOG_LEVEL: info
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: orders-sa
namespace: orders
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: orders
namespace: orders
spec:
replicas: 2
selector:
matchLabels:
app: orders
template:
metadata:
labels:
app: orders
spec:
serviceAccountName: orders-sa
containers:
- name: orders
image: nginx:stable
ports:
- containerPort: 80
envFrom:
- configMapRef:
name: orders-config
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
memory: 256Mi
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 3
periodSeconds: 5
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 10
periodSeconds: 10The manifest sets a memory limit but no CPU limit. That is a common choice, because CPU throttling can add latency while a memory overrun is a hard failure. Follow your team's policy if it differs.
Apply it and check the result:
kubectl apply -f orders.yaml
kubectl -n orders rollout status deployment/orders
kubectl -n orders get pods -o wide
kubectl -n orders describe deployment ordersThe -o wide output shows the node each pod landed on and its pod IP. On EKS that IP comes from your VPC subnet, which the next chapters explain.
Probes and traffic
A readiness probe decides whether a pod receives Service traffic. A failing pod is removed from the endpoints but not restarted. A liveness probe decides whether the container should be restarted. Behind an AWS load balancer, readiness has an extra role. Rolling updates use it to avoid sending traffic to pods that are not ready.
Common errors
| Symptom | Likely cause | First command to run |
|---|---|---|
| Pod stays `Pending` | No node has enough unreserved CPU or memory, or a volume is in another zone | `kubectl describe pod` and read Events |
| `ImagePullBackOff` | Wrong image name or tag, or the node cannot reach the registry | `kubectl describe pod` |
| `CrashLoopBackOff` | The container exits repeatedly | `kubectl logs --previous` |
| `OOMKilled` | Memory use exceeded the limit | `kubectl describe pod`, check Last State |
| Service has no endpoints | Selector does not match pod labels, or pods are not ready | `kubectl get endpoints` |
Exercises
Exercise
A Service named api exists, but kubectl get endpoints api shows no addresses, and the pods in the Deployment are Running. Name two things to check.
Show answer
First, check that the Service spec.selector exactly matches the labels in the Deployment's pod template (not just the Deployment's own metadata labels). Second, check that the pods pass their readiness probe, since pods that are not ready are left out of the endpoints. Also confirm the Service and pods are in the same namespace.
Exercise
Why can pods with no CPU or memory requests cause problems for node autoscaling on EKS?
Show answer
The scheduler and autoscalers such as the Cluster Autoscaler and Karpenter use requests to decide whether a pod fits and whether new nodes are needed. Pods without requests appear to need nothing, so they are packed onto nodes until the nodes are overloaded. The autoscaler then sees no unschedulable pods and adds no capacity.