Install Kubernetes on Ubuntu

Tiempo de lectura: 2 minutos

Let’s install Kubernetes on an Ubuntu environment (https://kubernetes.io/docs/tasks/tools/included/install-kubectl-linux/)

Install Docker

The first step is to install Docker in our environment:

Install Docker Compose on Ubuntu following the official Docker website instructions

Update dependencies and install necessary packages

sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg  sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable"  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Now install the latest version of Docker

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Run Docker

sudo docker run hello-world

You can configure it so that you don’t need to use the sudo command to run Docker (https://docs.docker.com/engine/install/linux-postinstall/):

sudo groupadd docker
sudo usermod -aG docker $USER
newgrp docker

Finally, Docker will run without the need for the sudo command

docker run hello-world

Using Docker Compose:

To execute Docker Compose, we will use:

docker compose <command>

Installing Kubernetes and Necessary Environment:

You can utilize tools like kubeadm, kubelet, and kubectl to configure and manage a Kubernetes cluster.

Disabling Swap:

Kubernetes requires swap to be disabled.

To disable swap in Linux, you typically need to comment out or remove the corresponding line in the /etc/fstab file. Here’s an example of what the typical swap line looks like in the /etc/fstab file:

UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx none swap sw 0 0

To disable swap, you simply need to comment out this line by adding a # at the beginning:

# UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx none swap sw 0 0

After making this change, save the file and restart the system or manually disable swap with the following command:

sudo swapoff -a

Note that these instructions may vary depending on the specific Linux distribution you are using. Additionally, it’s crucial to ensure that you are not disabling swap in an environment where it is essential for system performance.

Step 3: Install Docker
Installing Kubernetes:

Install kubeadm, kubelet, and kubectl with the following command:

sudo snap install kubeadm --classic
sudo snap install kubectl --classic
sudo snap install kubelet --classic

Initializing the Cluster with kubeadm:

Initiate the Kubernetes cluster using kubeadm. This command configures the current node as the master node:

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

Configuring Kubernetes Environment:

Follow the instructions provided by kubeadm upon finishing initialization. These instructions will include a command similar to this to configure kubectl:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Installing a Network Plugin (Optional):

To enable communication between pods in your cluster, you can install a network plugin. Calico is a common choice:

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

Final Steps:

If all goes well, your Kubernetes cluster should be up and running. You can check the status of nodes with:

kubectl get nodes

Now you can start deploying and managing applications in your Kubernetes cluster.

Remember, this is a basic guide and there may be changes depending on the specific version of Kubernetes or Ubuntu you are using. Refer to the official Kubernetes documentation for more detailed and updated information.

Leave a Comment