Installing Kubernetes

Installing Kubernetes on Ubuntu typically involves setting up a cluster with a control plane (master) node and one or more worker nodes. The following steps outline the general process using kubeadm, kubelet, and kubectl, along with containerd as the container runtime.

1. System Preparation (on all nodes):
Update and Upgrade.
sudo apt update
sudo apt upgrade -y

or
sudo dnf upgrade


Disable Swap: Kubernetes requires swap to be disabled.
sudo swapoff -a sudo sed -i '/swap/ s/^/# /' /etc/fstab

Add Kernel Parameters: Load necessary kernel modules and configure network settings.
Create /etc/modules-load.d/containerd.conf and add the following lines
# /etc/modules-load.d/containerd.conf
overlay
br_netfilter


Run these commands
sudo modprobe overlay
sudo modprobe br_netfilter


create /etc/sysctl.d/kubernetes.conf and add the following lines
# /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1


then run this command so it all takes effect
sudo sysctl --system

Install Containerd Runtime.
These are probably already installed on RHEL9
sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates


Add Docker's official GPG key and repository
For Ubuntu
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install -y containerd.io
sudo containerd config default | sudo tee /etc/containerd/config.toml

For RHEL
sudo dnf config-manager --add-repo=https://download.docker.com/linux/rhel/docker-ce.repo
sudo yum install containerd.io
sudo containerd config default | sudo tee /etc/containerd/config.toml



Change SystemdCgroup to true in /etc/containerd/config.toml
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd


Install Kubernetes Tools (kubeadm, kubelet, kubectl):
For Ubuntu
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | 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

For RHEL
Create /etc/yum.repos.d/kubernetes.repo and add the follow code
# /etc/yum.repos.d/kubernetes.repo [kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.32/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.32/rpm/repodata/repomd.xml.key
# exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni

Then run
sudo yum install kubelet kubeadm kubectl
sudo systemctl enable --now kubelet


2. Control Plane (Master) Node Initialization:
Replace 10.244.0.0/16 your pod CIDR bloack if you use a different one (for flannel to use).
sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Set up Kubectl for the current user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config


Deploy a Pod Network Add-on: For example, Calico:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml


3. Worker Node Join:
On each worker node, run the kubeadm join command provided in the output of kubeadm init on the control plane node. It will look similar to:
sudo kubeadm join : --token --discovery-token-ca-cert-hash sha256:


4. Verification:
Check Cluster Status (from control plane).
kubectl get nodes -o wide
All nodes should eventually show as Ready.


5. Create pod.yml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-example
  labels:
    app: nginx
spec:
  containers:
    - name: nginx
      image: linuxserver/nginx
      volumeMounts:
      - mountPath: /config/www
        name: host-path-volume
      ports:
        - containerPort: 80
          name: "nginx-http"
  volumes:
  - name: host-path-volume
    hostPath:
      path: /pods/www
      type: DirectoryOrCreate


6. Apply the file with the following command:
kubectl apply -f pod.yml


7. Create service.yml
NOTE:To use port 8080 below you will need to edit /etc/kubernetes/manifests/kube-apiserver.yaml and add "- --service-node-port-range=8000-32767" then restart.
or change the port number to something like 30080

apiVersion: v1
kind: Service
metadata:
  name: nginx-example
spec:
  type: NodePort
  ports:
    - name: http
      port: 80
      nodePort: 8080
      targetPort: nginx-http
  selector:
    app: nginx


8. Deploy service.yml
kubectl apply -f service.yml


9. You can check the status of this deployment with the following command:
kubectl get pods

10. Print join command
kubeadm token create --print-join-command


HOME