Skip to content

Install Kubernetes k8s | Ubuntu 22.04

Minimum requirements:

  • Master/s Linux Ubuntu 22.04 2 CPU 2 G RAM
  • Worker/s Linux Ubuntu 22.04 2 CPU 2 G RAM

Setup hostname on master node

Bash
sudo hostnamectl set-hostname k8smaster.kafana.dev
Setup hostname on worker nodes

Bash
sudo hostnamectl set-hostname k8sworker1.kafana.dev
sudo hostnamectl set-hostname k8sworker2.kafana.dev
nano /etc/hosts file on each node (worker and master node)

Text Only
ip_addr_here   k8smaster.kafana.dev k8smaster
ip_addr_here   k8sworker1.kafana.dev k8sworker1
ip_addr_here   k8sworker2.kafana.dev k8sworker2

Install Script

Bash
#!/bin/bash

# Disable swap
echo "removing swap"
echo "remove swap file manualy"
sleep 1

sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

# Load kernel modules on all nodes


sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF

sudo modprobe overlay

sudo modprobe br_netfilter

# Kubernetes kernel paramiters

sudo tee /etc/sysctl.d/kubernetes.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sudo sysctl --system

# Install containerd run time

sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates

# Docker Repo

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

sudo apt update
sudo apt install -y containerd.io

containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml

sudo systemctl restart containerd
sudo systemctl enable containerd

# Kuberenetes Repo

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"

# Install Kubernetes components Kubectl, kubeadm & kubelet

sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sleep 1
sudo apt-mark hold kubelet kubeadm kubectl

echo "done"
echo ""
echo "run to enable control plane"
echo ""
echo "sudo kubeadm init --control-plane-endpoint=k8smaster.example.net"

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

Bash
kubectl cluster-info
Bash
kubectl get nodes

  • Place manifests all in one place (Optional)

Create workspace / folder for kubernetes (good idea is to put it on a git)

Bash
sudo mkdir /kubernetes && sudo chown $USER:$USER /kubernetes && cd /kubernetes
  • Install Calico Pod Network Add-on

Bash
curl https://projectcalico.docs.tigera.io/manifests/calico.yaml -O
Bash
kubectl apply -f calico.yaml
Check with command:

Bash
kubectl get pods -n kube-system
Bash
kubectl get nodes

  • Install MetalLB - Loadbalancer for self hosted kubernetes cluster
Bash
curl https://raw.githubusercontent.com/metallb/metallb/v0.13.7/config/manifests/metallb-native.yaml -O
Bash
kubectl apply -f metallb-native.yaml
  • Metallb config

Create metallb-config.yaml

Bash
nano metallb-config.yaml
YAML
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
  name: first-pool
  namespace: metallb-system
spec:
# your VM IP range or whatever if your willing to use ssh tunneling from you proxy server (sshuttle does the trick)
  addresses:
  - 192.168.1.100-192.168.1.250 
Bash
kubectl apply -f matallb-config.yaml
  • MetalLB (Load Balancer fot self hosted k8s )

Check it out


  • You can test your kubernetes cluster with this simple nginx manifest

Create nginx.yaml

Bash
nano nginx.yaml
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
        ports:
        - containerPort: 80

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx
    svc: nginx
spec:
  type: LoadBalancer
  loadBalancerIP: 192.168.1.150
#  type: NodePort
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
Bash
kubectl apply -f nginx.yaml

Enjoy!

Back to top