Technical Theory

Preparing Infrastructure for Kubernetes Installation

Introduction

This tutorial guides you through preparing the underlying infrastructure for installing a Kubernetes cluster. It covers essential steps like configuring hostnames, disabling swap, setting up a container runtime (Docker or containerd), and opening necessary firewall ports. Prior knowledge of basic Linux administration, networking concepts, and SSH is assumed.

Task 1: Hostname Configuration

Proper hostname configuration is crucial for Kubernetes node identification and internal communication.

  1. Set Static Hostnames: Ensure each node has a unique and resolvable hostname. This is typically done via /etc/hostname and /etc/hosts.

    NODE_TYPE // bash
    sudo hostnamectl set-hostname <your-node-name>
    Replace <your-node-name> with the desired hostname for the node. This command requires sudo privileges.
  2. Verify Hostname Resolution: Confirm that all nodes can resolve each other’s hostnames. Edit /etc/hosts on each node to include entries for all other nodes if DNS is not configured.

    NODE_TYPE // bash
    sudo vi /etc/hosts

    Add lines similar to the following (adjust IP addresses and hostnames accordingly):

    NODE_TYPE // text
    192.168.1.10  node1.example.com node1
    192.168.1.11  node2.example.com node2
    192.168.1.12  node3.example.com node3
    Ensure the /etc/hosts file is consistent across all nodes. Inconsistent entries can lead to communication issues within the Kubernetes cluster.
  3. Test Hostname Resolution: Use ping or ssh to verify that the hostnames resolve correctly.

    NODE_TYPE // bash
    ping node2.example.com

    Expected Output:

    NODE_TYPE // output
    PING node2.example.com (192.168.1.11) 56(84) bytes of data.
    64 bytes from node2.example.com (192.168.1.11): icmp_seq=1 ttl=64 time=0.500 ms
    ...

Task 2: Disabling Swap

Kubernetes requires swap to be disabled for optimal performance and stability.

  1. Disable Swap: Turn off swap immediately.

    NODE_TYPE // bash
    sudo swapoff -a
    This command temporarily disables swap. It does not persist across reboots.
  2. Permanently Disable Swap: Edit /etc/fstab to prevent swap from being enabled on boot.

    NODE_TYPE // bash
    sudo vi /etc/fstab

    Comment out or remove any lines that refer to swap partitions or files. For example:

    NODE_TYPE // text
    # /dev/mapper/vg0-swap swap swap defaults 0 0
    #/swapfile none swap sw 0 0
  3. Verify Swap is Disabled: Check the swap status.

    NODE_TYPE // bash
    free -h

    Expected output should show Swap: 0B. If swap is still enabled, double-check /etc/fstab and reboot the system.

    NODE_TYPE // output
                  total        used        free      shared  buff/cache   available
    Mem:           7.7Gi       553Mi       6.2Gi        12Mi       985Mi       7.0Gi
    Swap:             0B          0B          0B

Task 3: Container Runtime Installation (Docker or Containerd)

