VibeKoding / Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat / Principles of Kubernetes OrchestrationPrinciples of Kubernetes Orchestration
VK

Principles of Kubernetes OrchestrationPrinciples of Kubernetes Orchestration

๐Ÿ“š Ensiklopedia ยท Fondasi KuatEnsiklopedia ยท Fondasi Kuat ๐ŸŒ Dual Bahasa (ID / EN) โšก VibeKoding Native

Ensiklopedia VibeKoding: Principles of Kubernetes Orchestration.Ensiklopedia VibeKoding: Principles of Kubernetes Orchestration.

๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

Docker solved the "packaging" problem, and Kubernetes solves the "management" problem. When you have dozens or hundreds of containers that need deployment, scaling, and fault recovery, manual management is impractical. Kubernetes (K8s) is the "operating system" for containers, automating the deployment, scaling, and operations of containerized applications.Docker solved the "packaging" problem, and Kubernetes solves the "management" problem. When you have dozens or hundreds of containers that need deployment, scaling, and fault recovery, manual management is impractical. Kubernetes (K8s) is the "operating system" for containers, automating the deployment, scaling, and operations of containerized applications.

What will you learn from this article?What will you learn from this article?

After completing this chapter, you will gain:After completing this chapter, you will gain:

ChapterContentCore Concepts
Chapter 1Why K8s is NeededChallenges of container orchestration
Chapter 2K8s ArchitectureControl plane, worker nodes, etcd
Chapter 3Core ResourcesPod, Deployment, Service, Ingress
Chapter 4Declarative ManagementYAML, kubectl, control loops
Chapter 5Operations PracticesRolling updates, HPA, health checks

------

1. Motivation for Kubernetesing1. Motivation for Kubernetesing

Docker makes packaging and running individual containers simple, but when you face the following scenarios, manual management becomes inadequate:Docker makes packaging and running individual containers simple, but when you face the following scenarios, manual management becomes inadequate:

ChallengeDescriptionK8s Solution
Multi-instance deploymentA service needs 10 replicas runningDeployment automatically manages replica counts
Fault recoveryA container crashes and needs automatic restartControllers automatically detect and recreate Pods
Service discoveryContainer IPs change; how to find each other?Service provides stable DNS and IP
Rolling updatesCan't stop service when updating versionsGradually replace old Pods with zero downtime
Elastic scalingAuto-scale during traffic peaksHPA automatically adjusts replica count based on CPU/memory
Resource schedulingPlace containers on the most suitable machinesScheduler intelligently schedules
๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

You don't tell K8s "start 3 containers" (imperative). Instead, you tell it "I want 3 replicas running" (declarative). K8s continuously monitors to ensure the actual state matches your declared desired state. If a Pod crashes, it automatically creates a new one to replace it.You don't tell K8s "start 3 containers" (imperative). Instead, you tell it "I want 3 replicas running" (declarative). K8s continuously monitors to ensure the actual state matches your declared desired state. If a Pod crashes, it automatically creates a new one to replace it.

------

2. Kubernetes Architecture2. Kubernetes Architecture

A K8s cluster consists of a Control Plane and Worker Nodes.A K8s cluster consists of a Control Plane and Worker Nodes.

Complete Path of a RequestComplete Path of a Request

CODE
User Request โ†’ Ingress Controller โ†’ Service โ†’ kube-proxy โ†’ Pod (Container) โ†‘ Endpoint list (maintained by Service)

------

3. Core Resource Objects3. Core Resource Objects

K8s describes the cluster's desired state through various "resource objects."K8s describes the cluster's desired state through various "resource objects."

Resource Object CategoriesResource Object Categories

CategoryResourcesPurpose
WorkloadsPod, Deployment, StatefulSet, DaemonSet, JobRun applications
NetworkingService, Ingress, NetworkPolicyService discovery and traffic management
ConfigurationConfigMap, SecretConfiguration and sensitive data management
StoragePersistentVolume, PersistentVolumeClaimPersistent storage
SchedulingNode, Namespace, ResourceQuotaResource isolation and limits

------

4. Declarative Management and kubectl4. Declarative Management and kubectl

Reconciliation LoopReconciliation Loop

K8s's core working mechanism is the reconciliation loop:K8s's core working mechanism is the reconciliation loop:

CODE
Observe โ†’ Diff โ†’ Act โ†’ Observe... โ†“ โ†“ โ†“ Read actual Compare Execute state with corrective desired actions state

You declare replicas: 3. The controller discovers only 2 Pods running and creates 1 new one. This loop executes every few seconds, ensuring the system always converges toward the desired state.You declare replicas: 3. The controller discovers only 2 Pods running and creates 1 new one. This loop executes every few seconds, ensuring the system always converges toward the desired state.

Common kubectl CommandsCommon kubectl Commands

CommandPurposeExample
kubectl apply -fApply YAML configurationkubectl apply -f deployment.yaml
kubectl getList resourceskubectl get pods -o wide
kubectl describeView resource detailskubectl describe pod my-app-xxx
kubectl logsView Pod logskubectl logs -f my-app-xxx
kubectl execEnter Pod terminalkubectl exec -it my-app-xxx -- sh
kubectl deleteDelete resourceskubectl delete -f deployment.yaml
kubectl scaleManual scalingkubectl scale deploy my-app --replicas=5
๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

