KUBERNETES · KCNA · CHAPTER 2

Kubernetes Resources

Pods, ReplicaSets, Deployments, Services and namespaces: the objects you work with every day in Kubernetes.

11 min readKCNA domain: Kubernetes Fundamentals (44%)Level: beginner

From point A to point B

Point A: you know how a Kubernetes cluster is organized, but you haven't created anything inside it yet. Point B: by the end of this chapter you'll write a pod manifest, keep several replicas running with a ReplicaSet, update an application with no downtime using a Deployment, expose it with a Service and organize it into namespaces.

These objects belong to the Kubernetes Fundamentals domain, the heaviest in the KCNA exam (44%). They're also the first thing you'll use in a real cluster.

Pods: the smallest unit

Kubernetes doesn't run bare containers: it wraps them in pods. A pod is the smallest unit you can deploy. Its containers share the same IP, the same volumes and the same lifecycle.

The norm is one container per pod. If you need more instances of your application, you create more pods, not more containers inside the same pod. A pod only carries a second container when it helps the main one: a service mesh proxy, a log agent or a process that prepares data. That helper is called a sidecar.

The four fields of every object

Every Kubernetes definition file has the same four top-level fields:

FieldWhat it's forExample in a pod
apiVersionAPI version that defines the objectv1
kindType of objectPod
metadataName, labels and annotationsname: web
specThe desired state: what you want to existthe list of containers
apiVersion: v1
kind: Pod
metadata:
  name: web
  labels:
    app: web
    tier: front-end
spec:
  containers:
    - name: nginx
      image: nginx:1.27-alpine
      ports:
        - containerPort: 80

Look at the labels (app: web, tier: front-end). They're free-form key-value pairs, but they're not decoration: ReplicaSets, Deployments and Services use them to find their pods.

Commands to get started

kubectl run nginx --image=nginx
kubectl apply -f pod.yaml
kubectl get pods
kubectl get pods -o wide
kubectl get pods -A
kubectl describe pod web
kubectl delete pod web

-o wide adds the pod IP and the node it runs on; -A shows pods from every namespace. A trick that helps both in the exam and day to day: let kubectl write the YAML for you.

kubectl run web --image=nginx --dry-run=client -o yaml > pod.yaml
Watch out. A pod you create by hand doesn't come back on its own. If its node fails, the pod is gone. That's what the controllers below are for.

ReplicaSets: always the right number

A ReplicaSet is a controller that makes sure a specific number of identical pods is always running. If a pod dies, it creates another; if there are too many, it removes the extras. That gives you high availability and lets you spread the load across several nodes.

The predecessor: ReplicationController

The first version of this idea was the ReplicationController. It still exists, but it's legacy: today you use the ReplicaSet, which does the same job and also supports more expressive selectors.

apiVersion: v1
kind: ReplicationController
metadata:
  name: web-rc
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.27-alpine

The ReplicaSet and its selector

The big difference is the selector field, which is required in a ReplicaSet. It tells the controller which pods belong to it by comparing labels. The labels in matchLabels must match the ones in the pod template.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: web-rs
  labels:
    app: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.27-alpine
          ports:
            - containerPort: 80

The ReplicaSet watches any pod with those labels, even if it didn't create it. If pods with app: web already exist, it counts them. And if you create an extra pod by hand with the same label when there are already three, it deletes it to get back to the desired number.

kubectl apply -f replicaset.yaml
kubectl get replicasets
kubectl describe rs web-rs
kubectl scale rs web-rs --replicas=5
kubectl delete pod <pod-name>
kubectl get pods

Scaling

You have two options. You can change replicas in the file and apply it again, or use kubectl scale. Keep in mind that kubectl scale changes the object in the cluster but not your YAML file: if you apply the file later, you go back to the number in the file.

Deployments: the usual way to deploy

In practice you almost never create ReplicaSets by hand. You create a Deployment, a higher-level object that creates and manages the ReplicaSet for you. The YAML is almost the same: the kind changes and a strategy section appears.

Deployment, ReplicaSet and Pods A Deployment manages a ReplicaSet, and the ReplicaSet keeps three pods with the label app: web running. The selector matches the pod labels. Deployment · web strategy · rollout history · rollback ReplicaSet · web-6d4f9 replicas: 3 · selector: app=web pod pod pod app: web app: web app: web SELECTOR app=web ⇄ POD LABELS app: web If a pod dies, the ReplicaSet creates another one to get back to 3.

The Deployment controls a ReplicaSet, and the ReplicaSet keeps the pods that match its selector. Diagram: The House of Routing.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  labels:
    app: web
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.27-alpine
          ports:
            - containerPort: 80
kubectl apply -f deployment.yaml
kubectl get deployments
kubectl get rs
kubectl get pods
kubectl get all

