PKCE (Proof Key for Code Exchange) :👈 👉:IaC (Infrastructure as Code)

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

Command Usage
kubectl version Show client and server version.
kubectl cluster-info Display cluster info.
kubectl config view View kubeconfig settings.
kubectl config use-context Switch to a different cluster context.

๐Ÿ“ฆ Pods

Command Usage
kubectl get pods List all pods in the current namespace.
kubectl get pods -A List pods across all namespaces.
kubectl describe pod Show details of a pod.
kubectl logs View logs of a pod.
kubectl exec -it -- sh Access a pod's shell.

๐Ÿš€ Deployments

Command Usage
kubectl get deployments List deployments.
kubectl create deployment --image= Create a new deployment.
kubectl scale deployment --replicas= Scale a deployment.
kubectl rollout status deployment/ Check rollout status.
kubectl rollout undo deployment/ Rollback a deployment.

๐ŸŒ Services & Networking

Command Usage
kubectl get services List all services.
kubectl expose deployment --port= --type=NodePort Expose a deployment as a service.
kubectl port-forward svc/ : Forward local port to service.

๐Ÿ“ Config & Secrets

Command Usage
kubectl get configmaps List all configmaps.
kubectl create configmap --from-literal=key=value Create configmap from literal values.
kubectl get secrets List secrets.
kubectl create secret generic --from-literal=key=value Create secret from literal values.

๐Ÿ”„ Namespace Management

Command Usage
kubectl get namespaces List all namespaces.
kubectl create namespace Create a new namespace.
kubectl delete namespace Delete a namespace.

๐Ÿงน Cleanup

Command Usage
kubectl delete pod Delete a pod.
kubectl delete deployment Delete a deployment.
kubectl delete service Delete a service.
kubectl delete all --all Delete all resources in the namespace.
Back to Index
PKCE (Proof Key for Code Exchange) :👈 👉:IaC (Infrastructure as Code)