RKE/RKE2 使用代理获取镜像

由于国内网络访问受限,从 Docker Hub 等海外镜像仓库拉取镜像时可能会非常缓慢,甚至出现拉取失败的情况。

如果环境中已有可用代理,可以为 RKE Docker 或 RKE2 Containerd 配置代理,从而通过代理拉取镜像。


准备代理服务器

本文使用 Clash 作为代理服务器。

准备 Clash 配置文件,将 Clash 的 .yaml 配置文件放到 /root/clash 目录下:

1
mkdir -pv /root/clash

例如:

1
/root/clash/config.yaml

启动 Clash 容器:

1
2
3
4
5
6
7
8
docker run -d \
--restart=unless-stopped \
--name clash \
-v /root/clash/config.yaml:/root/.config/clash/config.yaml:ro \
-p 9090:9090 \
-p 7890:7890 \
-p 7891:7891 \
harbor.warnerchen.com/dreamacro/clash:v1.18.0

验证代理是否可用:

1
curl -x http://localhost:7890 https://www.google.com -I

如果能够正常返回 HTTP 响应头,则说明代理可用。


为 RKE Docker 配置代理

对于 RKE 集群,容器运行时为 Docker,可以通过 systemd drop-in 文件为 Docker 配置代理。

创建 Docker systemd 代理配置:

1
2
3
4
5
6
7
8
mkdir -pv /etc/systemd/system/docker.service.d

cat <<EOF > /etc/systemd/system/docker.service.d/proxy.conf
[Service]
Environment="HTTP_PROXY=http://172.16.16.12:7890"
Environment="HTTPS_PROXY=http://172.16.16.12:7890"
Environment="NO_PROXY=localhost,127.0.0.1,0.0.0.0,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,10.42.0.0/16,10.43.0.0/16,cattle-system.svc,.svc,.cluster.local,.cn,.warnerchen.com"
EOF

重启 Docker:

1
2
systemctl daemon-reload
systemctl restart docker

确认 Docker 代理配置:

1
systemctl show --property Environment docker

验证镜像拉取:

1
docker pull nginx:mainline

为 RKE2 Containerd 配置代理

RKE2 使用内置 Containerd 作为容器运行时。可以通过 /etc/default/rke2-server/etc/default/rke2-agent 为 RKE2 内置 Containerd 配置代理。


Server 节点配置代理

在 RKE2 Server 节点上创建或修改 /etc/default/rke2-server

1
2
3
4
5
cat <<EOF > /etc/default/rke2-server
CONTAINERD_HTTP_PROXY=http://172.16.16.12:7890
CONTAINERD_HTTPS_PROXY=http://172.16.16.12:7890
CONTAINERD_NO_PROXY=localhost,127.0.0.1,0.0.0.0,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,10.42.0.0/16,10.43.0.0/16,cattle-system.svc,.svc,.cluster.local,.cn,.warnerchen.com
EOF

重新加载 systemd 并重启 RKE2 Server:

1
2
systemctl daemon-reload
systemctl restart rke2-server

Agent 节点配置代理

在 RKE2 Agent 节点上创建或修改 /etc/default/rke2-agent

1
2
3
4
5
cat <<EOF > /etc/default/rke2-agent
CONTAINERD_HTTP_PROXY=http://172.16.16.12:7890
CONTAINERD_HTTPS_PROXY=http://172.16.16.12:7890
CONTAINERD_NO_PROXY=localhost,127.0.0.1,0.0.0.0,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16,10.42.0.0/16,10.43.0.0/16,cattle-system.svc,.svc,.cluster.local,.cn,.warnerchen.com
EOF

重新加载 systemd 并重启 RKE2 Agent:

1
2
systemctl daemon-reload
systemctl restart rke2-agent

验证 RKE2 Containerd 镜像拉取

RKE2 内置 Containerd 可通过 crictlctr 验证镜像拉取:

1
2
ctr -n k8s.io images pull docker.io/library/nginx:mainline
crictl pull docker.io/library/nginx:mainline

如果镜像能够正常拉取,则说明代理配置已生效。

Author

Warner Chen

Posted on

2025-02-24

Updated on

2026-06-14

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.