Previous TCP-IP Communication IaC (Infrastructure as Code) Next

Kubernetes Introduction

πŸ“˜ What is Kubernetes?

Kubernetes is an open-source container orchestration platform originally developed by Google (now maintained by the Cloud Native Computing Foundation - CNCF).

It automates the deployment, scaling, and management of containerized applications (like Docker containers).

πŸ‘‰ Think of Kubernetes as the "operating system for your data center/cloud" that manages all your containerized workloads.

πŸ•°οΈ A Quick History

  • 2014 β†’ Google open-sourced Kubernetes.
  • 2015 β†’ Donated to CNCF.
  • Inspired by Google’s internal system Borg, which ran containers at massive scale.
  • Today, Kubernetes is the industry standard for container orchestration.

βš™οΈ Why Do We Need Kubernetes?

Containers (like Docker) are great but managing them at scale is hard:

  • How do you restart a failed container?
  • How do you scale up/down when traffic changes?
  • How do you load-balance across containers?
  • How do you roll out updates without downtime?

πŸ‘‰ Kubernetes solves all of this automatically.

🧩 Key Concepts in Kubernetes

1️⃣ Cluster

A Kubernetes cluster consists of:

  • Master Node (Control Plane) β†’ Manages the cluster (scheduler, API server, controller).
  • Worker Nodes β†’ Where containers (Pods) actually run.

2️⃣ Pod

The smallest deployable unit in Kubernetes.

A Pod wraps one or more containers (usually 1).

Example: A Pod could run a web server container + a logging sidecar container.

3️⃣ Deployment

Defines the desired state of Pods (e.g., 3 replicas of Nginx). Kubernetes ensures the cluster always matches this state.

Example: If 1 Pod crashes, Deployment brings it back automatically.

4️⃣ Service

Provides networking and load balancing for Pods. Since Pods are ephemeral (they come and go), a Service gives them a stable IP and DNS name.

5️⃣ Ingress

Manages external access to Services (HTTP/HTTPS). Lets you expose apps to the internet with domain names, SSL, routing, etc.

6️⃣ ConfigMaps & Secrets

  • ConfigMap β†’ Store configuration (key-value pairs).
  • Secret β†’ Store sensitive data (like API keys, passwords).

7️⃣ Autoscaling

Horizontal Pod Autoscaler (HPA) β†’ Increases/decreases Pod replicas based on CPU/memory usage.

πŸš€ Example: Running Nginx in Kubernetes

nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

πŸ‘‰ Run:

kubectl apply -f nginx-deployment.yaml
kubectl apply -f nginx-service.yaml

Now you have 3 replicas of Nginx running behind a load-balanced service.

🌟 Advantages of Kubernetes

  • Self-healing β†’ Restarts failed containers, replaces Pods, reschedules workloads.
  • Scalability β†’ Auto-scales applications up or down.
  • Portability β†’ Works on any cloud (AWS, Azure, GCP) or on-prem.
  • Load Balancing & Networking β†’ Distributes traffic across Pods.
  • Rolling Updates & Rollbacks β†’ Deploy updates without downtime.
  • Resource Efficiency β†’ Optimizes CPU/memory usage.
  • Ecosystem & Extensibility β†’ Works with Helm charts, Operators, Service Meshes, etc.

πŸ”‘ Real-World Use Cases

  • Running microservices architectures.
  • Hosting scalable web apps & APIs.
  • CI/CD pipelines (Jenkins, GitLab runners on Kubernetes).
  • Big Data & AI workloads (TensorFlow on K8s).
  • Hybrid cloud strategy β†’ Run the same workloads on-prem & cloud.

Essential kubectl CLI Commands 🧡

πŸš€ Basic Setup & Cluster Info

CommandUsage
kubectl versionShow client and server version.
kubectl cluster-infoDisplay cluster info.
kubectl config viewView kubeconfig settings.
kubectl config use-context <context>Switch to a different cluster context.

πŸ“¦ Pods

CommandUsage
kubectl get podsList all pods in the current namespace.
kubectl get pods -AList pods across all namespaces.
kubectl describe pod <pod-name>Show details of a pod.
kubectl logs <pod-name>View logs of a pod.
kubectl exec -it <pod-name> -- shAccess a pod's shell.

πŸš€ Deployments

CommandUsage
kubectl get deploymentsList deployments.
kubectl create deployment <name> --image=<image>Create a new deployment.
kubectl scale deployment <name> --replicas=<num>Scale a deployment.
kubectl rollout status deployment/<name>Check rollout status.
kubectl rollout undo deployment/<name>Rollback a deployment.

🌐 Services & Networking

CommandUsage
kubectl get servicesList all services.
kubectl expose deployment <name> --port=<port> --type=NodePortExpose a deployment as a service.
kubectl port-forward svc/<service> <local-port>:<service-port>Forward local port to service.

πŸ“ Config & Secrets

CommandUsage
kubectl get configmapsList all configmaps.
kubectl create configmap <name> --from-literal=key=valueCreate configmap from literal values.
kubectl get secretsList secrets.
kubectl create secret generic <name> --from-literal=key=valueCreate secret from literal values.

πŸ”„ Namespace Management

CommandUsage
kubectl get namespacesList all namespaces.
kubectl create namespace <name>Create a new namespace.
kubectl delete namespace <name>Delete a namespace.

🧹 Cleanup

CommandUsage
kubectl delete pod <pod-name>Delete a pod.
kubectl delete deployment <name>Delete a deployment.
kubectl delete service <name>Delete a service.
kubectl delete all --allDelete all resources in the namespace.
Back to Index
Previous TCP-IP Communication IaC (Infrastructure as Code) Next
*