Deployment

Use Case: Best suited for stateless applications where the identity of individual pods doesn't matter.

Features:

  • Ensures the specified number of replicas (pods) are running at any given time.
  • Handles rolling updates and rollbacks.
  • Pods are identical and can be created and destroyed in any order.

Example: A web server application like nginx or a frontend service where you want multiple instances for load balancing.

YAML Configuration


apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
	matchLabels:
	app: nginx
template:
	metadata:
	labels:
		app: nginx
	spec:
	containers:
	- name: nginx
		image: nginx:1.14.2
			  

StatefulSet

Use Case: Used for stateful applications where each pod requires a stable identity or persistent storage.

Features:

  • Pods are created in order and maintain a unique, stable identity (like pod-0, pod-1).
  • Useful for databases and applications that require persistent storage, where the identity and storage must remain consistent.
  • Supports the retention of network identities and the ordering of deployment.

Example: A MySQL database where each instance (pod) requires a unique identity and must maintain its own data.

YAML Configuration


apiVersion: apps/v1
kind: StatefulSet
metadata:
	name: mysql-statefulset
spec:
	serviceName: "mysql"
	replicas: 3
	selector:
	matchLabels:
		app: mysql
	template:
	metadata:
		labels:
		app: mysql
	spec:
		containers:
		- name: mysql
		image: mysql:5.7
		volumeMounts:
		- name: mysql-persistent-storage
			mountPath: /var/lib/mysql
	volumeClaimTemplates:
	- metadata:
		name: mysql-persistent-storage
	spec:
		accessModes: ["ReadWriteOnce"]
		resources:
		requests:
			storage: 1Gi
			  

DaemonSet

Use Case: Ensures that a copy of a pod runs on all (or some) nodes in a cluster. Ideal for system-level applications.

Features:

  • Ensures that one pod is created on every node (or specific nodes) in the cluster.
  • Used for tasks like logging, monitoring, or other node-wide services.

Example: A fluentd logging agent where each node requires its own logging instance, or Prometheus node exporter for monitoring.

YAML Configuration


apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-daemonset
spec:
selector:
	matchLabels:
	name: fluentd
template:
	metadata:
	labels:
		name: fluentd
	spec:
	containers:
	- name: fluentd
		image: fluent/fluentd
			  

Key Differences

Feature Deployment StatefulSet DaemonSet
Purpose For stateless applications For stateful applications To run on every node
Pod Identity No stable identity Stable, unique identity No identity, runs on every node
Pod Ordering No specific order Pods are created in sequence One pod per node
Scaling Horizontal scaling, flexible Scales based on unique pods No scaling, fixed per node
Examples Web servers, microservices Databases, Zookeeper Monitoring, logging agents

When to Use

  • Deployment: When your application is stateless, like an Nginx web server.
  • StatefulSet: When your application needs persistent storage and stable identity, like a database.
  • DaemonSet: When you want a service like monitoring or logging to run on all nodes in your cluster.