Deploying Containers with Kubernetes

1.61K 0 0 0 0

✅ Chapter 1: Kubernetes Architecture and Cluster Components

🔍 Introduction

Kubernetes is a powerful open-source system designed to automate the deployment, scaling, and operation of containerized applications. Understanding its architecture is essential for deploying and managing applications effectively.

In this chapter, we’ll explore the core components that make up a Kubernetes cluster, how they interact, and the roles they play in ensuring high availability, scalability, and automation.


🧱 What is a Kubernetes Cluster?

A Kubernetes cluster is a set of machines (virtual or physical) that run containerized applications. It consists of:

  • A control plane that manages the cluster
  • One or more worker nodes that run the applications in containers

🖥️ High-Level Cluster View

text

 

+------------------------+

|     Control Plane      |

|                        |

| +--------------------+ |

| | API Server         | |

| | Scheduler          | |

| | Controller Manager | |

| | etcd               | |

| +--------------------+ |

+------------------------+

 

           |

           v

 

+-------------------+     +-------------------+

|   Worker Node 1   |     |   Worker Node 2   |

| +---------------+ |     | +---------------+ |

| | Kubelet       | |     | | Kubelet       | |

| | Kube Proxy     | |     | | Kube Proxy     | |

| | Container Runtime| |   | | Container Runtime| |

| +---------------+ |     | +---------------+ |

|   [Pods/Containers]     [Pods/Containers]   |

+-------------------+     +-------------------+


📌 Key Kubernetes Components

🔷 Control Plane Components

These components maintain the cluster's desired state, such as which applications are running and their configurations.


🧠 1. kube-apiserver (API Server)

  • Acts as the front-end for the Kubernetes control plane
  • Accepts REST API calls from users, tools, and internal components
  • All operations go through it (via kubectl, Helm, dashboard)

bash

 

kubectl get pods

kubectl create deployment myapp --image=nginx


📖 2. etcd (Cluster Store)

  • Distributed key-value store that maintains all cluster data
  • Stores information about cluster state, secrets, config, etc.
  • Should be backed up regularly

🗂️ 3. kube-scheduler

  • Monitors for unscheduled Pods
  • Assigns Pods to suitable Nodes based on:
    • Resource availability (CPU, memory)
    • Affinity/anti-affinity rules
    • Taints and tolerations

🛠️ 4. kube-controller-manager

Runs controller loops that regulate cluster state:

Controller Type

Role

Node Controller

Detects and replaces failed nodes

Replication Controller

Ensures desired Pod replica count

Job Controller

Handles batch job lifecycle

Endpoints Controller

Populates Service endpoints


🌐 5. cloud-controller-manager (Optional)

Used when running Kubernetes in a cloud environment (GCP, AWS, Azure). It handles:

  • Node lifecycle integration with cloud provider
  • Load balancer creation
  • Persistent volume provisioning

🔶 Node (Worker) Components

Worker nodes run actual application workloads (your containers).


🧩 1. kubelet

  • Agent that runs on every node
  • Ensures containers are running in a Pod
  • Communicates with the API server
  • Reads PodSpecs and executes them via the container runtime

📦 2. Container Runtime

  • Software that runs containers (e.g., Docker, containerd, CRI-O)
  • Kubernetes interacts with it through the Container Runtime Interface (CRI)

🔄 3. kube-proxy

  • Handles network routing on each node
  • Ensures communication between Pods and Services
  • Implements load balancing using iptables or IPVS

️ Kubernetes Objects: Managing Cluster State

These are persistent entities in the Kubernetes system:

Object

Purpose

Pod

Single instance of a running container

Service

Exposes Pods as a network service

Deployment

Manages replica sets and rollout updates

ReplicaSet

Ensures specified number of Pods run

Namespace

Logical division of cluster resources


📊 Control Plane vs. Worker Nodes: Comparison Table

Feature

Control Plane

Worker Node

Purpose

Cluster management & orchestration

Run workloads (Pods)

Key Components

API Server, etcd, Scheduler

Kubelet, Kube-proxy, Runtime

Runs Application Pods

No

Yes

Stateful

Yes (stores cluster state)

No (stateless per node)


🧰 Kubernetes API and Declarative Config

Kubernetes works on a declarative model. You describe your desired state using YAML or JSON, and the control plane tries to match it.

Example Deployment YAML:

yaml

 

apiVersion: apps/v1

kind: Deployment

metadata:

  name: webapp

