Introduction to Kubernetes: Orchestrating the Chaos

10 min read

Welcome to your first step into the world of Kubernetes! корабль If you've ever heard developers talk about "containers," "scaling," and "orchestration" and felt a little lost, you're in the right place. We're going to demystify Kubernetes by starting with the absolute basics. Our goal isn't to learn everything, but to learn the right things—the concepts and commands you'll actually use.

Let's get started!


1. The 'Why' Before the 'How': Evolution from VMs to Kubernetes

Before we dive into what Kubernetes is, let's understand the problem it was born to solve. It's a story of evolution in how we run applications.

The Old Days: Physical Servers

The Problem: Imagine you have a single, powerful physical server (a big computer). You want to run three different applications on it. You install them, and everything seems fine until Application A needs a specific software version that crashes Application B. Worse, Application C is a beast and uses 90% of the server's memory, starving the others. Managing dependencies was a nightmare, and resources were either wasted or fought over. Scaling was a joke—you had to buy a whole new server, which took weeks.

The Next Step: Virtual Machines (VMs)

The Solution: Virtual Machines (VMs) came along. A VM is an emulation of a complete computer system. Using a "hypervisor," you could run multiple VMs on a single physical server. Each VM had its own entire operating system (OS), from the kernel all the way up to the application.

  • What it solved: It provided great isolation. Application A and its dependencies in VM1 couldn't mess with Application B in VM2. You could slice up your physical server's resources and dedicate them to different VMs.
  • The New Problem: VMs are HEAVY. 🏋️‍♂️ Each one includes a full copy of an operating system, which can be gigabytes in size. They are slow to start up and consume a lot of RAM and CPU just to run the guest OS, let alone the application. It was better, but still inefficient.

The Game Changer: Containers

