Table of Contents
- Introduction to Kubernetes
- Kubernetes Cluster Architecture
- Pods and the Service Abstraction
- Deployments and Manifests
- Fundamental Kubernetes Objects
- Installing and Configuring Kubernetes
- Cluster Initialization
- Deploying a Kubernetes Service
- Basic Administration with kubectl
- kubectl Reference
- Complete YAML Examples
1. Introduction to Kubernetes
Kubernetes (also written K8s, where the number 8 represents the 8 letters between K and S) is an open-source container orchestration platform created by Google and now maintained by the Cloud Native Computing Foundation (CNCF).
Why Kubernetes?
Companies like Google, Netflix, Spotify, and Amazon use Kubernetes to deploy and operate thousands of services at scale. Kubernetes provides:
- Automation of container deployment, scaling, and management
- High availability through pod replication
- Self-healing: automatic restart on failure
- Dynamic horizontal scalability
- Portability: on-premise, public cloud, hybrid
Hosting Options
| Type | Description | Examples |
|---|
| Self-managed | You deploy and manage every node yourself | kubeadm on VMs |
| Managed cluster | Cloud provider manages the control plane | EKS (AWS), AKS (Azure), GKE (GCP) |
| Local / Dev | For local development | minikube, kind, k3s |
2. Kubernetes Cluster Architecture
A Kubernetes cluster consists of two types of nodes: master nodes (control plane) and worker nodes.
Cluster Architecture Diagram
graph TB
subgraph "Control Plane (Master Nodes)"
API["API Server\n(kube-apiserver)"]
SCHED["Scheduler\n(kube-scheduler)"]
CM["Controller Manager\n(kube-controller-manager)"]
ETCD["etcd\n(cluster state store)"]
API --> ETCD
API --> SCHED
API --> CM
end
subgraph "Worker Node 1"
KP1["kubelet"]
KPX1["kube-proxy"]
POD1["Pod: nginx-1"]
POD2["Pod: nginx-2"]
KP1 --> POD1
KP1 --> POD2
end
subgraph "Worker Node 2"
KP2["kubelet"]
KPX2["kube-proxy"]
POD3["Pod: nginx-3"]
KP2 --> POD3
end
API -->|"schedule pods"| KP1
API -->|"schedule pods"| KP2
KUBECTL["kubectl (admin)"] -->|"kubectl apply / get / describe"| API
Control Plane Components
| Component | Role |
|---|
| kube-apiserver | Single entry point for all cluster communication. Exposes the Kubernetes REST API. |
| etcd | Distributed key-value database storing the complete cluster state. |
| kube-scheduler | Assigns new pods to worker nodes based on available resources and constraints. |
| kube-controller-manager | Runs controllers (ReplicaSet, Node, etc.) that maintain desired state. |
Worker Node Components
| Component | Role |
|---|
| kubelet | Agent on each worker node, receives control plane instructions and manages pods. |
| kube-proxy | Manages network rules to enable communication to pods. |
| Container Runtime | Container execution engine (containerd, CRI-O). |
Why an Odd Number of Master Nodes?
In production, use 3, 5, or 7 master nodes. Must be an odd number to avoid split-brain: with 4 masters, two could vote one way and two the other, with no majority. With 5 masters, 3 can always outvote 2, guaranteeing consensus.
3. Pods and the Service Abstraction
Pods
A Pod is the basic unit of deployment in Kubernetes. A pod is a group of one or more containers that share:
- The same network space (they communicate via
localhost)
- The same storage (shared volumes)
- The same lifecycle
Pods are deployed only on worker nodes. The Scheduler automatically decides which worker node to place each pod on based on available resources (CPU, RAM, storage).
Services
A Service is a stable abstraction that exposes one or more pods under a unique IP address and DNS name. It enables:
- Providing a stable endpoint even when pods restart (their IPs change)
- Load balancing between multiple identical pods
- Exposing applications inside or outside the cluster
The Service is the “façade”: clients never talk directly to pods, they always go through the Service.
Service Types
| Type | Description | Usage |
|---|
| ClusterIP | Accessible only inside the cluster | Inter-service communication |
| NodePort | Exposes a fixed port on each node | Simple external access (dev) |
| LoadBalancer | Creates a cloud load balancer | Production on cloud |
| ExternalName | Maps to an external DNS name | Access to external services |
4. Deployments and Manifests
What Is a Deployment?
A Deployment controls the complete lifecycle of an application in the cluster. It’s declarative management: you describe the desired state in a YAML file (the manifest), and Kubernetes ensures reality matches that description.
Deployment Flow Diagram
flowchart LR
DEV["Developer\n(YAML file)"]
KUBECTL["kubectl apply -f"]
API["API Server"]
ETCD["etcd\n(desired state)"]
CM["Controller Manager\n(ReplicaSet Controller)"]
SCHED["Scheduler"]
NODE1["Worker Node 1\nkubelet → Pod A"]
NODE2["Worker Node 2\nkubelet → Pod B, C"]
DEV -->|"YAML manifest"| KUBECTL
KUBECTL -->|"HTTP POST"| API
API -->|"persists"| ETCD
ETCD -->|"notifies"| CM
CM -->|"creates ReplicaSet"| SCHED
SCHED -->|"assigns pods"| NODE1
SCHED -->|"assigns pods"| NODE2
Key Deployment Features
| Feature | Description |
|---|
| Desired State Configuration | YAML describes the target state (replica count, image, ports…) |
| ReplicaSet Management | Maintains defined number of identical pods (e.g., 3 Nginx replicas) |
| Automated Rollouts | Zero-downtime updates: pods are progressively replaced |
| Automated Rollbacks | Automatic rollback to previous version on error |
| Scaling | Manual (kubectl scale) or automatic via HPA |
| Self-Healing | Continuously compares desired vs actual state, auto-corrects |
5. Fundamental Kubernetes Objects
Object Relationships Diagram
graph TD
NS["Namespace\n(logical isolation)"]
DEP["Deployment\n(lifecycle)"]
RS["ReplicaSet\n(replica count)"]
POD["Pod\n(containers)"]
SVC["Service\n(stable endpoint)"]
CM2["ConfigMap\n(configuration)"]
SEC["Secret\n(sensitive data)"]
PV["PersistentVolume\n(storage)"]
NS --> DEP
NS --> SVC
NS --> CM2
DEP --> RS
RS --> POD
SVC -->|"selects via labels"| POD
CM2 -->|"injects config"| POD
SEC -->|"injects secrets"| POD
PV -->|"mounts volume"| POD
Main Object Descriptions
| Object | apiVersion | Description |
|---|
| Pod | v1 | Basic unit, groups 1+ containers sharing network and storage |
| Deployment | apps/v1 | Manages lifecycle and replica count of an application |
| ReplicaSet | apps/v1 | Maintains desired number of identical pods (managed by Deployment) |
| Service | v1 | Exposes pods via a stable endpoint with load balancing |
| ConfigMap | v1 | Stores non-sensitive configuration (env vars, config files) |
| Secret | v1 | Stores sensitive base64-encoded data (passwords, tokens) |
| Namespace | v1 | Logically isolates resources in the cluster |
| PersistentVolume | v1 | Persistent storage resource in the cluster |
Default Namespaces
| Namespace | Contents |
|---|
default | Resources deployed without a specified namespace |
kube-system | Kubernetes system components (API server, scheduler, DNS…) |
kube-public | Public data (e.g., bootstrap config) |
kube-node-lease | Node heartbeats |
6. Installing and Configuring Kubernetes
Hardware Prerequisites (Production)
| Resource | Minimum | Recommended |
|---|
| CPU | 2 vCPUs | 4+ vCPUs |
| RAM | 4 GB | 8 GB |
| Storage | 40 GB | 100+ GB |
| OS | Ubuntu 22.04/24.04 | Ubuntu 24.04 LTS |
Minimum Architecture for a Dev Cluster
- 1 master node (or 3 in production)
- 2 worker nodes minimum
- All nodes must be in the same network zone (same VPC/subnet)
Required Network Ports
| Port | Protocol | Usage |
|---|
22 | TCP | SSH for node administration |
443 | TCP | HTTPS (Kubernetes API) |
6443 | TCP | kube-apiserver (kubectl access) |
2379-2380 | TCP | etcd (internal communication) |
10250 | TCP | kubelet API |
30000-32767 | TCP | NodePort Services |
Installation Steps with kubeadm (Ubuntu 24.04)
On all nodes (master + workers)
# 1. Disable swap (required for Kubernetes)
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
# 2. Enable required kernel modules
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# 3. Sysctl parameters for networking
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
# 4. Install containerd (container runtime)
sudo apt-get update
sudo apt-get install -y containerd
# 5. Install kubeadm, kubelet, kubectl
sudo apt-get install -y apt-transport-https ca-certificates curl gpg
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | \
sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] \
https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | \
sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
7. Cluster Initialization
On the Master Node Only
# Initialize the cluster with network CIDR for Calico
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Install Network Plugin (Calico)
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/calico.yaml
Join Worker Nodes
After master initialization, a join command is displayed. Run it on each worker:
# Example command generated by kubeadm init
sudo kubeadm join <MASTER_IP>:6443 --token <token> \
--discovery-token-ca-cert-hash sha256:<hash>
If you missed the join command, regenerate it with:
kubeadm token create --print-join-command
Verify Cluster State
kubectl get nodes
# Expected output:
# NAME STATUS ROLES AGE VERSION
# ps2-k8s-m Ready control-plane 5m v1.30.0
# ps2-k8s-w1 Ready <none> 3m v1.30.0
# ps2-k8s-w2 Ready <none> 3m v1.30.0
8. Deploying a Kubernetes Service
Deploy an Nginx Application via kubectl
# Quick Nginx deployment creation
kubectl create deployment nginx --image=nginx --replicas=3
# Expose the deployment via a NodePort Service
kubectl expose deployment nginx --port=80 --type=NodePort
# Verify deployment
kubectl get deployments
kubectl get pods
kubectl get services
9. Basic Administration with kubectl
kubectl Command Flow
flowchart LR
USER["Administrator"] -->|"kubectl <verb> <resource>"| KUBECTL["kubectl CLI"]
KUBECTL -->|"HTTP/HTTPS :6443"| API["kube-apiserver"]
API -->|"queries / modifies"| ETCD["etcd"]
API -->|"responds"| KUBECTL
KUBECTL -->|"displays"| USER
General Syntax
kubectl [verb] [resource] [name] [flags]
Main verbs: get, describe, apply, create, delete, logs, exec, scale, rollout, port-forward
Main resources: pods, deployments, services, configmaps, namespaces, nodes, replicasets
10. kubectl Reference
Essential Commands
| Command | Description |
|---|
kubectl get nodes | List all cluster nodes with their status |
kubectl get pods | List pods in default namespace |
kubectl get pods -n kube-system | List pods in kube-system namespace |
kubectl get pods -o wide | List pods with more information (IP, node) |
kubectl get all | List all resources in current namespace |
kubectl get deployments | List all deployments |
kubectl get services | List all services |
Inspection and Debugging
| Command | Description |
|---|
kubectl describe pod <name> | Full pod details (events, config) |
kubectl describe deployment <name> | Deployment details and events |
kubectl logs <pod> | Display pod logs |
kubectl logs <pod> -f | Follow logs in real time |
kubectl logs <pod> -c <container> | Logs for a specific container in a multi-container pod |
kubectl exec -it <pod> -- bash | Interactive shell inside a pod |
kubectl get events | Display recent cluster events |
Deployment and Management
| Command | Description |
|---|
kubectl apply -f <file.yaml> | Apply a YAML manifest (create or update) |
kubectl delete -f <file.yaml> | Delete resources defined in a manifest |
kubectl delete pod <name> | Delete a pod (will be recreated by ReplicaSet) |
kubectl scale deployment <name> --replicas=5 | Change replica count |
kubectl rollout status deployment/<name> | Check rollout status |
kubectl rollout history deployment/<name> | Deployment revision history |
kubectl rollout undo deployment/<name> | Roll back to previous version |
Namespaces and Contexts
| Command | Description |
|---|
kubectl get namespaces | List all namespaces |
kubectl get pods -n <namespace> | Pods in a specific namespace |
kubectl config get-contexts | List available kubeconfig contexts |
kubectl config use-context <name> | Change active context (switch cluster) |
kubectl config current-context | Display active context |
Useful Flags
| Flag | Description |
|---|
-n <namespace> | Specify namespace |
-o wide | Extended display |
-o yaml | YAML format output |
-o json | JSON format output |
--all-namespaces or -A | All namespaces |
-l <label>=<value> | Filter by label selector |
--dry-run=client | Simulate without applying |
11. Complete YAML Examples
Simple Pod
# pod-nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
namespace: default
labels:
app: nginx
tier: frontend
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
Namespace
# namespace-dev.yaml
apiVersion: v1
kind: Namespace
metadata:
name: development
labels:
environment: dev
ConfigMap
# configmap-app.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
namespace: default
data:
APP_ENV: "production"
APP_PORT: "8080"
LOG_LEVEL: "info"
app.properties: |
database.host=postgres-service
database.port=5432
cache.ttl=300
Complete Deployment
# deployment-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: default
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
env:
- name: APP_ENV
valueFrom:
configMapKeyRef:
name: app-config
key: APP_ENV
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 10
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 5
NodePort Service
# service-nginx.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: default
spec:
type: NodePort
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30080 # Exposed on each worker node (30000-32767)
Secret
# secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: db-credentials
namespace: default
type: Opaque
data:
# Values encoded in base64: echo -n "mypassword" | base64
username: YWRtaW4=
password: bXlwYXNzd29yZA==
Key Terms Glossary
| Term | Definition |
|---|
| kubeadm | CLI tool for initializing and managing a Kubernetes cluster |
| kubectl | Main CLI tool for interacting with the Kubernetes API |
| kubeconfig | Configuration file for kubectl cluster access |
| etcd | Distributed database storing cluster state |
| CIDR | IP address range for the cluster’s internal network |
| Manifest | YAML file describing the desired state of a Kubernetes object |
| Desired State | Target configuration defined in YAML |
| Actual State | Currently running state in the cluster |
| Rolling Update | Progressive update without service interruption |
| Rollback | Return to a previous version of a deployment |
| Lens | GUI for visually managing K8s clusters |
| Calico | Popular network plugin (CNI) for Kubernetes |
| HPA | Horizontal Pod Autoscaler — automatic scaling |
| Split Brain | Consensus problem with an even number of masters |
| K8s / K8S | Abbreviation for Kubernetes (8 letters between K and S) |
Search Terms
kubernetes · concepts · configuration · containers · cluster · deployment · kubectl · service · architecture · diagram · master · nodes · components · flow · initialization · namespaces · network · node · object · pods · worker