Kubernetes Basics
Learn Kubernetes fundamentals: architecture, pods, nodes, clusters, and the control plane. Understand how container orchestration works.
Learn Kubernetes fundamentals: architecture, pods, nodes, clusters, and the control plane. Understand how container orchestration works. This hands-on tutorial focuses on practical implementation of kubernetes basics concepts.
Kubernetes Basics
Kubernetes (K8s) is the industry-standard platform for automating deployment, scaling, and management of containerized applications.
What is Kubernetes?
Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services.
┌───────────────────────────────────────────────────────────────────┐
│ Kubernetes Cluster │
├───────────────────────────────────────────────────────────────────┤
│ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Control Plane │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ API │ │ etcd │ │ Scheduler│ │ Controller│ │ │
│ │ │ Server │ │ │ │ │ │ Manager │ │ │
│ │ │ (kube-apiserver)│ │ │ │ │ │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
│ └──────────────────────────────────────────────────────────┘ │
│ │ │
│ │ manages │
│ ▼ │
│ ┌──────────────────────────────────────────────────────────┐ │
│ │ Worker Nodes │ │
│ │ ┌──────────────┐ ┌──────────────┐ ┌──────────┐ │ │
│ │ │ Node 1 │ │ Node 2 │ │ Node 3 │ │ │
│ │ │ ┌──────────┐│ │ ┌──────────┐│ │ ┌──────┐ │ │ │
│ │ │ │ kubelet ││ │ │ kubelet ││ ││kubelet│ │ │ │
│ │ │ ├──────────┤│ │ ├──────────┤│ │├──────┤ │ │ │
│ │ │ │ Container││ │ │ Container││ ││Container│ │ │
│ │ │ │ Runtime ││ │ │ Runtime ││ ││Runtime│ │ │ │
│ │ │ ├──────────┤│ │ ├──────────┤│ │├──────┤ │ │ │
│ │ │ │ kube-proxy││ │ │ kube-proxy││ ││kube-proxy│ │ │
│ │ │ └──────────┘│ │ └──────────┘│ │└──────┘ │ │ │
│ │ │ │ │ │ │ │ │ │
│ │ │ Pods running│ │ Pods running│ │Pods run │ │ │
│ │ └──────────────┘ └──────────────┘ └──────────┘ │ │
│ └──────────────────────────────────────────────────────────┘ │
│ │
└───────────────────────────────────────────────────────────────────┘
Kubernetes Architecture
Control Plane Components
| Component | Purpose |
|---|---|
| kube-apiserver | Exposes Kubernetes API; front-end for control plane |
| etcd | Consistent and highly-available key-value store for all cluster data |
| kube-scheduler | Watches for new Pods and assigns them to nodes |
| kube-controller-manager | Runs controller processes (Node, Job, Endpoint, etc.) |
| cloud-controller-manager | Integrates with underlying cloud providers |
Node Components
| Component | Purpose |
|---|---|
| kubelet | Ensures containers are running in a Pod |
| kube-proxy | Maintains network rules on nodes |
| Container Runtime | Software responsible for running containers (containerd, CRI-O) |
Core Concepts
Pods
The smallest deployable unit in Kubernetes—one or more containers with shared resources.
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
tier: frontend
spec:
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "128Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /ready
port: 80
initialDelaySeconds: 5
periodSeconds: 5
Pod Characteristics:
- Ephemeral (not designed to persist)
- Share network namespace (same IP)
- Share storage volumes
- Containers in a pod can communicate via localhost
Nodes
Worker machines where pods run.
# View nodes
kubectl get nodes
kubectl get nodes -o wide
# Describe node details
kubectl describe node worker-1
# Check node labels
kubectl get nodes --show-labels
# Cordon node (prevent scheduling)
kubectl cordon worker-1
# Drain node (evict pods)
kubectl drain worker-1 --ignore-daemonsets
# Uncordon node (allow scheduling)
kubectl uncordon worker-1
Namespaces
Virtual clusters within a physical cluster for resource isolation.
# List namespaces
kubectl get namespaces
kubectl get ns
# Create namespace
kubectl create namespace production
kubectl create ns staging
# Set default namespace
kubectl config set-context --current --namespace=production
# Run command in specific namespace
kubectl get pods -n staging
kubectl get pods --namespace=production
kubectl Commands
Essential Commands
# Cluster info
kubectl cluster-info
kubectl version
kubectl api-resources
kubectl api-versions
# Context management
kubectl config get-contexts
kubectl config current-context
kubectl config use-context production
# Resource operations
kubectl get pods
kubectl get pods -o wide
kubectl get pods --all-namespaces
kubectl get pods -l app=nginx
kubectl get pods -o yaml
kubectl describe pod nginx-pod
kubectl logs nginx-pod
kubectl logs nginx-pod -f
kubectl logs nginx-pod --previous
# Execute in container
kubectl exec nginx-pod -- ls /etc/nginx
kubectl exec -it nginx-pod -- /bin/bash
# Port forwarding
kubectl port-forward nginx-pod 8080:80
# Copy files
kubectl cp nginx-pod:/etc/nginx/nginx.conf ./nginx.conf
kubectl cp ./config.conf nginx-pod:/etc/nginx/
# Delete resources
kubectl delete pod nginx-pod
kubectl delete -f manifest.yaml
kubectl delete pods -l app=nginx
Resource Shortcuts
| Resource | Shortname |
|---|---|
| pods | po |
| services | svc |
| deployments | deploy |
| namespaces | ns |
| nodes | no |
| configmaps | cm |
| secrets | - |
| persistentvolumeclaims | pvc |
| persistentvolumes | pv |
| replicasets | rs |
| statefulsets | sts |
| daemonsets | ds |
| jobs | - |
| cronjobs | cj |
| ingresses | ing |
Installation and Setup
Local Development (minikube/kind)
# Install minikube
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
# Start cluster
minikube start --driver=docker
minikube start --cpus=4 --memory=8192
# Or use kind
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.20.0/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind
kind create cluster --name dev
# Verify
kubectl get nodes
Production (kubeadm)
# On all nodes
sudo apt-get update
sudo apt-get install -y containerd kubelet kubeadm kubectl
# On control plane node
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
# Configure kubectl
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# Install CNI (Calico)
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
# Join worker nodes
kubeadm join 192.168.1.10:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Kubernetes Objects
Imperative vs Declarative
# Imperative (commands)
kubectl run nginx --image=nginx
kubectl create deployment nginx --image=nginx --replicas=3
kubectl expose deployment nginx --port=80 --type=LoadBalancer
# Declarative (manifests)
kubectl apply -f deployment.yaml
kubectl apply -f https://example.com/manifest.yaml
kubectl apply -k ./kustomize/ # Kustomize
kubectl apply -R -f ./manifests/ # Recursive
Resource Manifest Structure
apiVersion: v1 # API version
kind: Pod # Resource type
metadata: # Resource metadata
name: my-pod
namespace: default
labels:
app: myapp
annotations:
description: "My application pod"
spec: # Desired state
containers:
- name: app
image: nginx:alpine
Quiz
Quiz
Question 1 of 5What is the smallest deployable unit in Kubernetes?
Next Steps
Now let's explore Kubernetes core objects: Deployments, Services, ConfigMaps, and Secrets.