The Solution: Then came containers (you've probably heard of Docker). A container is a lightweight, standalone, executable package of software that includes everything needed to run it: code, runtime, system tools, system libraries, and settings.

  • Definition: Unlike a VM, a container does not bundle a full operating system. Instead, all containers on a host machine share the host's OS kernel.
  • What it solved:
    • Lightweight & Fast: They are measured in megabytes, not gigabytes. They start in seconds, not minutes.
    • Efficient: Since they share the host OS kernel, you don't have redundant operating systems eating up resources. You can run many more containers on a server than you can VMs.

The Modern Chaos: Too Many Containers!

The New Problem: Containers were amazing! So amazing, in fact, that modern applications (called "microservices") were now being built from hundreds, or even thousands, of them. This created a new kind of chaos.

  • How do you deploy 50 containers for your web server?
  • What happens if one of them crashes in the middle of the night? Do you get paged to wake up and restart it?
  • How do you scale up to 500 containers when traffic spikes, and then scale back down to save money?
  • How do all these containers discover and talk to each other?

Manually managing this fleet of containers is impossible. We need an automated system, a "manager" or an "orchestrator." And that is why Kubernetes exists.


2. Kubernetes Architecture Conceptually: Masters, Workers, and the Control Plane

The Problem: We need a robust, automated system to manage the lifecycle of our containers across a group of machines.

The Solution: Kubernetes provides this by organizing machines into a cluster. A Kubernetes cluster consists of at least one main machine that acts as the brain and several other machines that do the actual work.

Think of it like a film set. You have the Director who decides what happens, and you have the Actors and Crew who perform the tasks. You, the developer, just talk to the Director.

In Kubernetes, this "Director" is the Control Plane (historically called the Master Node), and the "Actors/Crew" are the Worker Nodes.

The Control Plane (The Brains 🧠)

The Control Plane's job is to make global decisions about the cluster (e.g., scheduling containers) and to detect and respond to cluster events (e.g., starting up a new container when one crashes). It's the central management unit.

It's made of a few key components:

  • kube-apiserver (API Server): This is the front door to the Kubernetes cluster. When you type a command using kubectl (we'll get to this!), you are talking to the API Server. It validates and processes all requests.
  • etcd: The cluster's database. It reliably stores the entire configuration and state of the cluster. If the API server is the front door, etcd is the locked file cabinet with all the important records.
  • kube-scheduler: When you want to run a container, the scheduler's job is to find a suitable Worker Node to run it on. It checks which nodes have available resources (CPU, memory) and assigns the work.
  • kube-controller-manager: This is the "watchdog." It runs several controller processes that watch the state of the cluster. For example, one controller notices when a node goes down, and another notices when a container has died and needs to be replaced. Its job is to make sure the current state of the cluster matches your desired state stored in etcd.

The Worker Nodes (The Muscle 💪)

Worker nodes are the machines where your applications actually run. They are the workhorses of the cluster. Each worker node is managed by the Control Plane and contains a few critical components:

  • kubelet: An agent that runs on each worker node. It communicates with the Control Plane's API server and ensures that the containers described in your configurations are running and healthy on its node. It's the on-site foreman for that specific node.
  • kube-proxy: A network proxy that runs on each worker node. It manages the network rules on the node, allowing your containers to communicate with each other and with the outside world. It's the network traffic cop for the node.
  • Container Runtime: The software that is responsible for running containers. Docker is a famous one, but others like containerd and CRI-O are also common. This is the engine that actually starts and stops the containers.

Interesting Fact: Kubernetes was originally an internal project at Google called "Borg." It managed Google's massive containerized workloads for years before being open-sourced in 2014. The name "Kubernetes" is Greek for "helmsman" or "pilot," which is why its logo is a ship's wheel. 舵


3. Setting Up Your Local K8s Playground (Minikube)

The Problem: You want to learn and test Kubernetes without paying for a cloud provider or setting up a multi-server lab. You need a simple, self-contained Kubernetes cluster that runs on your personal computer.

The Solution: We'll use Minikube. Minikube is a tool that quickly sets up a single-node Kubernetes cluster on your personal computer (Windows, macOS, or Linux). It's perfect for learning and development.

Step 1: Install a Hypervisor (or use Docker)

Minikube needs a driver to run. The most common and recommended approach today is to use the Docker driver. If you don't have Docker Desktop installed, please install it first from the official Docker website.

Step 2: Install Minikube

These instructions are based on the latest official documentation. Always check the docs for any recent changes!

macOS (using Homebrew):

bash
brew install minikube

Linux (binary download):

bash
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube

Windows (using Chocolatey or Winget):
Using Chocolatey:

powershell
choco install minikube

Using Winget:

powershell
winget install minikube

Step 3: Start Your Cluster!

Once Minikube is installed, starting your local cluster is as simple as running one command in your terminal.

bash
minikube start

This command will download the necessary images and start your single-node Kubernetes cluster. It might take a few minutes the first time. You should see an output like this:

vbnet
😄  minikube v1.32.0 on Darwin 14.3.1
✨  Using the docker driver based on existing profile
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
...
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

Step 4: Check the Status

To verify that your cluster is running, use the status command:

bash
minikube status

You should see something like this, indicating the host, kubelet, and API Server are all running.

avrasm
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured

Congratulations! 🎉 You now have a fully functional Kubernetes cluster running on your machine.


4. Your First K8s Commands: Meet kubectl

The Problem: Your cluster is running, but it's like a car without a steering wheel. How do you interact with it? How do you tell it what to do?

The Solution: We use kubectl (pronounced "koob-control"). It's the primary command-line tool for communicating with a Kubernetes cluster's API Server. It allows you to deploy applications, inspect and manage cluster resources, and view logs.

Minikube usually installs kubectl for you. You can verify by running:
kubectl version

If it's not installed, follow the official documentation for your OS.

The Basic kubectl Syntax

The command structure is very intuitive:

kubectl [command] [TYPE] [NAME] [flags]

  • command: What you want to do (e.g., get, describe, delete).
  • TYPE: The type of resource you're interested in (e.g., nodes, pods, services).
  • NAME: The specific name of a resource (optional).
  • flags: Extra options (e.g., -o wide to see more details).

Your First Commands

Let's run a few simple, read-only commands to inspect our new cluster. These are some of the most common commands you'll use every single day.

  1. Get Cluster Info
    This command gives you the addresses of the control plane and other services.

    bash
    kubectl cluster-info
  2. Get Nodes
    This shows the nodes that are part of your cluster. Since we're using Minikube, we'll only see one node, and its role will be "control-plane." In a real cluster, you would see all your worker nodes listed here.

    bash
    kubectl get nodes

    Output:

    apache
    NAME       STATUS   ROLES           AGE   VERSION
    minikube   Ready    control-plane   10m   v1.28.3
  3. Get Pods
    This is the big one. A Pod is the smallest and simplest unit in the Kubernetes object model that you create or deploy. It represents a single instance of a running process in your cluster and is a wrapper around one or more containers. We don't have any applications running yet, so this command won't return anything. But in the next guide, this will be the command you use to see your running apps!

    bash
    # This will show pods in the "default" namespace.
    kubectl get pods

    Output:

    pgsql
    No resources found in default namespace.

    To see the pods that make up the Kubernetes system itself (like the API server, scheduler, etc.), you can look in the kube-system namespace:

    bash
    kubectl get pods -n kube-system

You've now successfully set up a cluster and used kubectl to communicate with it. You've laid the foundation for deploying your very first application, which we'll cover in the next section!


References for Extra Reading

Challenge

You are a new DevOps engineer and have just started a Minikube cluster on your local machine. Your first task is to perform a basic health check and inventory using the command-line tool.

Write the sequence of kubectl commands required to perform the following actions. Each command should be on a new line.

  1. Display the endpoint URLs for the Kubernetes control plane and CoreDNS.
  2. List all the nodes that are part of your cluster.
  3. List all the pods that are running in the kube-system namespace to verify that the core components of Kubernetes are up.

Expected result

Your solution should consist of three distinct kubectl commands, written one per line.

  1. The first command should use the appropriate subcommand to display general information about the cluster's endpoints.
  2. The second command should use the get subcommand to list resources of type node.
  3. The third command must also use the get subcommand to list pods, but it must include the correct flag to specify the kube-system namespace.
shell

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.