k8s_random_notes
Kubernetes random notes
Kubernetes is designed to abstract infrastructure, so your apps donβt care if theyβre running on:
- Bare-metal physical machines
- VMs
- Cloud-managed Kubernetes services (like GKE, EKS, AKS)
Kubernetes thinks in Pods and Nodes, not physical hosts vs virtual machines.
Core Concepts
π₯οΈ Cluster
- A collection of machines (nodes) managed by Kubernetes.
- Includes the control plane and worker nodes.
π§± Node
- A physical or virtual machine in the cluster.
- Runs:
kubelet- container runtime (e.g. containerd)
kube-proxyfor networking
π¦ Pod
- The smallest deployable unit in Kubernetes.
- Wraps one or more containers sharing:
- Network namespace (same IP)
- Storage volumes (if defined)
π ReplicaSet
- Maintains a stable set of replica Pods.
- Ensures a specified number of Pods are running at all times.
π Deployment
- Manages ReplicaSets and enables rolling updates, rollback, and scaling.
- Declarative management of Pods.
πΉ Grouping, Configuration & Networking
ποΈ Namespace
- Logical partitions in a cluster.
- Useful for multi-tenancy and organizing resources (e.g. dev, staging, prod).
π§ ConfigMap
- Stores non-sensitive config data (like app settings, file names, flags).
π Secret
- Stores sensitive data (like passwords, tokens, keys).
- Base64-encoded by default.
π‘ Service
- A stable network abstraction to expose Pods.
- Types:
- ClusterIP (default, internal only)
- NodePort (exposes on a nodeβs IP/port)
- LoadBalancer (cloud providers)
- Headless (DNS without a ClusterIP)
π Ingress
- Exposes HTTP/HTTPS traffic externally.
- Allows routing based on domain or path.
πΉ Storage & Scheduling
π Volume
- Temporary or persistent storage for a Pod.
- Example types:
emptyDir,hostPath,persistentVolumeClaim,nfs, cloud storage.
π¦ PersistentVolume (PV) / PersistentVolumeClaim (PVC)
- PV: A piece of storage in the cluster.
- PVC: A request for storage by a user.
- Decouples how storage is provisioned and how itβs consumed.
π Scheduler
- Decides which node a Pod should run on based on resources, affinity, taints, etc.
πΉAdvanced Concepts
𧬠Custom Resource (CR)
- A user-defined API object.
- Lets you create types like
ModelTrainingJob,DatabaseCluster, etc.
π§Ύ CustomResourceDefinition (CRD)
- The schema used to define a new Custom Resource type.
- Once defined, K8s treats your resource like built-in ones.
π§ Controller
- Watches resources (like Pods, Deployments, or your CRs) and reconciles desired vs actual state.
- Keeps the system in the correct state.
- Runs a control loop: "observe β compare β act".
π§βπ§ Operator
- Combines:
- CRDs (define your custom API)
- Controllers (logic to manage CRs)
- Encapsulates domain-specific logic for managing complex apps (e.g. PostgreSQL, Kafka).
- Operators act like K8s-native automation scripts.
πΉ Ecosystem & Add-Ons
| Concept | Description |
|---|---|
| π Job / CronJob | For one-time or scheduled tasks |
| π‘οΈ Role / ClusterRole | Permissions for resources |
| π€ ServiceAccount | Identity for processes in Pods |
| π NetworkPolicy | Rules about how Pods communicate |
| β’οΈ Taints & Tolerations | Control Pod placement |
| π― Affinity / Anti-Affinity | Influence Pod scheduling (e.g. spread across zones) |
| π§΅ InitContainers | Run before the main container starts |
| π‘ DaemonSet | Run a Pod on every node |
| π StatefulSet | Like Deployment but with stable identities (e.g. DBs) |
| π§ͺ Sidecar Container | Helper containers in a Pod (e.g. logging, proxies) |
| π Metrics Server | Collects CPU/mem usage for autoscaling |
| π HorizontalPodAutoscaler | Auto-scales Pods based on metrics |
| ποΈ Helm | A package manager for Kubernetes apps |
| βοΈ Kustomize | Native Kubernetes YAML customization tool |
| π¦ Admission Controllers | Intercepts API requests (validating/mutating) |
| π§ͺ Etcd | The backend key-value store for Kubernetes cluster state |
π§ How These All Relate (Conceptual Diagram)
[Kubernetes Cluster]
β
βββ Namespaces
β βββ Groups resources logically
β
βββ Core Resources
β βββ Pod β Smallest compute unit
β βββ Deployment β Manages ReplicaSets & updates
β βββ ReplicaSet β Maintains # of Pods
β βββ Service β Exposes Pods
β βββ Ingress β Routes traffic to Services
β βββ ConfigMap / Secret β Inject config/env into Pods
β
βββ Storage Resources
β βββ Volume / PersistentVolumeClaim
β
βββ Custom Resources (CRs)
β βββ Defined by: CustomResourceDefinition (CRD)
β βββ Watched by: Custom Controller (e.g. Operator)
β
βββ Control Plane
βββ API Server β Everything talks to it
βββ Scheduler β Assigns Pods to nodes
βββ Controller Manager β Reconciliation loops
π³ What is a Container Runtime?
- A container runtime is the low-level software responsible for running containers on a host machine.
- A container runtime is what Kubernetes uses to turn your container images into running processes inside Pods. It handles image pulling, process isolation, resource limits, and lifecycle control.
- In a Kubernetes context, itβs what Nodeβs kubelet uses to create, start, stop, and delete containers.
What a container runtime like containerd does:
| Task | Description |
|---|---|
| π§± Image Handling | Pulls container images (e.g. from Docker Hub or private registry) |
| π¨ Container Lifecycle | Creates, starts, stops, and deletes containers |
| π§ Namespace Setup | Sets up isolated process, network, mount namespaces (Linux features) |
| π§ Resource Limits | Applies cgroups (CPU/mem limits) to containers |
| π Networking Hook | Connects containers to virtual networks (via CNI plugin) |
| π¦ Storage Mounting | Mounts volumes and file systems into the container |
| π‘ Communication | Exposes an API for Kubernetes (kubelet) to control containers |
π§ Common Container Runtimes
| Runtime | Notes |
|---|---|
| containerd | Fast, lightweight. Official CNCF project. Widely used (even by Docker) |
| Docker | Originally the full stack (runtime + tools). Now replaced in K8s by containerd |
| CRI-O | Lightweight runtime specifically for Kubernetes (follows CRI) |
| runc | Low-level OCI runtime used by containerd and Docker to run containers |
| gVisor, Kata Containers | More secure/isolated runtimes (sandboxed) |
π§© How It Fits Into Kubernetes
[ Kubernetes Node ]
βββ kubelet
β βββ talks to container runtime via CRI (Container Runtime Interface)
βββ containerd / CRI-O
β βββ talks to runc to actually launch the container
βββ container
βββ Runs your app inside isolated namespaces with limits and network rules
Kubernetes uses the CRI (Container Runtime Interface) to abstract away which runtime it uses β so you can plug in different runtimes (containerd, CRI-O, etc.).
π» What does a single physical node contain?
A physical machine may run:
- One or more virtual machines (if using virtualization like KVM, VMware, etc.)
- Or it might run Kubernetes directly (bare-metal setup)
Each Node in Kubernetes is typically a:
VM (most common in cloud setups)
or
Physical host (bare-metal Kubernetes)
On each Node, you will find:
- OS + container runtime (like
containerd) kubelet(talks to control plane)kube-proxy(networking rules)- Your scheduled Pods, each containing 1 or more containers
π¦ Do we need VMs if we have containers?
Not necessarily!
| Scenario | Do you need VMs? |
|---|---|
| Cloud Kubernetes (e.g. EKS, GKE) | Yes (your nodes are VMs in EC2 or GCE) |
| Bare-metal Kubernetes | No (K8s runs directly on physical machines) |
| Local dev (e.g. kind/minikube) | Usually inside a local VM or Docker container |
So: VMs and containers solve different problems:
- VMs: Isolate full OS environments
- Containers: Isolate application-level processes, much lighter
Kubernetes runs on VMs or physical nodesβit doesn't care. What it cares about is abstracting compute capacity.
π§ Can a container belong to different Pods?
π No.
- Each container belongs to exactly one Pod.
- A Pod is the unit of deployment and resource allocation.
- Containers in different Pods are totally independent (even if they run the same image).
π« Can one Pod span multiple nodes (machines)?
π No.
- A Pod is always scheduled onto exactly one Node.
- All containers inside that Pod live on the same machine.
- This is by design for tight coupling (e.g., sidecars, shared volumes, local communication)
So if you need to scale across nodes:
- Use multiple Pods, not one big multi-node Pod.
- Let a Service abstract across those Pods.
π§± 6. Whatβs the real boundary then?
| Concept | Physical Resource? | Virtual / Logical Resource? |
|---|---|---|
| Physical Machine | β Yes | β No |
| Virtual Machine | β Yes | β Created via hypervisor |
| Node (in K8s) | β Yes (bare metal or VM) | β Not virtual, it's real |
| Pod | β No | β Logical unit running on one Node |
| Container | β No | β Logical unit inside Pod |
| Namespace / Service / Deployment / VolumeClaim | β No | β Purely logical |
π§ TL;DR Mental Model
[Physical Hardware]
βββ [Optional: VMs]
βββ [Kubernetes Node]
βββ [Pods]
βββ [Containers]
β
Each Pod = runs on one Node only
β
Each Node = can run many Pods
β
Containers = belong to only one Pod
β
Physical vs virtual = blurred on purpose to keep K8s cloud-agnostic
π’ What is Multi-Tenancy in Kubernetes?
Multi-tenancy = multiple teams, users, or applications sharing the same Kubernetes cluster, but:
- Isolated from each other
- With controlled access
Think of it like:
One office building with many companies β they share the building (cluster) but each has their own space (namespace), keys (permissions), and policies.
π§© Why use multi-tenancy?
- Avoid running separate clusters for each team/app
- Lower cost, centralized infrastructure
- But still provide logical and secure isolation
ποΈ What are Namespaces?
Namespaces are virtual clusters within a Kubernetes cluster.
Each namespace:
- Contains its own Pods, Deployments, Services, Secrets, ConfigMaps, etc.
- Has its own quotas and policies
- Can be locked down to specific users/teams
Think of it as:
βThis is the dev teamβs sandbox.β
βThis is production. Only ops can touch it.β
π What is RBAC (Role-Based Access Control)?
RBAC = The way Kubernetes manages user permissions.
It defines who can do what in which namespace or cluster.
π Core Components:
| Component | What it means |
|---|---|
Role |
Defines permissions (e.g., "can read pods", "can create secrets") within a namespace |
ClusterRole |
Same as Role, but at cluster scope (or shared across namespaces) |
RoleBinding |
Assigns a Role to a user/service account in a namespace |
ClusterRoleBinding |
Same as RoleBinding, but for ClusterRole (across namespaces) |
π¦ Example:
Letβs say you have a dev team and prod team:
Namespaces:
- dev
- prod
Users:
- alice
- bob
Roles:
- dev-reader (can view stuff in dev)
- prod-admin (can manage everything in prod)
RoleBindings:
- dev-reader is bound to alice in dev
- prod-admin is bound to bob in prod
Result:
- β Alice can view Pods in dev
- β Alice cannot do anything in prod
- β Bob can manage everything in prod
- β Bob cannot access dev (unless separately bound)
π Multi-Tenancy = Namespaces + RBAC
| Part | What it does |
|---|---|
| Namespaces | Isolate workloads and configs |
| RBAC | Control who can access and operate on those workloads |
Together they create logical and security boundaries for safe multi-tenancy.