The version and download address of the installed software
Environmental description
server preparation
Load balancing IP address preparation
installation steps
environment settings
turn off firewall
close SELinux
Close the swap partition
Set up hostname resolution
set hostname
Load the br_netfilter module
Bridged IPv4 traffic passed to iptables
Upgrade the operating system kernel
import elrepo gpg key
Install elrepo YUM source repository
Install kernel-lt version
Set grub2 default boot to 0
Regenerate the grub2 boot file
reboot
Install ipset and ipvsadm
Configure ipvsadm module loading method
Install containerd
Download containerd
Unzip containerd
Generate containerd configuration file
Start containerd and set it to start automatically
install runc
Download libseccomp
Install libseccomp
download runc
install runc
install kubernetes
Configure the yum source of kubernetes
install kubernetes
Configure cgroup driver to systemd
Build a kubernetes cluster
Initialize the cluster
Set kubelet to start automatically at boot
Install the Calico network plugin
Install MetalLB load balancer
Modify the configuration file of kube-proxy
Install MetalLB
Assign an IP address to MetalLB
deploy application
deploy nginx
Expose to external network access
Video tutorial address: https://space.bilibili.com/3461573834180825/channel/seriesdetail?sid=3316691
Document description
I wrote an article on installing kubernetes based on docker before. In this document, we use containerd to install kubernetes. Compared with docker, containerd is more efficient when running containers and is compatible with docker images. The article address of installing kubernetes based on docker: https://blog.csdn.net/m0_51510236/article/details/123477488
Before installing kubernetes, you need to prepare virtual machines or physical machines with the following configurations for installation (at least three):
CPU: 2 cores
Memory: 2GB
Hard disk: 50GB
The above is the minimum configuration, you can improve the configuration according to your own hardware conditions.
Version and download address of the installed software
The corresponding software version and download address are as follows:
Software Name (Software Description)
Version
Download Address
CentOS (operating system)
7-2207-02
Aliyuan/Tsinghuayuan
kubernetes (Container orchestration tool)
1.26.5
Provided in the document
containerd (container service)
1.6.21
Click to download
libseccomp (calculation plug-in)
2.5.4
Click Download
runc (container runtime)
1.1.7
Click to download
Calico (network plug-in)
3.25
Provided in the document
Metallb (load balancing plug-in)
v0.13.9
Provided in the document
Some software is downloaded from foreign servers, if the download is too slow, you can private message me to get it
Environment description
Server preparation
server list:
Server Name
IP Address
Configuration
Server Purpose
k8s-master
192.168.79.50
2c2g50g
kubernetes master node
k8s-node01
192.168.79.52
2c2g50g
kubernetes working node 1
k8s-node02
192.168.79.54
2c2g50g
kubernetes working node 2
< /table>
All three servers are ready:
You also need to ensure that these servers can ping each other
Load balancing IP address preparation
Metallb’s loadbalancer needs some reserved IP addresses for load balancing IP address allocation. The IP addresses reserved in this article are 192.168.79.60~192.168.79.69
Installation steps
Environment settings
Next, we need to perform some operations on the three servers at the same time, so you can click Tool (T) on the toolbar in Xshell -> Send key input to (K) -> Linked session (C) to ensure that a command can be executed in three terminals. Different ssh tools have different settings, please check by yourself.
There are a lot of steps, remember not to miss it
Turn off the firewall
Turn off the firewall with the following command:
systemctl stop firewalld
systemctl disable firewalld
systemctl status firewalld
Send it to three terminals at the same time. Check the execution results and you can see that the firewalls are all closed:
Turn off SELinux
Turn off SELinux with the following command:
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
Close the swap partition
Close the swap partition with the following command:
# close permanently
swapLine=$(cat /etc/fstab | grep swap | awk '{print $1}')
sed -i "s/$swapLine/#$swapLine/" /etc/fstab
# temporarily close
swapoff -a
After success, use the free -h command to see that the size of the swap partition has changed to 0
Set host name resolution
Use the following command to set the host name resolution, but you need to pay attention to change it to correspond to your own IP address:
This setting needs to be set individually per host, so you can temporarily turn off outputting commands to all sessions by clicking the OFF button in the upper right corner of the terminal:
After setting, remember to click the ON button in the upper right corner of each terminal to open and output commands to all terminals at the same time:
Load the br_netfilter module
Since the br_netfilter module needs to be loaded to enable kernel ipv4 forwarding, this module is loaded, but this module will not be loaded by default, so we need to set this module to automatically load after booting
# Set the module to be automatically loaded at startup
cat >> /etc/rc.d/rc.local << EOF
/usr/sbin/modprobe br_netfilter
EOF
chmod +x /etc/rc.d/rc.local
# load immediately
modprobe br_netfilter
Because the version installed this time is 1.26.5, which is the second newest version, the installation may fail due to a lower kernel version, so we perform this step to upgrade the kernel to the latest stable version
After all the above commands are executed, use the reboot command to restart the computer to make the upgraded kernel take effect
After restarting, use uname -r to view the system kernel version. Different upgrade times may lead to different stable kernel versions:
Install ipset and ipvsadm
When there are many requests, the response may be too slow. Installing these two software can improve the forwarding speed of ipvs to a certain extent.
The configuration file (/etc/containerd/config.toml) needs to change the version number and mirror address of sandbox_image, because the default mirror address is on Google , Google cannot access domestically
In the same way, because github downloads are slow, I downloaded it in advance and uploaded it to the server:
Install runc
# Delete the runc that comes with containerd
rm -rf /usr/local/sbin/runc
# Give execution permission to our own runc
chmod +x runc.amd64
# Copy runc to the installation directory
mv runc.amd64 /usr/local/sbin/runc
Execute runc again and find that no error is reported:
Install kubernetes
Configure the yum source of kubernetes
The yum source of Alibaba Cloud needs to be configured, because the default yum source of the official document is in Google, which cannot be accessed in China. You can execute the following command to configure the yum source:
We need to change the cgroup of kubernetes to systemd, and we need to change the content of /etc/sysconfig/kubelet to KUBELET_EXTRA_ARGS="--cgroup-driver=systemd", Use the following command:
sed -i 's/KUBELET_EXTRA_ARGS=/KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"/g' /etc/sysconfig/kubelet
after modification:
Build a kubernetes cluster
We have completed the installation of kubernetes above, and then we will start to build the cluster
Initialize the cluster
This step only needs to be executed on the master, so click the OFF button on the master to turn off sending commands to all terminals:
We can use the following command to initialize (note that the IP address of the master is modified):
--apiserver-advertise-address=192.168.79.50: the address of k8s-master, pay attention to modify it to your own master address
--image-repository=registry.aliyuncs.com/google_containers: The default image pull address is Google, so here is changed to the image address of Alibaba Cloud
--kubernetes-version=v1.26.5: version number of kubernetes
--service-cidr=10.96.0.0/12: network segment of service
--pod-network-cidr=10.244.0.0/16: The network segment address of the pod. Note that this address will also be used when installing the Calico network plug-in later
--cri-socket=unix:///var/run/containerd/containerd.sock: set the socket of cri to use the sock of containerd
Seeing this means that the initialization is successful:
View execution results (files downloaded in advance):
Use the command to view the deployment results:
kubectl get all -n metallb-system
Similarly, if all STATUS are in the Running state, the installation is successful:
Allocate an IP address for MetalLB
We may create multiple services that are exposed to the outside world, so we need to assign multiple unused IP addresses to MetalLB, and add a new metallb-ip-pool.yaml file. The content of the file is:
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:
name: first-pool
namespace: metallb-system
spec:
addresses:
# Pay attention to change to the IP address you assigned for MetalLB
- 192.168.79.60-192.168.79.69
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:
name: example
namespace: metallb-system
spec:
ipAddressPools:
- first-pool
View file content:
Execute this file:
kubectl apply -f metallb-ip-pool.yaml
View execution results:
Next we can deploy the application
Deploy application
Deploy nginx
I will deploy an nginx program and expose the access address to the external network through the address pool of metallb, and use the command to deploy an nginx:
kubectl create deployment nginx --image=nginx
This command will pull the latest version of nginx from dockerhub and run it. You can view the running results:
Also use the command to view the deployment status
kubectl get deploy,pod -o wide
STATUS is Running:
Exposed to external network access
The application is successfully deployed. Now it is necessary to create a LoadBalancer for external network access. Use the command to expose nginx to the external network, and the service type is LoadBalancer: