Deployment

Understand Deployments, ReplicaSets, and Pods, run two Node.js replicas, and verify automatic Pod replacement and manual scaling.

🎯 Introduction

In the previous article, we created an order service Pod directly. It ran the Node.js program, but deleting it did not cause Kubernetes to create another bare Pod.

In real applications, we usually want a specified number of service instances to remain running. For example, we may want two order service Pods, with an automatic replacement if one disappears. This article uses a Deployment to achieve that and demonstrates recovery by deleting a Pod.

By the end, you should understand how Deployments, ReplicaSets, and Pods relate, create and inspect a Deployment, and increase its replica count from two to three.


🧱 A Deployment does not manage a fixed machine

Creating an ordinary Pod tells Kubernetes how to run it, but does not create a management object that continuously requires two order service Pods to exist.

A Deployment declares and manages this requirement. You specify how many replicas you want, and Kubernetes attempts to maintain that count without requiring you to track a particular Pod name.

You also need to know about ReplicaSets, which maintain a specified number of Pods. You generally do not create them yourself: a Deployment creates and manages them automatically.

courseComponents.mermaid.loading

Their responsibilities are:

ResourceMain responsibility
DeploymentDeclares the desired replica count and Pod template, and manages ReplicaSets
ReplicaSetCreates, replaces, or removes Pods to match the desired replica count
PodRuns the Node.js order service container

For example, after a Pod is deleted, the old Pod does not come back to life: the ReplicaSet attempts to create a new Pod to restore the count.

A Deployment's replica count specifies how many Pods you want to maintain. It does not guarantee that they run on different machines or that order requests cannot fail. Replacing Pods automatically and protecting business data are separate concerns.

🛠 Creating two order service instances

Continue using the course-orders:v1 image built and loaded into kind earlier, along with the course Namespace.

First, confirm that the cluster is still running:

kubectl get nodes

Simulated output:

NAME                        STATUS   ROLES           AGE   VERSION
node-course-control-plane   Ready    control-plane   15m   v1.x.y

Ready means the node is ready. Your version and age will depend on the environment.

1. Remove the previous bare Pod

The orders-demo Pod from the previous exercise also uses the app: orders label. Delete it first to avoid sharing that label with this Deployment. Skip this if you already deleted it:

kubectl delete pod orders-demo -n course --ignore-not-found

Simulated output if the old Pod still exists:

pod "orders-demo" deleted

If it no longer exists, the command may print nothing. --ignore-not-found prevents a missing resource from being treated as an error.

Do not create additional bare Pods labeled app: orders during this article. The Deployment's selector uses this label to identify its Pods, and unrelated Pods with the same label can interfere with your observations.

2. Write the Deployment configuration

Create deployment.yaml in orders-app:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: orders
  namespace: course
spec:
  replicas: 2
  selector:
    matchLabels:
      app: orders
  template:
    metadata:
      labels:
        app: orders
    spec:
      containers:
        - name: orders
          image: course-orders:v1
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 3000

Compared with the previous pod.yaml, the key fields are:

FieldPurpose
kind: DeploymentCreates a Deployment rather than a Pod directly
metadata.name: ordersNames the Deployment orders
replicas: 2Requests two Pods
selector.matchLabelsMatches managed Pods by label
templateDefines the template for new Pods
template.metadata.labelsAdds labels to Pods created from the template
template.spec.containersSpecifies the Node.js container inside each Pod

These two sections are easy to confuse:

selector:
  matchLabels:
    app: orders
template:
  metadata:
    labels:
      app: orders

The first means manage Pods labeled app: orders; the second means new Pods created from the template must also carry app: orders. They must match for the Deployment to manage its Pods correctly.

The spec.containers section within template closely resembles the container configuration in the previous article, because the Deployment ultimately creates Pods to run the application.

3. Create the Deployment

Run this in the directory containing deployment.yaml:

kubectl apply -f deployment.yaml

Simulated output:

deployment.apps/orders created

The Deployment named orders has been created. The first apply usually reports created; applying the same configuration again may report unchanged.

Wait for the initial deployment to finish:

kubectl rollout status deployment/orders -n course

Simulated output:

deployment "orders" successfully rolled out

This means the Deployment rollout completed. If the image is unavailable or Pods cannot become ready, the command may keep waiting. The output above is not a guaranteed result.

4. Inspect the Deployment, ReplicaSet, and Pods

First, inspect the Deployment:

kubectl get deployments -n course

Simulated output:

NAME     READY   UP-TO-DATE   AVAILABLE   AGE
orders   2/2     2            2           45s
FieldMeaning
READY 2/2Two replicas are desired and two are ready
UP-TO-DATE 2Two replicas match the current Pod template
AVAILABLE 2Two replicas meet Kubernetes availability conditions
AGETime since the Deployment was created

Next, list all three resource types:

kubectl get deployment,replicaset,pods -n course

Simulated output; generated suffixes will differ:

NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/orders   2/2     2            2           1m

NAME                                DESIRED   CURRENT   READY   AGE
replicaset.apps/orders-7c8d9f6b5   2         2         2       1m

NAME                          READY   STATUS    RESTARTS   AGE
pod/orders-7c8d9f6b5-a1b2c   1/1     Running   0          55s
pod/orders-7c8d9f6b5-d3e4f   1/1     Running   0          55s

This shows one Deployment, one ReplicaSet, and two Pods. Pod names include generated suffixes, so do not copy these example names when running commands.

Running means the Pod is in the running phase, and READY 1/1 means its single container is ready. Neither guarantees that the order endpoints and external database are working correctly.


🔎 Observing automatic replacement

Now deliberately delete one Pod and see whether the Deployment maintains two replicas.

1. Confirm that both Pods exist

kubectl get pods -n course

Simulated output:

NAME                      READY   STATUS    RESTARTS   AGE
orders-7c8d9f6b5-a1b2c   1/1     Running   0          2m
orders-7c8d9f6b5-d3e4f   1/1     Running   0          2m

2. Delete just one Pod

Choose a name from the Pods listed in your own terminal. For example, if your Pod is named orders-7c8d9f6b5-a1b2c:

kubectl delete pod orders-7c8d9f6b5-a1b2c -n course

Simulated output:

pod "orders-7c8d9f6b5-a1b2c" deleted
This Pod name is only an example; replace it with your actual name. Do not demonstrate single-Pod deletion with kubectl delete pod -l app=orders, because that deletes every Pod matching the label.

3. Inspect the replacement

Run again:

kubectl get pods -n course

After a short wait, simulated output might be:

NAME                      READY   STATUS    RESTARTS   AGE
orders-7c8d9f6b5-d3e4f   1/1     Running   0          3m
orders-7c8d9f6b5-g5h6j   1/1     Running   0          12s

Notice the new orders-7c8d9f6b5-g5h6j: the ReplicaSet created a new Pod. The deleted Pod did not restart.

If you check immediately after deletion, you may briefly see states such as Terminating, Pending, or ContainerCreating, since deletion and creation take time.

The process looks like this:

courseComponents.mermaid.loading

The key point: the Deployment declares the goal, and the ReplicaSet continuously maintains the Pod count. If a Pod disappears, the system attempts to create a replacement.

To watch changes continuously, first run kubectl get pods -n course -w in another terminal, then delete the Pod. -w watches for changes; press Ctrl + C to stop. New Pods may appear quickly, so you are not guaranteed to see every intermediate state.

⚖️ How this relates to Pod configuration

Previously, we created a Pod directly with kind: Pod at the top level. With a Deployment, the Pod configuration lives under spec.template, and the Deployment manages its creation.

Compare the two approaches:

ApproachWho creates the Pod?After Pod deletion
Create a bare Pod directlyYou submit the Pod YAML manuallyNo corresponding controller recreates it automatically
Use a DeploymentThe Deployment manages a ReplicaSet, which creates PodsThe ReplicaSet attempts to replace missing Pods

For production deployments of stateless Node.js web services, a Deployment is usually preferable to relying on a manually created bare Pod.

Changing the image, configuring environment variables, or adding health probes also involves the Deployment's Pod template. Version updates and rollout strategies are covered later.


❓ Questions and answers

Q: Do I need to create the ReplicaSet myself?

Usually not. When you deploy a web service with a Deployment, it creates and manages ReplicaSets automatically.

Q: Does setting two replicas guarantee high availability?

No. In this course's default single-node kind cluster, both Pods may run on the same node. If it fails, both are affected. Having two replicas also does not automatically solve database, network, or application problems.

Q: Does a Deployment guarantee that every order request succeeds?

No. A Deployment manages Pod replicas and updates. It does not automatically guarantee business behavior such as request retries, order deduplication, or transaction consistency.


🧪 Exercise

We have verified automatic replacement after deleting a Pod. Next, verify manual scaling.

  1. Open deployment.yaml and change replicas: 2 to replicas: 3.
  2. Apply the configuration again:
kubectl apply -f deployment.yaml

Simulated output:

deployment.apps/orders configured
  1. Wait for the rollout to complete, then inspect resources:
kubectl rollout status deployment/orders -n course
kubectl get deployments -n course
kubectl get pods -n course

Simulated output, omitting repeated Pod rows:

deployment "orders" successfully rolled out

NAME     READY   UP-TO-DATE   AVAILABLE   AGE
orders   3/3     3            3           6m

If you query immediately after the change, you may initially see 2/3, followed by 3/3 once the new Pod is ready.

Success criteria: Explain why another Pod appeared, which resource created it, and what replicas and READY mean.

Keep the Deployment for later articles. There is no need to delete the cluster here.


🎉 You can now manage Node.js service replicas with a Deployment. By deleting a Pod and changing the replica count, you have verified how Kubernetes continuously maintains the desired state.