-
作者:SRE运维博客
博客地址: https://www.cnsre.cn/
文章地址:https://www.cnsre.cn/posts/211109907029/
相干话题:https://www.cnsre.cn/tags/k3s/
- -
双十一各大云厂商纷纷撒种子种韭菜(抢用户),良心云
也是判若两人的良心,新用户更是通过某宝148
就能够买到三年2C4G8M
的轻量应用服务器。于是我也发售薅了羊毛动手了一台。
然而对于各种组件自身就对资源耗费比拟大的k8s
来说,跑起来还是有点费劲的,于是我打算将这台实例部署一台轻量级的Kubernetes
: k3s
- -
k8s VS k3s
k3s 是 Rancher 推出的轻量级 k8s。k3s 自身蕴含了 k8s 的源码,而二进制包却只有 60M
然而实质上和 k8s 没有区别。但为了升高资源占用,k3s 和 k8s 还是有一些区别的,次要是:
- 应用了相比 Docker 更轻量的 containerd 作为容器运行时(Docker 并不是惟一的容器抉择)。
- 去掉了 k8s 的 Legacy, alpha, non-default features。
- 用 sqlite3 作为默认的存储,而不是 etcd。
- 其余的一些优化,最终 k3s 只是一个 binary 文件,十分易于部署。、
所以 k3s 实用于边缘计算,IoT 等资源缓和的场景。同时 k3s 也是非常容易部署的,官网上提供了一键部署的脚本。
k3s的长处
- k3s将装置Kubernetes所需的所有打包进仅有60MB大小的二进制文件中,并且齐全实现了Kubernetes API。为了缩小运行Kubernetes所需的内存,Rancher删除了很多不必要的驱动程序,并用附加组件对其进行替换。
- k3s是一款齐全通过CNCF认证的Kubernetes发行版,这意味着你能够编写YAML来对完整版的Kubernetes进行操作,并且它们也将实用于k3s集群。
- 因为它只须要极低的资源就能够运行,因而它可能在任何512MB RAM以上的设施上运行集群,换言之,咱们能够让pod在master和节点上运行。
k3s的毛病
- 首先,以后k3s的版本(k3s v0.8.1)仅能运行单个master,这意味着如果你的master宕机,那么你就无奈治理你的集群,即使已有集群要持续运行。然而在k3s v0.10的版本中,多主模式曾经是试验性功能,兴许在下一个版本中可能GA。
- 其次,在单个master的k3s中,默认的数据存储是SQLite,这对于小型数据库非常敌对,然而如果蒙受重击,那么SQLite将成为次要痛点。然而,Kubernetes管制立体中产生的更改更多是与频繁更新部署、调度Pod等无关,因而对于小型开发/测试集群而言,数据库不会造成太大负载。
论断
K8s和k3s各有优劣,应用场景也有所区别,因而不能一概而论。如果你要进行大型的集群部署,那么我倡议你抉择应用K8s;
如果你像我一样只是为了开发或者测试,那么抉择k3s则是性价比更高的抉择。
装置 k3s
确保你是一台洁净的 CentOS7
服务器。
依照常规先更新,更新前顺便把源换为国内的yum源。
# 改国内yum源curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repocurl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo# 更新yum update -y
批改主机名
hostnamectl set-hostname k3s-master
批改结束后,断开重连一下。
{{< alert theme="warning" dir="ltr" >}}
⚠️ 留神
K3s 默认将应用 containerd
作为容器环境,请在下边抉择 应用Docker装置
或者应用Containerd装置
。
{{< /alert >}}
{{< tabs 应用docker装置 应用containerd装置>}}
{{< tab >}}
应用 docker 装置
# 装置 docker-ceyum remove -y docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engineyum install -y yum-utilsyum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repoyum install -y docker-ce docker-ce-cli containerd.io# 解决内核查看问题 重启失效grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"systemctl enable dockersystemctl start docker# 批改 docker 源cat << EOF > /etc/docker/daemon.json{ "registry-mirrors":["https://3laho3y3.mirror.aliyuncs.com"]}EOFsystemctl daemon-reloadsystemctl restart docker# 装置 docker 完结# 关 firewalld 防火墙systemctl stop firewalldsystemctl disable firewalld# 装置 k3scurl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - --docker
{{< /tab >}}
{{< tab >}}
应用 containerd 装置
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
{{< /tab >}}
{{< /tabs >}}
装置完查看
装置实现后,能够执行以下命令进行主机查看。
# 运行配置查看k3s check-config# 查看节点状态以及 k3s 版本[root@k3s-master ~]# kubectl get nodeNAME STATUS ROLES AGE VERSIONvm-16-8-centos Ready control-plane,master 52m v1.21.5+k3s2# 查看所有 pod 信息[root@k3s-master ~]# kubectl get pods -ANAMESPACE NAME READY STATUS RESTARTS AGEkube-system local-path-provisioner-5ff76fc89d-bbps4 1/1 Running 0 52mkube-system coredns-7448499f4d-42v9x 1/1 Running 0 52mkube-system metrics-server-86cbb8457f-xqlrg 1/1 Running 0 52mkube-system helm-install-traefik-crd-9wk9v 0/1 Completed 0 52mkube-system helm-install-traefik-d8llf 0/1 Completed 3 52mkube-system svclb-traefik-jqxvf 2/2 Running 0 49mkube-system traefik-97b44b794-wv6zv 1/1 Running 0 49m
截止到这里 k3s 曾经装置结束。
装置 nfs
装置 nfs 服务
yum -y install nfs-utilssystemctl start nfs && systemctl enable nfs
创立nfs目录
mkdir -p /home/k8s/nfs
批改权限
chmod -R 755 /home/k8s/nfs
编辑export文件
cat >>/etc/exports << EOF/home/k8s/nfs *(rw,no_root_squash,sync)EOF
配置失效
exportfs -r
启动rpcbind、nfs服务
systemctl restart rpcbind && systemctl enable rpcbindsystemctl restart nfs && systemctl enable nfs
到这里 k3s 以及 nfs 曾经装置实现,上面就能够去体验了。
如果想以上都比拟麻烦,你能够用上面的一键装置脚本来执行
一键装置 k3s 脚本
#!/bin/bashPATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/binexport PATHLANG=en_US.UTF-8RD="\033[31m" # 谬误音讯GR="\033[32m" # 胜利音讯YL="\033[33m" # 告警音讯BL="\033[36m" # 日志音讯PL='\033[0m'clearecho -e "${YL}##################################################${PL}"echo -e "${YL}#${PL} ${GR}脚本名称${PL}: 一键装置 k3s 脚本 ${YL}#${PL}"echo -e "${YL}#${PL} ${GR}作 者${PL}: sre运维博客 ${YL}#${PL}"echo -e "${YL}#${PL} ${GR}网 址${PL}: https:www.cnsre.cn ${YL}#${PL}"echo -e "${YL}#${PL} ${GR}文章地址${PL}: https://cnsre.cn/posts/211109907029/ ${YL}#${PL}"echo -e "${YL}##################################################${PL}"sleep 0.5set -eecho echo echoecho -e "${RD}是否确定装置 dockerb版本的 k3s? ${PL}"read -r -p "确定请按 y 任意键则退出!请抉择:[y/n]" input if [[ $input != "y" ]]; then exit 1 else echo -e "$GR正在开始装置 dockerb版本的 k3s$PL" fiif [ `command -v docker` ];then echo -e "${YL}docker 曾经装置,正在增加docker减速源${PL}"else echo -e "${GR}install docker${PL}" curl https://download.daocloud.io/docker/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo yum -y install https://download.daocloud.io/docker/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm curl -fsSL https://get.daocloud.io/docker | bash -s docker --mirror Aliyunfisudo mkdir -p /etc/dockertee /etc/docker/daemon.json <<-'EOF'{ "registry-mirrors": ["http://f1361db2.m.daocloud.io"]}EOFgrubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"sudo systemctl daemon-reloadsudo systemctl restart dockersudo systemctl enable dockerif [ `command -v k3s` ];then echo -e "${YL}k3s 曾经装置${PL}" exit 1else export K3S_NODE_NAME=${HOSTNAME//_/-} export INSTALL_K3S_EXEC="--docker --kube-apiserver-arg service-node-port-range=1-65000 --no-deploy traefik --write-kubeconfig ~/.kube/config --write-kubeconfig-mode 666" curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -fiecho -e "${GR}export K3S_TOKEN=$(cat /var/lib/rancher/k3s/server/node-token)${PL}"echo -e "${GR}export K3S_URL=https://$(ip addr | grep -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -E -v "^127\.|^255\.|^0\." | head -n 1):6443${PL}"echo -e "${GR}装置完结,请重启服务器${PL}"read -r -p "确定请按 y 任意键则退出!请抉择:[y/n]" input if [[ $input != "y" ]]; then reboot else exit 1 fi
-
作者:SRE运维博客
博客地址: https://www.cnsre.cn/
文章地址:https://www.cnsre.cn/posts/211109907029/
相干话题:https://www.cnsre.cn/tags/k3s/
- -