AWS Builder Center

A Pod Crashed. Your Website Shouldn't: Self-Healing with Kubernetes on AWS

What happens when your backend crashes in the middle of serving users? Explore how Kubernetes detects failures, replaces unhealthy Pods, and maintains application availability with Amazon EKS.

I am a student pursuing BTech in Computer Science
Imagine you've developed a web application with a React frontend and a Node.js backend.
Everything works perfectly on your local machine. You deploy it, users start accessing it, and things seem to be going well.
Then something unexpected happens.
Your backend crashes.
Maybe it ran out of memory. Maybe an unexpected error caused the process to terminate. Maybe the server hosting it became unavailable.
In a traditional deployment, someone might need to log into the server, investigate the problem, and manually restart the application.
But what if your application could recover from certain failures automatically?
That's one of the most interesting capabilities of Kubernetes: self-healing.
And with Amazon Elastic Kubernetes Service (Amazon EKS), you can use these Kubernetes capabilities without having to operate the Kubernetes control plane yourself.
Let's understand how this works.

1. First, Understand What Can Actually Fail

Before discussing self-healing, we need to understand the different components involved in running an application.
In Kubernetes, applications run inside containers, and containers run inside Pods.
A Pod is the smallest deployable unit in Kubernetes. It usually contains one application container, although it can contain multiple closely related containers.
Pods run on machines called Nodes.
A collection of Nodes forms a Kubernetes cluster.
Now imagine the following situation:
  • Your application is running inside a Pod.
  • The Pod is hosted on a Node.
  • The Node experiences a hardware or infrastructure failure.
What happens to your application?
This is where Kubernetes controllers become important.
They continuously compare the actual state of the cluster with the desired state defined by you.
If something goes wrong, Kubernetes attempts to bring the application back to its desired state.
This is the foundation of self-healing.

2. How Deployments and ReplicaSets Save Your Application

Suppose your backend needs to run three instances.
You create a Kubernetes Deployment with:
spec:
replicas: 3
This tells Kubernetes that you want three replicas of your application running.
But you don't need to manage each Pod individually.
A Deployment manages a ReplicaSet, and the ReplicaSet ensures that the desired number of Pods exists.
Consider this example:
Desired state: 3 Pods
Actual state: 2 Pods
One Pod crashes and disappears.
The ReplicaSet notices that only two Pods are available and creates another one to restore the desired count.
The process looks like this:
  1. The Deployment defines the desired number of replicas.
  1. The ReplicaSet monitors the Pods.
  1. A Pod disappears or is deleted.
  1. The ReplicaSet detects the difference.
  1. Kubernetes creates a replacement Pod.
You don't need to manually create the replacement.
That's the beauty of declarative infrastructure: you describe what you want, and Kubernetes works to maintain it.
One important distinction: if a container crashes while its Pod still exists, the kubelet can restart that container according to the Pod's restart policy. A replacement Pod is not required for every container failure.

3. Where Does Amazon EKS Come Into the Picture?

Kubernetes is powerful, but operating its infrastructure is another responsibility.
You need to manage the control plane, maintain infrastructure, handle updates, and ensure that the cluster remains available.
This is where Amazon EKS helps.
Amazon Elastic Kubernetes Service is AWS's managed Kubernetes service.
It provides a Kubernetes control plane managed by AWS, while your application workloads run on compute capacity configured for your cluster.
An EKS architecture generally contains two major parts:
Control Plane
This is responsible for managing the Kubernetes cluster. It includes components such as the API server, scheduler, and controller manager.
AWS manages the availability and maintenance of this control plane.
Data Plane
This is where your actual application workloads run.
It consists of worker nodes that host your Pods. These can use EC2 instances through managed node groups, self-managed nodes, or other supported compute options.
Amazon EKS managed node groups also help automate node provisioning, updates, and lifecycle management.
You can explore the  Amazon EKS architecture documentation  for a deeper understanding.
The important thing to remember is that AWS manages the control plane, but you still need to design and manage your application workloads appropriately.

4. Let's Deploy a Simple Node.js Backend

Let's turn the concept into something practical.
Assume you have a Node.js API that listens on port 3000 and exposes two endpoints:
  •  /health/live 
  •  /health/ready 
The first checks whether the application process is functioning.
The second checks whether the application is ready to handle requests. For example, it could verify whether the required database connection has been established.
Here is a basic example using Express:
app.get("/health/live", (req, res) => {
res.status(200).send("Application is alive");
});

app.get("/health/ready", (req, res) => {
if (!databaseReady) {
return res.status(503).send("Not ready");
}

res.status(200).send("Application is ready");
});
Now we can create a Kubernetes Deployment.

Creating the Deployment

Create a file named  deployment.yaml :
apiVersion: apps/v1
kind: Deployment

metadata:
name: node-api

spec:
replicas: 2

selector:
matchLabels:
app: node-api

template:
metadata:
labels:
app: node-api

spec:
containers:
- name: node-api

image: YOUR_ECR_IMAGE_URI

ports:
- containerPort: 3000

resources:
requests:
cpu: "100m"
memory: "128Mi"

limits:
cpu: "500m"
memory: "512Mi"

readinessProbe:
httpGet:
path: /health/ready
port: 3000
periodSeconds: 5

livenessProbe:
httpGet:
path: /health/live
port: 3000
initialDelaySeconds: 15
periodSeconds: 10
Replace  YOUR_ECR_IMAGE_URI  with the address of your container image stored in Amazon Elastic Container Registry (Amazon ECR), or another accessible container registry.
Notice something interesting here: we've configured two replicas and two different health checks.
Let's understand why.

5. Liveness vs Readiness: They Are Not the Same