Kubernetes uses a container runtime to manage containers. Docker and containerd are the most commonly used runtimes. This example uses containerd.

  1. Install containerd: Follow the official containerd installation instructions for your operating system. This example assumes a Debian-based system.

    NODE_TYPE // bash
    sudo apt-get update
    sudo apt-get install -y containerd
    Refer to the official containerd documentation for the most up-to-date installation instructions for your specific operating system: https://containerd.io/docs/getting-started/
  2. Configure containerd: Create the default containerd configuration file.

    NODE_TYPE // bash
    sudo mkdir -p /etc/containerd
    sudo containerd config default | sudo tee /etc/containerd/config.toml
  3. Enable Systemd Cgroup: Edit /etc/containerd/config.toml and set SystemdCgroup = true under the [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options] section.

    NODE_TYPE // bash
    sudo vi /etc/containerd/config.toml
    NODE_TYPE // toml
    [plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
        SystemdCgroup = true
    Using systemd cgroup manager is highly recommended for Kubernetes. Not doing so can cause issues with resource management.
  4. Restart containerd: Restart the containerd service to apply the changes.

    NODE_TYPE // bash
    sudo systemctl restart containerd
  5. Verify containerd Status: Ensure that containerd is running and enabled.

    NODE_TYPE // bash
    sudo systemctl status containerd

    Expected Output (status should be “active (running)”):

    NODE_TYPE // output
     containerd.service - containerd container runtime
         Loaded: loaded (/lib/systemd/system/containerd.service; enabled; vendor preset: enabled)
         Active: active (running) since Thu 2026-04-09 14:00:00 UTC; 10s ago
           Docs: https://containerd.io
       Main PID: 1234 (containerd)
          Tasks: 42
         Memory: 100.0M
            CPU: 2.500s
         CGroup: /system.slice/containerd.service
    ...

Task 4: Firewall Configuration

Kubernetes requires certain ports to be open for communication between nodes and the control plane. Configure your firewall to allow the necessary traffic.

  1. Identify Required Ports: Consult the Kubernetes documentation for the specific ports required for your deployment scenario. Some common ports include:

    • Control Plane Nodes:
      • 6443: Kubernetes API server
      • 2379-2380: etcd server client API
      • 10250: Kubelet API
      • 10251: kube-scheduler
      • 10252: kube-controller-manager
    • Worker Nodes:
      • 10250: Kubelet API
      • 30000-32767: NodePort Services
  2. Open Firewall Ports: Use ufw, firewalld, or your preferred firewall management tool to open the required ports. This example uses ufw.

    NODE_TYPE // bash
    sudo ufw allow 6443
    sudo ufw allow 2379:2380
    sudo ufw allow 10250
    sudo ufw allow 10251
    sudo ufw allow 10252
    sudo ufw allow 30000:32767
    sudo ufw enable
    Adjust the port ranges and protocols based on your specific Kubernetes configuration. Consult your operating system’s documentation for firewall management.
  3. Verify Firewall Rules: Check the active firewall rules to ensure the ports are open.

    NODE_TYPE // bash
    sudo ufw status

    Example output:

    NODE_TYPE // output
    Status: active
    
    To                         Action      From
    --                         ------      ----
    6443                       ALLOW       Anywhere
    2379:2380                  ALLOW       Anywhere
    10250                      ALLOW       Anywhere
    10251                      ALLOW       Anywhere
    10252                      ALLOW       Anywhere
    30000:32767                ALLOW       Anywhere

Tuning kernel parameters can improve the performance and stability of your Kubernetes cluster.

  1. Configure Kernel Parameters: Modify /etc/sysctl.conf to set specific kernel parameters.

    NODE_TYPE // bash
    sudo vi /etc/sysctl.conf

    Add the following lines (or adjust values as needed):

    NODE_TYPE // text
    net.bridge.bridge-nf-call-iptables  = 1
    net.ipv4.ip_forward                 = 1
    vm.max_map_count                    = 262144
    These parameters are crucial for enabling network functionalities in Kubernetes, especially when using network plugins like Calico or Flannel.
  2. Apply Kernel Parameters: Apply the changes without rebooting.

    NODE_TYPE // bash
    sudo sysctl -p
  3. Verify Kernel Parameters: Check that the parameters have been applied correctly.

    NODE_TYPE // bash
    sysctl net.bridge.bridge-nf-call-iptables
    sysctl net.ipv4.ip_forward
    sysctl vm.max_map_count

    Expected Output:

    NODE_TYPE // output
    net.bridge.bridge-nf-call-iptables = 1
    net.ipv4.ip_forward = 1
    vm.max_map_count = 262144

Conclusion

You have successfully prepared the underlying infrastructure for a Kubernetes cluster. This includes configuring hostnames, disabling swap, setting up a container runtime, and opening the necessary firewall ports. These steps are essential for ensuring a stable and performant Kubernetes environment. Proceed to install Kubernetes components such as kubeadm, kubelet, and kubectl to set up your cluster.

Next Topic