Introduction to Kubernetes

Kubernetes (often called K8s) is an open-source platform for automating the deployment, scaling, and management of containerized applications. Kubernetes helps you manage many containers across multiple machines efficiently.

Why Kubernetes?
Modern applications are often microservices-based—many small services that need to communicate and scale independently. Managing these manually is tough. Kubernetes provides:
Automatic scaling: Adjusts the number of running containers based on demand.

Self-healing: Restarts failed containers automatically.

Service discovery & load balancing: Routes traffic to the right containers.

Rolling updates: Updates applications without downtime.

Storage orchestration: Connects applications to storage systems automatically.

Think of it as a traffic controller and manager for containers, ensuring they run reliably, scale efficiently, and communicate properly.

Core concepts
Cluster: A group of machines (nodes) where Kubernetes runs your applications. There’s usually a master node (control plane) and worker nodes.

Node: A single machine (physical or virtual) that runs your containerized apps.

Pod: The smallest deployable unit in Kubernetes. It can contain one or more containers that share resources like storage and network.

Deployment: Defines how to run and scale your pods (e.g., “run 5 instances of this app”).

Service: Exposes your pods to the network and balances traffic between them.

ConfigMap / Secret: Manage configuration and sensitive data separately from your application code.

Ingress: Manages external access to your services, usually via HTTP/HTTPS.

Kubernetes Architecture

  • Control Plane (Master Node):
    API Server: Entry point for all commands.
    Scheduler: Decides where pods should run.
    Controller Manager: Maintains desired state (e.g., number of pods).
  • Worker Nodes: Run the pods and containers.
    Kubelet: Ensures containers in pods run as expected.
    Kube-proxy: Handles networking and load balancing.
  • Add-ons:
    Ingress for external traffic.
    ConfigMaps/Secrets for configuration and sensitive data.

Kubernetes Basics

  • kubectl – CLI to interact with Kubernetes.
  • Cluster – Set of nodes (master + worker nodes).
  • Node – VM or physical machine running pods.
  • Pod – Smallest deployable unit; can contain one or more containers.
  • Deployment – Manages stateless apps, scaling, and rolling updates.
  • Service – Exposes pods inside or outside the cluster.
  • Namespace – Virtual cluster for resource isolation.

Common kubectl Commands

# Cluster info
kubectl cluster-info

# Get resources
kubectl get nodes
kubectl get pods
kubectl get services
kubectl get deployments
kubectl get configmaps
kubectl get secrets

# Get resources with details
kubectl describe pod <pod-name>
kubectl describe node <node-name>

# Create resources from YAML
kubectl apply -f <file.yaml>
kubectl create -f <file.yaml>

# Delete resources
kubectl delete pod <pod-name>
kubectl delete -f <file.yaml>

# Logs and debugging
kubectl logs <pod-name>           # Single container pod
kubectl logs <pod-name> -c <container-name>
kubectl exec -it <pod-name> -- /bin/bash

# Scaling
kubectl scale deployment <name> --replicas=<n>

# Configurations
kubectl config view
kubectl config use-context <context>

Pod Management

# Run a pod
kubectl run my-pod --image=nginx --restart=Never

# Expose pod as a service
kubectl expose pod my-pod --type=NodePort --port=80

# Port forwarding
kubectl port-forward pod/my-pod 8080:80

Deployment Management

# Create a deployment
kubectl create deployment my-deploy --image=nginx

# Update deployment image
kubectl set image deployment/my-deploy nginx=nginx:latest

# Rollback
kubectl rollout undo deployment/my-deploy

# View rollout status
kubectl rollout status deployment/my-deploy

Services

  • ClusterIP – Default, internal only.
  • NodePort – Exposes service on each node’s port.
  • LoadBalancer – External load balancer (cloud provider).
  • ExternalName – Maps service to external DNS.
kubectl expose deployment my-deploy --type=LoadBalancer --name=my-service
kubectl get svc

ConfigMaps & Secrets

# Create ConfigMap from file
kubectl create configmap my-config --from-file=config.properties

# Create Secret from literal
kubectl create secret generic my-secret --from-literal=password=12345

# View (encoded) secret
kubectl get secret my-secret -o yaml

# Decode secret
echo "base64-encoded-value" | base64 --decode

Scaling & Autoscaling

# Manual scaling
kubectl scale deployment my-deploy --replicas=5

# Horizontal Pod Autoscaler (HPA)
kubectl autoscale deployment my-deploy --cpu-percent=50 --min=2 --max=10
kubectl get hpa

Namespaces

# Create namespace
kubectl create namespace my-namespace

# Use namespace
kubectl config set-context --current --namespace=my-namespace

# Get resources in namespace
kubectl get pods -n my-namespace

Useful Tips

kubectl get allLists all resources in current namespace.
kubectl get pods -o wideShows node and IP info.
kubectl explain <resource>Shows documentation for a resource.
kubectl diff -f <file.yaml>Preview changes before applying.

Leave a Reply

Your email address will not be published. Required fields are marked *

footer