Many beginners assume that if an application is running, it is automatically ready to receive traffic.
That's not necessarily true.
Imagine your Node.js application starts successfully, but it is still establishing a database connection.
The process is running, but the application isn't ready to handle requests that depend on the database.
Kubernetes provides different probes to handle these situations.
Liveness Probe
Checks whether the application is functioning.
If the liveness probe repeatedly fails, Kubernetes restarts the container.
This helps recover from situations where an application becomes stuck or stops responding.
Readiness Probe
Checks whether the application is ready to receive traffic.
If the readiness probe fails, Kubernetes marks the Pod as not ready and removes it from the endpoints used by matching Services.
The container does not have to be restarted.
Startup Probe
Useful when an application takes a long time to initialize.
It gives the application time to start before liveness and readiness checks begin.
For a Node.js API, these checks can make a significant difference.
For example, temporarily losing a database connection should generally make the application unready rather than cause Kubernetes to restart every application instance.
A badly designed health check can create more problems than it solves.
For more details, see the  Kubernetes health probe documentation .

6. Connecting Your Pods Through a Kubernetes Service

We now have two backend Pods running.
But how does another component communicate with them without knowing their individual IP addresses?
This is where Kubernetes Services help.
Create a file named  service.yaml :
apiVersion: v1
kind: Service

metadata:
name: node-api-service

spec:
type: ClusterIP

selector:
app: node-api

ports:
- port: 80
targetPort: 3000
This creates a stable internal endpoint for accessing your backend.
The Service uses the  app: node-api  label to identify matching Pods and distributes traffic across available endpoints.
If one Pod becomes unready, it is removed from the Service's eligible endpoints.
The other healthy Pod can continue receiving requests.
This is especially useful when combined with multiple application replicas.
One important detail:  ClusterIP  is accessible inside the cluster. To expose an application externally, you can configure an Ingress or an AWS load balancer integration.

7. Can Kubernetes Automatically Increase the Number of Pods?

Now consider another situation.
Your application normally receives 100 requests per minute.
Suddenly, traffic increases to 5,000 requests per minute.
Two Pods might no longer be sufficient.
Instead of manually changing the replica count every time traffic increases, Kubernetes provides something called the Horizontal Pod Autoscaler (HPA).
It can automatically increase or decrease the number of Pods based on observed metrics.
For example:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler

metadata:
name: node-api-autoscaler

spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: node-api

minReplicas: 2
maxReplicas: 6

metrics:
- type: Resource
resource:
name: cpu

target:
type: Utilization
averageUtilization: 60
This configuration tells Kubernetes to maintain between two and six Pods, targeting average CPU utilization of 60%.
For CPU-based autoscaling, you need resource requests and a working resource metrics API, commonly provided by Metrics Server.
There is another important distinction here.
HPA scales Pods, not EC2 instances.
Suppose HPA decides to increase the application from two Pods to six, but there isn't enough room on your existing Nodes.
The additional Pods may remain in the Pending state.
To solve that problem, your cluster also needs an appropriate node-capacity management strategy, such as a configured node autoscaler.
This means application scaling and infrastructure scaling are related, but they are not the same thing.

8. Try Breaking Your Application on Purpose

One of the best ways to understand self-healing is to watch it happen.
Assuming you have an EKS cluster, an accessible container image, and the appropriate  kubectl  configuration, apply your deployment:
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
Check your running Pods:
kubectl get pods
You should see two application Pods.
Now, open another terminal and watch the Pods:
kubectl get pods -w
Find one of the running Pod names and delete it:
kubectl delete pod <pod-name>
Watch what happens.
Because the Deployment's ReplicaSet is maintaining two replicas, Kubernetes creates a replacement Pod.
You can also inspect the Deployment:
kubectl get deployments
And check its events:
kubectl describe deployment node-api
This is a simple experiment, but it demonstrates one of Kubernetes' most important concepts.
You are not telling Kubernetes exactly how to recover from every possible failure.
You are defining the desired state and letting its controllers work toward maintaining that state.
For an actual EKS deployment walkthrough, AWS provides a  sample application deployment guide .

9. Does Self-Healing Mean Your Application Can Never Go Down?

Not quite.
Self-healing is powerful, but it isn't magic.
Kubernetes can restart failed containers, replace deleted Pods, and help reschedule workloads when Nodes become unavailable.
However, it cannot automatically fix broken application code, incorrect environment variables, invalid container images, or a database that has permanently failed.
Consider these examples:
  • If your application has a memory leak, restarting the container may temporarily restore service, but the underlying issue remains.
  • If your database is unavailable, restarting every backend Pod won't bring the database back.
  • If all available Nodes run out of resources, replacement Pods may remain Pending.
  • If all replicas are placed on the same failed Node, they may all become unavailable together.
This is why production systems also need appropriate monitoring, resource planning, failure handling, and distribution of workloads across Nodes and Availability Zones.
Multiple replicas are useful, but their placement matters just as much as their number.
For EKS, managed node groups can also optionally use node auto repair to detect certain node health problems and replace affected nodes.
The goal is not to eliminate every failure. It is to reduce the impact of individual failures and make recovery more predictable.

Final Thoughts

One of the most important differences between running an application locally and operating it in the cloud is learning to expect failures.
A container will eventually crash. A Pod might disappear. A Node might become unavailable. Traffic might suddenly increase.
These aren't unusual situations in distributed systems.
With Kubernetes and Amazon EKS, developers can build applications that respond to these situations more intelligently.
Deployments maintain the desired number of replicas. Health probes help identify unhealthy application instances. Services route traffic to ready Pods. Autoscalers adjust capacity as demand changes.
The real value isn't that Kubernetes prevents failures.
It's that you can design your application to recover from failures without requiring someone to manually restart everything.
And that's a much better way to spend your time than babysitting a server.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article