DevOps笔记 在 pve 中使用 LXC 创建 k3s

2023年 9月 12日 87.5k 0

在 pve 中使用 LXC 创建 k3s

在之前这里是使用 vm的方式创建了一个新的虚拟机来创建 k8s 节点,但是带来的问题就是在基本没有任何应用的情况下负载居高不下

image-20230903151110805

所以考虑使用 k3s 来替代 k8s。

准备工作

在 pve 使用 lxc安装k3s需要修改一些宿主机配置

  • 节点内核参数开启 bridge-nf-call-iptables

    sysctl -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, 注意不要勾选无特权的容器
  • image-20230903152233468

  • 选择模板和存储

    image-20230903153635605

  • 注意内存的 swap 置为 0

    image-20230903153907136

  • 确认配置

    image-20230903154145694

  • 返回宿主机修改 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
    

    image-20230903161327317

  • 或者使用官方脚本安装(注意这里不要使用默认版本,有可能不兼容 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
    

    image-20230909172532270

  • 获取集群令牌

    cat /var/lib/rancher/k3s/server/node-token
    

    image-20230909172615654

  • 在worker节点运行以下命令来设置 K3s 并加入现有集群:

    curl -fsL https://get.k3s.io | INSTALL_K3S_VERSION=v1.26.8+k3s1 K3S_URL=https://:6443 K3S_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
    

    image-20230903171702063

    浏览器访问 http://192.168.31.110:8080

    image-20230903171818636

  • 卸载

    /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 查看安装版本

    相关文章

    服务器端口转发,带你了解服务器端口转发
    服务器开放端口,服务器开放端口的步骤
    产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
    如何使用 WinGet 下载 Microsoft Store 应用
    百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
    百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

    发布评论