EthicsComputerCourses
Join training +91 90532 10052

Kubernetes Introduction

Kubernetes · Lesson 1 of 30 Intermediate 7 min read Updated

Kubernetes is a system that runs your containers across a group of machines and keeps them running. You tell it what you want, for example "three copies of my web app, version 2.1", and it starts them, watches them, replaces them when they crash and moves them when a machine fails.

In this lesson you will learn what problem Kubernetes solves, the idea of desired state that everything else is built on, and how to decide whether a project needs Kubernetes at all. This course assumes you already know containers and images. If you do not, start with the Docker tutorial first, because every Pod in Kubernetes runs ordinary container images.

The problem Kubernetes solves

Imagine Meera runs a small ticket-booking site for bus operators in Jaipur. She packs the app into a Docker image and runs it with Docker Compose on one server. That works well until festival season:

  • Traffic jumps five times in one evening, and one server cannot handle it.
  • The server's disk fails at 2 a.m. and the site is down until someone wakes up.
  • Every deploy stops the old container and starts a new one, so users see errors for a few seconds.
  • With three servers, someone has to decide which container runs where, and update that plan by hand whenever a server is added or removed.

Running many containers on many machines, and keeping them healthy, is called container orchestration. Kubernetes (often written K8s, because there are eight letters between the K and the s) is the most widely used orchestrator. The name comes from the Greek word for a ship's helmsman. Google open-sourced it in 2014, based on its experience with internal systems, and today it is maintained by a large open-source community under the Cloud Native Computing Foundation (CNCF).

Desired state: the one big idea

With Docker you give commands: "start this container", "stop that one". With Kubernetes you mostly give declarations: "this is what should exist". Kubernetes stores your declaration, compares it with what is really running, and acts to close the gap. This never-ending compare-and-fix cycle is called a reconciliation loop, and the programs that run it are called controllers.

Here is the idea in action on a small cluster. The first command declares a Deployment (a Kubernetes object that keeps a number of identical copies running) with three copies of an Nginx web server:

Terminal
kubectl create deployment hello --image=nginx:1.30-alpine --replicas=3
kubectl get pods
Output
deployment.apps/hello created
NAME                     READY   STATUS    RESTARTS   AGE
hello-p947wfgpc6-cl8jw   1/1     Running   0          38s
hello-p947wfgpc6-p42hh   1/1     Running   0          38s
hello-p947wfgpc6-q6gh7   1/1     Running   0          38s

Each line is a Pod, the smallest thing Kubernetes runs (one or more containers that live together). Now delete one Pod, as if it had crashed, and look again:

Terminal
kubectl delete pod hello-p947wfgpc6-p42hh
kubectl get pods
Output
pod "hello-p947wfgpc6-p42hh" deleted from default namespace
NAME                     READY   STATUS    RESTARTS   AGE
hello-p947wfgpc6-cl8jw   1/1     Running   0          71s
hello-p947wfgpc6-q6gh7   1/1     Running   0          71s
hello-p947wfgpc6-vntzl   1/1     Running   0          4s

You never asked for a new Pod. The Deployment's controller noticed that only two copies existed while three were wanted, and created hello-p947wfgpc6-vntzl within seconds. That is self-healing, and it comes directly from desired state.

What Kubernetes gives you

FeatureWhat it means for Meera
SchedulingKubernetes picks a machine with enough free CPU and memory for each container
Self-healingCrashed containers restart; Pods on a dead machine are recreated elsewhere
ScalingChange replicas: 3 to replicas: 10, or let an autoscaler do it
Service discovery and load balancingOther apps reach "the booking API" by one stable name, whichever Pods are alive
Rolling updates and rollbacksNew versions replace old ones gradually, and a bad version can be undone with one command
Configuration and secretsSettings and passwords are stored separately from images
StorageDatabases get disks that follow them when Pods move

Kubernetes is also not a few things. It does not build your images (Docker or a CI pipeline does), it does not include databases or message queues (you run them on it or use a managed service), and it does not choose your logging and monitoring tools for you.

Everything is an object in YAML

In real projects you rarely type kubectl create. You write your desired state in YAML files called manifests, keep them in Git and apply them. This file describes the same Deployment as the command above:

YAML
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
        - name: nginx
          image: nginx:1.30-alpine

You would apply it with kubectl apply -f hello.yaml. Every field is explained in YAML Manifests & kubectl apply and Deployments & ReplicaSets. For now, notice the shape: what kind of object, its name, and a spec that describes what you want.

Compose or Kubernetes?

Docker ComposeKubernetes
MachinesOneOne to thousands
Recovery when a machine diesManualAutomatic, if other machines have room
Zero-downtime updatesNeeds extra workBuilt in
Learning effortLowHigh
Good forSmall apps, side projects, developmentMany services, many servers, frequent deploys

Kubernetes is powerful, but it adds real work: more concepts, more YAML, and a cluster to upgrade and secure. A single VPS with Compose and good backups serves many small businesses perfectly well. Kubernetes starts to pay off when you need several machines for capacity or reliability, when many teams deploy many services, or when you need automatic scaling. Cloud providers also sell managed Kubernetes, where they run the control machines for you, which removes some of that work (see Managed Kubernetes).

Common mistakes

  • Learning Kubernetes before containers. If images, ports and volumes are unclear, Kubernetes will feel like magic. Learn Docker first.
  • Thinking "Kubernetes replaces Docker images". Kubernetes runs standard container images; you still build them with Docker or another builder.
  • Fixing things by hand inside a running container. Kubernetes replaces Pods freely, so manual changes vanish. Change the image or the manifest instead.
  • Choosing Kubernetes for a tiny app "because big companies use it". Match the tool to the problem.

Key points

  • Kubernetes (K8s) orchestrates containers across many machines: scheduling, self-healing, scaling, updates and service discovery.
  • You declare the desired state; controllers keep comparing it with reality and fix any difference.
  • A Pod is the smallest unit Kubernetes runs; a Deployment keeps a set number of identical Pods alive.
  • Desired state is written in YAML manifests that you keep in Git and apply with kubectl apply -f.
  • Kubernetes adds complexity; Docker Compose on one server is often enough for small apps.

Exercise

Meera's bus-booking site runs on one server with Compose. List two situations from her festival season that Kubernetes would handle automatically, and one situation where it would not help at all. Then write the kubectl command that creates a Deployment called booking running 4 copies of the image ghcr.io/meera-jain/booking:2.1.0.

Show solution

Kubernetes would help when a server dies (Pods are recreated on healthy machines) and during deploys (rolling updates avoid downtime). It would not help if the app has a bug in its booking logic, because Kubernetes runs whatever image you give it.

Terminal
kubectl create deployment booking --image=ghcr.io/meera-jain/booking:2.1.0 --replicas=4
Output
deployment.apps/booking created

The Deployment's controller now keeps four Pods running and replaces any that disappear.

Quick Quiz

1. What does "desired state" mean in Kubernetes?

2. You delete one Pod of a Deployment that wants 3 replicas. What happens?

3. Which of these is NOT a job Kubernetes does for you?

4. Why is Kubernetes often written K8s?

Learn Kubernetes with a mentor

Free lessons get you started. Our Cloud & DevOps training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call