0
0
KubernetesHow-ToBeginner · 4 min read

How to Install Kubernetes: Step-by-Step Guide

To install Kubernetes, use the kubeadm tool which simplifies cluster setup. First, install Docker and kubeadm components, then initialize the cluster with kubeadm init.
📐

Syntax

The main command to install and initialize Kubernetes cluster is kubeadm init. Before that, you install required components like kubeadm, kubelet, and kubectl. Docker or another container runtime must be installed first.

  • kubeadm init: Initializes the Kubernetes control-plane node.
  • kubeadm join: Used on worker nodes to join the cluster.
  • kubectl: Command-line tool to interact with the cluster.
bash
sudo apt-get update && sudo apt-get install -y docker.io
sudo systemctl enable docker && sudo systemctl start docker
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
💻

Example

This example shows how to install Kubernetes on an Ubuntu machine, initialize the cluster, and set up the local kubectl configuration.

bash
sudo apt-get update && sudo apt-get install -y docker.io
sudo systemctl enable docker && sudo systemctl start docker
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes
Output
NAME STATUS ROLES AGE VERSION master Ready control-plane 1m v1.27.3
⚠️

Common Pitfalls

Common mistakes when installing Kubernetes include:

  • Not installing or starting Docker/container runtime before kubeadm init.
  • Skipping the kubectl config setup, causing inability to run commands.
  • Using incompatible Kubernetes and Docker versions.
  • Not setting the correct --pod-network-cidr for the chosen network plugin.

Always follow the exact order: install container runtime, install Kubernetes tools, initialize cluster, then configure kubectl.

bash
Wrong order example:

sudo kubeadm init --pod-network-cidr=10.244.0.0/16
sudo apt-get install -y docker.io

Right order example:

sudo apt-get install -y docker.io
sudo systemctl start docker
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
📊

Quick Reference

Summary tips for installing Kubernetes:

  • Install Docker or another container runtime first.
  • Install kubeadm, kubelet, and kubectl from official Kubernetes repos.
  • Run kubeadm init with the correct pod network CIDR.
  • Set up kubectl config to manage the cluster.
  • Deploy a pod network plugin like Flannel or Calico after initialization.

Key Takeaways

Install a container runtime like Docker before installing Kubernetes components.
Use kubeadm to initialize the cluster with the correct pod network CIDR.
Configure kubectl after initialization to interact with the cluster.
Follow the installation steps in order to avoid common errors.
Deploy a pod network plugin after cluster setup for full functionality.