Kubernetes On Ubuntu 18.04: A Step-by-Step Installation Guide

by Team 62 views
Kubernetes Installation on Ubuntu 18.04: A Step-by-Step Guide

So, you're ready to dive into the world of Kubernetes, huh? Awesome! Kubernetes, often abbreviated as K8s, is this amazing open-source system for automating deployment, scaling, and management of containerized applications. And Ubuntu 18.04? Well, it's a solid, reliable Linux distribution that makes a fantastic foundation for your Kubernetes cluster. This guide will walk you through setting up a Kubernetes cluster on Ubuntu 18.04 step by step. Let's get started, shall we?

Prerequisites

Before we even begin, there are a few things you need to have in place. Think of it as gathering your ingredients before you start baking a cake. You wouldn't want to realize you're out of flour halfway through, right?

  • Ubuntu 18.04 Servers: You'll need at least two Ubuntu 18.04 servers. One will act as your master node (the brains of the operation), and the other will be a worker node (doing the actual work). You can have more worker nodes, of course, and that's often the case in real-world deployments.
  • Root or sudo Privileges: You'll need root or sudo privileges on both servers to install and configure Kubernetes.
  • Internet Connection: This one's a no-brainer. You'll need an internet connection to download the necessary packages.
  • Basic Linux Knowledge: A little familiarity with the Linux command line will go a long way. You don't need to be a guru, but knowing how to use basic commands like apt, systemctl, and nano will be super helpful.
  • Unique Hostnames & Static IP Addresses: Ensure each of your servers has a unique hostname and a static IP address. This prevents network conflicts and ensures reliable communication between nodes.

Why Ubuntu 18.04?

You might be wondering, "Why Ubuntu 18.04 specifically?" Well, Ubuntu is known for its stability, ease of use, and extensive community support. Ubuntu 18.04, in particular, is a Long Term Support (LTS) release, which means it receives security updates and support for five years. This makes it a reliable and secure choice for running your Kubernetes cluster. Plus, there are tons of tutorials and documentation available online specifically for Ubuntu, making it easier to troubleshoot any issues you might encounter.

Step 1: Update and Upgrade Your Servers

First things first, let's make sure our servers are up to date. This is always a good practice before installing any new software. Log in to each of your Ubuntu servers (both master and worker nodes) and run the following commands:

sudo apt update
sudo apt upgrade -y

The apt update command refreshes the package lists, while apt upgrade upgrades all installed packages to their latest versions. The -y flag automatically answers "yes" to any prompts, so you don't have to sit there and type "y" a million times. This ensures you're starting with a clean and updated system, minimizing the chances of encountering compatibility issues later on.

Step 2: Install Docker

Kubernetes uses Docker to run containerized applications. So, we need to install Docker on all our servers. Here's how:

sudo apt install docker.io -y

After the installation is complete, start and enable the Docker service:

sudo systemctl start docker
sudo systemctl enable docker

The systemctl start docker command starts the Docker service, and systemctl enable docker ensures that Docker starts automatically on boot. Verify Docker installation by running docker --version. This should output the installed Docker version. Docker is the backbone for running containers, so make sure this step is successful. Without Docker, Kubernetes wouldn't be able to manage and run your applications.

Step 3: Configure Docker

Next, let's configure Docker to use the systemd cgroup driver. This is important for Kubernetes to properly manage the resources used by Docker containers. Edit the /etc/docker/daemon.json file (create it if it doesn't exist):

sudo nano /etc/docker/daemon.json

Add the following content to the file:

{
  "exec-opts": ["native.cgroupdriver=systemd"]
}

Save the file and restart Docker:

sudo systemctl restart docker

Now, verify the configuration by running docker info | grep Cgroup Driver. The output should show Cgroup Driver: systemd. This ensures that Docker is using the correct cgroup driver, which is crucial for Kubernetes compatibility and resource management. Failing to configure this properly can lead to unpredictable behavior and resource contention within your Kubernetes cluster.

Step 4: Install kubeadm, kubelet, and kubectl

Now comes the fun part: installing the Kubernetes components! We'll be using kubeadm to bootstrap the cluster, kubelet to run containers on each node, and kubectl to manage the cluster from the command line. Run these commands on all servers:

sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl
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:

  • The first two lines update the package lists and install some necessary dependencies.
  • The curl command downloads the Kubernetes signing key and adds it to your system. This allows you to verify the authenticity of the Kubernetes packages.
  • The echo command adds the Kubernetes repository to your system's package sources.
  • The apt install command installs kubelet, kubeadm, and kubectl.
  • The apt-mark hold command prevents these packages from being automatically updated. This is important because Kubernetes updates can sometimes break compatibility with your cluster. Holding the packages ensures that you have control over when and how they are updated.

Step 5: Initialize the Kubernetes Master Node

Now it's time to initialize the Kubernetes master node. Run the following command only on the master node:

sudo kubeadm init --pod-network-cidr=10.244.0.0/16

The --pod-network-cidr flag specifies the IP address range that will be used for pods (the smallest deployable units in Kubernetes). The value 10.244.0.0/16 is a common choice, but you can use a different range if you prefer. Take note of the kubeadm join command displayed at the end of the initialization process. It will look something like this:

kubeadm join <master-ip>:<master-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

You'll need this command to join the worker nodes to the cluster. Also, to use kubectl as a non-root user, 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

These commands copy the Kubernetes configuration file to your home directory and set the correct permissions. This allows you to use kubectl without having to use sudo every time. Confirm initialization success by running kubectl get nodes. The master node should be in the Ready state.

Step 6: Deploy a Pod Network

Kubernetes requires a pod network to allow pods to communicate with each other. We'll be using Calico, a popular and easy-to-configure pod network. Run the following command only on the master node:

kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml

This command deploys Calico to your Kubernetes cluster. It may take a few minutes for Calico to become fully operational. Verify Calico installation by running kubectl get pods -n kube-system. You should see several Calico pods in the Running state. A pod network is essential for Kubernetes to function correctly, so make sure this step is successful. Without a pod network, pods won't be able to communicate, and your applications won't work.

Step 7: Join Worker Nodes to the Cluster

Now it's time to join the worker nodes to the cluster. Run the kubeadm join command that you noted down in Step 5 on each worker node. It should look something like this:

kubeadm join <master-ip>:<master-port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>

Replace <master-ip>, <master-port>, <token>, and <hash> with the actual values from your kubeadm join command. After running the command, the worker node will join the cluster. Verify worker node joining by running kubectl get nodes on the master node. You should see all your worker nodes in the Ready state. Joining worker nodes is crucial for expanding your cluster's capacity and distributing the workload. The more worker nodes you have, the more resources are available to run your applications.

Step 8: Verify the Cluster

Finally, let's verify that our Kubernetes cluster is working correctly. Run the following command on the master node:

kubectl get nodes
kubectl get pods --all-namespaces

The kubectl get nodes command should show all your nodes (master and worker nodes) in the Ready state. The kubectl get pods --all-namespaces command should show all the pods in your cluster, including the system pods in the kube-system namespace. All pods should be in the Running or Completed state. If everything looks good, congratulations! You've successfully installed a Kubernetes cluster on Ubuntu 18.04. This is the final step, confirming all previous configurations were successfully made.

Conclusion

And there you have it! You've successfully set up a Kubernetes cluster on Ubuntu 18.04. This is just the beginning, though. Now you can start deploying your own applications to the cluster and exploring the many features that Kubernetes has to offer. Remember to consult the official Kubernetes documentation for more information and advanced configurations. Happy Kuberneting!