Kubernetes Cluster Setup On Ubuntu 20.04: A Step-by-Step Guide
So, you're looking to dive into the world of Kubernetes, huh? Awesome! Getting a Kubernetes cluster up and running on Ubuntu 20.04 might seem daunting at first, but trust me, it's totally doable. This guide will walk you through each step, making the process as smooth as possible. We'll cover everything from installing the necessary components to deploying your first application. Let's get started!
Prerequisites
Before we jump into the setup, let's make sure you have everything you need. You'll want at least two Ubuntu 20.04 servers (or VMs). One will act as the master node, and the other(s) will be worker nodes. Here’s a quick checklist:
- Ubuntu 20.04 Servers: At least two, with one designated as the master node.
- User with sudo privileges: Make sure you can run commands with
sudo. - Internet Connection: For downloading packages.
- Basic Linux Knowledge: Familiarity with the command line is helpful.
- Unique Hostnames and Static IPs: Each node should have a unique hostname and a static IP address.
Having these prerequisites in place will ensure that you have a smooth journey setting up your Kubernetes cluster. Trust me, skipping these steps can lead to a lot of headaches later on, so double-check everything!
Step 1: Install Container Runtime (Docker)
Okay, first things first, let's install Docker. Kubernetes needs a container runtime to run your applications, and Docker is a popular choice. Here’s how you do it:
-
Update Package Index:
sudo apt updateKeeping your package index up-to-date ensures you get the latest versions of software. It's like cleaning your room before starting a new project – good practice!
-
Install Docker:
sudo apt install docker.io -yThis command installs Docker from the Ubuntu repositories. The
-yflag automatically confirms the installation, saving you a keystroke. Efficiency is key, right? -
Start and Enable Docker:
sudo systemctl start docker sudo systemctl enable dockerThese commands start the Docker service and ensure it starts automatically on boot. This is super important because you want Docker running all the time.
-
Verify Docker Installation:
docker --versionRunning this command will display the Docker version, confirming that Docker is installed correctly. If you see the version number, you're golden!
Repeat these steps on all your nodes – master and worker nodes alike. You need Docker everywhere for Kubernetes to work its magic.
Step 2: Install Kubernetes Components (kubeadm, kubelet, kubectl)
Alright, now that Docker is up and running, let's install the Kubernetes components. We're talking about kubeadm, kubelet, and kubectl. Here’s the lowdown:
-
Update Package Index:
sudo apt updateYep, we're doing this again. Always a good idea to start with a fresh package index.
-
Install Required Packages:
sudo apt install apt-transport-https curl -yThese packages are needed to add the Kubernetes repository.
apt-transport-httpsallowsaptto access repositories over HTTPS, andcurlis a tool for transferring data with URLs. -
Add Kubernetes Repository:
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.listThis adds the Kubernetes repository to your system. The first command downloads the Kubernetes signing key and adds it to your system's trusted keys. The second command adds the Kubernetes repository to your APT sources.
-
Update Package Index (Again!):
sudo apt updateNow that we've added the Kubernetes repository, we need to update the package index again to include the new packages.
-
Install kubelet, kubeadm, and kubectl:
sudo apt install kubelet kubeadm kubectl -y sudo apt-mark hold kubelet kubeadm kubectlThis installs the Kubernetes components.
kubeletis the agent that runs on each node,kubeadmis a tool for bootstrapping Kubernetes clusters, andkubectlis the command-line tool for interacting with the cluster. Theapt-mark holdcommand prevents these packages from being automatically updated, which can cause issues with your cluster.
Repeat these steps on all your nodes. Consistency is key!
Step 3: Initialize the Kubernetes Master Node
Now comes the exciting part – initializing the Kubernetes master node! This is where the magic happens. Here’s how to do it:
-
Initialize the Master Node:
sudo kubeadm init --pod-network-cidr=10.244.0.0/16This command initializes the Kubernetes master node. The
--pod-network-cidrflag specifies the network range for pods. This example uses10.244.0.0/16, which is the default for Calico (a network plugin we’ll install later). -
Configure kubectl:
After the
kubeadm initcommand completes, it will output some instructions. Follow those instructions to configurekubectlfor your user:mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/configThese commands copy the Kubernetes configuration file to your user's home directory and set the correct permissions. This allows you to use
kubectlto interact with your cluster. -
Deploy a Pod Network:
Kubernetes needs a pod network to allow pods to communicate with each other. We'll use Calico, a popular and easy-to-use network plugin:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yamlThis command deploys Calico to your cluster. It might take a few minutes for all the pods to become ready.
-
Verify the Master Node:
kubectl get nodesThis command will show you the status of your nodes. The master node should be in the
Readystate. If it’s not, give it a few minutes and try again.
Step 4: Join Worker Nodes to the Cluster
With the master node up and running, it's time to add the worker nodes. This is how you make your cluster truly useful. Here’s the process:
-
Get the Join Command:
When you initialized the master node,
kubeadm initoutput akubeadm joincommand. It looks something like this:kubeadm join <master-ip>:<master-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>If you don't have this command, you can regenerate it on the master node:
kubeadm token create --print-join-command -
Run the Join Command on Worker Nodes:
Copy the
kubeadm joincommand and run it on each of your worker nodes. This command tells the worker nodes to join the cluster managed by the master node. -
Verify the Worker Nodes:
Back on the master node, run:
kubectl get nodesYou should now see your worker nodes listed, along with the master node. They should all be in the
Readystate.
Step 5: Deploy a Sample Application
Now that your cluster is up and running, let’s deploy a sample application to make sure everything is working as expected. We'll deploy a simple Nginx deployment.
-
Create a Deployment:
kubectl create deployment nginx --image=nginxThis command creates a deployment named
nginxusing thenginximage from Docker Hub. -
Expose the Deployment:
kubectl expose deployment nginx --port=80 --type=NodePortThis command exposes the
nginxdeployment on port 80 using aNodePortservice.NodePortmakes the service accessible on each node's IP address at a specific port. -
Get the Service Information:
kubectl get service nginxThis command will show you the service information, including the
NodePort. It will look something like30000-32767. -
Access the Application:
Open a web browser and navigate to
http://<node-ip>:<node-port>, where<node-ip>is the IP address of one of your nodes and<node-port>is theNodePortyou obtained in the previous step. You should see the default Nginx welcome page.
Conclusion
And there you have it! You've successfully set up a Kubernetes cluster on Ubuntu 20.04. You've installed Docker, configured the Kubernetes components, initialized the master node, joined the worker nodes, and deployed a sample application. Give yourself a pat on the back!
This is just the beginning, though. Kubernetes is a powerful platform with a lot to offer. Keep exploring, keep learning, and keep building! There's a whole world of Kubernetes features waiting for you to discover, from advanced networking to persistent storage and beyond. So, what are you waiting for? Go explore and have fun!