使用 Docker Registry 自托管一个缓存镜像源

部署

由于是在一个 1C1G 的机器上部署,就不用 K3s 了,直接用 Docker Compose 拉起一个实例。官方文档有提供 docker-compose.yml,这里也给出一份示例:

version: '3'

services:
  registry-docker:
    image: registry:2
    restart: unless-stopped
    environment:
      REGISTRY_PROXY_REMOTEURL: https://registry-1.docker.io
      REGISTRY_PROXY_USERNAME: username
      REGISTRY_PROXY_PASSWORD: dckr_pat_value
      # REGISTRY_AUTH: htpasswd
      # REGISTRY_AUTH_HTPASSWD_PATH: /auth/htpasswd
      # REGISTRY_AUTH_HTPASSWD_REALM: Registry Realm
      REGISTRY_STORAGE_DELETE_ENABLED: "true"
    ports:
      - 127.0.0.1:50000:5000
    volumes:
      - ./data/auth:/auth
      - ./data/docker:/var/lib/registry

这里的账密可以提高拉取限频,但是根据官方文档,使用镜像的用户就可以读到该账号下的私有镜像了,最好是一个独立的账号。

服务拉起来之后,Nginx 反代一下就可以使用了。

拉取

有两种方式可以使用,首先是最普遍且最优的做法,在 /etc/docker/daemon.json 中加入 registry-mirrors 选项。例如:

{
  "registry-mirrors": [
    "https://<docker-registry-host>"
  ]
}

然后重启 Docker Engine 即可。

不过此种方式不支持 HTTP Basic 身份验证,这也就意味着无法通过事先 docker login 的方式来拉取私有的镜像源。目前各镜像站通常的做法是使用泛域名,在域名中提供唯一身份信息,显然这种方式并不安全,只是对当下现状的妥协。

这种情况应该在下一个版本的 Docker 中应该可以得到解决,在即将发布的 24.08/25.0 版本中,我们可以在 [24.0 backport] registry: allow mirror path prefix in config by thaJeztah · Pull Request #46944 · moby/moby (github.com) 中看到,可以支持填带路径的镜像源了,这就意味着可以在路径上实现类似的认证,因为 HTTPS 中路径是受保护的,这样的话就不会有问题了。

另一种方式是手动拉取,这需要以 docker pull <docker-registry-host>/library/nginx:stable 的格式进行拉取。这种方式支持 HTTP Basic 认证。

认证

Docker Registry 上支持认证,可以参照上面的 docker-compose.yml 处理;也可以在反代上面认证,其官方文档给出了 Apache/Nginx 上面的配置示例,具体请参考 Authenticate proxy with nginx | CNCF Distribution

引用

Registry as a pull through cache | Docker Docs

Docker daemon won’t start if a registry-mirror is configured in /etc/docker/daemon.json · Issue #36598 · moby/moby (github.com)

registry: allow mirror path prefix in config · thaJeztah/docker@3d8f7d0 (github.com)

Pull-through cache is not working for private registry: Challenge established with upstream · Issue #3081 · distribution/distribution (github.com)

Implement Basic authentication to upstream registry in proxy mode by ArthurMelin · Pull Request #3256 · distribution/distribution (github.com)

Private Docker registry in pull through cache mode return “invalid authorization credential” – Stack Overflow

Private docker registry behind a subpath : r/docker (reddit.com)

Private Docker registry in pull through cache mode return “invalid authorization credential” – Stack Overflow

Lens 安装 kube-state-metrics 超时

使用 Lens 安装 kube-state-metrics 时发现,镜像是从 k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0 拉取的,国内的 TKE 集群拉取会超时。可以直接修改 Deployment 的镜像为:ccr.ccs.tencentyun.com/tkeimages/kube-state-metrics:v2.0.0。

也可以从 k8s-gcrio.azureedge.net/kube-state-metrics/kube-state-metrics:v2.0.0 拉取。

如果只有单节点而且不想修改 Deployment 的话,可以在对应机器上拉取国内镜像,然后使用对应运行时的镜像工具修改镜像 Tag。以 containerd 为例:

crictl pull ccr.ccs.tencentyun.com/tkeimages/kube-state-metrics:v2.0.0
ctr -n k8s.io image tag ccr.ccs.tencentyun.com/tkeimages/kube-state-metrics:v2.0.0 k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0

修改 Kubernetes 默认 StorageClass

今天部署一个应用的时候卡在新建 PVC,看到报错:

Internal error occurred: 2 default StorageClasses were found

编辑那个现在是默认,但是实际上不需要是默认的 sc,把:

storageclass.kubernetes.io/is-default-class

注解改为 false,或直接删除。

引用

https://kubernetes.io/docs/tasks/administer-cluster/change-default-storage-class/

增加 Kubernetes CoreDNS Hosts 以自定义解析结果

编辑 kube-system 中的 coredns 这个 ConfigMap,可以:

kubectl edit configmap coredns -n kube-system

在 Corefile 的:

.:53 {
  ... ...
}

添加:

hosts {
  127.0.0.1 localhost
  fallthrough
}

如果需要对单个 Pod 级别生效,可以设置 hostAliases。见:

https://kubernetes.io/docs/tasks/network/customize-hosts-file-for-pods/#adding-additional-entries-with-hostaliases

引用

https://stackoverflow.com/a/65283959
https://coredns.io/plugins/hosts/

我还发现了什么好玩的

由于不知道 Corefile 语法高亮应该用啥,于是看到了:

https://coredns.io/2017/07/23/corefile-explained/