K8s by Example: Overview

Kubernetes orchestrates containers across a cluster of machines. This guide shows how the core components fit together: Cluster, Nodes, Pods, Deployments, and Services.

cluster-overview.txt

A Cluster is a set of machines (nodes) running Kubernetes. The Control Plane manages the cluster, worker nodes run your applications.

+-----------------------------------------------------------+
|                         CLUSTER                           |
|                                                           |
|  +---------------+      +---------------------------+     |
|  | Control Plane |      |       Worker Nodes        |     |
|  |               |      |                           |     |
|  |  - API Server |----->|  +--------+   +--------+  |     |
|  |  - Scheduler  |      |  | Node 1 |   | Node 2 |  |     |
|  |  - etcd       |      |  | (Pods) |   | (Pods) |  |     |
|  |               |      |  +--------+   +--------+  |     |
|  +---------------+      +---------------------------+     |
|                                                           |
+-----------------------------------------------------------+
node.txt

A Node is a machine (physical or virtual) in the cluster. Each node runs kubelet (agent), kube-proxy (networking), and a container runtime. Nodes host Pods.

+-----------------------------------------+
|                  NODE                   |
|                                         |
|  +-----------------------------------+  |
|  | kubelet          (talks to API)   |  |
|  | kube-proxy       (networking)     |  |
|  | container runtime (runs pods)     |  |
|  +-----------------------------------+  |
|                                         |
|  +---------+ +---------+ +---------+    |
|  |  Pod A  | |  Pod B  | |  Pod C  |    |
|  +---------+ +---------+ +---------+    |
|                                         |
+-----------------------------------------+
pod.txt

A Pod is the smallest deployable unit. It wraps one or more containers that share network (same IP) and storage. Containers in a Pod communicate via localhost.

+---------------------------------------+
|                 POD                   |
|            IP: 10.244.1.5             |
|                                       |
|  +-------------+   +-------------+    |
|  |  Container  |   |  Container  |    |
|  |    nginx    |   |   sidecar   |    |
|  |     :80     |   |    :9090    |    |
|  +-------------+   +-------------+    |
|         ^                 ^           |
|         +--- localhost ---+           |
|                                       |
|  +----------------------------------+ |
|  |          Shared Volume           | |
|  +----------------------------------+ |
+---------------------------------------+
deployment-replicaset-pod.txt

Deployment manages ReplicaSets, which ensure N copies of a Pod run. You define desired state, Kubernetes maintains it. Deployments handle rolling updates.

+-----------------------------------------------+
|                  DEPLOYMENT                   |
|                  replicas: 3                  |
|                                               |
|  +-----------------------------------------+  |
|  |               REPLICASET                |  |
|  |                                         |  |
|  |  +---------+ +---------+ +---------+    |  |
|  |  |   Pod   | |   Pod   | |   Pod   |    |  |
|  |  |  nginx  | |  nginx  | |  nginx  |    |  |
|  |  +---------+ +---------+ +---------+    |  |
|  |                                         |  |
|  +-----------------------------------------+  |
|                                               |
+-----------------------------------------------+
service.txt

A Service provides a stable IP and DNS name to access Pods. Pods are ephemeral (they come and go), but Services give a fixed endpoint. Load balances across matching Pods.

                +-----------------+
                |     SERVICE     |
 requests ----->|  nginx-service  |
                |   10.96.0.100   |
                +--------+--------+
                         |
         +---------------+---------------+
         |               |               |
         v               v               v
   +------------+   +------------+   +------------+
   |     Pod    |   |     Pod    |   |     Pod    |
   |    nginx   |   |    nginx   |   |    nginx   |
   | 10.244.1.5 |   | 10.244.2.3 |   | 10.244.3.7 |
   +------------+   +------------+   +------------+
namespace.txt

Namespaces divide a cluster into virtual clusters. Use them to separate teams, environments (dev/prod), or projects. Resources in different namespaces are isolated by default.

+-------------------------------------------------+
|                    CLUSTER                      |
|                                                 |
|  +---------------+     +---------------+        |
|  |  namespace:   |     |  namespace:   |        |
|  |     dev       |     |     prod      |        |
|  |               |     |               |        |
|  | +-----------+ |     | +-----------+ |  same  |
|  | | nginx-svc | |     | | nginx-svc | |  names |
|  | | nginx-pod | |     | | nginx-pod | |        |
|  | +-----------+ |     | +-----------+ |        |
|  |               |     |               |        |
|  +---------------+     +---------------+        |
|                                                 |
+-------------------------------------------------+
labels-selectors.txt

Labels are key-value tags on resources. Selectors find resources by labels. This is how Services find Pods, and how you organize resources.

    Service selector: app=nginx
                      |
        +-------------+-------------+
        |             |             |
        v             v             v
  +-----------+ +-----------+ +-----------+
  |    Pod    | |    Pod    | |    Pod    |
  | app=nginx | | app=nginx | | app=redis |
  +-----------+ +-----------+ +-----------+
       [x]           [x]       [ ] not selected
configmap-secret.txt

ConfigMap stores configuration (env vars, config files). Secret stores sensitive data (passwords, tokens). Both inject data into Pods without hardcoding.

+----------------+     +----------------+
|   ConfigMap    |     |     Secret     |
|                |     |                |
| DB_HOST=mysql  |     | DB_PASS=****   |
| LOG_LEVEL=info |     | API_KEY=****   |
+-------+--------+     +-------+--------+
        |                      |
        +----------+-----------+
                   |
                   v
            +-------------+
            |     Pod     |
            |             |
            | DB_HOST     | <- ConfigMap
            | DB_PASS     | <- Secret
            +-------------+
full-picture.txt

Putting it all together: Deployments manage Pods, Services expose them, ConfigMaps/Secrets configure them, all organized in Namespaces.

+---------------- namespace: production ----------------+
|                                                       |
|  +-------------+    +---------------------------+     |
|  |  ConfigMap  |    |        Deployment         |     |
|  |  Secret     |--->|  +-----+ +-----+ +-----+  |     |
|  +-------------+    |  | Pod | | Pod | | Pod |  |     |
|                     |  +-----+ +-----+ +-----+  |     |
|                     +-------------+-------------+     |
|                                   |                   |
|                          +--------+--------+          |
|                          |     Service     |          |
|     users -------------->|   (ClusterIP)   |          |
|                          +-----------------+          |
|                                                       |
+-------------------------------------------------------+
terminal

See all main resources in the current namespace. This shows Pods, Services, Deployments, and ReplicaSets at a glance.

$ kubectl get all
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-6799fc88d8-7xj2k   1/1     Running   0          5m
pod/nginx-6799fc88d8-9abc2   1/1     Running   0          5m
pod/nginx-6799fc88d8-def34   1/1     Running   0          5m

NAME                 TYPE        CLUSTER-IP     PORT(S)   AGE
service/nginx        ClusterIP   10.96.100.50   80/TCP    5m

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   3/3     3            3           5m

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-6799fc88d8   3         3         3       5m

Index | GitHub | Use arrow keys to navigate |