Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[咨询]能否用代理地址拉取后Rename成原来镜像地址 #1

Open
lakevilladom opened this issue Jan 25, 2024 · 6 comments
Open

Comments

@lakevilladom
Copy link

场景:今天用Dockerfile构建了本地镜像(未推到Hub),拉取策略:IfNotPresent。本想直接引用本地镜像,但是pod一直报拉取失败,后来才发现镜像直接替换成了docker.ketches.cn/xxx.最后按照这个名称构建镜像后OK了

问题:代理思路能否实现成用代理地址拉取,拉取到本地后重新修改成原地址。

@reckless-huang
Copy link

reckless-huang commented Jan 25, 2024 via email

@lakevilladom
Copy link
Author

本地构建了镜像:docker.com/library/xxx:20240125,但是没有推到仓库里,然后pod的yaml 引用了这个镜像,拉取策略是IfNotPresent,这时候理论上本地有这个镜像,就不会再拉取,pod能正常启动,但是结果是镜像没正常启动,最后发现是Pod模板里面的镜像变成了docker.ketches.com/library/xxx:20240125。这个镜像由于没推到仓库,就拉取不到

我主要是碰到这个问题的时候我才看我们这个项目实现原理, 把yaml中的镜像替换为代理镜像,这样总感觉是修改了yaml文件的,为什么实现原理不是:用代理地址拉取到本地后修改为原来地址

@reckless-huang
Copy link

@poneding @lakevilladom 很高兴能享受到第一个issue的喜悦,对于这个问题而言,如果需要把镜像拉下来重命名是超出了apiserver所能扩展的能力的,对于本地是否存在这个镜像apiserver也无法直接判断,这属于CRI部分。但是在这个示例中是不是没有正确配置需要代理的目标,一般而言我们只需要代理gcr等无法在国内正确访问的仓库。这是我个人的一些想法,等待作者给您一个完整的答复

@poneding
Copy link
Member

poneding commented Jan 26, 2024

@lakevilladom @reckless-huang

  1. 因为 pod 的调度到节点具有随机性,所以即使拉取策略是 IfNotPresent,也不可能保证调度的节点本地已经存在这个镜像了;
  2. 用代理地址拉取到本地后修改为原来地址,那么思路就跟现在的项目不一样了。可能会需要一个具有较大权限的 DeamonSet 运行在各个节点上,监听 pod 调度节点后,拉取镜像并 retag,这个过程与使用的 cri 有关,并且只有 IfNotPresent 的拉取策略才有效。

如果想让 Pod 直接使用本地的镜像(拉取策略 IfNotPresent),当前的解决方法:

  1. 修改配置 :将 dockerhub 的代理删除;
  2. 修改配置:排除 pod 的命名空间;
  3. 临时停用代理(全局),等待目标 pod 使用本地镜像运行起来之后再重新启用;
  4. 你可以将本地镜像命名为一个非代理的地址,例如:registry.me/xxx:20240125

可能我后面还会再思考思考你这种场景的解决方法,谢谢~

@lakevilladom
Copy link
Author

lakevilladom commented Jan 26, 2024

@poneding 感谢你的回复!
其实我的这个场景不是重点,我也解决了。
主要我个人觉得,这种修改镜像地址前缀的方法,可以理解为是间接修改了yaml文件,我本地的yaml是这样,但是部署到环境的yaml是那样,就让人感觉到有一丢丢的不可控[笑脸],这对没有提前了解项目原理的人,有时候容易出现不可预期的结果(我其实就是从上面提到的场景才发现镜像地址前缀变了,然后才看的项目原理)

总之感谢这个项目,让我不再为拉取镜像发愁,还顺便参考学习了cert-manager,
附上为了加速镜像到处搜集的代理地址([心酸]:

sudo mkdir -p /etc/containerd/certs.d/docker.io
sudo sh -c 'cat >/etc/containerd/certs.d/docker.io/hosts.toml << EOF
server = "https://docker.io"
[host."https://dockerproxy.com"]
[host."https://mirror.baidubce.com"]
[host."https://docker.m.daocloud.io"]
[host."https://docker.nju.edu.cn"]
[host."https://docker.mirrors.sjtug.sjtu.edu.cn"]
[host."https://xxx.mirror.aliyuncs.com"]
capabilities = ["pull", "resolve"]
EOF'

sudo mkdir -p /etc/containerd/certs.d/gcr.io
sudo sh -c 'cat > /etc/containerd/certs.d/gcr.io/hosts.toml << EOF
server = "https://gcr.io"
[host."https://gcr.mirrors.ustc.edu.cn"]
[host."https://gcr.m.daocloud.io"]
[host."https://gcr.lank8s.cn"]
capabilities = ["pull", "resolve"]
EOF'

sudo mkdir -p /etc/containerd/certs.d/ghcr.io
sudo sh -c 'cat > /etc/containerd/certs.d/ghcr.io/hosts.toml << EOF
server = "https://ghcr.io"
[host."https://ghcr.lank8s.cn"]
capabilities = ["pull", "resolve"]
EOF'

sudo mkdir -p /etc/containerd/certs.d/k8s.gcr.io
sudo sh -c 'cat > /etc/containerd/certs.d/k8s.gcr.io/hosts.toml << EOF
server = "https://k8s.gcr.io"
[host."https://gcr.mirrors.ustc.edu.cn/google-containers/"]
[host."https://k8s-gcr.m.daocloud.io/"]
[host."https://registry.aliyuncs.com/k8sxio"]
[host."https://lank8s.cn/"]
[host."https://k8s.lank8s.cn"]
capabilities = ["pull", "resolve"]
EOF'

sudo mkdir -p /etc/containerd/certs.d/quay.io
sudo sh -c 'cat > /etc/containerd/certs.d/quay.io/hosts.toml << EOF
server = "https://quay.io"
[host."https://quay-mirror.qiniu.com"]
[host."https://quay.m.daocloud.io"]
[host."https://quay.mirrors.ustc.edu.cn"]
[host."https://quay.tencentcloudcr.com"]
capabilities = ["pull", "resolve"]
EOF'

sudo mkdir -p /etc/containerd/certs.d/registry.k8s.io
sudo sh -c 'cat > /etc/containerd/certs.d/registry.k8s.io/hosts.toml << EOF
server = "https://registry.k8s.io"
[host."https://lank8s.cn/"]
[host."https://registry.lank8s.cn/"]
[host."https://k8s.m.daocloud.io/"]
capabilities = ["pull", "resolve"]
EOF'

sudo mkdir -p /etc/containerd/certs.d/ghcr.io
sudo sh -c 'cat >/etc/containerd/certs.d/ghcr.io/hosts.toml << EOF
server = "https://ghcr.io"
[host."https://ghcr.lank8s.cn/"]
pabilities = ["pull", "resolve"]
EOF'

@poneding
Copy link
Member

这个只会修改pod的声明镜像,但并不会修改pod控制器(Deployment,Statefulset)中声明的镜像,所以我感觉这个还可以接受的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants