Here’s a guide to installing and configuring Kubernetes on a Linux server. This guide covers the essential steps for setting up a single-node Kubernetes cluster, commonly used for testing, development, or as a foundation for scaling to a multi-node setup.
What is Kubernetes? #
It is an open-source platform designed to automate the deployment, scaling, and management of containerized applications. It enables the creation and orchestration of multiple containers across clusters of hosts, providing high availability, scalability, and efficient resource usage.
Prerequisites #
- Linux Server (Ubuntu 20.04+, Debian, CentOS 7+ recommended)
- Minimum 2 CPUs, 2 GB RAM for a single-node setup
- User with sudo privileges
- Firewall settings adjusted to allow communication between nodes if you’re setting up a multi-node cluster
Pre-setup Steps #
- Disable Swap: Kubernetes requires swap to be disabled.
sudo swapoff -a
# Comment out the swap line in /etc/fstab to disable swap on boot
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
2. Install Docker or containerd: Kubernetes needs a container runtime, and Docker is a popular choice.
To install Docker:
sudo apt update
sudo apt install -y docker.io
sudo systemctl enable --now docker
Step 1: Install kubeadm, kubelet, and kubectl #
- Add the Kubernetes apt repository (for Debian/Ubuntu-based distributions):
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl
sudo curl -fsSL 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 update
2. Install Kubernetes tools:
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
For RHEL-based systems (Like Almalinux/CentOS), use:
sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
3. Enable and start kubelet:
sudo systemctl enable kubelet
sudo systemctl start kubelet
Step 2: Initialize the Kubernetes Control Plane #
For single-node clusters, the control plane and worker nodes will be on the same server.
- Initialize the cluster with
kubeadm
:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
- The
--pod-network-cidr=10.244.0.0/16
flag specifies the network range for the pod network, which is compatible with several network plugins like Flannel.
2. Configure kubectl for your user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
3. Save the join command: After initialization, kubeadm
will provide a command to add worker nodes to your cluster. Save this command as it’s needed for adding additional nodes later.
Step 3: Install a Pod Network (Flannel) #
A pod network enables communication between pods across nodes in a cluster.
- Install Flannel as the network plugin:
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
2. Verify that all nodes are ready by running:
kubectl get nodes
You should see the status as Ready
for your node.
Step 4: Allow Scheduling on the Control Plane Node (Single-Node Setup Only) #
- If you’re setting up a single-node cluster, you need to allow the control plane to run pods:
kubectl taint nodes --all node-role.kubernetes.io/control-plane-
- This removes the taint that prevents the master node from scheduling pods.
Step 5: Basic kubectl Commands #
Now that your Kubernetes cluster is up and running, you can start interacting with it using kubectl
.
- Check Cluster Status:
kubectl get nodes
kubectl get pods -A
- Create a Test Deployment:
kubectl create deployment nginx --image=nginx
- Expose Deployment as a Service:
kubectl expose deployment nginx --port=80 --type=NodePort
- Get the Service Port:
kubectl get svc nginx
Step 6: Set Up Kubernetes Dashboard (Optional) #
The Kubernetes Dashboard is a web-based UI that allows you to manage your cluster graphically.
- Apply the Dashboard YAML:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml
2. Create a Service Account and Cluster Role Binding for accessing the dashboard:
kubectl create serviceaccount dashboard-admin-sa
kubectl create clusterrolebinding dashboard-admin-sa --clusterrole=cluster-admin --serviceaccount=default:dashboard-admin-sa
3. Get the Authentication Token for the dashboard:
kubectl get secret $(kubectl get serviceaccount dashboard-admin-sa -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
4. Access the Dashboard:
Use kubectl proxy
to access the dashboard locally:
kubectl proxy
The dashboard will be accessible at:
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
Step 7: Scaling and Managing Your Cluster (Optional) #
To scale your Kubernetes setup beyond a single node:
- Get the Join Command from the control plane (output from
kubeadm init
). - Run the join command on additional worker nodes to add them to the cluster.
Conclusion #
You’ve now set up a Kubernetes cluster on a Linux server. This guide covered the installation of Kubernetes components, setting up a pod network, and verifying cluster functionality with test deployments. With Kubernetes in place, you can now deploy, manage, and scale containerized applications effectively.