Registry v2:访问 Registry

使用此页面可从集群内部、外部客户端以及通过 Image API 访问 Registry v2

前提条件

  • Registry v2 已安装并在目标集群中可用。
  • image 命名空间已存在。
  • 当前用户或 service account 具有所需的命名空间权限。

所需的命名空间权限

你的用户账户或工作负载 service account 必须在拥有 image repository 的命名空间中具有权限。当你需要跨命名空间访问时,请请求命名空间管理员授予所需的 image 角色。

Registry v2 使用 ImageStream layer authorization:

操作常见角色Image API 权限
拉取system:image-pullerimage.alauda.io imagestreams/layers get
推送system:image-pusherimage.alauda.io imagestreams/layers update
删除system:image-deleter目标 image metadata 的 Image API 删除权限

有关 role binding 示例,请参见 管理 Registry v2 访问和清理

从集群内部访问 Registry

对集群内的工作负载使用内部服务地址:

image-registry.image-registry-system.svc:5000

示例工作负载 image 引用:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  namespace: team-a
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      serviceAccountName: default
      containers:
        - name: app
          image: image-registry.image-registry-system.svc:5000/team-a/my-app:v1

对于自定义 service account,请确保其在 image 命名空间中具有拉取权限。当 Registry v2 配置为管理 service account pull secret 时,Operator 会注入受管的 pull secret。

在工作负载命名空间中创建 service account:

kubectl create serviceaccount app-puller -n team-b

请让命名空间管理员在 image 命名空间中授予该 service account 拉取权限:

kubectl create rolebinding image-puller-app-puller \
  --clusterrole=system:image-puller \
  --serviceaccount=team-b:app-puller \
  -n team-a

验证是否已注入受管的 pull secret:

kubectl -n team-b get serviceaccount app-puller \
  -o jsonpath='{.imagePullSecrets[*].name}{"\n"}'

预期结果:

  • 该命令至少返回一个 image pull secret 名称。

如果未列出任何 pull secret,请联系 Registry 管理员,确认已启用受管的 service account pull secret,且工作负载命名空间未被忽略。

在工作负载中使用该 service account:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  namespace: team-b
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      serviceAccountName: app-puller
      containers:
        - name: app
          image: image-registry.image-registry-system.svc:5000/team-a/my-app:v1

验证 rollout:

kubectl -n team-b rollout status deployment/my-app --timeout=300s
kubectl -n team-b get pods -l app=my-app

对外部 OCI 客户端进行身份验证

对于外部访问,使用 ac registry login 为具有命名空间权限的 账户写入凭据:

ac registry login \
  --registry registry.example.com

如需将凭据写入特定的 Docker-compatible auth 文件,请设置 DOCKER_CONFIG 或使用 --to

export DOCKER_CONFIG=/tmp/registry-auth

ac registry login \
  --registry registry.example.com \
  --to "$DOCKER_CONFIG/config.json"

如果 registry 使用私有 CA,请在执行 push 和 pull 操作前配置客户端信任。仅在 HTTP endpoint 或客户端不信任的非生产测试证书场景下使用 --insecure

推送和拉取镜像

为镜像打标签并推送:

nerdctl tag my-app:latest registry.example.com/team-a/my-app:v1
nerdctl push registry.example.com/team-a/my-app:v1

按 tag 拉取:

nerdctl pull registry.example.com/team-a/my-app:v1

按 digest 拉取:

nerdctl pull registry.example.com/team-a/my-app@sha256:<digest>

查询 Image API 资源

列出 ImageStreams:

kubectl get imagestreams.image.alauda.io -A
ac get imagestreams -A

显示一个 ImageStream:

ac get imagestreams my-app -n team-a -o yaml

显示某个 tag 的当前和历史 digest:

ac get imagestreamtags my-app:v1 -n team-a -o wide

显示某个 digest 的 Image metadata:

ac get imagestreamimages my-app@sha256:<digest> -n team-a -o yaml

列出当前用户可见的 Registry HTTP image repositories:

ac get images --registry-url=https://registry.example.com
ac get images -n team-a --registry-url=https://registry.example.com