Kubernetes Learnings & Reference Guide

1. Core Concepts & Hierarchy

The Structural Hierarchy

Labels, Names & Selectors

In Kubernetes, there are two main ways to wire resources together:

Deployment Wiring Rule: The matchLabels in your selector must match the labels in your template. If they don't, the Deployment will try to create a Pod, won't recognize the Pod it just created, and will get stuck in a loop trying to create more.

2. Architecture & Components

Control Plane

Worker Nodes

External & Other Components

3. Networking & Services

Basic Networking Flow

Service (Layer 4)

A Kubernetes Service is an internal or external network abstraction that exposes a set of Pods. You need it when Pods are replicated, may restart, or when you need stable discovery and load balancing.

A Service is a distributed, cluster-wide routing rule. It causes the cluster to write routing rules (tables) on nodes. Ingress sends traffic to the Service IP, which uses kernel routing rules to reach the Pod IP. The Service IP is a virtual IP that exists conceptually in the cluster.

Port Definitions

Port Type Analogy Description
targetPort Internal room number in a building The Pod container port (e.g., nginx port)
port Building's internal doorway The K8s Service port (inside the cluster)
nodePort Main gate from the street External access port opened on every node (Range 30000–32767)

Service Types Explained

Ingress (Layer 7)

Alternative to LoadBalancer and NodePort to expose services outside the cluster. It provides HTTP/HTTPS routing, TLS termination, and virtual hosts. Requires an Ingress Controller (e.g., NGINX, Traefik) which runs as pods inside the cluster.

LoadBalancer vs Ingress

Every service exposed externally gets its own cloud load balancer. Typically, you deploy one external load balancer in front of the Ingress Controller to provide a stable internet entry point. The Ingress Controller then routes traffic internally to many services.

The Routing Chain

IngressController → watches → Ingress → points to → Service → selects → Pods
Deployment → manages → Pods

Data Flow: Ingress → Service → Secret (for TLS)
Resource Usage: Pods/Deployments/Ingress → may use → Secrets

Service Discovery & DNS

Allows apps to find each other automatically. Kubernetes injects a custom /etc/resolv.conf into every Pod. This points to the internal DNS service (CoreDNS) and includes search domains (e.g., default.svc.cluster.local), allowing you to curl by service name.

Cross-Namespace Rules:

ExternalDNS: Automates DNS updates for external providers (AWS Route 53, Cloudflare) when services/ingresses get new IPs.

4. Workload Management

Workload Types

Probes

Volumes in Deployments

A volume defined in a Deployment (Pod spec) is available to all containers, but is mounted only in the containers that list it in volumeMounts.

5. Scheduling & Placement

6. Configuration, Storage & Security

Configuration & Storage

Secrets

Can be created via YAML (using Base64 encoding) or CLI:

# CLI Example for DB Passwords:
echo -n "root" > ./username.txt
echo -n "password" > ./password.txt
kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt

# CLI Example for SSL:
kubectl create secret generic ssl-certificate --from-file=ssh-privatekey=~/.ssh/id_rsa --ssl-cert-=ssl-cert=mysslcert.crt

Security & Resource Limits

7. Extensions

8. Common Troubleshooting & Commands