Kubernetes, also known as K8s, is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It’s the de facto standard for running production-grade containerized applications, providing powerful features like automatic scaling, rolling updates, and self-healing capabilities. This guide will walk you through setting up a Kubernetes cluster from scratch, providing a solid foundation for deploying and managing your containerized applications.
Prerequisites
Before starting, ensure that you have the following:
- Basic Understanding of Containers: Familiarity with Docker and containerization concepts is helpful.
- A Machine with a Linux OS: The setup guide assumes you’re using a Linux distribution, such as Ubuntu, as the host operating system.
- Sufficient Resources: Ensure your machine meets the minimum hardware requirements: at least 2 CPUs, 2GB RAM, and 20GB of disk space.
Step 1: Install Docker
Kubernetes uses Docker as its default container runtime. Install Docker on your machine if it’s not already installed:
- Update the Package Index:
sudo apt-get update
- Install Required Packages:
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
- Add Docker’s Official GPG Key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
- Add Docker Repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
- Install Docker:
sudo apt-get update
sudo apt-get install docker-ce
- Verify Docker Installation:
sudo systemctl status docker
Docker should now be running on your system.
Step 2: Install kubeadm, kubelet, and kubectl
Kubernetes provides three main tools: kubeadm
(to set up the cluster), kubelet
(to run the Kubernetes nodes), and kubectl
(the command-line tool to interact with the cluster).
- Update the Package Index and Install Transport Layer:
sudo apt-get update
sudo apt-get install -y apt-transport-https curl
- Add the Kubernetes Signing Key:
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
- Add the Kubernetes Repository:
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
- Install kubeadm, kubelet, and kubectl:
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
- Check the Status of Kubelet:
sudo systemctl status kubelet
The kubelet service should be running, but it will fail to start fully until you initialize the cluster.
Step 3: Initialize the Kubernetes Cluster
Now that the tools are installed, you can initialize your Kubernetes cluster using kubeadm
.
- Disable Swap: Kubernetes requires swap to be disabled. Disable swap temporarily:
sudo swapoff -a
To permanently disable swap, remove or comment out the swap entry in /etc/fstab
.
- Initialize the Cluster:
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
- The
--pod-network-cidr
flag specifies the CIDR block for the Pod network. We’ll use192.168.0.0/16
, which is compatible with the Calico network plugin.
- Set Up kubeconfig for kubectl: After initializing the cluster, you’ll see instructions to set up
kubectl
. Run the following commands:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
- Verify the Cluster: Check the status of your nodes and components:
kubectl get nodes
Your master node should be listed as Ready
.
Step 4: Install a Pod Network Add-on
A Pod network is required for containers within the Kubernetes cluster to communicate with each other. There are several networking options available, such as Calico, Flannel, and Weave. In this guide, we’ll install Calico.
- Install Calico:
kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yaml
- Verify the Installation: Ensure that all the Calico components are running:
kubectl get pods -n kube-system
You should see several Calico pods listed as Running
.
Step 5: Join Worker Nodes to the Cluster (Optional)
If you’re setting up a multi-node Kubernetes cluster, you need to join worker nodes to the master node.
- Get the Join Command: When you initialized the cluster with
kubeadm
, it provided akubeadm join
command. This command includes a token and the IP address of the master node. - Run the Join Command on Worker Nodes: On each worker node, run the
kubeadm join
command:
sudo kubeadm join <master-ip>:<master-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
- Verify Nodes in the Cluster: After the worker nodes join, check the nodes from the master:
kubectl get nodes
You should see all your nodes listed, including the worker nodes.
Step 6: Deploy a Sample Application
Now that your Kubernetes cluster is up and running, let’s deploy a simple application to ensure everything is working correctly.
- Deploy a Nginx Application: Create a deployment for the Nginx web server:
kubectl create deployment nginx --image=nginx
- Expose the Deployment: Create a service to expose the Nginx deployment on a specific port:
kubectl expose deployment nginx --port=80 --type=NodePort
This command will expose the Nginx application on a NodePort, making it accessible from outside the cluster.
- Access the Application: To access the Nginx web server, find the NodePort that Kubernetes assigned:
kubectl get svc
Access the application using the IP address of the node and the NodePort:
curl http://<node-ip>:<node-port>
You should see the Nginx welcome page.
Step 7: Enable Persistent Storage (Optional)
For applications that require persistent data storage, you need to set up persistent volumes (PVs) and persistent volume claims (PVCs).
- Create a Persistent Volume: Define a PV in a YAML file, specifying the storage capacity, access modes, and storage location.
- Create a Persistent Volume Claim: Define a PVC that requests storage from the PV. Applications will use this PVC to access the persistent storage.
- Mount the PVC to a Pod: Modify your Pod or deployment YAML file to include the PVC as a volume. This mounts the persistent storage to the Pod, allowing it to read and write data.
Conclusion
Setting up a Kubernetes cluster from scratch is a critical step in learning how to manage and deploy containerized applications at scale. By following this guide, you’ve installed Docker and Kubernetes, initialized a cluster, set up a networking solution, and deployed your first application. Kubernetes offers powerful features that make it the ideal choice for managing complex, distributed systems in production environments. As you continue to explore Kubernetes, you can delve deeper into advanced topics like multi-cluster management, automated scaling, and integrating CI/CD pipelines.