在 pve 中使用 LXC 创建 k3s
在之前这里是使用 vm的方式创建了一个新的虚拟机来创建 k8s 节点,但是带来的问题就是在基本没有任何应用的情况下负载居高不下
所以考虑使用 k3s 来替代 k8s。
准备工作
在 pve 使用 lxc安装k3s需要修改一些宿主机配置
-
节点内核参数开启
bridge-nf-call-iptablessysctl -w net.bridge.bridge-nf-call-iptables=1这个主要是为了解决Service 同节点通信问题(启用
bridge-nf-call-iptables这个内核参数 (置为 1),表示 bridge 设备在二层转发时也去调用 iptables 配置的三层规则)) -
关闭swap
sysctl vm.swappiness=0 swapoff -a -
启用 IP 转发
echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf sysctl --system
创建 k3s 容器
- 创建k3s, 注意不要勾选无特权的容器
-
选择模板和存储
-
注意内存的 swap 置为 0
-
确认配置
-
返回宿主机修改 lxc 容器配置(/etc/pve/lxc/$ID.conf),这里 ID 如上图 vmid = 106
nano /etc/pve/lxc/106.conf 添加如下内容 lxc.apparmor.profile: unconfined lxc.cgroup.devices.allow: a lxc.cap.drop: lxc.mount.auto: "proc:rw sys:rw" -
容器配置
在 ect创建rc.local
touch /etc/rc.local写入如下内容
nano /etc/rc.local #!/bin/sh -e # Kubeadm 1.15 needs /dev/kmsg to be there, but it's not in lxc, but we can just use /dev/console instead # see: https://github.com/kubernetes-sigs/kind/issues/662 if [ ! -e /dev/kmsg ]; then ln -s /dev/console /dev/kmsg fi # https://medium.com/@kvaps/run-kubernetes-in-lxc-container-f04aa94b6c9c mount --make-rshared /赋予文件可执行权限
chmod +x /etc/rc.local重启容器
安装 k3s
注意这里我们安装使用只有单 master,并没有采用高可用方式(高可用占用的资源更多一般要求最低 4C8G)
-
先执行换源
nano /etc/apt/sources.list内容如下
# deb http://archive.ubuntu.com/ubuntu kinetic main restricted universe multiverse # deb http://archive.ubuntu.com/ubuntu kinetic-updates main restricted universe multiverse # deb http://archive.ubuntu.com/ubuntu kinetic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ kinetic main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ kinetic-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ kinetic-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ kinetic-backports main restricted universe multiverse然后更新
sudo apt update -
执行安装脚本
curl -sLS https://get.k3sup.dev | sh mv k3sup ~/bin/k3sup && chmod +x ~/bin/k3sup k3sup install --ip $CONTAINER_IP --user root -
或者使用官方脚本安装(注意这里不要使用默认版本,有可能不兼容 rancher,尽量使用前查阅支持的产品矩阵ranchermanager.docs.rancher.com/zh/versions…
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=v1.26.8+k3s1 sh - k3s kubectl get node -
获取集群令牌
cat /var/lib/rancher/k3s/server/node-token -
在worker节点运行以下命令来设置 K3s 并加入现有集群:
curl -fsL https://get.k3s.io | INSTALL_K3S_VERSION=v1.26.8+k3s1 K3S_URL=https://<control node ip>:6443 K3S_TOKEN=<cluster token> sh -s - --node-name worker-0 -
测试
kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml启用外部访问
kubectl expose deployment hello-world --type=LoadBalancer --name=hl查看地址和 ip
kubectl describe services hl浏览器访问
http://192.168.31.110:8080 -
卸载
/usr/local/bin/k3s-uninstall.sh # maser 执行 /usr/local/bin/k3s-agent-uninstall.sh # Node 执行
安装 Helm
默认k3s是没有附带 helm 的,需要我们手动安装,安装步骤如下。
准备
- 首先下载资源
wget https://get.helm.sh/helm-v3.12.3-linux-amd64.tar.gz
正常应该十几秒下载完成,如果不能下载的话建议挂代理。
- 解压
tar -zxvf helm-v3.12.3-linux-amd64.tar.gz
- 将可执行文件移动到 liunx 的 bin 目录
mv linux-amd64/helm /usr/local/bin/helm
结果
执行helm version 查看安装版本