After you apply the file, kubectl get all shows the three levels: the web Deployment, its web-xxxxxxxxx ReplicaSet and the web-xxxxxxxxx-yyyyy pods. So what does a Deployment give you that a ReplicaSet doesn't? Controlled updates, version history and rollback. That's the next section.

Rollouts, strategies and rollback

Every time you change a Deployment's pod template, for example the image, Kubernetes starts a rollout. The Deployment creates a new ReplicaSet with the new version and moves pods from the old one to the new one. Each rollout is saved as a revision, and that's what lets you go back.

Two strategies

  • RollingUpdate (default): replaces pods a few at a time. With the default values (maxSurge: 25% and maxUnavailable: 25%), part of the application is always serving traffic. No downtime.
  • Recreate: deletes all the old pods and then creates the new ones. It's simpler, but there's a period with no service. It makes sense when two versions can't run side by side.
RollingUpdate and Recreate strategies With RollingUpdate, new pods replace old ones a few at a time and the application stays available. With Recreate, all old pods are deleted first, so there is downtime before the new ones start. RollingUpdate · default T1 v1 v1 v1 v1 T2 v2 v1 v1 v1 T3 v2 v2 v2 v1 T4 v2 v2 v2 v2 always some pods serving traffic Recreate T1 v1 v1 v1 v1 T2 T3 v2 v2 v2 v2 downtime: no pods running

With RollingUpdate there are always pods serving traffic; with Recreate there's an outage between versions. Diagram: The House of Routing.

Update, review and roll back

kubectl set image deployment/web nginx=nginx:1.28-alpine
kubectl annotate deployment/web kubernetes.io/change-cause="nginx 1.28"
kubectl rollout status deployment/web
kubectl rollout history deployment/web
kubectl rollout undo deployment/web
kubectl rollout undo deployment/web --to-revision=1
  • kubectl set image changes the image without editing the file; you can also edit the YAML and apply it.
  • rollout status follows the progress; rollout history lists the revisions.
  • rollout undo goes back to the previous revision. With --to-revision you choose which one.
Updated. Many courses use --record to save the command in the history. That flag is deprecated. Today you record the reason for the change with kubernetes.io/change-cause, as in the example.

Services: a stable address

Pods are ephemeral: when they're recreated, their IP changes. So how does the frontend find the backend? With a Service, which gives a group of pods a stable IP and DNS name and spreads traffic across them. Just like a ReplicaSet, a Service finds its pods with a label selector.

TypeWho can reach itWhen to use it
ClusterIP (default)Only from inside the clusterCommunication between microservices
NodePortFrom outside, through any node's IP and a port between 30000 and 32767Tests and labs
LoadBalancerFrom outside, through a public IP of a cloud provider's load balancerExposing a production application in the cloud
ClusterIP, NodePort and LoadBalancer Services A ClusterIP Service gives pods a stable internal IP and DNS name. NodePort also opens the same port, from 30000 to 32767, on every node. LoadBalancer adds an external load balancer from the cloud provider in front of the NodePort. LoadBalancer · cloud LB NODE 1 · :30080 NODE 2 · :30080 ClusterIP · 10.96.12.40:80 web.default.svc.cluster.local pod pod pod pod ClusterIP: inside the cluster · NodePort: node IP + port 30000–32767 · LoadBalancer: external IP from the cloud The Service finds its pods with a label selector, just like a ReplicaSet.

Each Service type builds on the previous one: LoadBalancer uses a NodePort, and NodePort uses a ClusterIP. Diagram: The House of Routing.

apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  type: NodePort
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30080

Note the three ports: port is the Service's, targetPort the container's and nodePort the one opened on every node. If you don't set nodePort, Kubernetes picks a free one from the range.

kubectl apply -f service.yaml
kubectl get services
kubectl describe svc web
kubectl expose deployment web --port=80 --type=ClusterIP

There's a fourth type, ExternalName, which only returns an external DNS name. To route HTTP traffic with rules by host or path you use Ingress, which you'll see in the container orchestration domain.

Namespaces: order inside the cluster

A namespace splits a cluster into logical spaces. Each one has its own objects, its own permissions and, if you want, its own resource quotas. It's how you separate environments (development, testing, production) or teams inside the same cluster.

A new cluster comes with four namespaces:

  • default: where everything goes when you don't specify another namespace.
  • kube-system: Kubernetes' internal components. Leave it alone.
  • kube-public: resources that everyone must be able to read, even without authentication.
  • kube-node-lease: the leases each node uses to signal that it's still alive.

DNS names across namespaces

Inside its own namespace, a Service is reached by its short name (db). From another namespace you add the namespace: db.dev, or the full name db.dev.svc.cluster.local.

