「DOCKER」- 搭建 Registry Mirror 服务 | Registry as a Pull Through Cache

相关资源

源码:https://github.com/distribution/distribution/
镜像:https://hub.docker.com/_/registry/
文档:https://distribution.github.io/distribution/

on Kubernetes | with Helm Chart | by twuni

Docker Hub/Official Image/Registry
twuni/docker-registry.helm
docker-registry 2.1.0 · twuni/twuni

该笔记将记录:在 Kubernetes Cluster 中,通过 Helm 部署 Docker Registry Mirror 的方法,以及相关问题的解决方案。

https://github.com/twuni/docker-registry.helm

helm repo add twuni https://helm.twun.io
helm repo update twuni

helm pull twuni/docker-registry                                               # CHART 2.1.0, APP 2.7.1
helm show values ./docker-registry-2.2.3.tgz > docker-registry-2.2.3.tgz.helm-values.yaml
... ingress: 
... persistence.storageClass: gp2
... proxy.enabled: true

helm upgrade --install --namespace registry-mirror --create-namespace          \
    docker-registry-mirror ./docker-registry-2.2.3.tgz -f docker-registry-2.2.3.tgz.helm-values.yaml

然后,测试是否生效:

# vim /etc/docker/daemon.json
{
  "registry-mirrors": ["https://registry-mirror.example.com"]
}

# docker pull hello-world

# kubectl exec -it docker-registry-mirror-79c994c65b-64hjv -- ls /var/lib/registry/docker/registry/v2/repositories/library/hello-world
_layers     _manifests  _uploads

// 如上所示,已经存在 hello-world 镜像缓存;

通过 Docker Compose 运行

Configuring a registry | Docker Documentation
harbor/Configure_mirror.md at v2.1.5 · goharbor/harbor
Registry as a pull through cache | Docker Documentation

该笔记将记录:在 Linux 中,如何搭建 Registry Mirror 服务,以及相关问题处理方法;

补充说明:

as a Cache (Mirror) It’s currently possible to mirror only one upstream registry at a time. —— 03/12/2024

Registry as a pull through cache | CNCF Distribution

在部署前,我们通读配置选项(参考 Configuring a registry 文档),以决定哪些选项该如何配置。我们将简单的记录及常见配置方法。

能够使用环境变量指定配置参数,采用 REGISTRY_variable 格式,下划线(_)表示缩进深度:

storage:
  filesystem:
    rootdirectory: /var/lib/registry

REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY=/somewhere

// 这两种写法是等价的;

覆盖默认配置文件:

# docker run -d -p 5000:5000 --restart=always              \
    --name registry                                        \
    -v `pwd`/config.yml:/etc/docker/registry/config.yml    \
    registry:2

第一步、创建配置文件

在阅读官方文档之后,我们使用的配置:

cat > ./config.yml <<EOF
# 版本号,必选参数
version: 0.1

# 日志相关配置
log:
  accesslog:
    disabled: false
  level: debug
  formatter: text
  fields:
    service: registry-mirror
    environment: production

# 镜像存储配置
storage:
  filesystem:
    rootdirectory: /registry/storage/

# 监听设置
http:
    addr: 0.0.0.0:5000
    secret: <a-secret-for-local-development>
    # 我们将使用 Nginx 提供 HTTPS 入口,然后反向代理到 Registry Mirror 服务
    # 因此,这里不会再配置 TLS 证书信息;
    # tls:
    #   certificate: /path/to/x509/public
    #   key: /path/to/x509/private

# 镜像仓库地址
# 当配置 proxy 属性后,Registry 自动会以 Mirror 的形式运行;
proxy:
  remoteurl: https://registry-1.docker.io
  username: your-dockerhub-account
  password: your-dockerhub-password
EOF

第二步、运行测试服务

docker run -d -p 5000:5000 --name registry   \
    -v $(realpath ./config.yml):/etc/docker/registry/config.yml \
    -v $(realpath ./storage/):/registry/storage/ \
    registry:2

docker container logs -f registry

docker container stop registry

docker container rm registry

第三步、使用 Docker Compose 运行

如下是 Docker Compose 编排文件:

version: '3'

services:
  registry-mirror:
    image: "registry:2"
    container_name: "registry-mirror"
    hostname: "registry-mirror"
    ports:
      - 65000:5000
    restart: always
    # 运行命令、及其环境信息
    volumes:
      - /etc/localtime:/etc/localtime
      - /path/to/data:/registry/storage/
      - /path/to/config.yml:/etc/docker/registry/config.yml
    # entrypoint: /bin/entrypoint
    # command: "ls"
    environment:
    - REGISTRY_PROXY_REMOTEURL=https://hub.docker.com
    - REGISTRY_PROXY_USERNAME=xxxxxxx
    - REGISTRY_PROXY_PASSWORD=xxxxxxx

第四步、配置 Nginx 代理

在上面的配置中,我们没有为 Registry Mirror 启动 HTTPS 监听。我们将通过 Nginx 方向代理的方式来提供 HTTPS 访问。这主要是因为 Let’s Encrypt 证书的维护问题,nginx reload 比 restart Registry Mirror 更容易;

第五步、配置 Docker 服务

配置本地 Docker 服务,以使用自建的 Registry Mirror 服务;

connection reset by peer

问题描述:

level=warning msg="Error getting v2 registry: Get http://127.0.0.1:5000/v2/: read tcp 127.0.0.1:53612->127.0.0.1:5000: read: connection reset by peer"
level=info msg="Attempting next endpoint for pull after error: Get http://127.0.0.1:5000/v2/: read tcp 127.0.0.1:53612->127.0.0.1:5000: read: connection reset by peer"

原因分析:这是因为我们本地启动 HTTP 代理,导致镜像拉取失败;

解决方案:去掉本地 HTTP 代理,然后重新测试服务访问即可;