Comprehensive Guide: Creating Your Own Kubernetes Cluster Step by Step

Comprehensive Guide: Creating Your Own Kubernetes Cluster Step by Step

Introduction

This guide will walk you through the process of creating your own Kubernetes (k8s) cluster from scratch. Whether you're a beginner or an experienced developer looking to deepen your understanding of Kubernetes, this guide will provide you with detailed, step-by-step instructions to set up a functional cluster.

Prerequisites

  • Three Ubuntu servers (one for the control plane, two for worker nodes)
  • Root or sudo access on all servers
  • Basic understanding of Linux command line

1. Initial Setup

1.1 Update System Packages

On all three servers, update the package lists:

sudo apt update        

1.2 Set Hostnames

Set unique hostnames for each server:

For the control plane:

sudo hostnamectl set-hostname k8s-control        

For the first worker node:

sudo hostnamectl set-hostname k8s-worker1        

For the second worker node:

sudo hostnamectl set-hostname k8s-worker2        

1.3 Configure Hosts File

On all three servers, edit the /etc/hosts file:

sudo nano /etc/hosts        

Add the following lines, replacing privateip with the actual private IP addresses of your servers:

privateip k8s-worker1
privateip k8s-worker2
privateip k8s-control        

Save and exit the file (Ctrl+X, then Y, then Enter).

2. Install Required Libraries

2.1 Load Kernel Modules

On all three servers, add the necessary kernel modules:

cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF        

Load the modules:

sudo modprobe overlay
sudo modprobe br_netfilter        

2.2 Configure System Settings

On all three servers, set up required sysctl params:

cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-ip6tables=1
EOF        

Apply sysctl params without reboot:

sudo sysctl --system        

3. Install Containerd

3.1 Install Prerequisites

On all three servers, install required packages:

sudo apt-get install -y curl ca-certificates gnupg        

3.2 Add Docker Repository

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null        

3.3 Update and Install Containerd

sudo apt-get update
sudo apt-get install -y containerd.io        

3.4 Configure Containerd

On all three servers, Create default configuration file:

sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml        

3.5 Disable Swap

sudo swapoff -a        

3.6 Restart Containerd

sudo systemctl restart containerd
sudo systemctl status containerd        

Ensure that the status is "active (running)".

4. Install Kubernetes Components

4.1 Add Kubernetes Repository

On all three servers:

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -        

Add Kubernetes repository:

sudo tee /etc/apt/sources.list.d/kubernetes.list <<EOF
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF        

4.2 Install Kubernetes Tools

sudo apt-get update
sudo apt-get install -y kubelet=1.24.0-00 kubeadm=1.24.0-00 kubectl=1.24.0-00        

4.3 Hold Package Versions

Prevent automatic updates:

sudo apt-mark hold kubelet kubeadm kubectl        

5. Initialize Control Plane

CAUTION: The following steps are to be performed only on the control plane node (k8s-control).

5.1 Initialize Kubernetes

sudo kubeadm init --pod-network-cidr 192.168.0.0/16 --kubernetes-version 1.24.0        

5.2 Set Up kubectl

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config        

5.3 Verify Node Status

kubectl get nodes        

You should see your control plane node listed, but its status will be "NotReady" until we set up networking.

6. Install Networking Plugin (Calico)

On the control plane node:

kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.1/manifests/calico.yaml        

Wait a few moments, then check the node status again:

kubectl get nodes        

The status should now be "Ready".

7. Join Worker Nodes

7.1 Generate Join Command

On the control plane node:

kubeadm token create --print-join-command        

This will output a command that looks something like:

kubeadm join [control-plane-ip]:6443 --token [token] --discovery-token-ca-cert-hash sha256:[hash]        

7.2 Join Workers

Copy the output from the previous command. On each worker node, run this command with sudo:

sudo [paste-join-command-here]        

7.3 Verify Cluster Status

Back on the control plane node, check the status of your cluster:

kubectl get nodes        

You should now see all of your nodes listed with a status of "Ready".

Conclusion

Congratulations! You've successfully set up your own Kubernetes cluster. This cluster is now ready for deploying applications and exploring the vast ecosystem of Kubernetes.

Remember, this is a basic setup suitable for learning and testing. For production environments, additional security measures and high-availability configurations would be necessary.

Next Steps

  • Learn how to deploy applications to your cluster
  • Explore Kubernetes concepts like Pods, Services, and Deployments
  • Set up monitoring and logging for your cluster
  • Learn about Kubernetes security best practices

Happy exploring in the world of Kubernetes!

要查看或添加评论,请登录

Kundan Antyakula??的更多文章

社区洞察

其他会员也浏览了