Gaming industry under DDoS attack. Get DDoS protection now. Start onboarding

Products

  1. Home
  2. Developers
  3. Kubernetes. Replication and self-healing

Kubernetes. Replication and self-healing

  • By Gcore
  • March 18, 2023
  • 3 min read
Kubernetes. Replication and self-healing

To start with, I would like to explain what does self-healing means in terms of Kubernetes. Self-healing is a fantastic feature of Kubernetes to recover from service or node failure automatically. In the following article, we will consider the benefit of using replication for your micro-services and how the Kubernetes cluster can automatically recover from a service failure.

Prerequisite

One of the great features of Kubernetes is the ability to replicate pods and their underlying containers across the cluster. So, before we set up our self-healing feature please make sure you have managed replication and here is a simple example of a deployment file that will deploy nginx container with replication factor 3:

apiVersion: apps/v1kind: Deployment-examplemetadata:  name: nginx-deployment-example  labels:    app: nginxspec:  replicas: 3  selector:    matchLabels:      app: nginx  template:    metadata:      labels:        app: nginx    spec:      containers:      - name: nginx        image: nginx:1.15.4        ports:        - containerPort: 80

All right, so let’s create the deployment:

kubectl create -f deployment.yaml

Now, let’s check whether our nginx-deployment-example was created:

 kubectl get deployments -n default

You should see your nginx-deployment-example deployment in the default namespace. If we want to see more details about those pods, please run the following command:

kubectl get pods -n default

We will see our 3 nginx-deployment-example pods:

NAME                                     READY   STATUS    RESTARTS   AGEnginx-deployment-example-f4cd8584-f494x   1/1     Running   0          94snginx-deployment-example-f4cd8584-qvkbg   1/1     Running   0          94snginx-deployment-example-f4cd8584-z2bzb   1/1     Running   0          94s

Self-healing

Kubernetes ensures that the actual state of the cluster and the desired statue of the cluster are always in-sync. This is made possible through continuous monitoring within the Kubernetes cluster. Whenever the state of a cluster changes from what has been defined, the various components of Kubernetes work to bring it back to its defined state. This automated recovery is often referred to as self-healing.
So, let’s copy one of the pods mentioned in the prerequisite and see what happens when we delete it:

kubectl delete pod nginx-deployment-example-f4cd8584-f494x

And after a few seconds, we see that our pod was deleted:
pod "nginx-deployment-example-f4cd8584-f494x" deleted
Let’s go ahead and list the pods one more time:

kubectl get pods -n defaultNAME                                     READY   STATUS    RESTARTS   AGEnginx-deployment-example-f4cd8584-qvkbg   1/1     Running   0          109snginx-deployment-example-f4cd8584-sgfqq   1/1     Running   0          5snginx-deployment-example-f4cd8584-z2bzb   1/1     Running   0          109s

And we see that the pod nginx-deployment-example-f4cd8584-sgfqq was automatically created to replace our deleted pod nginx-deployment-example-f4cd8584-f494x. And the reason is that nginx deployment is set to have 3 replicas. So, even though one of these was deleted, our Kubernetes cluster works to make sure that the desired state is the actual state that we have.
So, now let’s consider the case when there is an actual node failure in your cluster. First, let’s check our nodes:

kubectl get nodes

You will see your Master and Worker nodes. Now, let’s figure out what server our pods are running on. To do that, we have to describe it:

kubectl describe pod nginx-deployment-example-f4cd8584-qvkbg

Under the Events, you can see to which server the pod was assigned. We can also scroll up and under Node will also see where it has been assigned, which is of course will be the same server. Once you’ve identified all servers that are pods running on, you have to pick one server and simulate a node failure by shutting down the server.
Once the node has been shut down let us head back to the Master and check on the status of the nodes:

kubectl get nodes

We see that the cluster knows that one node is down. Let’s also list our pods:

kubectl get pods -n default

You see that the state of the pod that was running on "failed" node is Unknown, but you see that another pod took its place. Let’s go ahead and list the deployments:

kubectl get deployments -n default

And as we expected we have 3 pods available which are now in sync with our Desired amount of pods.
Now, let’s try to describe the Unknown pod:

kubectl describe pod <pod_in_unknown_state>

You see that the Status of Unknown pod is Terminating, Termination Grace Period will be the 30s by default and the reason for this is NodeLost. Also, you will see the messages that specifies that our node which was running our pod is unresponsive.
Now, let’s start our "failed" node and wait till it successfully rejoins the cluster.
Alright, once "failed" node is up and running, let’s go ahead and check the status of our deployment:

kubectl get deployments -n default

We will see that our pod count is in sync. So, let’s list our pods out:

kubectl get pods -n default

We will see that our Kubernetes cluster has finally terminated the old pod, and we are left with our desired count of 3 pods.

Conclusion

As we see, Kubernetes takes a self-healing approach to infrastructure that reduces the criticality of failures, making fire drills less common. Kubernetes heals itself when there is a discrepancy and ensures the cluster always matches the declarative state. In other words, Kubernetes kicks in and fixes a deviation, if detected. For example, if a pod goes down, a new one will be deployed to match the desired state. Alternatively you can also use a cloud container hosting platform.

Discover more with Gcore Managed Kubernetes

Related articles

Secure data server connected to cloud and other servers, illustrating data management and security.
What Is a High Availability Server?

Every minute your servers are down, your business is bleeding. For e-commerce sites, healthcare platforms, and revenue-critical applications, an outage isn't just an inconvenience. It's a direct hit to your bottom line, your reputation, and

Visual comparison of cloud-based infrastructure connected to devices versus traditional server racks.
Cloud vs Dedicated Server: Which Is Right for You?

Your server choice could be quietly costing you, or quietly holding you back. Pick the wrong infrastructure for your workload and you're either overpaying for idle hardware every month or watching your site buckle under traffic spikes you c

VPS vs Dedicated Server
VPS vs Dedicated Server: Which One Do You Need?

Your site is humming along fine, until it isn't. Traffic spikes, page loads crawl, and your hosting plan buckles under pressure right when it matters most. Choosing between a VPS and a dedicated server isn't just a technical checkbox. It's

Illustration of cloud computing infrastructure, data storage, network security, and digital services.
Multi-Cloud Plan: What It Is and How It Works

Your cloud provider goes down. Applications fail. Customers can't access your services. And because you've built everything around a single vendor, there's nothing you can do but wait. For organizations locked into one cloud platform, this

Padlocked cloud secured with chains, representing digital data protection and cybersecurity measures.
Vendor Lock-In in Cloud Computing: What It Is and How to Avoid It

Imagine discovering that migrating your company's data to a new cloud provider will cost hundreds of thousands of dollars in egress fees alone, before you've even touched the re-engineering work. Or worse, picture being in Synapse Financial

Secure cloud computing infrastructure protecting data, with people, servers, and government institutions.
What Is Sovereign Cloud and Why Does It Matter?

Picture this: a foreign government issues a legal order forcing your cloud provider to hand over sensitive patient records, classified research data, or critical national infrastructure details. You can't stop it. This isn't hypothetical. G

Subscribe to our newsletter

Get the latest industry trends, exclusive insights, and Gcore updates delivered straight to your inbox.