Summary
Overview
This course session provides a hands-on, practical deep dive into Kubernetes resource management, focusing on the creation, scaling, and deletion of Deployments, StatefulSets, and DaemonSets. The instructor emphasizes YAML syntax accuracy as a primary source of troubleshooting errors, walks through real-time cluster operations using kubectl, and highlights key behavioral differences between resource types—particularly the requirement to scale StatefulSets to zero before deletion. The session concludes with a review of core concepts including namespace isolation, label-based resource querying, and cluster-wide application deployment via DaemonSets.
Topic (Timeline)
1. Introduction and YAML Syntax Emphasis [00:00:00 - 00:01:04]
- Instructor begins by noting pending participants and recapping the prior lesson: namespace creation and pod manifest deployment.
- Highlights that ~50% of Kubernetes troubleshooting stems from YAML syntax errors.
- Stresses the importance of learning to read and debug manifests—even those created by others (e.g., Helm
values.yaml)—to identify structural issues.
2. Deployment and ReplicaSet Management [00:01:04 - 00:05:00]
- Guides students through creating a Deployment using a YAML manifest on a fresh Minikube cluster.
- Demonstrates
kubectl get deployments,kubectl get replicasets, andkubectl describeto inspect resource relationships. - Explains that Deployments automatically create ReplicaSets with unique hash suffixes, and each Pod inherits a unique hash from its ReplicaSet.
- Shows scaling a Deployment from 3 to 4 replicas using
kubectl scale deployment <name> --replicas=4without modifying the YAML file. - Verifies changes via
describeand observes new Pod hash generation. - Scales back to 3 replicas and confirms deletion of the most recently created Pod.
- Deletes the Deployment, which automatically removes its associated ReplicaSet and Pods.
3. StatefulSet Creation and Behavior [00:05:00 - 00:15:50]
- Instructs students to create a StatefulSet by modifying the prior Deployment YAML file (e.g.,
nginx-stapleset.yaml). - Clarifies that ReplicaSet controllers are deprecated; Deployments now manage ReplicaSets.
- Highlights key differences: StatefulSets assign stable, sequential names (e.g.,
nginx-stapleset-0,nginx-stapleset-1) without hash suffixes. - Demonstrates applying the StatefulSet, correcting a common error (incorrect replica count: 3 → 2).
- Uses
kubectl get pods -l app=nginxto query resources by label. - Explains label usage: labels enable targeted resource discovery; missing labels cause resources to be excluded from queries.
- Scales StatefulSet from 2 to 1 pod: confirms deletion of the highest-indexed pod (
-1), leaving-0(always deletes backward). - Scales from 1 to 10 pods: observes sequential creation (
-0to-9) and confirms no deletion events for existing pods. - Emphasizes critical behavior: StatefulSets must be scaled to zero replicas before deletion—otherwise, deletion may fail silently.
- Warns that Helm charts often fail to delete StatefulSets properly because they do not auto-scale to zero, leaving running pods.
4. DaemonSet Deployment and Cluster-Wide Behavior [00:15:50 - 00:20:40]
- Sets up a 3-node Minikube cluster (
minikube start --nodes=3) to demonstrate cluster-wide deployment. - Confirms presence of CNI (kindnet) and kube-proxy on all nodes.
- Deploys Fluentd as a DaemonSet to illustrate one-pod-per-node behavior.
- Observes that DaemonSet Pods may have different creation times due to image pull delays across nodes.
- Shows
kubectl get daemonsetsandkubectl get pods -o wideto verify pod distribution. - Deletes the DaemonSet using
kubectl delete daemonset fluentd(unlike StatefulSets, no scale-to-zero required). - Notes that DaemonSets are ideal for cluster-level services (logging, monitoring, networking).
5. Review and Lesson Wrap-up [00:20:40 - 00:22:40]
- Summarizes key learnings:
- Namespace isolation and resource cleanup via namespace deletion.
- Label-based resource querying (
kubectl get all -l app=...). - Deployments manage ReplicaSets; scaling and deletion are automatic.
- StatefulSets use stable, ordered names and require scale-to-zero before deletion.
- DaemonSets ensure one Pod per node for cluster-wide services.
- Announces upcoming break and transition to Lesson 5.
Appendix
Key Principles
- YAML Syntax: Critical to success; errors are common and often non-descriptive. Always validate structure, indentation, and field names.
- Label Queries: Use
-l key=valueto filter resources across namespaces. Missing labels = invisible in queries. - StatefulSet Deletion: Must scale replicas to 0 before
kubectl delete. Helm charts may not handle this, leading to orphaned pods. - DaemonSet Behavior: Automatically schedules one Pod per node; ideal for infrastructure services (logging, monitoring, CNI).
Tools Used
minikube start --nodes=3— Multi-node cluster setupkubectl get deployments,replicasets,statefulsets,daemonsets,podskubectl describe <resource> <name>kubectl scale deployment/statefulset <name> --replicas=Nkubectl delete -f <file>— For declarative deletionkubectl delete daemonset <name>— Direct deletion supportedkubectl get pods -l app=<label>— Label-based filtering
Common Pitfalls
- Incorrect YAML indentation (e.g.,
spec:vsspec:with wrong spacing). - Forgetting to delete StatefulSet pods before deleting the StatefulSet itself.
- Assuming
kubectl delete -fworks the same for all resource types (it does not for StatefulSets). - Misunderstanding label scope: labels are per-resource, not global; cross-namespace queries require
-A. - Assuming DaemonSet Pods deploy instantly; image pulls cause staggered creation times.
Practice Suggestions
- Recreate all four resource types (Deployment, ReplicaSet, StatefulSet, DaemonSet) from scratch without reference.
- Intentionally break YAML syntax and interpret the error output.
- Label a mix of resources and query them with
kubectl get all -l <label>. - Test StatefulSet deletion without scaling to zero—observe the behavior.
- Deploy a DaemonSet on a 2-node cluster and verify one Pod per node.