kubectl create is imperative โ€” "create this resource," and will error if it already exists. kubectl apply is declarative โ€” "ensure the resource is in this state," creating if it doesn't exist or updating if it does. In production, you should always use apply.kubectl create is imperative โ€” "create this resource," and will error if it already exists. kubectl apply is declarative โ€” "ensure the resource is in this state," creating if it doesn't exist or updating if it does. In production, you should always use apply.

------

5. Operations Practices5. Operations Practices

5.1 Rolling Updates and Rollbacks5.1 Rolling Updates and Rollbacks

Deployment uses a rolling update strategy by default: gradually creating new version Pods while gradually terminating old version Pods.Deployment uses a rolling update strategy by default: gradually creating new version Pods while gradually terminating old version Pods.

yaml
spec: strategy: type: RollingUpdate rollingUpdate: maxSurge: 1 # At most create 1 extra Pod maxUnavailable: 0 # No Pods allowed to be unavailable
OperationCommand
Update imagekubectl set image deploy/my-app app=my-app:2.0
View update statuskubectl rollout status deploy/my-app
View revision historykubectl rollout history deploy/my-app
Rollback to previous versionkubectl rollout undo deploy/my-app

5.2 Auto Scaling (HPA)5.2 Auto Scaling (HPA)

HPA (Horizontal Pod Autoscaler) automatically adjusts the number of Pod replicas based on CPU, memory, or custom metrics.HPA (Horizontal Pod Autoscaler) automatically adjusts the number of Pod replicas based on CPU, memory, or custom metrics.

yaml
apiVersion: autoscaling/v2 kind: HorizontalPodAutoscaler metadata: name: my-app-hpa spec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: my-app minReplicas: 2 maxReplicas: 10 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70

5.3 Health Checks (Probes)5.3 Health Checks (Probes)

K8s monitors Pod health through three types of probes:K8s monitors Pod health through three types of probes:

ProbePurposeFailure Consequence
livenessProbeDetect if container is aliveRestart container
readinessProbeDetect if container is readyRemove from Service, don't receive traffic
startupProbeDetect if container has finished startingDon't run other probes during startup
๐Ÿ’ก Tips Praktis๐Ÿ’ก Pro Tip

Without health check probes configured, K8s can only determine health by whether the process exists. But often the process is still running while the service is no longer responding (like deadlocks, edge of OOM). Configuring livenessProbe allows K8s to automatically restart these "zombie" containers.Without health check probes configured, K8s can only determine health by whether the process exists. But often the process is still running while the service is no longer responding (like deadlocks, edge of OOM). Configuring livenessProbe allows K8s to automatically restart these "zombie" containers.

------

SummarySummary

Kubernetes is the de facto standard for container orchestration, and understanding its core concepts is the foundation of cloud-native development.Kubernetes is the de facto standard for container orchestration, and understanding its core concepts is the foundation of cloud-native development.

Key takeaways from this chapter:Key takeaways from this chapter:

  1. Declarative management: Tell K8s "what I want," not "how to do it" โ€” the control loop automatically convergesDeclarative management: Tell K8s "what I want," not "how to do it" โ€” the control loop automatically converges
  2. Layered architecture: Control plane makes decisions, worker nodes execute, etcd stores stateLayered architecture: Control plane makes decisions, worker nodes execute, etcd stores state
  3. Core resources: Pod (smallest unit), Deployment (replica management), Service (service discovery), Ingress (external entry point)Core resources: Pod (smallest unit), Deployment (replica management), Service (service discovery), Ingress (external entry point)
  4. Operations automation: Rolling updates with zero downtime, HPA elastic scaling, automatic fault recovery with probesOperations automation: Rolling updates with zero downtime, HPA elastic scaling, automatic fault recovery with probes
  5. Configuration separation: ConfigMap and Secret decouple configuration from imagesConfiguration separation: ConfigMap and Secret decouple configuration from images
  6. Further ReadingFurther Reading

    • [Kubernetes Official Documentation](https://kubernetes.io/docs/) - The most authoritative reference[Kubernetes Official Documentation](https://kubernetes.io/docs/) - The most authoritative reference
    • [Kubernetes the Hard Way](https://github.com/kelseyhightower/kubernetes-the-hard-way) - Build a K8s cluster from scratch manually[Kubernetes the Hard Way](https://github.com/kelseyhightower/kubernetes-the-hard-way) - Build a K8s cluster from scratch manually
    • [The Illustrated Children's Guide to Kubernetes](https://www.cncf.io/phippy/) - CNCF's fun beginner's guide[The Illustrated Children's Guide to Kubernetes](https://www.cncf.io/phippy/) - CNCF's fun beginner's guide
    • [Kubernetes Patterns](https://www.oreilly.com/library/view/kubernetes-patterns-2nd/9781098131678/) - K8s design patterns[Kubernetes Patterns](https://www.oreilly.com/library/view/kubernetes-patterns-2nd/9781098131678/) - K8s design patterns