Kubernetes Building Blocks

Kubernetes is built around the idea of objects that represent the desired state of your system — what you want to run.

Each Kubernetes object has:

apiVersion: API version of Kubernetes.

kind: Type of object (Pod, Service, Deployment, etc.).

metadata: Information like name, labels.

spec: Desired state (what you want Kubernetes to create/manage).

status: Actual state (what is currently running, managed by Kubernetes).

Examples of Kubernetes object types are Nodes, Namespaces, Pods, ReplicaSets Deployments, DaemonSets, etc.

apiVersion: v1
kind: Pod
metadata:
  name: my-dotnet-pod
spec:
  containers:
    - name: my-dotnet-container
      image: mcr.microsoft.com/dotnet/samples:aspnetapp
      ports:
        - containerPort: 80



minikube start --driver=docker

kubectl apply -f my-dotnet-pod.yaml

kubectl get pods

kubectl describe pod my-dotnet-pod

2. Nodes

●A Node is a machine (VM or physical) where Kubernetes runs your workloads.


Each node runs:

  • kubelet (agent)
  • container runtime (like Docker)
  • kube-proxy

●There are two types:

  • Master Node (Control Plane)
  • Worker Node
kubectl get nodes

kubectl describe node <node-name>

3. Namespace

Namespaces help you organize Kubernetes resources into separate environments (like dev, test, prod).

●If multiple users and teams use the same Kubernetes cluster we can partition the cluster into virtual sub-clusters using Namespaces

●To list all the Namespaces, we can run the following command:

$ kubectl get namespaces

$ kubectl create namespace new-namespace-name

$ kubectl create namespace dev

$ kubectl get namespaces
Demo of Namespace
apiVersion: v1
kind: Pod
metadata:
  name: dotnet-pod
  namespace: dev
spec:
  containers:
    - name: dotnet
      image: mcr.microsoft.com/dotnet/samples:aspnetapp


Apply and check:

kubectl apply -f pod-dev.yaml

kubectl get pods -n dev

4. Pods

  • A Pod is the smallest deployable unit in Kubernetes.
  • It can contain one or more containers that share:
  • Storage
  • Network (IP address)
  • Lifecycle
Demo of PODS
apiVersion: v1
kind: Pod
metadata:
  name: dotnet-pod
spec:
  containers:
    - name: dotnet
      image: mcr.microsoft.com/dotnet/samples:aspnetapp
      ports:
        - containerPort: 8080



kubectl apply -f pod.yaml

kubectl get pods

kubectl port-forward pod/dotnet-pod 8084:8080

5. Labels

Labels are key/value pairs attached to objects.

● They help organize, filter, and select specific objects

metadata:
  labels:
    app: dotnet-app
    env: dev


kubectl get pods --show-labels

6. Label Selectors

Label selectors allow you to filter and select objects using their labels.

kubectl get pods -l app=dotnet-app

This command returns all pods labeled app=dotnet-app.

7. ReplicationController

A ReplicationController ensures that a specified number of pod replicas are always running.

apiVersion: v1
kind: ReplicationController
metadata:
  name: dotnet-rc
spec:
  replicas: 2
  selector:
    app: dotnet-app
  template:
    metadata:
      labels:
        app: dotnet-app
    spec:
      containers:
        - name: dotnet
          image: mcr.microsoft.com/dotnet/samples:aspnetapp
          ports:
            - containerPort: 8080


kubectl apply -f rc.yaml

kubectl get pods
Option 1: Delete the ReplicationController

When you delete the ReplicationController itself, Kubernetes also removes its managed pods.

kubectl delete rc dotnet-rc

verify:
kubectl get pods

Now the pods will not auto-recreate anymore.

Option 2: Scale Down the ReplicationController

kubectl scale rc dotnet-rc --replicas=0

Option 3: Delete All (RC + Pods)

kubectl delete -f rc.yaml

8. ReplicaSets

ReplicaSet is a newer version of ReplicationController.
It also maintains a desired number of identical pods.

demo of ReplicaSets

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: dotnet-rs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: dotnet-app
  template:
    metadata:
      labels:
        app: dotnet-app
    spec:
      containers:
        - name: dotnet
          image: mcr.microsoft.com/dotnet/samples:aspnetapp
          ports:
            - containerPort: 80



kubectl apply -f replicaset.yaml

kubectl get replicaset

kubectl get pods

9. Deployments

A Deployment manages ReplicaSets and provides rolling updates, rollbacks, and versioning.

●A Deployment is one of the most important and powerful Kubernetes objects.

●It is a higher-level abstraction that manages your application Pods through a ReplicaSet and provides automated updates, rollback, scaling, and version control.

FeatureDescription
🧩 Self-healingIf a Pod fails, a new one is created automatically.
🔁 Rolling updatesYou can update your app version without downtime — old Pods are replaced gradually by new ones.
RollbackIf a new version fails, you can revert to the previous version easily.
📈 ScalingYou can increase or decrease replicas with a single command.
📜 Declarative updatesYou define your desired state (YAML), and Kubernetes ensures it matches.
Here’s what a typical Deployment YAML looks like (for a .NET app):

Practice: Step-by-Step on Ubuntu + Minikube

Step 1️⃣ — Start Minikube

minikube start –driver=docker

Step 2️⃣ — Create Deployment YAML

Save this as deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: dotnet-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: dotnet-app
  template:
    metadata:
      labels:
        app: dotnet-app
    spec:
      containers:
        – name: dotnet-container
          image: mcr.microsoft.com/dotnet/samples:aspnetapp
          ports:
            – containerPort: 80

Step 3️⃣ — Apply the Deployment

kubectl apply -f deployment.yaml

Check it:

kubectl get deployments

kubectl get pods

You should see 3 running Pods automatically created by the Deployment (through a ReplicaSet).

🔁 Rolling Update Demonstration

Now suppose you want to update your .NET app version (for example, change the image).

Step 4️⃣ — Update the Deployment

Edit the YAML or use kubectl edit deployment dotnet-deployment.

Change:

image: mcr.microsoft.com/dotnet/samples:aspnetapp

to

image: mcr.microsoft.com/dotnet/samples:aspnetapp:7.0

Then apply the changes:

kubectl apply -f deployment.yaml

Kubernetes will create new Pods with the new version and gradually terminate old ones — this is the rolling update.

You can monitor this with:

kubectl rollout status deployment/dotnet-deployment

Rollback Demonstration

If something goes wrong after your update (say, Pods crash):

Step 5️⃣ — Rollback to previous version

kubectl rollout undo deployment/dotnet-deployment

Then check rollout status again:

kubectl rollout status deployment/dotnet-deployment

✅ This brings your app back to the previous working version.

📈 Scaling Your Deployment

To scale your app (increase or decrease replicas):

kubectl scale deployment dotnet-deployment –replicas=5

Now you’ll have 5 running Pods.

Check:

kubectl get pods

🌐 Exposing the Deployment (Optional but useful)

To access the app from your browser:

kubectl expose deployment dotnet-deployment –type=NodePort –port=80

minikube service dotnet-deployment

This command opens your .NET app in the browser

11. Services

● A Service exposes your Pods to other applications or to the outside world.

● It provides a stable IP and DNS name even if pods are recreated.

Types:

ClusterIP (default) – internal communication

NodePort – exposes app on a node’s IP and port

LoadBalancer – used in cloud environments

apiVersion: v1
kind: Service
metadata:
  name: dotnet-service
spec:
  type: NodePort
  selector:
    app: dotnet-app
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30008


kubectl apply -f service.yaml

kubectl get svc

minikube service dotnet-service
Scroll to Top