暴露 Registry
默认情况下,对集群内的工作负载使用内部服务。仅当开发人员计算机、CI 系统或其他外部客户端必须推送或拉取镜像时,才暴露 Alauda Container
Platform Registry。
前提条件
- Registry 已安装且可用。
- 你有权限更新
Config/cluster,并在 image-registry-system 中创建或引用 TLS Secret。
- 集群中有一个 Ingress controller,可以将流量路由到
image-registry-system。
- 对于自定义主机,请准备 DNS 记录和 TLS 证书,其 Subject Alternative Name 与 Registry 主机名匹配。
暴露默认 Registry
在 Config/cluster 中启用默认外部端点:
kubectl patch configs.imageregistry.operator.alauda.io cluster \
--type=merge \
-p '{"spec":{"defaultRoute":true}}'
Operator 会在 image-registry-system 中呈现一个名为 default-route 的 Kubernetes Ingress。
验证生成的 Ingress 和 Registry 配置:
kubectl get configs.imageregistry.operator.alauda.io cluster -o yaml
kubectl -n image-registry-system get ingress default-route
获取生成的主机:
REGISTRY_RULE_HOST="$(
kubectl -n image-registry-system get ingress default-route \
-o jsonpath='{.spec.rules[0].host}'
)"
REGISTRY_STATUS_ADDRESS="$(
kubectl -n image-registry-system get ingress default-route \
-o jsonpath='{.status.loadBalancer.ingress[0].hostname}'
)"
if [ -z "$REGISTRY_STATUS_ADDRESS" ]; then
REGISTRY_STATUS_ADDRESS="$(
kubectl -n image-registry-system get ingress default-route \
-o jsonpath='{.status.loadBalancer.ingress[0].ip}'
)"
fi
printf 'Ingress rule host: %s\n' "$REGISTRY_RULE_HOST"
printf 'Ingress status address: %s\n' "$REGISTRY_STATUS_ADDRESS"
预期结果:
Config/cluster.spec.defaultRoute 为 true。
default-route Ingress 存在于 image-registry-system 中。
- 非空的
spec.rules[].host 是可用的 Registry 主机名。规则主机为空表示该 Ingress 是 catch-all 规则;状态地址标识 Ingress controller 端点,不会自动成为 Registry DNS 名称。
如果默认 Ingress 没有可用的主机名,请改为配置自定义安全主机。不要将空规则主机用作 --registry 的值。
有可用主机后,将其分配给 REGISTRY_HOST。如果客户端使用私有 CA,请在运行 TLS 检查前配置该 CA。在登录前,必须使用客户端的常规信任设置成功通过检查:
REGISTRY_HOST="$REGISTRY_RULE_HOST"
test -n "$REGISTRY_HOST" || {
echo "No usable Registry hostname was found; configure a custom secure host." >&2
exit 1
}
openssl s_client \
-connect "$REGISTRY_HOST:443" \
-servername "$REGISTRY_HOST" \
-verify_return_error </dev/null
为生成的主机写入凭据:
ac registry login \
--registry "$REGISTRY_HOST"
暴露自定义安全 Registry 主机
在 image-registry-system 中创建或提供 TLS Secret,然后配置 Config.spec.routes[]。
使用 Subject Alternative Name 与自定义主机名匹配的证书创建 Secret:
kubectl -n image-registry-system create secret tls registry-tls \
--cert=/path/to/registry.example.com.crt \
--key=/path/to/registry.example.com.key
当设置了 secretName 时,TLS Secret 必须存在于 image-registry-system 中。为 registry.example.com 创建 DNS 记录,使其解析到 Ingress controller。
生成的 Ingress 使用 Registry service 作为后端,并将后端协议设置为 HTTPS。
使用自定义路由修补 Config/cluster。使用合并修补,以免删除其他 Config/cluster.spec 字段,例如存储和 pull-secret 设置。此修补中的 routes 数组会替换当前的路由数组,因此请包含所有必须保留配置的自定义路由:
kubectl patch configs.imageregistry.operator.alauda.io cluster \
--type=merge \
-p '{
"spec": {
"managementState": "Managed",
"defaultRoute": false,
"routes": [
{
"name": "public-registry",
"hostname": "registry.example.com",
"secretName": "registry-tls"
}
]
}
}'
验证配置和生成的 Ingress:
kubectl get configs.imageregistry.operator.alauda.io cluster -o yaml
kubectl -n image-registry-system get ingress public-registry
kubectl -n image-registry-system describe ingress public-registry
预期结果:
Config/cluster.spec.routes[] 包含 public-registry。
public-registry Ingress 使用 registry.example.com。
- Ingress 引用了
registry-tls Secret。
按照配置客户端信任中的说明配置客户端信任,然后验证 TLS 信任并登录:
openssl s_client \
-connect registry.example.com:443 \
-servername registry.example.com \
-verify_return_error </dev/null
ac registry login \
--registry registry.example.com
如果 Ingress 证书由私有 CA 签名,请在登录以及推送或拉取操作之前,将 CA 添加到每个外部客户端的信任存储中。
对于使用不安全 Registry 选项的测试客户端,请仅将不安全设置限定到 Registry 主机:
ac registry login \
--registry registry.example.com \
--insecure
如果 OCI 客户端还连接到 HTTP 端点或使用不受信任的证书,请单独配置该客户端的信任设置或不安全 Registry 设置。
验证外部访问
检查 Ingress:
kubectl -n image-registry-system get ingress
kubectl -n image-registry-system describe ingress public-registry
从外部客户端写入凭据:
ac registry login \
--registry registry.example.com
推送和拉取测试镜像:
nerdctl tag my-app:latest registry.example.com/team-a/my-app:external-test
nerdctl push registry.example.com/team-a/my-app:external-test
nerdctl pull registry.example.com/team-a/my-app:external-test
故障排除