Kubernetes Basics

Learn Kubernetes container orchestration from the ground up. Understand pods, deployments, services, and how to manage containerized applications at scale.

What is Kubernetes?

Kubernetes (often abbreviated as K8s) is an open-source container orchestration platform designed to automate the deployment, scaling, and management of containerized applications. Originally developed by Google, it is now maintained by the Cloud Native Computing Foundation (CNCF).

Kubernetes provides a framework to run distributed systems resiliently. It takes care of scaling and failover for your application, provides deployment patterns, and more.

Why Use Kubernetes?

  • Service Discovery & Load Balancing - Kubernetes can expose containers using DNS or IP addresses and load balance traffic
  • Storage Orchestration - Automatically mount storage systems of your choice
  • Automated Rollouts & Rollbacks - Deploy changes gradually and rollback if something goes wrong
  • Self-Healing - Restarts failed containers, replaces and reschedules containers
  • Secret & Configuration Management - Deploy and update secrets and configuration without rebuilding images

Kubernetes Architecture

Control Plane Components

  • kube-apiserver - The API server that exposes the Kubernetes API
  • etcd - Consistent and highly-available key-value store for cluster data
  • kube-scheduler - Watches for newly created Pods and selects nodes for them to run on
  • kube-controller-manager - Runs controller processes

Node Components

  • kubelet - An agent that runs on each node, ensuring containers are running in a Pod
  • kube-proxy - Network proxy that maintains network rules on nodes
  • Container Runtime - Software responsible for running containers (Docker, containerd, CRI-O)

Key Kubernetes Objects

  • 🔷 Pod - Smallest deployable unit, contains one or more containers
  • 🔷 Deployment - Manages Pod replicas and updates
  • 🔷 Service - Exposes Pods to network traffic
  • 🔷 ConfigMap - Stores configuration data as key-value pairs
  • 🔷 Secret - Stores sensitive data like passwords and tokens
  • 🔷 Namespace - Virtual clusters for resource isolation
đŸ’ģ Essential kubectl Commands
# Get cluster info
kubectl cluster-info

# List all nodes
kubectl get nodes

# List all pods in default namespace
kubectl get pods

# List all pods in all namespaces
kubectl get pods --all-namespaces

# Describe a pod
kubectl describe pod <pod-name>

# View pod logs
kubectl logs <pod-name>

# Execute command in a pod
kubectl exec -it <pod-name> -- /bin/bash

# Apply a configuration
kubectl apply -f deployment.yaml

# Delete resources
kubectl delete -f deployment.yaml
Output
Kubernetes control plane is running at https://127.0.0.1:6443 NAME STATUS ROLES AGE VERSION node-1 Ready master 10d v1.28.0 node-2 Ready worker 10d v1.28.0 NAME READY STATUS RESTARTS AGE nginx-7c658794b9-abc12 1/1 Running 0 5m
💡 These kubectl commands are essential for managing Kubernetes clusters. Practice these to become proficient in K8s operations.
đŸ’ģ Kubernetes Deployment Manifest
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.21
        ports:
        - containerPort: 80
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
          requests:
            memory: "64Mi"
            cpu: "250m"
Output
deployment.apps/nginx-deployment created NAME READY UP-TO-DATE AVAILABLE AGE nginx-deployment 3/3 3 3 1m
💡 This Deployment creates 3 replicas of nginx with resource limits. Kubernetes will ensure these pods are always running.
đŸŽ¯

Test Your Knowledge

Answer these questions to check your understanding

1 What is the smallest deployable unit in Kubernetes?
💡 A Pod is the smallest deployable unit in Kubernetes. It can contain one or more containers that share storage and network resources.
2 What Kubernetes object is used to expose pods to network traffic?
💡 A Service in Kubernetes is used to expose pods to network traffic, providing stable networking and load balancing.