Monday, 22 September 2025

Kubernetes

🟢 1️⃣ What is Kubernetes?

  • Kubernetes (K8s) is a container orchestration platform.

  • It manages, deploys, and scales containers automatically.

  • Think of it as a manager for Docker containers in a large system.

Analogy:

  • Docker = single shipping container

  • Kubernetes = entire shipping port with cranes, storage, and delivery trucks


🟢 2️⃣ Why Kubernetes for .NET Core?

  1. Scaling: Automatically increase or decrease the number of instances.

  2. Self-healing: Restarts failed containers automatically.

  3. Load balancing: Distributes traffic between multiple instances.

  4. Rolling updates: Update services without downtime.

  5. Service discovery: Easy communication between microservices.


🟢 3️⃣ Key Kubernetes Concepts

TermMeaning
PodSmallest deployable unit (usually 1 container, can have multiple)
NodeA server (VM or physical machine) running Kubernetes
ClusterA group of nodes managed by Kubernetes
DeploymentDefines how many replicas of a pod to run and manages updates
ServiceExposes pods internally or externally for communication
ConfigMapStores configuration (key-value) for pods
SecretStores sensitive info (passwords, API keys) securely
NamespaceLogical separation of resources in a cluster

🟢 4️⃣ Example: Deploy a .NET Core API to Kubernetes

Step 1: Dockerize Your .NET Core App

  • Create Dockerfile and build image as explained earlier.

Step 2: Push Docker Image to Registry

docker tag mywebapi:1.0 mydockerhubuser/mywebapi:1.0 docker push mydockerhubuser/mywebapi:1.0

Step 3: Create Kubernetes Deployment

deployment.yaml

apiVersion: apps/v1 kind: Deployment metadata: name: mywebapi-deployment spec: replicas: 3 selector: matchLabels: app: mywebapi template: metadata: labels: app: mywebapi spec: containers: - name: mywebapi image: mydockerhubuser/mywebapi:1.0 ports: - containerPort: 80
  • replicas: number of pods (instances) to run.

Step 4: Expose the Deployment as a Service

service.yaml

apiVersion: v1 kind: Service metadata: name: mywebapi-service spec: type: LoadBalancer selector: app: mywebapi ports: - protocol: TCP port: 80 targetPort: 80
  • LoadBalancer exposes your API to the outside world.


Step 5: Apply Config to Cluster

kubectl apply -f deployment.yaml kubectl apply -f service.yaml
  • Check pods: kubectl get pods

  • Check services: kubectl get svc


🟢 5️⃣ How Microservices Work on Kubernetes

  • Each microservice runs in its own deployment + pods.

  • Services communicate internally via Kubernetes Service (DNS name).

  • Messages/events can be processed asynchronously using message queues (RabbitMQ, Kafka).


🟢 6️⃣ Benefits for .NET Core Apps

  1. Easy to scale APIs or background workers independently.

  2. Can run cross-platform containers (.NET Core works on Linux).

  3. Automatic updates and self-healing reduce downtime.

  4. Supports microservices architecture with multiple small services.


🟢 7️⃣ Quick Summary

  • Docker: packages app into container

  • Kubernetes: manages many containers automatically

  • .NET Core + Docker + K8s → scalable, resilient microservices architecture

No comments:

Post a Comment