sealos部署干净K8S环境,实现集群优化
引言
本文旨在通过sealos工具实现对K8S环境的快速部署,以适用于便携集群环境的部署。主详解sealos部署过程中出现的问题,以得到干净的集群环境。初步部署过程中使用ai进行进行优化建议,后去糟优化完成,适用对需要一个简洁集群环境的童鞋。
其主要解决问题:
- 部署的helm在进行安装动作时会因cni插件问题导致pod沙箱创建失败;
- 部署后存在使用ctr命令版本告警;
注:当前服务器系统为ubuntu22.04
安装准备
在我们进行部署集群环境前,我们需要对当前服务器(虚拟机)环境做一个简单的"大扫除",避免集群因内部环境出现问题:
- 关闭swap分区:
swapoff -a && sysctl -w vm.swappiness=0 # 临时关闭
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab # 基于配置文件关闭
- 确保各个节点MAC地址或product_uuid唯一:
ifconfig eth0 | grep ether | awk '{print $2}'
cat /sys/class/dmi/id/product_uuid
- 检查网络节点是否互通:
ping baidu.com
- 允许iptable检查桥接流量:
cat <<EOF | tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
cat <<EOF | 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
sysctl --system
- 关闭防火墙,设置时间同步:
# 停止
systemctl stop firewalld.service
# 禁用
systemctl disable firewalld.service
# selinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux
# 设置时区
timedatectl set-timezone Asia/Shanghai
开始安装
添加sealos源 (所有集群服务器)
echo "deb [trusted=yes] https://apt.fury.io/labring/ /" | sudo tee /etc/apt/sources.list.d/labring.list
apt update #更新源
apt list | grep sealos #检查sealos包及其版本(如需指定版本,使用apt指定下载相关版本)
apt install sealos #下载
部署集群
部署过程中需要使用到sealos以工具拉取镜像,需下载相应集群版本或相应工具版本可前往: https://hub.docker.com/u/labring
使用 Registry Explorer 可以查看 K8s 集群镜像的所有版本,直接输入 registry.cn-shanghai.aliyuncs.com/labring/kubernetes,然后点击 “Submit Query”:
master节点部署环境:
sealos run registry.cn-shanghai.aliyuncs.com/labring/kubernetes:v1.29.9 registry.cn-shanghai.aliyuncs.com/labring/helm:v3.15.4-amd64 registry.cn-shanghai.aliyuncs.com/labring/calico:v3.27.4
#添加主master节点,选定组件及其配置
master节点与nodes节点进行免密要,添加nodes节点:
ssh-keygen ssh-copy-id 配置免密要
sealos add --nodes 10.0.0.111,10.0.0.112 #添加nodes节点
配置命令补全
需下载completion补全工具
kubectl completion bash > ~/.kube/completion.bash.inc
echo source '$HOME/.kube/completion.bash.inc' >> ~/.bashrc
source ~/.bashrc
集群优化
ctr命令提示符优化
默认使用containerd作为容器运行时,但是使用ctr命令会不断进行一个提高版本的告警),需进行调整
root@sealos111:~# ctr i ls
WARN[0000] DEPRECATION: The `configs` property of `[plugins."io.containerd.grpc.v1.cri".registry]` is deprecated since containerd v1.5 and will be removed in containerd v2.1. Use `config_path` instead.
优化
cp /etc/systemd/system/containerd.service /usr/lib/systemd/system
containerd config default > /etc/containerd/config.toml
grep SystemdCgroup /etc/containerd/config.toml
sed -ri 's#(SystemdCgroup = )false#\1true#' /etc/containerd/config.toml
sed -i 's#registry.k8s.io/pause:3.8#registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.8#' /etc/containerd/config.toml
grep sandbox_image /etc/containerd/config.toml
systemctl enable --now containerd
systemctl restart containerd
cni优化
sealos安装的calico插件namespace不属于system
可能存在问题原因:
Calico 配置错误:Calico 的配置文件或相关配置可能存在问题,导致无法正常获取集群信息。
RBAC 权限问题:可能是 Calico 组件缺少必要的 RBAC(Role-Based Access Control)权限,无法访问集群信息。
证书或身份验证问题:可能与证书过期、缺失或配置错误有关,导致 Calico 无法通过身份验证来获取集群信息。
解决方案:
】1. 首先查看 calico-apiserver-auth-reader 角色的详细权限,使用以下命令:
kubectl get role calico-apiserver-auth-reader -n kube-system -o yaml
#若在 kube-system 命名空间中不存在该角色,则进行创建
我们需要创建一个新的角色和角色绑定,以确保 Calico 组件具有相应的权限。
创建一个 YAML 文件,例如 calico-rbac.yaml,包含以下内容:
root@sealos110:~# vim /etc/kubernetes/calico/calico-rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: calico-clusterinfo-reader
namespace: kube-system
rules:
- apiGroups: ["crd.projectcalico.org"]
resources: ["clusterinformations"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: calico-clusterinfo-reader-binding
namespace: kube-system
roleRef:
kind: Role
name: calico-clusterinfo-reader
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: calico-node
namespace: kube-system
】2. 应用角色进行绑定应用到 kube-system 命名空间中:
kubectl apply -f calico-rbac.yaml
】3.检查角色是否创建成功
kubectl get roles -n kube-system | grep calico-clusterinfo-reader
kubectl get rolebindings -n kube-system | grep calico-clusterinfo-reader-binding
】4.重启 Calico 组件:
查找 Calico 的节点代理 DaemonSet:
root@sealos110:~# kubectl get pods -n calico-system | grep calico-node
calico-node-5m42z 1/1 Running 0 19h
calico-node-cvbc4 1/1 Running 0 19h
calico-node-n2j6t 1/1 Running 0 19h
删除 Calico 节点代理的 Pod,让它们重新创建:
kubectl delete pod -n calico-system <calico-node-pod-name> #上面的几个