Skip to Content

Kubernetes Explained: A Beginner’s Dive into DaemonSets and ReplicaSets

Welcome to the fascinating world of Kubernetes, where managing and scaling applications becomes a streamlined process.

If you’re just starting your journey into Kubernetes, you might find yourself surrounded by a sea of new terms and concepts.

Today, we’re going to focus on two key players in the Kubernetes ecosystem: DaemonSets and ReplicaSets.

These might sound like complex terms, but fear not! We’re here to break them down into bite-sized, easy-to-understand pieces.

Through this article, we’ll embark on a journey to uncover the mysteries behind these crucial components, turning the complex into the comprehensible.

Understanding DaemonSets and ReplicaSets in Kubernetes

Kubernetes, often abbreviated as k8s, emerged from the need to manage complex, containerized applications across a cluster of machines.

It automates the deployment, scaling, and operation of application containers, making it easier for teams to manage their applications’ lifecycle.

Within this framework, DaemonSets and ReplicaSets serve specific, yet complementary, purposes.

ReplicaSets are the Kubernetes way to ensure that a specified number of identical pods are running at any given time.

They are crucial for scaling applications up or down based on demand, providing high availability, and facilitating load balancing.

Think of ReplicaSets as managers ensuring that their team (pods) has the right number of members at all times, replacing any that fail or are terminated.

DaemonSets, on the other hand, take a different approach. They ensure that a copy of a specific pod runs on all or certain nodes in a Kubernetes cluster.

This is particularly useful for deploying system-wide services that need to be present on every node, such as log collectors, monitoring agents, or network proxies.

DaemonSets are like the essential utilities of a city, ensuring that services like electricity and water are available in every home.

What is a ReplicaSet?

A ReplicaSet is one of the most fundamental workloads in Kubernetes. Its primary purpose is to maintain a stable set of replica Pods running at any given time.

It is often used to ensure the availability and scalability of stateless applications. If a Pod in a ReplicaSet fails, the ReplicaSet controller automatically creates a new Pod to replace it, maintaining the desired number of replicas.

Example: Imagine you have a web application that you want to scale out to handle an increasing load. You could define a ReplicaSet with a specified number of replicas for your web application Pods.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: webapp-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp-container
        image: webapp:latest
        ports:
        - containerPort: 80

This ReplicaSet ensures that three instances of your web application are running at all times. If one instance fails, the ReplicaSet creates a new one to replace it.

What is a DaemonSet?

DaemonSets serve a different purpose. They ensure that a copy of a Pod runs on all or some specific nodes in the Kubernetes cluster.

When you add a new node to the cluster, the DaemonSet automatically adds a Pod to it. Similarly, if a node is removed from the cluster, the Pod is garbage collected. DaemonSets are ideal for running system daemons like log collectors, monitoring agents, or any service that needs to run on every node.

Example: Suppose you want to deploy a log collection agent on every node in your cluster to aggregate logs.

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: log-collector-daemonset
spec:
  selector:
    matchLabels:
      name: log-collector
  template:
    metadata:
      labels:
        name: log-collector
    spec:
      containers:
      - name: log-collector-container
        image: log-collector:latest
        volumeMounts:
        - name: varlog
          mountPath: /var/log
      volumes:
      - name: varlog
        hostPath:
          path: /var/log

This DaemonSet configuration ensures that the log collector runs on every node, monitoring the host’s /var/log directory.

Key Differences

  • Purpose: ReplicaSets ensure a specified number of Pod replicas are running, primarily used for stateless applications. DaemonSets ensure a Pod runs on all or some nodes, used for node-level tasks.
  • Scaling: With ReplicaSets, you control the number of replicas, scaling up or down as needed. DaemonSets automatically scale with the cluster, adding or removing Pods based on the number of nodes.
  • Node Coverage: ReplicaSets don’t guarantee that Pods are distributed across all nodes. DaemonSets ensure that each node in the cluster runs a copy of the specified Pod.

Conclusion

While both DaemonSets and ReplicaSets are crucial for Kubernetes workload management, they serve distinct roles. ReplicaSets are about maintaining a set number of identical Pods, ensuring that your application can handle the load and remain available.

DaemonSets are about node coverage, ensuring that each node in your cluster runs critical services necessary for the system’s and applications’ performance and monitoring.

As you delve deeper into Kubernetes, understanding these and other abstractions will become second nature, allowing you to architect and manage your containerized applications effectively in complex environments.

Understanding Kubernetes: ReplicaSet vs Deployment

2 ways to list all the containers in Kubernetes cluster

ClusterIP NodePort LoadBalancer: Kubernetes Service Types