Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Kubernetes Workload Controllers: Deployment, DaemonSet, and StatefulSet

Tech May 16 2

Controllers in Kubernetes manage pods and ensure the desired state is mainatined in the cluster. They idnetify pods through labels and handle scaling, updates, and fault recovery automatically.

Controller Types

Deployment:  Suitable for long-running stateless applications. Pods are distributed randomly across the cluster.

DaemonSet:   Runs one pod on each node. When a node is removed, its pod is automatically deleted. Ideal for cluster-wide daemons like monitoring agents.

StatefulSet: Designed for stateful applications requiring stable network identities and persistent storage. Pods start in a predictable order.
# View controller schema
kubectl explain deployment
KIND:     Deployment
VERSION:  apps/v1

Deployement

Defining a Deployment in YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
        - name: nginx
          image: nginx:1.18.0
# Apply the configuration
kubectl apply -f web-deployment.yaml

# Verify pod creation
kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
web-server-7d9f4b6c2-xh4kp  1/1     Running   0          18s

# Delete a pod manually
kubectl delete pod web-server-7d9f4b6c2-xh4kp

# The controller immediately creates a replacement
kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
web-server-7d9f4b6c2-m9r2n  1/1     Running   0          10s

Scaling a Deployment

# Edit configuration directly
kubectl edit deployment web-server
# Modify: replicas: 3

# Patch using JSON
kubectl patch deployment web-server -p '{"spec":{"replicas":4}}'

# Use scale command
kubectl scale deployment web-server --replicas=2

Updating a Deployment

# Patch with new image
kubectl patch deployment web-server -p '{"spec":{"template":{"spec":{"containers":[{"image":"nginx:1.17.0","name":"nginx"}]}}}}'

# Edit configuration file
kubectl edit deployment web-server

# Set image directly
kubectl set image deployment web-server nginx=nginx:1.16.0

Rolling Back a Deployment

# View revision history
kubectl rollout history deployment web-server
deployment.apps/web-server 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>
4         <none>

# Rollback to previous version
kubectl rollout undo deployment web-server

# Rollback to specific revision
kubectl rollout history deployment web-server
# Identify target revision number

kubectl rollout undo deployment web-server --to-revision=3
deployment.apps/web-server rolled back

DaemonSet

Defining a DaemonSet in YAML

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: monitoring-agent
spec:
  selector:
    matchLabels:
      component: agent
  template:
    metadata:
      labels:
        component: agent
    spec:
      containers:
        - name: agent
          image: zabbix/zabbix-agent:5.2.6-centos

Updating a DaemonSet

# Edit configuration directly
kubectl edit daemonset monitoring-agent
daemonset.apps/monitoring-agent edited

# Patch using JSON
kubectl patch daemonset monitoring-agent -p '{"spec":{"template":{"spec":{"containers":[{"image":"zabbix/zabbix-agent:centos-5.2.4","name":"agent"}]}}}}'

# Set image directly
kubectl set image daemonset monitoring-agent agent=zabbix/zabbix-agent:centos-5.2.3

Rolling Back a DaemonSet

# Rollback to previous version
kubectl rollout undo daemonset monitoring-agent

# Rollback to specific revision
kubectl rollout undo daemonset monitoring-agent --to-revision=1

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.