Kubernetes Cluster On Ubuntu With Kubeadm: A Step-by-Step Guide
Hey guys! Today, we're diving into the awesome world of Kubernetes and I'm going to show you how to create a Kubernetes cluster on Ubuntu using Kubeadm. Kubernetes, often abbreviated as K8s, has become the go-to platform for container orchestration. If you're looking to deploy, scale, and manage containerized applications, knowing how to set up a Kubernetes cluster is crucial. So, let’s get started!
Prerequisites
Before we jump into the nitty-gritty, let’s make sure you have everything you need. This is like gathering your ingredients before starting a recipe – super important!
- Ubuntu Servers: You’ll need at least two Ubuntu servers. One will act as the master node, and the others will be worker nodes. I recommend using Ubuntu 18.04 or 20.04, but newer versions should also work. Ensure each server has a static IP address and can communicate with each other.
- SSH Access: Make sure you can SSH into all your servers. This will make your life a lot easier when you need to run commands on each node.
- Root Privileges: You'll need
sudoor root access on all the servers to install and configure the necessary components. - Internet Connection: All servers need an internet connection to download packages.
- Basic Linux Knowledge: A basic understanding of Linux commands will definitely help you along the way.
Step 1: Install Container Runtime (Docker)
First off, we need a container runtime. Docker is a popular choice, so that's what we’ll use. Here's how to install Docker on your Ubuntu servers. We will be focusing on installing docker in this part, so that kubernetes can manage containers.
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
Let's break this down:
sudo apt update: Updates the package index.sudo apt install -y apt-transport-https ca-certificates curl gnupg lsb-release: Installs necessary packages to allow apt to use repositories over HTTPS.curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg: Adds Docker’s official GPG key.echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null: Adds the Docker repository to your sources list.sudo apt install -y docker-ce docker-ce-cli containerd.io: Installs Docker, the Docker CLI, and containerd.io.
After installation, start and enable Docker:
sudo systemctl start docker
sudo systemctl enable docker
Verify Docker is running:
sudo docker run hello-world
If you see the “Hello from Docker!” message, you're good to go!
Step 2: Install Kubeadm, Kubelet, and Kubectl
Next up, we need to install Kubeadm, Kubelet, and Kubectl on all the servers. Kubeadm is the tool that helps you bootstrap the Kubernetes cluster. Kubelet is the agent that runs on each node and communicates with the master node. Kubectl is the command-line tool to interact with your cluster. Installing Kubeadm, Kubelet, and Kubectl will set the foundation of our cluster.
sudo apt update
sudo apt install -y apt-transport-https ca-certificates
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 update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
Let's break down these commands:
sudo apt update: Updates the package index.sudo apt install -y apt-transport-https ca-certificates: Installs necessary packages for HTTPS.curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -: Adds the Kubernetes GPG key.echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list: Adds the Kubernetes repository.sudo apt install -y kubelet kubeadm kubectl: Installs Kubelet, Kubeadm, and Kubectl.sudo apt-mark hold kubelet kubeadm kubectl: Prevents these packages from being accidentally updated.
Step 3: Initialize the Kubernetes Cluster (Master Node)
Now, let's initialize the Kubernetes cluster on the master node. This is where Kubeadm really shines. This part is super important, so make sure you initialize the Kubernetes cluster correctly.
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
--pod-network-cidr=10.244.0.0/16: Specifies the IP address range for pods. We're using the default range for Calico, which we'll install later.
After running this command, you'll see a lot of output. Make sure to copy the kubeadm join command – you'll need it to join the worker nodes to the cluster. It will look something like this:
kubeadm join <master-ip>:<port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Next, configure kubectl to connect to the cluster:
mkdir -p $HOME/.kube
sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
These commands do the following:
mkdir -p $HOME/.kube: Creates the.kubedirectory in your home directory.sudo cp /etc/kubernetes/admin.conf $HOME/.kube/config: Copies the Kubernetes configuration file to your.kubedirectory.sudo chown $(id -u):$(id -g) $HOME/.kube/config: Changes the ownership of the configuration file to your user.
Step 4: Install a Pod Network Add-on (Calico)
Kubernetes needs a pod network add-on to enable communication between pods. Calico is a popular choice. Let’s install Calico to get our pod network up and running.
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
This command applies the Calico manifest, which sets up the Calico network policy engine.
Step 5: Join Worker Nodes to the Cluster
Now, it's time to join the worker nodes to the cluster. Remember that kubeadm join command you copied earlier? Run it on each worker node. This step focuses on joining worker nodes to the cluster for expanding our kubernetes deployment.
kubeadm join <master-ip>:<port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
If you didn't copy it, don't worry! You can regenerate it on the master node with:
sudo kubeadm token create --print-join-command
Step 6: Verify the Cluster
After joining the worker nodes, head back to the master node and verify that everything is working correctly. We're going to verify the cluster to make sure everything is running smoothly.
kubectl get nodes
You should see all your nodes listed, with their status as Ready.
kubectl get pods --all-namespaces
This command shows all the pods running in the cluster, including the Kubernetes system pods. Make sure all the core components are running and healthy.
Step 7: Deploy a Sample Application
Alright, let’s deploy a simple application to make sure everything is working as expected. We're going to deploy a sample application to test our setup.
Create a deployment file named nginx-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Apply the deployment:
kubectl apply -f nginx-deployment.yaml
Create a service to expose the deployment:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Apply the service:
kubectl apply -f nginx-service.yaml
Check the status of the deployment and service:
kubectl get deployments
kubectl get services
Once the service has an external IP (or you can access it via NodePort), you should be able to access the Nginx welcome page in your browser.
Troubleshooting
Sometimes things don’t go as planned. Here are a few common issues and how to troubleshoot them.
- Nodes Not Joining: Double-check the
kubeadm joincommand. Make sure the token and certificate hash are correct. Also, ensure that the master node is reachable from the worker nodes. - Pods Not Starting: Check the pod logs using
kubectl logs <pod-name> -n <namespace>. Look for any error messages. - Network Issues: If pods can’t communicate with each other, check your pod network add-on (Calico) configuration. Make sure it’s properly installed and configured.
Conclusion
And there you have it! You've successfully created a Kubernetes cluster on Ubuntu using Kubeadm. This is a huge step towards mastering container orchestration. Keep experimenting, keep learning, and soon you'll be a Kubernetes pro! Remember, practice makes perfect, so don't be afraid to break things and learn from your mistakes. Kubernetes is a powerful tool, and with a little effort, you'll be able to leverage it to deploy and manage your applications with ease. Happy Kuberneting, guys!
This guide walked you through creating a Kubernetes cluster, installing necessary components like Docker, Kubeadm, Kubelet, and Kubectl, and deploying a sample application. With this foundation, you can now explore more advanced Kubernetes concepts, such as deployments, services, namespaces, and more. The journey of mastering Kubernetes is a continuous one, so keep exploring and building!