When to Use Deployments,StatefulSets, and DaemonSets on Kubernetes

Описание к видео When to Use Deployments,StatefulSets, and DaemonSets on Kubernetes

This videos covers how to create deployments, statefulsets, and daemonsets with yaml. Also explains differences between deployments vs statefulsets vs daemonsets.

#kubernetes #devops #k8s #kubernetestutorial

00:00 Intro
00:04 What are Deployments in Kubernetes? (example of a deployment)
01:20 What are StatefulSets in Kubernetes? (example of a stateful application)
02:52 What are DaemonSets in Kubernetes? (example of a daemonset)
04:16 Outro

What Are Deployments?
First up, Deployments! Deployments are the go-to resource for stateless applications. They ensure that your application pods are up and running as expected and make scaling and updates straightforward. Think of a Deployment as the manager of your application pods.

Use cases include running web servers, APIs, or any stateless app where you don’t care which pod handles a request.

Example YAML:

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:latest
ports:
containerPort: 80

This Deployment creates three replicas of an NGINX web server. To apply it, use the kubectl apply command.

Command:
kubectl apply -f nginx-deployment.yaml

Each pod name is a combination of the Deployment name, a unique identifier, and a random string.

StatefulSets for Stateful Applications

These are designed for applications that need stable, unique pod identities or persistent storage. Common use cases include databases, message queues, and other stateful workloads.

StatefulSets ensure each pod has a unique identity, like mongo-0, mongo-1, and mongo-2, and maintain order during scaling or updates.

Example YAML:

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mongo
spec:
serviceName: "mongo"
replicas: 3
selector:
matchLabels:
app: mongo
template:
metadata:
labels:
app: mongo
spec:
containers:
name: mongo
image: mongo
ports:
containerPort: 27017
volumeMounts:
name: mongo-pvc
mountPath: /data/db
volumeClaimTemplates:
metadata:
name: mongo-pvc
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi

Notice the volumeClaimTemplates section? It ensures that each pod gets its own persistent volume. Apply it with the same command as before.

Command:

kubectl apply -f mongodb-sts.yaml

DaemonSets for Node-Level Tasks

A DaemonSet ensures that a copy of a pod runs on every node in your cluster. This is perfect for tasks like logging, monitoring, or running a network proxy.

With a DaemonSet, you can automatically deploy system-level tasks across your nodes.

Example YAML:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-daemonset
spec:
selector:
matchLabels:
app: fluentd
template:
metadata:
labels:
app: fluentd
spec:
containers:
name: fluentd
image: fluentd:latest
resources:
limits:
memory: 200Mi
cpu: 100m

This DaemonSet deploys Fluentd, a popular log collector, to every node in your cluster. Pods created by DaemonSets follow a predictable naming convention based on the DaemonSet's name and the node on which they are running.

Command:
kubectl apply -f fluentd-daemonset.yaml

Differences between Deployments, StatefulSets, and DaemonSets in Kubernetes

Deployments

Purpose: Used to manage stateless applications. Ideal for workloads where all replicas are identical.
Behavior: Ensures a specified number of identical pods are running and handles updates, rollbacks, and scaling.
Use Case: Web servers, REST APIs, or any app without a requirement for stable network identities or persistent storage.

StatefulSets

Purpose: Designed for stateful applications that need stable network identities and persistent storage.
Behavior: Maintains stable pod names (e.g., pod-0, pod-1) and storage volumes that persist across restarts.
Use Case: Databases, message queues, and any app that requires order, uniqueness, or stable storage.

DaemonSets
Purpose: Ensures that a copy of a pod runs on every (or selected) node in the cluster.
Behavior: Automatically adds pods to new nodes and removes them from nodes that are deleted.
Use Case: Log collectors, monitoring agents, and network proxies that need to run on all nodes.

DevOps Tips and Tricks is a YouTube channel dedicated to helping developers and IT professionals learn about DevOps. We create short, informative videos that cover a wide range of topics, from the basics of DevOps to more advanced concepts like Kubernetes and cloud computing. Our goal is to make DevOps accessible to everyone, regardless of their experience level.

Комментарии

Информация по комментариям в разработке