# Kubernetes

Kubernetes architecture is a distributed system designed to manage containerized applications across multiple physical or virtual machines called **Nodes**. It follows a client-server model with two primary components:

### Core Components

1. **Control Plane**: This includes key components such as:
   * **kube-apiserver**: Manages cluster interactions.
   * **etcd**: Stores cluster data.
   * **kube-scheduler**: Assigns pods to nodes.
   * **kube-controller-manager**: Handles cluster operations[1](https://spacelift.io/blog/kubernetes-architecture)[3](https://kubernetes.io/docs/concepts/overview/components/)[5](https://devtron.ai/blog/kubernetes-architecture-the-ultimate-guide/).
2. **Worker Nodes**: These are where application workloads run and include:
   * **kubelet**: Manages pod execution.
   * **kube-proxy**: Handles network communications.
   * **Container Runtime**: Executes containers[1](https://spacelift.io/blog/kubernetes-architecture)[3](https://kubernetes.io/docs/concepts/overview/components/)[5](https://devtron.ai/blog/kubernetes-architecture-the-ultimate-guide/).

### Additional Components

* **Add-ons**: Extend functionality with tools for networking, monitoring, and storage[1](https://spacelift.io/blog/kubernetes-architecture)[3](https://kubernetes.io/docs/concepts/overview/components/).
* **Pods**: Logical groups of containers.
* **Services**: Provide stable network access to pods[2](https://www.aquasec.com/cloud-native-academy/kubernetes-101/kubernetes-architecture/)[3](https://kubernetes.io/docs/concepts/overview/components/).

Kubernetes is highly scalable and fault-tolerant, making it suitable for large-scale deployments[1](https://spacelift.io/blog/kubernetes-architecture)[5](https://devtron.ai/blog/kubernetes-architecture-the-ultimate-guide/).

<figure><img src="https://2810768633-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FXV2CevcQp5B7scCOY3WF%2Fuploads%2F1jTb0b3r2zcC79d2tVrL%2Fimage.png?alt=media&#x26;token=253750fc-2758-4834-a7fb-27fd9de280d2" alt=""><figcaption><p>Kubernetes Architecture - Image Credits: Platform9</p></figcaption></figure>

Kubernetes offers several tools for installing and managing clusters, each with unique features and use cases. Here's a comparison of **kubeadm** with other popular tools:

### Comparison of Kubernetes Installation Tools

### 1. kubeadm

* **Purpose**: Initializes a Kubernetes control plane node and joins worker nodes to form a cluster.
* **Platforms**: Supports most Linux distributions, including Ubuntu, CentOS, and Fedora.
* **Features**: Easy to use, supports both bare-metal and cloud environments. It provides a simple way to create and manage clusters.
* **Limitations**: Does not handle infrastructure provisioning; requires manual setup of nodes.

### 2. kops

* **Purpose**: Automates the provisioning and management of Kubernetes clusters on cloud platforms.
* **Platforms**: Primarily supports AWS, with beta support for GCE and alpha for VMware vSphere.
* **Features**: Handles infrastructure provisioning and cluster management, making it ideal for cloud-native environments.
* **Limitations**: Limited flexibility in terms of deployment platforms compared to other tools.

### 3. Kubespray

* **Purpose**: Uses Ansible for provisioning and orchestrating Kubernetes clusters across multiple platforms.
* **Platforms**: Supports bare metal and various cloud providers (AWS, GCE, Azure, OpenStack).
* **Features**: Offers high flexibility and customization options due to its use of Ansible. Supports a wide range of Linux distributions.
* **Limitations**: Requires Ansible knowledge and can be more complex to set up compared to kubeadm.

### 4. Cluster API

* **Purpose**: Provides a declarative API for managing Kubernetes cluster lifecycle, including provisioning and upgrading.
* **Platforms**: Supports multiple infrastructure providers (e.g., AWS, Azure, vSphere).
* **Features**: Focuses on infrastructure as code (IaC) practices, making it suitable for large-scale and multi-cluster environments.
* **Limitations**: Requires more expertise in managing infrastructure as code.

### Key Differences

| **Tool**        | **Primary Use Case**                        | **Platforms**                              | **Complexity** |
| --------------- | ------------------------------------------- | ------------------------------------------ | -------------- |
| **kubeadm**     | Simple cluster setup                        | Most Linux distributions                   | Low            |
| **kops**        | Cloud-native cluster management             | Primarily AWS, GCE (beta), vSphere (alpha) | Medium         |
| **Kubespray**   | Flexible, multi-platform cluster deployment | Bare metal, multiple clouds                | High           |
| **Cluster API** | Declarative cluster lifecycle management    | Multiple infrastructure providers          | High           |

### Choosing the Right Tool

* **Use kubeadm** for quick, straightforward cluster setup on existing infrastructure.
* **Choose kops** for cloud-native environments, especially AWS.
* **Select Kubespray** for complex, multi-platform deployments requiring high customization.
* **Opt for Cluster API** when managing large-scale, multi-cluster environments with infrastructure as code.

Each tool has its strengths and is suited to different scenarios, making it important to evaluate your specific needs before choosing a Kubernetes installation tool.



## Setup

Here’s a comprehensive step-by-step guide to set up a Kubernetes cluster using **kubeadm** on Ubuntu/Debian-based systems, combining best practices from multiple sources:

### Prerequisites

* **Minimum 2 nodes** (1 master, 1+ worker) running Ubuntu 22.04+
* SSH access with sudo privileges
* At least **2GB RAM** and **2 CPUs** per node
* Unique hostnames for each node (e.g., `master-node`, `worker-1`)

### Step 1: Prepare All Nodes

### Disable Swap

```bash
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab  # Permanent disable [1][4]
```

### Configure Kernel Modules

```bash
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay && sudo modprobe br_netfilter [1][4]
```

### Enable Bridged Traffic

```bash
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system [1][4]
```

### Step 2: Install Container Runtime (containerd)

### Install Dependencies

```bash
sudo apt update && sudo apt install -y curl gnupg2 [1]
```

### Add Docker Repository

```bash
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update [1][3]
```

### Install containerd

```bash
sudo apt install -y containerd.io
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml
sudo systemctl restart containerd [1][4]
```

### Step 3: Install Kubernetes Tools

### Add Kubernetes Repo

```bash
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list [1][3]
```

### Install Packages

```bash
sudo apt update && sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl  # Prevent auto-updates [1][3]
```

### Step 4: Initialize Control Plane (Master Node)

### Bootstrap Cluster

```bash
sudo kubeadm init --pod-network-cidr=192.168.0.0/16 --control-plane-endpoint=master-node [1][4]
```

### Configure kubectl

```bash
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config [1][4]
```

### Step 5: Install Network Plugin (Calico)

```bash
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.27.0/manifests/calico.yaml [4]
```

### Step 6: Join Worker Nodes

Use the `kubeadm join` command generated during master initialization:

```bash
sudo kubeadm join <master-ip>:6443 --token <token> --discovery-token-ca-cert-hash sha256:<hash> [1][4]
```

### Step 7: Verify Cluster

```bash
kubectl get nodes  # Should show all nodes as 'Ready'
kubectl get pods -A  # Check system pods [4]
```

### Alternative: Single-Node Setup with K3s

For development/testing:

```bash
curl -sfL https://get.k3s.io | sh -
sudo cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
export KUBECONFIG=~/.kube/config
kubectl get nodes [5]
```

### Troubleshooting Tips

1. If nodes show `NotReady`, verify network plugin installation
2. Check `journalctl -u kubelet` for service errors
3. Ensure port 6443 is open between nodes

This guide combines methodologies from phoenixNAP[1](https://phoenixnap.com/kb/install-kubernetes-on-ubuntu), Kubernetes docs[2](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/), LinuxConfig[3](https://linuxconfig.org/how-to-install-kubernetes-on-all-linux-distros), and DevOpsCube[4](https://devopscube.com/setup-kubernetes-cluster-kubeadm/). For production, consider using managed Kubernetes services like EKS or GKE.
