Tuesday, July 30, 2024

Ubuntu: Setup Kubernetes cluster

To install and configure a Kubernetes cluster on Ubuntu Linux, follow these steps:

1. Preparing hosts for the Kubernetes cluster.

1.1 Set the hostname of our new Ubuntu OS:
sudo hostnamectl set-hostname "k8nodename1.mydomain.local"

1.2 Next, edit the “hosts” file:
sudo nano /etc/hosts
Add all nodes of the future Kubernetes cluster:
192.168.0.11 k8nodename1.mydomain.local k8nodename1
192.168.0.12 k8nodename2.mydomain.local k8nodename2
192.168.0.13 k8nodename3.mydomain.local k8nodename3
192.168.0.14 k8nodename4.mydomain.local k8nodename4
192.168.0.15 k8nodename5.mydomain.local k8nodename5


1.3 Disable SWAP:
sudo swapoff -a
sudo sed -ri 's/.*swap.*/#&/' /etc/fstab


1.4 Add Linux kernel parameters:
sudo tee /etc/modules-load.d/k8s.conf <<EOF
overlay
br_netfilter
EOF

Run:
sudo modprobe overlay
Run:
sudo modprobe br_netfilter
Check:
lsmod | egrep "br_netfilter|overlay"

1.5 Configuring sysctl parameters:
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

Apply without rebooting:
sudo sysctl --system
Check that the parameters have been changed:
sysctl net.ipv4.ip_forward
sysctl net.bridge.bridge-nf-call-ip6tables
sysctl net.bridge.bridge-nf-call-iptables


2. Setup and configuring Docker and containerd.

2.1 Add official  GPG-key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc


2.2 Add the repository to "apt" sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null


2.3 Install Docker:
sudo apt-get update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin \
docker-compose-plugin

2.4 Check Docker and run test container:
sudo docker run hello-world

2.5 Setup "containerd":
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


3. Setup and configuring Kubernetes.

3.1 Setup utilities:
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl gpg


3.2 It the directory "/etc/apt/keyrings" doesn't exist, it should be created.
sudo mkdir -p -m 755 /etc/apt/keyrings

3.3 Add official GPG-key and install Kubernetes (v1.30):
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key |\
sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Run:
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] \
https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
Install:
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl


3.4 Protect executable files from updates:
sudo apt-mark hold kubelet kubeadm kubectl

4. Setup Kubernetes cluster.

4.1 Initialize the cluster on the master node by specifying a specific CIDR:
sudo kubeadm init --pod-network-cidr=10.96.0.0/16 --v=5
If something went wrong and you need to restart the entire process, reset the cluster settings with the command:
sudo kubeadm reset

4.2 After successfully creating a cluster, for the utilities to work correctly under the user, you need to run the commands:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

If you are doing everything under "root", then do:
export KUBECONFIG=/etc/kubernetes/admin.conf

4.3 To add another nodes to the Kubernetes cluster, run command below to get needed line for adding:
sudo kubeadm token create --print-join-command

4.4 Add the remaining nodes to the cluster using the command received in the previous step.

4.5 Check the cluster settings and the correct operation of the commands:
kubectl get pods -n kube-system
kubectl get nodes
kubectl get all -A
kubectl cluster-info
You can view the logs in real time using the command:
tail -f /var/log/syslog

4.6 If you want to run application containers on the master node, run the following command:
sudo kubectl taint nodes --all node-role.kubernetes.io/control-plane-

5. Setup network plugin Calico.

5.1 Install plugin (v3.28.0):
kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.28.0/manifests/calico.yaml

5.2 Check containers "calico-" running every node of the cluster:
kubectl get pods -n kube-system

6. Test run of the application on the Kubernetes cluster.

6.1 Create deployment "nginx":
kubectl create deployment nginx-app --image=nginx --replicas=2

6.2 Check running containers "nginx-app":
kubectl get deployment nginx-app

6.3 Publish the container port to the node’s ip address:
kubectl expose deployment nginx-app --type=NodePort --port=80

6.4 Check published port of the application "nginx-app":
kubectl get svc nginx-app
Or:
kubectl describe svc nginx-app

6.5 Using the utility "curl", check the availability of the service from the outside (where 30718 is the port of the node with the published "nginx-app"):
curl http://node_ip_address:30718
If we receive a response from the server in the format "Welcome to nginx!", then the check was completed successfully.

No comments:

Post a Comment