Wener Site

K3S

约 6 分钟阅读
  • 节点的名字需要唯一
    • 默认使用 hostname
    • 可使用 K3S_NODE_NAME 或 --node-name 修改
  • k3s 会读取 /etc/machine-id 或 /var/lib/dbus/machine-id 作为节点 UUID
  • kubconfig 文件 /etc/rancher/k3s/k3s.yaml
  • 部署 traefik 作为 ingress
  • k3s-io/k3sGitHubk3s-io/k3s节点的名字需要唯一 · 默认使用 hostname · 可使用 或 修改 · k3s 会读取 或 作为节点 UUID · kubconfig 文件 · 部署 traefik 作为 ingress · k3s-io/k3s · 环境要求 · Linux 3.10+笔记:K3S
  • 环境要求
    • Linux 3.10+
    • Server 内存 512 MB+
    • Agent 内存 75 MB
    • 磁盘 200 MB
    • 架构 x86_64, ARMv7, ARM64
    • AlpineLinux 额外配置
    • Production
      • Small <= 10 节点 - Server 2C4G - Database 1C2G
      • Medium <= 100 节点 - Server 4C8G - Database 2C8G
  • 端口
    • 6443 - server - 节点通信 - Kubernetes API
    • 6444
    • 8472/udp - server/agent - Flannel VXLAN
    • 10250 - server/agent - kubelet
    • 10251
    • 10010 - containerd
    • 10248 - 10252
    • 10249 - kube-prpxy
    • 30518
    • 30643
    • 46517
  • K3S 组件 - 安装时可禁用内置组件 --disable
  • 参考
  • 问题
  • /etc/rancher/node/password
  • /var/lib/rancher/k3s/server/cred/node-passwd
    • kubectl -n kube-system delete secrets <agent-node-name>.node-password.k3s
bash
apk add util-linux
[ -f /etc/machine-id ] || ( uuidgen | sudo tee -a /etc/machine-id )

apk add wireguard-virt wireguard-tools

# INSTALL_K3S_EXEC 默认为 agent
# flannel wireguard - https://github.com/coreos/flannel/blob/master/dist/extension-wireguard
K3S_NODE_NAME=k3s-server INSTALL_K3S_EXEC="server --flannel-backend=wireguard" INSTALL_K3S_SKIP_START=true INSTALL_K3S_BIN_DIR=/opt/k3s/bin curl -sfL https://get.k3s.io | sh -

k3s server --flannel-backend=wireguard

# 如果是 root 安装 - 修改下 kubeconfig 权限
sudo chmod a+r /etc/rancher/k3s/k3s.yaml
# k3s 默认会访问该文件

# 其他访问
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml

k3s server

  • k3s server 配置
  • Advanced Options and Configuration
  • --docker - 使用 docker - 默认 containerd
  • 在 /var/lib/rancher/k3s/server/manifests 下面的文件会被自动部署 - kubectl apply
  • 默认使用 containerd, 启动 agent 的时候添加 --docker 可使用 docker
  • 针对 containerd 生成的配置位于 /var/lib/rancher/k3s/agent/etc/containerd/config.toml
  • AlpineLinux 需要额外的配置 /etc/update-extlinux.conf
    • default_kernel_opts 添加 cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory
    • 然后 update-extlinux && reboot
  • 非 root 数据存放于 ~/.rancher/k3s/data
  • root 数据存放于 /var/lib/rancher/k3s/data
  • 集群 cidr 10.42.0.0/16
    • 节点 IP
    • cni0 - 本地网口 - 附带 IP
    • flannel1.1 - 集群通信
    • 会分配给每个 Pod
    • 每个节点一个 /24 地址 - 不同节点之间进行转发
  • 服务 cidr 10.43.0.0/16
    • 服务 IP
    • 不能 ping
    • 虚拟地址,通过 iptables 配置
  • 集群域名 cluster.local
  • coredns 10.43.0.10
  • 网络
  • 私有仓库 /etc/rancher/k3s/registries.yaml
  • 服务配置

get.k3s.io

  • get.k3s.io 安装脚本
  • 下载地址为 STORAGE_URL=storage.googleapis.com/k3s-ci-builds
  • 会安装 openrc 服务
    • /etc/rancher/k3s/k3s.env
    • /etc/rancher/k3s/k3s-agent.env
    • /etc/init.d/k3s
    • /etc/init.d/k3s-agent
  • 日志文件 /var/log/k3s.log
  • 安装选项
    • 默认安装为 server 启动
      • 设置 K3S_URL 且设置 K3S_TOKEN 或 K3S_CLUSTER_SECRET
      • 或直接后面指定 agent
    • INSTALL_K3S_SKIP_DOWNLOAD - 不下载
    • INSTALL_K3S_SYMLINK - 创建 kubectl,crictl,ctr 符号链接 - 设置为 skip 会调过,设置为 force 会覆盖
    • INSTALL_K3S_SKIP_ENABLE - 不启用和启动 k3s - 即不会 add openrc 的 service 也不会 start
    • INSTALL_K3S_SKIP_START - 不启动服务
    • INSTALL_K3S_BIN_DIR - 安装目录 - 默认 /usr/local/bin
    • INSTALL_K3S_EXEC - 指向命令
      • 默认 agent 除非有 K3S_URL
    • INSTALL_K3S_CHANNEL_URL - 默认 update.k3s.io/v1-release/channels
    • INSTALL_K3S_CHANNEL - 默认 stable
    • 额外安装
      • /usr/local/bin/k3s-killall.sh
        • 调用 service k3s stop
        • umount
          • /run/k3s
          • /var/lib/rancher/k3s
          • /var/lib/kubelet/pods
          • /run/netns/cni-
        • 移除 cni0 和 flannel1.1
        • 删除 /var/lib/cni/
        • 移除 iptables 里的 KUBE 和 CNI 内容
      • /usr/local/bin/k3s-uninstall.sh
      • /etc/rancher/k3s/k3s.env
        • service 启动会 source 这个文件
      • /etc/init.d/k3s
        • 安装参数会直接在这里
        • 日志文件为 /var/log/k3s.log
  • INSTALL_K3S_EXEC="--disable=traefik" 可禁用安装某些服务
bash
curl -sfL https://get.k3s.io | sh -

# 可直接指定参数
curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644
# 也可以环境变量指定
curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" sh -s -

# INSTALL_K3S_EXEC 指定命令
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="--no-flannel" sh -s -
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server --no-flannel" sh -s -
curl -sfL https://get.k3s.io | INSTALL_K3S_EXEC="server" sh -s - --no-flannel
curl -sfL https://get.k3s.io | sh -s - server --no-flannel
curl -sfL https://get.k3s.io | sh -s - --no-flannel
curl -sfL https://get.k3s.io | INSTALL_K3S_SKIP_ENABLE=1 sh -s -

手动镜像安装

bash
ver=$(curl -sfL https://rancher-mirror.rancher.cn/k3s/channels/stable)
curl -sfLO https://rancher-mirror.rancher.cn/k3s/${ver/+/-}/k3s
curl -sfLO https://rancher-mirror.rancher.cn/k3s/${ver/+/-}/sha256sum-amd64.txt
sha256sum -c sha256sum-amd64.txt --ignore-missing

chmod +x k3s
mv k3s /usr/bin/k3s
k3s check-config
# k3s server

openrc

  • /etc/rancher/k3s/${SYSTEM_NAME}.env
/etc/init.d/k3s
#!/sbin/openrc-run

# Based on ...
#   https://raw.githubusercontent.com/rancher/k3s/master/install.sh
K3S_LOGFILE="${K3S_LOGFILE:-/var/log/${RC_SVCNAME}.log}"

supervisor=supervise-daemon

name="k3s"
command="/usr/bin/k3s"
command_args="${K3S_EXEC} ${K3S_OPTS} >>${K3S_LOGFILE} 2>&1"

output_log=${K3S_LOGFILE}
error_log=${K3S_LOGFILE}

pidfile="/run/k3s.pid"
respawn_delay=5
respawn_max=0

rc_ulimit="${K3S_ULIMIT:--c unlimited -n 1048576 -u unlimited}"

depend() {
        want cgroups
        after firewall
}

start_pre() {
        checkpath -f -m 0644 -o root:root "${K3S_LOGFILE}"
        rm -f /tmp/k3s.*
}
/etc/init.d/k3s
#!/sbin/openrc-run

depend() {
    after network-online
    want cgroups
}

start_pre() {
    rm -f /tmp/k3s.*
}

supervisor=supervise-daemon
name=${SYSTEM_NAME}
command="${BIN_DIR}/k3s"
command_args="$(escape_dq "${CMD_K3S_EXEC}")
    >>${LOG_FILE} 2>&1"

output_log=${LOG_FILE}
error_log=${LOG_FILE}

pidfile="/var/run/${SYSTEM_NAME}.pid"
respawn_delay=5
respawn_max=0

set -o allexport
if [ -f /etc/environment ]; then source /etc/environment; fi
if [ -f ${FILE_K3S_ENV} ]; then source ${FILE_K3S_ENV}; fi
set +o allexport
/etc/logrotate.d/k3s
/var/log/k3s.log {
	missingok
	notifempty
	copytruncate
}

手动安装启动

bash
mkdir -p /opt/k3s
cd /opt/k3s
# https://github.com/rancher/k3s/releases
ver=$(curl -sL https://api.github.com/repos/rancher/k3s/releases/latest | jq .tag_name -r)

curl -LOC- https://github.com/rancher/k3s/releases/download/$ver/k3s
curl -LOC- https://github.com/rancher/k3s/releases/download/$ver/k3s-images.txt

ssh k3s -- "sudo sh -c 'mkdir -p /opt/k3s && chown admin:admin /opt/k3s'"
scp k3s k3s:/opt/k3s
scp k3s-images.txt k3s:/opt/k3s

# ssh k3s --
cat /opt/k3s/k3s-images.txt | xargs -n 1 docker pull

k3s server --cluster-init --alsologtostderr --log $PWD/k3s-server.log --docker

containerd

registries

  • /etc/rancher/k3s/registries.yaml
  • 启动时检测,containerd 会使用这里的定义
yaml
mirrors:
  # 镜像 DockerHub
  docker.io:
    endpoint:
      - 'https://mycustomreg.com:5000'
configs:
  # 添加授权和证书
  'mycustomreg:5000':
    auth:
      username: xxxxxx # this is the registry username
      password: xxxxxx # this is the registry password
    tls:
      cert_file: # path to the cert file used in the registry
      key_file: # path to the key file used in the registry
      ca_file: # path to the ca file used in the registry

笔记

  • kubeconfig 位于 /etc/rancher/k3s/k3s.yaml
  • K3S_TOKEN 位于 /var/lib/rancher/k3s/server/node-token
  • 节点需要有唯一主机名 - K3S_NODE_NAME

代理节点注册逻辑

  • k3s agent 初始化 Agent 节点的 websocket 链接。链接会有客户端进行负载均衡。
  • Agent 会使用集群的密钥和随机生成的密码注册,密码存储于 /etc/rancher/node/password,服务端会存储节点的密码到 /var/lib/rancher/k3s/server/cred/node-passwd。
  • 节点上的 /etc/rancher/node 目录被移除后密码会被从新生成,或由服务端移除。
  • 启动时可为节点附加唯一节点标示,--with-node-id。

快速启动

bash
curl -LO 'https://echo.wener.cc/https://github.com/k3s-io/k3s/releases/download/v1.24.1%2Bk3s1/k3s'
chmod +x k3s
./k3s server

关联信息

反向链接和本文引用的外部资料。

反向链接

  • K3S vs K0S
    故事

    | vs. | k0s | k3s | · | since | 2020-06 | 2019-03 | · | by | Mirants | Rancher/SUSE | · | Governance | Mirants | CNCF | · | Github | [k0sproject/k0s] | [k3s-io/k3s] |

References

GitHub

13 条

另有 5 个未显示 references。

其他外链

14 条

另有 6 个未显示 references。

另有 11 个 references 未显示。

最近更新commit b1c319aEdit

On this page