大家好,今天给大家介绍一个开源项目:registry-proxy。
目前支持的境外镜像仓库:
PS:感谢 Docker Proxy 提供的镜像代理服务,本项目才能得以实现。💕
使用 Mutating Webhook 准入控制器实现。当集群中 Pod 创建时,Mutating Webhook 的工作流程如下:
如果集群中已经安装了 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
官方文档:Install cert-manager。
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 代理镜像。
通过 ConfigMap 修改默认配置,修改会实时生效。
示例:限定代理命名空间 default、dev 和 staging 中 docker.io 的镜像。
kubectl apply -f - <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: registry-proxy-config
namespace: registry-proxy
data:
config.yaml: |
excludeNamespaces:
- kube-system
- kube-public
- kube-node-lease
includeNamespaces:
- default
- dev
- staging
includeRegistries:
- "docker.io"
EOF
kubectl apply -f https://raw.githubusercontent.com/ketches/registry-proxy/master/examples/dockerhub-nginx.yaml
# 代理地址
kubectl apply -f https://ghproxy.com/https://raw.githubusercontent.com/ketches/registry-proxy/master/examples/dockerhub-nginx.yaml
示例中的 Pod 镜像为 nginx:latest
,经过 registry-proxy 处理后,容器镜像变为 dockerproxy.com/library/nginx:latest
。
kubectl get pod dockerhub-nginx -o=jsonpath='{.spec.containers[*].image}'
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
kubectl delete -f https://raw.githubusercontent.com/ketches/registry-proxy/master/examples/dockerhub-nginx.yaml
# 代理地址
kubectl delete -f https://ghproxy.com/https://raw.githubusercontent.com/ketches/registry-proxy/master/examples/dockerhub-nginx.yaml
欢迎大家体验或参与该项目。
作者:dp 出处:https://juejin.cn/post/7292948455549698060