如何使用国内资源安装 K3s

国内资源:

root@demo-1:~# time curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | \
  INSTALL_K3S_MIRROR=cn \
  K3S_TOKEN=12345 sh -s - \
  --system-default-registry=registry.cn-hangzhou.aliyuncs.com

[INFO]  Finding release for channel stable
[INFO]  Using v1.29.4+k3s1 as release
[INFO]  Downloading hash rancher-mirror.rancher.cn/k3s/v1.29.4-k3s1/sha256sum-amd64.txt
[INFO]  Downloading binary rancher-mirror.rancher.cn/k3s/v1.29.4-k3s1/k3s
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Skipping installation of SELinux RPM
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Creating /usr/local/bin/ctr symlink to k3s
[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
[INFO]  Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO]  env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO]  systemd: Creating service file /etc/systemd/system/k3s.service
[INFO]  systemd: Enabling k3s unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
[INFO]  systemd: Starting k3s

real	0m18.052s
user	0m0.850s
sys	0m0.462s
root@demo-1:~# kubectl get pods -A
NAMESPACE     NAME                                      READY   STATUS      RESTARTS   AGE
kube-system   coredns-58c9946f4-b92sb                   1/1     Running     0          80s
kube-system   helm-install-traefik-crd-zfpb8            0/1     Completed   0          79s
kube-system   svclb-traefik-e80607b2-c6dmt              2/2     Running     0          36s
kube-system   helm-install-traefik-k4hjs                0/1     Completed   1          79s
kube-system   metrics-server-5bbb74b77-kzv8n            1/1     Running     0          80s
kube-system   traefik-57c89d7764-795wg                  1/1     Running     0          36s
kube-system   local-path-provisioner-7f4c755b68-ccdrz   1/1     Running     0          80s
root@demo-1:~# crictl images
IMAGE                                                                TAG                    IMAGE ID            SIZE
registry.cn-hangzhou.aliyuncs.com/rancher/klipper-helm               v0.8.3-build20240228   0929b4140ada6       91.2MB
registry.cn-hangzhou.aliyuncs.com/rancher/klipper-lb                 v0.4.7                 edc812b8e25d0       4.78MB
registry.cn-hangzhou.aliyuncs.com/rancher/local-path-provisioner     v0.0.26                c54dcef6214cb       17.2MB
registry.cn-hangzhou.aliyuncs.com/rancher/mirrored-coredns-coredns   1.10.1                 ead0a4a53df89       16.2MB
registry.cn-hangzhou.aliyuncs.com/rancher/mirrored-library-traefik   2.10.7                 ee69e8120b64a       43.2MB
registry.cn-hangzhou.aliyuncs.com/rancher/mirrored-metrics-server    v0.7.0                 b9a5a1927366a       19.3MB
registry.cn-hangzhou.aliyuncs.com/rancher/mirrored-pause             3.6                    6270bb605e12e       298kB

K3s agent 节点安装:

root@demo-2:~# curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | \
  INSTALL_K3S_MIRROR=cn \
  K3S_URL=https://172.16.0.98:6443 \
  K3S_TOKEN=12345 \
  sh -

# K3s agent 节点不需要配置 `system-default-registry`。
root@demo-2:~# crictl images
IMAGE                                                      TAG                 IMAGE ID            SIZE
registry.cn-hangzhou.aliyuncs.com/rancher/klipper-lb       v0.4.7              edc812b8e25d0       4.78MB
registry.cn-hangzhou.aliyuncs.com/rancher/mirrored-pause   3.6                 6270bb605e12e       298kB

高可用安装

第一个 K3s Server 节点:

curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | \
  INSTALL_K3S_MIRROR=cn \
  K3S_TOKEN=12345 \
  sh -s - server \
  --cluster-init \
  --token 12345 \
  --system-default-registry=registry.cn-hangzhou.aliyuncs.com

将第二台和第三台 K3s Server 加入集群::

curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | \
  INSTALL_K3S_MIRROR=cn \
  K3S_TOKEN=12345 \
  sh -s - server \
  --server https://172.16.0.98:6443 \
  --system-default-registry=registry.cn-hangzhou.aliyuncs.com

通过配置文件安装 K3s

K3s Server:

root@demo-1:~# mkdir -p /etc/rancher/k3s/
root@demo-1:~# cat >/etc/rancher/k3s/config.yaml <<EOL
token: 12345
system-default-registry: registry.cn-hangzhou.aliyuncs.com
EOL

root@demo-1:~# curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
1 个赞

楼主好人一生平安。
真的十分感激,又帮我解决了一个大问题,–system-default-registry=registry.cn-hangzhou.aliyuncs.com 简直棒极了!
顺便问一下,为什么:

K3s agent 节点不需要配置 system-default-registry

小弟没有agent机器的条件,烦请楼主简单指点一下,后续我一定认真验证测试。

k3s-agent并没有这个参数,程序直接会从 k3s master节点获取该配置

答谢楼主热心回复
首先 agent节点我也验证过了,结果和预期一样,agent节点从主节点获取该配置,正常从镜像源下载镜像

ExecStart=/usr/local/bin/k3s \
    server \
        '--system-default-registry=registry.cn-hangzhou.aliyuncs.com' \

另外 为感谢楼主帮助,决定为社区贡献点我自己躺过的弯路,供大家闭坑:

参数“–system-default-registry=registry.cn-hangzhou.aliyuncs.com
可以解决k3s自身使用的镜像更换为镜像源的效果,但是k3s集群内部署的各种应用还是会从docker.io下载镜像
为了解决应用从docker.io下载镜像的问题,有两种解决方式,一种是部署完k3s后新增配置文件registries.yaml
然后重启,显然这种方式有点繁琐,像我这样的懒人肯定不喜欢

cat > /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
  docker.io:
    endpoint:
      - "https://registry.cn-hangzhou.aliyuncs.com/"
      - "https://mirror.ccs.tencentyun.com"
  quay.io:
    endpoint:
      - "https://quay.tencentcloudcr.com/"
  registry.k8s.io:
    endpoint:
      - "https://registry.aliyuncs.com/v2/google_containers"
  gcr.io:
    endpoint:
      - "https://gcr.m.daocloud.io/"
  k8s.gcr.io:
    endpoint:
      - "https://registry.aliyuncs.com/google_containers"
  ghcr.io:
    endpoint:
      - "https://ghcr.m.daocloud.io/"
EOF
systemctl restart k3s

另一种是安装k3s的时候直接添加环境变量 INSTALL_K3S_REGISTRIES=“https://registry.cn-hangzhou.aliyuncs.com

curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | \
  INSTALL_K3S_MIRROR=cn INSTALL_K3S_REGISTRIES="https://registry.cn-hangzhou.aliyuncs.com,https://mirror.ccs.tencentyun.com" sh -s - \
  --system-default-registry=registry.cn-hangzhou.aliyuncs.com

验证发现 变量INSTALL_K3S_REGISTRIES 不生效,原因是k3s安装脚本k3s-install.sh中函数setup_registry() 只有声明,没有调用

# --- Setup a custom Registry or Mirror
setup_registry() {
    REGISTRIES_FILE="/etc/rancher/k3s/registries.yaml"
    if [ "${INSTALL_K3S_REGISTRIES}" -a ! -f "$REGISTRIES_FILE" ]; then
        INSTALL_K3S_REGISTRIES=`echo ${INSTALL_K3S_REGISTRIES} | awk '{gsub(/,/," "); print $0}'`
        $SUDO mkdir -p `dirname $REGISTRIES_FILE`
        $SUDO cat >> $REGISTRIES_FILE <<EOF
mirrors:
  "docker.io":
    endpoint:
EOF
        for registry in ${INSTALL_K3S_REGISTRIES}; do
            echo "      - $registry" >> "$REGISTRIES_FILE"
        done
    fi
}

在适当位置修改安装脚本k3s-install.sh 即可完成变量INSTALL_K3S_REGISTRIES生效的目的,
比如在函数setup_env() 的最后调用setup_registry()

以上小结:参数“–system-default-registry”可以解决k3s自身用到的镜像换源的效果,
但是k3s自身内的应用无法通过该参数换源,但是可用用变量 INSTALL_K3S_REGISTRIES 在安装K3s的时候换源,
需要注意的是使用变量 INSTALL_K3S_REGISTRIES 时需要修改k3s安装脚本,添加函数 setup_registry 调用。
新增配置文件registries.yaml也可以完成以上效果,但是需要重启服务。

再次感谢~ graces

感谢各位大佬的探索和回复, 在这里我再补充一点: 如何通过代理去下载镜像?

新启个话题吧,别在这个帖子下面跟踪一些和这个主题无关的话题