--- id: mat-02-k8s-objects-and-getting-started title: "Kubernetes Objects & Getting Started Locally" --- # Kubernetes objects and getting started ## Declarative model (desired state) Kubernetes is declarative: - you declare the desired state in a manifest (YAML) - you post it to the API server - Kubernetes stores desired state - controllers continuously reconcile current state to desired state Example scenarios: - desired replicas = 10, 2 fail → controller schedules 2 new replicas - change replicas from 3 to 6 → controller schedules 3 more replicas - update app version from 1.0 to 2.0 → controller rolls out 2.0 gradually and rolls back if needed ## Pods A Pod is the atomic unit of scheduling in Kubernetes. - simplest use: run one container per pod - advanced: multiple containers in one pod share network stack (same IP), volumes, IPC/shared memory Important: - scale by increasing the number of pods, not containers inside a pod - pod lifecycle is “brutal”: pods are replaced (immutable); they do not “come back” after death ## Deployments (controllers) In practice you rarely deploy pods directly. You use higher-level controllers such as Deployments to get: - self-healing - scaling - rollouts/rollbacks Common controller names you may encounter: - Deployment - DaemonSet - StatefulSet ## Services Pods are ephemeral (they can and will die). Services provide a stable facade: - stable DNS name - load balancing - “service discovery” style routing to healthy pods ## Getting Kubernetes locally: installers overview Kubernetes is complex (control plane, etcd, kubelet, networking…). Installers help. Examples of installers/distributions: - K3s - K3d (runs K3s in Docker) - kind (Kubernetes in Docker) - MicroK8s - Minikube - Docker Desktop (Windows/Mac) ## kubectl kubectl is the CLI tool for controlling clusters via the API server. Version rule of thumb: - kubectl should be within one minor version of the cluster control plane. Config: - uses local kubeconfig file: ~/.kube/config - supports switching between clusters ## K3d quick start Create a cluster: - k3d cluster create mycluster Delete it: - k3d cluster delete mycluster Example verification commands: - kubectl get nodes - kubectl get pods --all-namespaces ## K3d complex setup Example: 3 server nodes and 5 agent nodes: - k3d cluster create complex -s 3 -a 5 K3d also supports a config file (useful for versioning and sharing setup).