k8s部署镜像自动代理服务,帮您在 K8S 中拉取gcr等镜像

0
(0)

是一个开源项目:registry-proxy,帮助您在 Kubernetes 集群中愉快地拉取国外容器镜像(仅限公有镜像)。 官网: https://ketches.cn/registry-proxy/

目前支持的境外镜像仓库:

实现原理

使用 Mutating Webhook 准入控制器实现。当集群中 Pod 创建时,Mutating Webhook 的工作流程如下:

  1. 判断 Pod 是否属于排除的命名空间,如果是,结束流程;
  2. 判断 Pod 是否属于包含的命名空间,如果不是,结束流程;
  3. 依次判断 Pod 中的容器镜像是否属于包含的镜像仓库,如果是,替换为 Docker Proxy 代理镜像;

快速安装

安装 cert-manager

如果集群中已经安装了 cert-manager,可以跳过这一步。

这里提供快速安装的方式:

kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.13.0/cert-manager.yaml

# 代理地址
kubectl apply -f https://ghproxy.com/https://github.com/cert-manager/cert-manager/releases/download/v1.13.0/cert-manager.yaml

安装 registry-proxy

kubectl apply -f https://raw.githubusercontent.com/ketches/registry-proxy/master/deploy/manifests.yaml

# 代理地址
kubectl apply -f https://ghproxy.com/https://raw.githubusercontent.com/ketches/registry-proxy/master/deploy/manifests.yaml

配置

三个配置参数,以下给出默认配置,只有在命名空间范围内的 Pod,且 Pod 镜像在 Registry 范围内,容器镜像才会修改为 Docker Proxy 代理镜像。

  1. excludeNamespaces:[“kube-system”, “kube-public”, “kube-node-lease”]
  2. includeNamespaces: [“*”]
  3. includeRegistries: [“http://docker.io“, “http://ghcr.io“, “http://gcr.io“, “http://k8s.gcr.io“, “http://registry.k8s.io“, “http://quay.io“, “http://mcr.microsoft.com“]

通过 ConfigMap 修改默认配置,修改会实时生效。

ConfigMap: registry-proxy-config 创建好后的例子:

enabled: true
proxies:
    docker.cloudsmith.io: cloudsmith.ketches.cn
    #docker.io: docker.ketches.cn
    gcr.io: gcr.ketches.cn
    ghcr.io: ghcr.ketches.cn
    k8s.gcr.io: k8s-gcr.ketches.cn
    quay.io: quay.ketches.cn
    registry.k8s.io: k8s.ketches.cn
excludeNamespaces:
    - kube-system
    - kube-public
    - kube-node-lease
    - registry-proxy
includeNamespaces:
    - '*'

卸载&清理

卸载 registry-proxy

kubectl delete -f https://raw.githubusercontent.com/ketches/registry-proxy/master/deploy/manifests.yaml

# 代理地址
kubectl delete -f https://ghproxy.com/https://raw.githubusercontent.com/ketches/registry-proxy/master/deploy/manifests.yaml

这篇文章有用吗?

平均评分 0 / 5. 投票数: 0

到目前为止还没有投票!成为第一位评论此文章。

很抱歉,这篇文章对您没有用!

让我们改善这篇文章!

告诉我们我们如何改善这篇文章?

分类: