Sobes.tech
Middle

What is the difference between Deployment and DaemonSet in Kubernetes?

sobes.tech AI

Answer from AI

Deployment manages a set of Pod replicas, ensuring their desired number and automatic scaling. It is well-suited for stateless applications.

DaemonSet guarantees that a copy of a Pod will be running on each node (or a subset of nodes selected by a label selector) in the cluster. It is used for background tasks such as log collection or monitoring.

Here are the key differences:

Characteristic Deployment DaemonSet
Purpose Maintaining a specified number of Pod replicas Running one Pod on each node (or subset)
Number of Pods Managed via the replicas parameter Determined by the number of nodes
Scaling Horizontal, on demand Related to adding/removing nodes
Use cases Web servers, microservices Log collectors, monitoring agents, network daemons
Update Gradual (rolling update) or replacement Automatically updates on nodes as needed

Example Deployment manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3 # Run 3 copies of Pod
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

Example DaemonSet manifest:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentbit-daemonset
spec:
  selector:
    matchLabels:
      name: fluentbit # Label for Pod selector
  template:
    metadata:
      labels:
        name: fluentbit
    spec:
      containers:
      - name: fluentbit
        image: fluent/fluent-bit:1.8 # Log collector image
        # Additional settings for log file access, etc.
      tolerations:
      # Often needed to run on master nodes
      - key: node-role.kubernetes.io/master
        effect: NoSchedule