k3s部署文档
安装环境
参考文档:https://docs.k3s.io/zh/quick-start
ip地址 | 主机名字 | 操作系统 |
---|---|---|
172.19.69.2 | k3s-master01 | centos7.9 |
172.19.69.3 | k3s-agent01 | centos7.9 |
172.19.69.4 | k3s-agent02 | centos7.9 |
1. 服务器初始化优化操作
1.1 内核升级
内核升级脚本如下所示
# 内核升级脚本
[root@k3s-master01 shell]# cat neuedu-update-kernel.sh
#!/bin/bash
#这个脚本用于升级centos7的内核
kernel=$(uname -r)
kernelNum=$(echo ${kernel:0:1})
if [ "$kernelNum" -ge 5 ]
then
echo "内核是最新版本,无需升级"
exit 0
fi
#载入公钥
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
#安装 ELRepo 最新版本
yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
#获取可用内核版本信息
enableNum=$(yum list available --disablerepo=* --enablerepo=elrepo-kernel |grep "kernel-lt.x86_64" | awk '{print $2}')
yum install -y kernel-lt-$enableNum --enablerepo=elrepo-kernel
#获取最新安装的内核版本
numFalg=$(cat /boot/grub2/grub.cfg | grep menuentry |grep $enableNum)
if [ -z "$numFalg" ]
then
echo "内核安装失败,请重新安装或手动升级"
exit
fi
grub2-set-default 'CentOS Linux ('$enableNum'.x86_64) 7 (Core)'
grub2-editenv list
#重启
read -p "reboot now y/n:" falg
case $falg in
y)
echo "reboot now"
reboot
;;
n)
echo "请重启服务器使内核生效"
echo "请重启服务器使内核生效"
echo "请重启服务器使内核生效"
;;
*)
echo "error input"
;;
esac
#报错说明
#遇到报错,执行 cat /boot/grub2/grub.cfg | grep menuentry 查看是否已安装最新的kernel,如果未安装,重新安装,如果已安装,按下一步走
#执行 grub2-set-default 'Centos Linux xxxx (Core)' 其中的xxxx为最新安装的内核,自行替换,然后运行grub2-editenv list验证,随后重启即可
1.2 服务器参数优化
# 关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
# 关闭防火墙
systemctl disable firewalld --now
swapoff -a
# 禁用swap分区
sed -ri 's/.*swap.*/#&/' /etc/fstab
# 参数优化
cat > /etc/sysctl.d/k8s_better.conf << EOF
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
vm.swappiness=0
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
EOF
# 载入配置
sysctl -p /etc/sysctl.d/k8s_better.conf
# 内核优化
cat >> /etc/security/limits.conf <<EOF
* soft nofile 1048576
* hard nofile 1048576
* soft nproc 1048576
* hard nproc 1048576
* hard memlock unlimited
* soft memlock unlimited
* soft msgqueue unlimited
* hard msgqueue unlimited
EOF
sed -i "s/#DefaultLimitNOFILE=/DefaultLimitNOFILE=1048576/g" /etc/systemd/system.conf
sed -i "s/#DefaultLimitNPROC=/DefaultLimitNPROC=1048576/g" /etc/systemd/system.conf
sed -i "s/#DefaultLimitMEMLOCK=/DefaultLimitMEMLOCK=infinity/g" /etc/systemd/system.conf
systemctl daemon-reexec
2. K3s 安装部署
2.1 基础命令安装
mkdir -p /export0/k3s/
yum install -y wget vim bash-completion
# k3s 安装脚本
curl -o /export0/k3s/k3s-install.sh https://rancher-mirror.rancher.cn/k3s/k3s-install.sh
2.2 安装部署
cd /export0/k3s/
# k3s 安装脚本
curl -o /export0/k3s/k3s-install.sh https://rancher-mirror.rancher.cn/k3s/k3s-install.sh
# 使用国内源下载
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
2.3 验证是否安装成功
kubectl get node -o wide
[root@k3s-master01 k3s]# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k3s-master01 Ready control-plane,master 16s v1.28.5+k3s1 172.19.69.2 <none> CentOS Linux 7 (Core) 5.4.267-1.el7.elrepo.x86_64 containerd://1.7.11-k3s2
2.4 agent 节点加入
# 在master节点中 查看token
[root@k3s-master01 k3s]# cat /var/lib/rancher/k3s/server/node-token
K1039d8f0237ebdaac12ee628b9d5b51d6393f2cb08fd8906bf58d424e16503161f::server:f484d102101f7c7db6aea774f4aa7fc7
2.5 agent节点执行
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn K3S_URL=https://172.19.69.2:6443 K3S_TOKEN=K1039d8f0237ebdaac12ee628b9d5b51d6393f2cb08fd8906bf58d424e16503161f::server:f484d102101f7c7db6aea774f4aa7fc7 sh -
等待安装完毕后,集群初始化是否成功
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k3s-master01 Ready control-plane,master 11m v1.28.5+k3s1 172.19.69.2 <none> CentOS Linux 7 (Core) 5.4.267-1.el7.elrepo.x86_64 containerd://1.7.11-k3s2
k3s-agent01 Ready <none> 2m53s v1.28.5+k3s1 172.19.69.3 <none> CentOS Linux 7 (Core) 5.4.267-1.el7.elrepo.x86_64 containerd://1.7.11-k3s2
k3s-agent02 Ready <none> 38s v1.28.5+k3s1 172.19.69.4 <none> CentOS Linux 7 (Core) 5.4.267-1.el7.elrepo.x86_64 containerd://1.7.11-k3s2
2.6 修改roles
# 自动补全
echo 'source <(kubectl completion bash)' >> ~/.bashrc
source ~/.bashrc
kubectl label nodes k3s-agent01 node-role.kubernetes.io/agent=
kubectl label nodes k3s-agent02 node-role.kubernetes.io/agent=
[root@k3s-master01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k3s-master01 Ready control-plane,master 16m v1.28.5+k3s1
k3s-agent01 Ready agent 7m58s v1.28.5+k3s1
k3s-agent02 Ready agent 5m43s v1.28.5+k3s1
3. Rancher管理
注意事项:
这里由于使用的公司域名,外网解析到的和内网解析到的地址不同。
所以需要在coreDNS中加入域名解析
直接修改configmap中的域名映射部分即可
4. K3s 安装helm
这里需要注意:
在安装完helm后会有如下报错
[root@k3s-master01 ~]# helm list
Error: Kubernetes cluster unreachable: Get "http://localhost:8080/version": dial tcp 127.0.0.1:8080: connect: connection refused
原因:
helm v3版本不再需要Tiller,而是直接访问ApiServer来与k8s交互,通过环境变量KUBECONFIG来读取存有ApiServre的地址与token的配置文件地址,默认地址为~/.kube/config
解决方案:
- 执行: vi /etc/profile
- 写入内容: export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
- 执行: source /etc/profile
5. k3s containerd 私有仓库配置
私有仓库配置需要在所有的节点上都进行配置
/etc/rancher/k3s
在该目录下创建文件registries.yaml
写入内容格式如下所示:
mirrors:
"xxx.xxx.xxx.xxx":
endpoint:
- "http://xxx.xxx.xxx.xxx"
configs:
"xxx.xxx.xxx.xxx":
auth:
username: xxxxxxxx # this is the registry username
password: xxxxxxxx # this is the registry password
注意: 需要在所有节
点都配置,配置完毕后重启k3s
# 重启控制节点
systemctl restart k3s
# agent 节点重启
systemctl restart k3s-agent
6. 注意事项
6.1 边缘网络路由器
k3s 默认就已经安装了 边缘网络路由器 Traefik
所以无需安装ingress-nginx
6.2 k3s重启后coredns重置
若需要保留自定义域名
需要将域名添加到如下位置
修改coredns的configmap
命名空间:kube-system
修改 K3s manifests 中的 CoreDNS 配置文件,文件位置:/var/lib/rancher/k3s/server/manifests/coredns.yaml
7. storage classes 部署
部署NFS服务器为:172.19.69.4
# 添加nfs的helm源
helm repo add nfs-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner
# 创建nfs的命名空间
kubectl create ns nfs
[root@k8s-master nfs-client-provisioner]# kubectl get storageclasses.storage.k8s.io
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs-client cluster.local/storageclass-nfs-client-provisioner Delete Immediate true 29s
# 下载chart
[root@k8s-master helm_chat]# helm pull nfs-provisioner/nfs-subdir-external-provisioner
# 解压
[root@k8s-master helm_chat]# tar -xvf nfs-subdir-external-provisioner-4.0.18.tgz
# 进入
[root@k8s-master helm_chat]# cd nfs-subdir-external-provisioner/
# 部署
helm install nfs-subdir-external-provisioner ./ \
--set nfs.server=172.19.69.4 \
--set nfs.path=/data/nfs/storage \
-n nfs
8. K3s-安装 nerdctl
将文件安装放置到指定位置
设置环境变量
echo 'export CONTAINERD_ADDRESS=/run/k3s/containerd/containerd.sock' >> ~/.bashrc
source ~/.bashrc
sudo ls -l /run/k3s/containerd/containerd.sock
# 验证是否正常
nerctl -n k8s.io image
ctr -n k8s.io i ls
crictl images
kuectl get pod -A
评论区