kubectl get namespaces
kubectl create namespace dev
kubectl get pods --namespace=dev
kubectl get pods -n kube-system
kubectl get pods --all-namespaces
kubectl config set-context --current --namespace=dev

To create an object in a specific namespace, use --namespace (or -n) or put namespace: dev in the YAML metadata. kubectl config set-context --current --namespace=dev changes the default namespace for your session.

Resource quotas

A ResourceQuota limits what a namespace can consume: number of pods, CPU and memory.

apiVersion: v1
kind: ResourceQuota
metadata:
  name: compute-quota
  namespace: dev
spec:
  hard:
    pods: "10"
    requests.cpu: "4"
    requests.memory: 5Gi
    limits.cpu: "10"
    limits.memory: 10Gi

Imperative vs declarative

There are two ways to work with Kubernetes, and the exam expects you to tell them apart.

  • Imperative: you tell the system what to do and how, step by step. "Create this pod", "scale to five", "change the image".
  • Declarative: you describe the end result in a file and Kubernetes works out the steps to get there.

Imperative commands

kubectl run web --image=nginx
kubectl create deployment web --image=nginx --replicas=3
kubectl expose deployment web --port=80
kubectl scale deployment web --replicas=5
kubectl set image deployment/web nginx=nginx:1.28-alpine
kubectl edit deployment web
kubectl create -f web.yaml
kubectl replace -f web.yaml
kubectl replace --force -f web.yaml
kubectl delete -f web.yaml

They're fast for tests and for the exam, but they have a drawback: what you change with kubectl edit, scale or set image doesn't end up in your file. kubectl replace --force deletes the object and creates it again.

The declarative approach: kubectl apply

kubectl apply -f web.yaml
kubectl apply -f ./manifests/
kubectl diff -f web.yaml

kubectl apply creates the object if it doesn't exist and updates it if it does. To work out what changed it compares three versions: your local file, the last applied configuration and the live object in the cluster. The last applied configuration is stored as JSON in the object's own kubectl.kubernetes.io/last-applied-configuration annotation.

How kubectl apply compares configurations kubectl apply compares three versions: your local YAML file, the last applied configuration stored as an annotation, and the live object in the cluster. From that comparison it decides what to add, change or remove. LOCAL FILEweb.yaml what you want now LAST APPLIEDannotation (JSON) what you applied last time LIVE OBJECTin etcd what is running kubectl apply -f web.yaml A field in last-applied but missing from your file is removed from the live object. kubectl.kubernetes.io/last-applied-configuration

Thanks to the last applied configuration, kubectl apply knows which fields you removed from your file and removes them from the cluster too. Diagram: The House of Routing.

Rule of thumb. Pick one approach per object. If you manage an object with apply, don't change it with imperative commands: the next apply will undo those changes. Keep your YAML in Git and your infrastructure is versioned.

KCNA-style challenge

Answer before you open the solution. Getting it wrong here is the cheapest way to learn.

1. Which object automatically creates and manages a ReplicaSet?

The Deployment. That's why you almost never create ReplicaSets by hand: the Deployment creates them, one per version, and that's how it can do rollouts and rollbacks.

2. What's the default deployment strategy, and what's its advantage?

RollingUpdate. It replaces pods a few at a time, so the application keeps serving during the update. Recreate, by contrast, causes an outage.

3. Two microservices need to talk to each other only inside the cluster. Which Service type do you use?

ClusterIP, the default type. NodePort and LoadBalancer are for receiving traffic from outside the cluster.

4. A pod in the "front" namespace must reach the "db" Service in the "dev" namespace. Which name does it use?

db.dev or the full name db.dev.svc.cluster.local. The short name db only works inside the same namespace.

5. Where does kubectl apply store the last configuration you applied?

In an annotation on the object itself, kubectl.kubernetes.io/last-applied-configuration, as JSON.

Key takeaways

  • Every object has four top-level fields: apiVersion, kind, metadata and spec.
  • The pod is the smallest unit; the norm is one container per pod.
  • A ReplicaSet keeps the number of replicas and finds its pods by label with its selector.
  • A Deployment manages ReplicaSets and adds rollouts, revisions and rollback; RollingUpdate is the default strategy.
  • A Service gives a group of pods a stable IP and name: ClusterIP inside the cluster, NodePort and LoadBalancer to the outside.
  • Namespaces separate environments and teams; a Service in another namespace is called service.namespace.
  • Imperative means saying how; declarative means describing the result. kubectl apply is declarative.

In the next chapter you'll see how Kubernetes decides which node each pod goes to: scheduling, labels and selectors, taints and tolerations.

Official resources

Source and credits. Chapter written from Jesús Espinoza's study notes on KodeKloud's KCNA course (January 2026). Text drafted with AI assistance; original diagrams by The House of Routing. Technical details and YAML examples were checked against the official Kubernetes and CNCF documentation.

Navigate