How to Install Kubernetes on Ubuntu: A Beginner's Guide

5 feb 2024 3 min di lettura
How to Install Kubernetes on Ubuntu: A Beginner's Guide
Indice dei contenuti

Introduction

Setting up Kubernetes on Ubuntu is a journey that transforms your infrastructure into a powerful container orchestration center, allowing you to deploy, scale, and manage application containers across a cluster of machines. Kubernetes, also known as K8, automates the deployment, sizing, and operation of application containers, making it easier for developers and system administrators to manage applications.

This article provides a complete guide to setting up Kubernetes on Ubuntu, covering everything from installation to operation. Whether you're setting up a single-node cluster for development or a multi-node cluster for production, this guide will walk you through the steps needed to get your Kubernetes cluster up and running.

Prerequisites

Before you begin, make sure you have:

  • One or more machines running Ubuntu 18.04 LTS or later, each with at least 2 GB of RAM and 2 CPUs.
  • A user account with sudo privileges on each machine.
  • The machines are networked together, able to communicate with each other.

Step 1: Prepare the environment

Update your system: Start by updating your system to ensure all packages are up to date.

sudo apt-get update
sudo apt-get upgrade -y

Install Docker: Kubernetes uses Docker as its container runtime. Install Docker on each machine.

sudo apt-get install docker.io -y

After installation, add your user to the Docker group to run Docker commands without sudo.

sudo usermod -aG docker $USER

Disable swapping: Kubernetes requires you to disable swapping on each machine.

sudo swapoff -a

To make this change permanent, comment out any swap entries in /etc/fstab.

Step 2: Install the Kubernetes components

Add Kubernetes Repository - Add the Kubernetes package repository to each machine.

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb http://apt.kubernetes.io/kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

Install Kubernetes - Install kubeadm, kubelet, and kubectl on each machine.

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl

Lock their versions to prevent automatic updates.

sudo apt-mark hold kubelet kubeadm kubectl

Step 3: Initialize the cluster

On the master node, initialize the cluster using kubeadm.

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

After initialization, follow the on-screen instructions to start using the cluster. This typically involves running the following commands as a normal user.

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

Step 4: Set up a Pod network

Install a pod networking add-on on your cluster so your pods can communicate with each other. Calico is a popular choice that can be installed with:

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

Step 5: Join the nodes to the cluster

To add worker nodes to your cluster, use the kubeadm join command provided at the end of the master initialization process. Run this command on each worker node.

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

Step 6: Deploy your applications

With your cluster up and running, you are ready to deploy applications. You can start by deploying a simple application using a deployment YAML file.

apiVersion: apps/v1
 kind: Deployment
 metadata:
 name: nginx-deployment
 spec:
 replicas: 3
 selector:
 matchLabels:
 app: nginx
 template:
 metadata:
 labels:
 app: nginx
 spec:
 containers:
 - name: nginx
 image: nginx:latest
 ports:
 - containerPort: 80

Apply this distribution using: kubectl apply -f <filename>.yaml.

Step 7: Scale and manage your cluster

Scale your applications by adjusting the number of replicas in your deployment and manage your cluster resources using kubectl, the Kubernetes command-line tool. Explore more complex deployments, configure Kubernetes Dashboard for a GUI, and consider installing Helm for package management.

Conclusion

Congratulations! You have successfully configured a Kubernetes cluster on Ubuntu. This cluster is now ready for application deployment. Remember, Kubernetes is a powerful tool, and there is much more to learn about its features and capabilities. Experiment with deploying different applications, scaling them, and exploring the many features of Kubernetes to become comfortable managing your containerized environments.

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.