访问 Registry
使用本页面从集群内部、外部客户端以及通过 Image API 访问 Alauda Container Platform Registry。
前提条件
- Alauda Container Platform Registry 已安装并可在目标集群中使用。
- 镜像命名空间已存在。
- 当前用户或服务账户具有所需的命名空间权限。
- 使用 Registry 或 Image API 命令时,已安装并完成身份验证的
ac CLI。
对于 ac Image API 命令,请选择 ACP 4.4 Registry 后端:
ac config set-registry-mode modern
所需的命名空间权限
您的用户账户或工作负载服务账户必须在拥有镜像仓库的命名空间中具有权限。需要跨命名空间访问时,请请求命名空间管理员授予所需的镜像角色。
Registry 使用 ImageStream 层授权:
其他 Registry 角色(system:image-signature-writer、
system:image-registry-catalog-reader 和 system:image-registry-metrics-reader)列在
管理访问权限和清理中的完整权限表中。
有关角色绑定示例,请参阅管理访问权限和清理。
从集群内部访问 Registry
对于集群内部的工作负载,请使用内部服务地址:
image-registry.image-registry-system.svc:5000
工作负载镜像引用示例:
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
将清单保存为 my-app.yaml 并应用:
kubectl apply -f my-app.yaml
kubectl -n team-a rollout status deployment/my-app --timeout=300s
kubectl -n team-a get pods -l app=my-app
预期结果:
- Deployment 在
team-a 中创建,其 Pod 使用内部 Registry 镜像并变为就绪状态。
镜像仓库路径限定在命名空间范围内。使用 <registry-host>/<namespace>/<repository>:<tag>;
不要在命名空间后包含其他路径段。
对于自定义服务账户,请确保其在镜像命名空间中具有拉取权限。当 Registry 配置为管理服务账户拉取 Secret 时,Operator 会注入托管的拉取 Secret。
在工作负载命名空间中创建服务账户:
kubectl create serviceaccount app-puller -n team-b
请命名空间管理员在镜像命名空间中授予该服务账户拉取权限:
kubectl create rolebinding image-puller-app-puller \
--clusterrole=system:image-puller \
--serviceaccount=team-b:app-puller \
-n team-a
验证是否已注入托管的拉取 Secret:
PULL_SECRET="$(
kubectl -n team-b get serviceaccount app-puller \
-o jsonpath='{.imagePullSecrets[0].name}'
)"
test -n "$PULL_SECRET" || {
echo "The managed pull Secret has not been injected yet." >&2
exit 1
}
printf 'Managed pull Secret: %s\n' "$PULL_SECRET"
kubectl -n team-b get secret "$PULL_SECRET" \
-o jsonpath='{.data}' | wc -c
预期结果:
- 命令会输出托管拉取 Secret 名称和非零数据大小。不要打印或解码 Secret 数据。
控制器会异步刷新托管的拉取凭据。如果未列出拉取 Secret,请等待协调完成,并请求 Registry 管理员确认已启用托管服务账户拉取 Secret,且工作负载命名空间未被忽略。
在工作负载中使用该服务账户:
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
imagePullPolicy: Always
将清单保存为 my-app-app-puller.yaml 并应用:
kubectl apply -f my-app-app-puller.yaml
验证发布状态:
kubectl -n team-b rollout status deployment/my-app --timeout=300s
kubectl -n team-b get pods -l app=my-app
预期结果:
- 发布成功完成。
- Pod 已就绪且未报告镜像拉取错误。如果 Pod 处于
ImagePullBackOff,请验证 ServiceAccount 权限、注入的拉取 Secret、内部服务地址和镜像仓库路径。
验证外部 OCI 客户端
对于外部访问,请使用 ac registry login 为具有命名空间权限的 账户写入凭据:
ac registry login \
--registry registry.example.com
要将凭据写入指定的 OCI registry 身份验证文件,请使用 --to:
mkdir -p /tmp/registry-auth
ac registry login \
--registry registry.example.com \
--to /tmp/registry-auth/config.json
export REGISTRY_AUTH_FILE=/tmp/registry-auth/config.json
该命令会将 registry.example.com 的凭据写入指定的身份验证文件。将导出的 REGISTRY_AUTH_FILE 与需要 Registry 身份验证的 ac image 命令一起使用。
如果 registry 使用私有 CA,请在推送和拉取操作之前配置客户端信任。仅当端点使用 HTTP,或使用客户端不信任的非生产环境测试证书时,才使用 --insecure。
推送和拉取镜像
标记并推送镜像:
nerdctl tag my-app:latest registry.example.com/team-a/my-app:v1
nerdctl push registry.example.com/team-a/my-app:v1
按标签拉取:
nerdctl pull registry.example.com/team-a/my-app:v1
按摘要拉取:
nerdctl pull registry.example.com/team-a/my-app@sha256:<digest>
查询镜像 API 资源
列出 ImageStreams:
kubectl get imagestreams.image.alauda.io -A
ac get imagestreams -A
显示一个 ImageStream:
ac get imagestreams my-app -n team-a -o yaml
显示某个标签的当前摘要和历史摘要:
ac get imagestreamtags my-app:v1 -n team-a -o wide
显示某个摘要对应的 Image 元数据:
ac get imagestreamimages my-app@sha256:<digest> -n team-a -o yaml
通过当前的 modern registry 模式列出集群范围的 Image 资源:
ac config set-registry-mode modern
ac get images
ac get images -o yaml
对于命名空间范围的仓库视图,请查询 ImageStream 资源,而不是传递 Registry URL
覆盖:
ac get imagestreams -n team-a
ac get imagestreamtags my-app:v1 -n team-a -o wide