Build a K8s cluster on Ubuntu 22.04 2023

By XiaoXin
A Bit Randomly

Script kiddies are a type of hacker who lacks the technical skills and knowledge of more advanced hackers, but use pre-existing tools and scripts to perform simple hacks. They may download and use tools and scripts created... Read Define Script Kiddies

Main Contents

Build a K8s cluster on Ubuntu 22.04

Table of contents

  • 1. Virtual machine basic configuration
    • configure static IP
    • set hostname
    • set hosts
    • install ssh
  • 2. Ubuntu system settings
    • disable swap
    • Modify kernel parameters
  • 3. Install contained
  • 4. Install Kubernetes components
    • add apt repo
    • Install Kubectl, kubeadm & kubelet
  • 5. Initialize the Master node
  • 6. Join the Node node
  • 7. Configure the cluster network
  • 8. Test the K8s cluster

This article is based on the Mac platform and Parallels software, in which three Ubuntu systems are created, and a K8s cluster with 3 nodes (1 master and 2 Nodes) is built. The following steps have no special instructions and must be executed on all nodes separately. It is also possible to copy the current virtual machine as another node after execution on a virtual machine.

1. Virtual machine basic configuration

Based on the Parallels virtual machine software, install three ubuntu systems in it, and then use it to create the master node and two Node nodes of the K8s cluster. The three nodes all use static IP. The specific configuration is as follows:

  • Master node: master

  • Node1 node: node1

  • Node2 node: node2

configure static IP 

It needs to be configured separately on the three nodes. The following takes the master as an example:

Modify the virtual machine network to bridge mode:

set hostname

Run on the Master node

sudo hostnamectl set-hostname master

The other nodes run separately

sudo hostnamectl set-hostname node1
sudo hostnamectl set-hostname node2

Set hosts

To facilitate mutual access between nodes, in each node /etc/hostsAdd the following configuration to it: master node1 node2

After configuration, try to ping master and node1 to see if they can be accessed normally.

install ssh

Install ssh to facilitate subsequent connection to the virtual machine to execute cmd.

sudo apt update
sudo apt install openssh-server
sudo systemctl status ssh
sudo ufw allow ssh

In order to facilitate entering commands in multiple terminals at the same time, you can use item to open three windows at the same time, and then enter cmd + shift + IEnter the mode of multiple inputs, if you need to exit, you can enter ⌘Command + ⇧Shift + ⌥Option + I

2. Ubuntu system settings disable swap

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

After this step, it is best to restart the ubuntu system to ensure that the changes take effect.

Modify kernel parameters 

Load the following kernel modules,

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

sudo modprobe overlay
sudo modprobe br_netfilter

Related information reference:

  • Analysis of Linux overlay file system

  • Linux transparent firewall --br_netfilter

Configure the following network parameters:

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

Run the following command for the changes to take effect:

sudo sysctl --system

3. Install contained

When tools such as Docker and Kubernetes run a container, they will call components (CRI) such as containers and CRI-O to complete the actual work of creating, running, and destroying the container. Docker uses containers as its runtime; Kubernetes supports contained, CRI-O, etc. These components follow the OCI specification and use runc to interact with the operating system kernel to complete the creation and operation of containers.

The interrelationship between them is as follows:

The specific installation steps are as follows:

  • Install dependencies:
sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates
  • Add docker repo:
sudo curl -fsSL | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
sudo add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
  • Install contained:
sudo apt update
sudo apt install -y
  • Configure container to use systemd as cgroup
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
  • Reboot and set autostart
sudo systemctl restart containerd
sudo systemctl enable containerd

4. Install Kubernetes components add apt repo

curl -s | sudo apt-key add -
sudo apt-add-repository "deb kubernetes-xenial main"

Install Kubectl, kubeadm & kubelet
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl

apt-mark is used to mark/unmark packages for automatic installation. The hold option is used to mark a package as on hold to prevent it from being automatically installed, upgraded or removed. The main purpose here is to prevent automatic upgrade of components such as kubelet.

5. Initialize the Master node

This step needs to be set on the Master node. Run the following node to initialize the entire k8s cluster.

sudo kubeadm init --control-plane-endpoint=

When you see the following output, it means that the initialization of the system master node is complete.

Then follow the prompt information to carry out subsequent initialization work:

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

After completing the above work, you can try to run the following command to view the current status of the cluster:

kubectl cluster-info
kubectl get nodes

Input similar to the following

6. Join the Node node

At the bottom of the initialization output of the master node, the command added by the node will be listed, just copy it directly and then run it on each node. The following is an example, which has to be replaced with an actual command,

sudo kubeadm join --token mjy0xx.95lsse7r7fw5sb00 --discovery-token-ca-cert-hash sha256:73b92db9baf19a3e14d679e6d44b5c7a804902d6ffa3d170858d2ccfd5e0c93f

After successfully joining, you can view the node status of the current cluster. At this time, because no network-related plug-ins are installed, all nodes are in the NotReady state. The next step will be the installation.

kubectl get nodes

7. Configure the cluster network

Generally, plug-ins such as Calico, Flannel, and Weave-net can be installed. The following uses Calico as an example of operate.

curl -O
kubectl apply -f calico.yaml

The output is rough as follows:

At this point, you can view the running status of all basic components under the system namespace.

kubectl get pods -n kube-system

Next check the node status:

kubectl get nodes

So far, the entire k8s cluster has been built, and some basic tests can be performed below.

8. Test the K8s cluster

An Nginx app is deployed here for testing,

kubectl create deployment nginx-app --image=nginx --replicas=2

View the status of Nginx:

kubectl get deployment nginx-app

Expose the deployment, using the NodePort method (this method will open the same port on each node, and the outside can be accessed through the node ip+port method)

kubectl expose deployment nginx-app --type=NodePort --port=80

You can check the status of the service,

kubectl get svc nginx-app
kubectl describe svc nginx-app

Here are the test results:

It shows that Nginx is running normally, and the entire k8s node is deployed successfully.

Please Share This Article Thank You!

How To Improve Website Performance In PHP

To improve the performance of a website built with PHP, you can try a few different strategies. First, you should optimize your code to ensure that it is efficient and well-structured. This can help to reduce the amount of...

Windi CSS - The Rising Star of CSS Framework

What is Windi CSS? Windi CSS is a tool that helps you handle CSS styles easily and quickly. In this article, you will learn about the concept of Windi CSS, the features of Windi CSS, and the most important part - how to us...