Kubernetes has quickly become the go-to solution for container orchestration, allowing organizations to automate the deployment, scaling, and management of containerized applications. If you’re ready to dive deeper into Kubernetes, setting up your own Kubernetes cluster is an essential step to understanding how it works. In this tutorial, we’ll walk you through the process of setting up a Kubernetes cluster on your own machine using Minikube (for local setups) or kubeadm (for a more robust, production-like environment).
Let’s get started with the step-by-step guide for setting up a Kubernetes cluster!
Prerequisites
Before we jump into the setup, make sure you have the following prerequisites:
- A Linux or macOS machine, or Windows with WSL 2 (Windows Subsystem for Linux) installed.
- A terminal or command-line interface to run the commands.
- A basic understanding of containers and Kubernetes concepts.
- Docker installed on your system (for local setups using Minikube or kubeadm).
- kubectl installed (this is the Kubernetes command-line tool used to interact with the cluster).
Install kubectl
If you haven’t installed kubectl yet, you can easily do it by following these steps:
- On Linux or macOS:
- curl -LO “https://storage.googleapis.com/kubernetes-release/release/v1.27.1/bin/darwin/amd64/kubectl”
- chmod +x ./kubectl
- sudo mv ./kubectl /usr/local/bin/kubectl
- On Windows (using PowerShell):
- Invoke-WebRequest -Uri https://storage.googleapis.com/kubernetes-release/release/v1.27.1/bin/windows/amd64/kubectl.exe -OutFile kubectl.exe
After installing, verify by running:
kubectl version –client
If everything is set up, you should see the version of kubectl printed in the terminal.
Option 1: Setting Up a Kubernetes Cluster Using Minikube (Local Setup)
If you’re just getting started or need a local Kubernetes environment, Minikube is an excellent tool. It creates a single-node Kubernetes cluster on your local machine, making it easy to test and develop Kubernetes workloads.
Step 1: Install Minikube
- On Linux:
- curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
- chmod +x minikube
- sudo mv minikube /usr/local/bin/
- On macOS (using Homebrew):
- brew install minikube
- On Windows (using Chocolatey):
- choco install minikube
Step 2: Start Minikube
Once Minikube is installed, you can start your Kubernetes cluster:
minikube start
Minikube will download a lightweight VM image and configure the Kubernetes cluster for you. Depending on your system and internet speed, this may take a few minutes.
Step 3: Verify the Cluster
Once Minikube has successfully started the cluster, you can verify it with:
kubectl cluster-info
This command will show you the URL of the Kubernetes control plane, confirming that your local Kubernetes cluster is up and running.
Step 4: Interact with the Cluster
You can now use kubectl to interact with your cluster. For example, to check the nodes in the cluster:
kubectl get nodes
The output should show the status of the single Minikube node (usually called “minikube”) in your cluster.
Option 2: Setting Up a Kubernetes Cluster Using kubeadm (Production-Like Setup)
For a more robust setup, such as in a production-like environment with multiple nodes, we will use kubeadm. This process involves configuring multiple virtual machines or physical servers to form a Kubernetes cluster.
Step 1: Set Up Your Servers
You need at least two machines: one for the master node and one (or more) for worker nodes. These can be physical machines, virtual machines, or cloud instances. All nodes should run a Linux distribution (Ubuntu 20.04 or later is preferred).
Ensure that each node has the following:
- Docker installed
- Kubernetes components (kubeadm, kubelet, kubectl) installed
- Proper networking configurations, such as disabling swap (important for Kubernetes performance).
To disable swap on each node, run:
sudo swapoff -a
Then, to permanently disable swap, edit the /etc/fstab file and comment out the swap line.
Step 2: Install Kubernetes on All Nodes
To install Kubernetes components on all nodes, follow these steps on each machine:
- Update package listings:
- sudo apt-get update && sudo apt-get upgrade -y
- Install required packages and dependencies:
- sudo apt-get install -y apt-transport-https curl
- Add the Kubernetes APT repository:
- curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add –
- sudo apt-add-repository “deb https://apt.kubernetes.io/ kubernetes-xenial main”
- Install kubeadm, kubelet, and kubectl:
- sudo apt-get update
- sudo apt-get install -y kubeadm kubelet kubectl
- sudo apt-mark hold kubeadm kubelet kubectl
Step 3: Initialize the Master Node
On the master node, initialize the Kubernetes control plane with kubeadm:
sudo kubeadm init –pod-network-cidr=10.244.0.0/16
This command will output a join token, which is needed to connect worker nodes to the master. Save this token for the next step.
After the master node initialization, set up the kubeconfig file so that your user can interact with the cluster:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Step 4: Install a Network Plugin
Kubernetes requires a network plugin to enable communication between the nodes. In this tutorial, we’ll use Flannel, but you can choose others like Calico or Weave.
Run the following command on the master node:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
This command will deploy Flannel, which will manage the networking between pods.
Step 5: Join Worker Nodes to the Cluster
On each worker node, use the kubeadm join command (which was output during the initialization step of the master node). It should look something like this:
sudo kubeadm join <MASTER_IP>:6443 –token <YOUR_TOKEN> –discovery-token-ca-cert-hash sha256:<HASH>
Once the worker nodes join the cluster, you can verify that they are connected by running the following command on the master node:
kubectl get nodes
You should see the master node and the worker nodes listed as part of the cluster.
Step 6: Deploy Your First Pod
Now that your cluster is set up, you can deploy applications! To test everything is working, let’s deploy a simple pod:
kubectl run nginx –image=nginx –replicas=2 –port=80
This command will create a pod running the Nginx web server. You can check the status of your pod with:
kubectl get pods
Conclusion
You’ve successfully set up a Kubernetes cluster, either locally with Minikube or using kubeadm for a more production-like setup. Now you have the power of Kubernetes at your fingertips, enabling you to deploy, scale, and manage your containerized applications with ease.
As you progress, you’ll want to explore Kubernetes features like persistent storage, Ingress controllers, Helm for package management, and advanced networking solutions. With Kubernetes at the heart of your application infrastructure, you’ll be able to scale your workloads efficiently and manage them with confidence.
Happy Kubernetes learning!