spec:

  replicas: 3

  selector:

    matchLabels:

      app: web

  template:

    metadata:

      labels:

        app: web

    spec:

      containers:

      - name: nginx

        image: nginx:1.17

        ports:

        - containerPort: 80

Apply it with:

bash

 

kubectl apply -f deployment.yaml


🔄 How Kubernetes Maintains Desired State

If a Pod crashes or a node fails, Kubernetes:

  • Detects the issue
  • Triggers the appropriate controller (e.g., ReplicaSet)
  • Recreates or reschedules the Pod on a healthy node

This self-healing mechanism makes Kubernetes resilient and production-ready.


📦 Add-Ons and Optional Components

Beyond core components, Kubernetes often includes:

Add-on

Purpose

Dashboard

Web UI for managing cluster

Metrics Server

Collects CPU/memory metrics

CoreDNS

Internal DNS for Pods/Services

Helm

Kubernetes package manager


🌐 Cluster Networking Overview

  • Every Pod gets a unique IP
  • All Pods can communicate with each other by default
  • Services abstract IPs for stable communication
  • Network plugins (CNI): Flannel, Calico, Weave

🔐 Security & Role Management (Overview)

Security Concept

Description

RBAC

Controls user access to resources

Namespaces

Segregates environments (dev, staging, prod)

ServiceAccounts

Secure access for Pods

PodSecurityPolicies

Define security context for Pods


🧪 Setting Up a Local Kubernetes Cluster

🔹 Minikube

bash

 

minikube start

kubectl get nodes

🔹 Docker Desktop

Enable Kubernetes in Docker Desktop settings (macOS/Windows).

🔹 Kind (Kubernetes in Docker)

bash

 

kind create cluster

kubectl get nodes


Summary: What You Learned in Chapter 1


  • The architecture of Kubernetes includes a control plane and worker nodes.
  • Key components include the API Server, etcd, kubelet, kube-proxy, and container runtime.
  • Kubernetes uses a declarative model to manage application state.
  • Kubernetes maintains high availability and self-healing with built-in controllers.
  • You can set up a local cluster using tools like Minikube, Kind, or Docker Desktop.

Back

FAQs


✅ 1. What is Kubernetes, and how does it differ from Docker?

Answer: Docker is used to build and run containers, while Kubernetes is a container orchestration platform that manages the deployment, scaling, and operation of multiple containers across a cluster of machines.

✅ 2. Do I need to learn Docker before learning Kubernetes?

Answer: Yes, a basic understanding of Docker is essential since Kubernetes is designed to manage and orchestrate Docker (or OCI-compatible) containers. You'll need to know how to build and run container images before deploying them with Kubernetes.

✅ 3. What is a Pod in Kubernetes?

Answer: A Pod is the smallest deployable unit in Kubernetes. It encapsulates one or more containers that share the same network, storage, and lifecycle. Pods are used to run containerized applications.

✅ 4. How do I expose my application to the internet using Kubernetes?

Answer: You can expose your application using a Service of type LoadBalancer or NodePort. For more advanced routing (e.g., domain-based routing), you can use an Ingress Controller.

✅ 5. What is a Deployment in Kubernetes?

Answer: A Deployment is a Kubernetes object that ensures a specified number of replicas (Pods) are running at all times. It handles rolling updates, rollback, and maintaining the desired state of the application.

✅ 6. Can Kubernetes run locally for learning and development?

Answer: Yes. Tools like Minikube, Kind, and Docker Desktop (with Kubernetes enabled) allow you to run a local Kubernetes cluster on your machine for development and testing.

✅ 7. What’s the difference between ConfigMap and Secret in Kubernetes?

Answer: Both are used to inject configuration data into Pods. ConfigMaps store non-sensitive data like environment variables, while Secrets are designed to store sensitive data like passwords, API tokens, or keys—encrypted at rest.

✅ 8. How does Kubernetes handle application failure or crashes?

Answer: Kubernetes automatically restarts failed containers, replaces them, reschedules Pods to healthy nodes, and ensures the desired state (like the number of replicas) is always maintained.

✅ 9. How do I monitor applications running in Kubernetes?

Answer: Kubernetes integrates well with monitoring tools like Prometheus, Grafana, Kube-state-metrics, and ELK stack (Elasticsearch, Logstash, Kibana). These tools help you track performance, health, and logs.

✅ 10. Is Kubernetes suitable for small projects or just large enterprises?

Answer: While Kubernetes shines in large, scalable environments, it can also be used for small projects—especially with tools like Minikube or cloud-managed clusters. However, simpler alternatives like Docker Compose may be better suited for truly small-scale applications.