私有注册表配置
Containerd 可以配置为连接到私有注册表,并根据 kubelet 的需要使用它们来拉取镜像。
在启动时,RKE2 将检查 /etc/rancher/rke2/registries.yaml 是否存在。如果存在,则在生成 containerd 配置时将使用此配置文件中包含的注册表配置。
-
如果您想将私有注册表用作公共注册表(如 docker.io)的镜像,则需要在每个希望使用该镜像的节点上配置
registries.yaml。 -
如果您的私有注册表需要身份验证、使用自定义 TLS 证书或不使用 TLS,则需要在每个将从您的注册表拉取镜像的节点上配置
registries.yaml。
请注意,服务器节点默认是可调度的。如果您没有标记服务器节点并且将在其上运行工作负载,请确保在每个服务器上也创建 registries.yaml 配置文件。
默认端点回退
Containerd 对所有注册表都有一个隐式的 "默认端点"。即使在 registries.yaml 中列出了该注册表的其他端点,默认端点也始终作为最后的手段进行尝试。重写不会应用于对默认端点的拉取。例如,当拉取 registry.example.com:5000/rancher/mirrored-pause:3.6 时,containerd 将使用默认端点 https://registry.example.com:5000/v2。
-
docker.io的默认端点是https://index.docker.io/v2。 -
所有其他注册表的默认端点是
https://<REGISTRY>/v2,其中<REGISTRY>是注册表主机名和可选端口。
为了被识别为注册表,镜像名称的第一个组件必须包含至少一个句点或冒号。出于历史原因,名称中未指定注册表的镜像被隐式识别为来自 docker.io。
Nodes may be configured with the disable-default-registry-endpoint: true option. When this is set, containerd will not fall back to the default registry endpoint, and will only pull from configured mirror endpoints, along with the distributed registry if it is enabled.
如果您的集群处于真正的隔离环境中,上游注册表不可用,或者您希望只有某些节点从上游注册表拉取,则可能需要这样做。
禁用默认注册表端点仅适用于通过 registries.yaml 配置的注册表。如果注册表未通过 registries.yaml 中的镜像条目显式配置,则仍将使用默认回退行为。
注册表配置文件
该文件由两个顶级键组成,每个注册表都有子键:
mirrors:
<REGISTRY>:
endpoint:
- https://<REGISTRY>/v2
configs:
<REGISTRY>:
auth:
username: <BASIC AUTH USERNAME>
password: <BASIC AUTH PASSWORD>
token: <BEARER TOKEN>
tls:
ca_file: <PATH TO SERVER CA>
cert_file: <PATH TO CLIENT CERT>
key_file: <PATH TO CLIENT KEY>
insecure_skip_verify: <SKIP TLS CERT VERIFICATION BOOLEAN>
镜像
镜像部分定义了注册表的名称和端点,例如:
mirrors:
registry.example.com:
endpoint:
- "https://registry.example.com:5000"
每个镜像必须有一个名称和一组端点。从注册表拉取镜像时,containerd 将尝试这些端点,以及默认端点,并使用第一个可用的端点。
|
如果未配置端点,containerd 假定注册表可以通过 HTTPS 在 443 端口匿名访问,并且使用的是主机操作系统信任的证书。有关更多信息,您可以 查阅 containerd 文档。 |
重定向
如果私有注册表用作另一个注册表的镜像,例如在配置 拉取缓存 时,镜像拉取操作会被透明地重定向到列出的端点。原始注册表名称通过 ns 查询参数传递给镜像端点。
例如,如果您为 docker.io 配置了一个镜像:
mirrors:
docker.io:
endpoint:
- "https://registry.example.com:5000"
那么拉取 docker.io/rancher/mirrored-pause:3.6 将透明地拉取镜像作为 registry.example.com:5000/rancher/mirrored-pause:3.6。
重写
每个镜像端点可以配置一组重写规则,这些规则在镜像从该端点拉取时,使用正则表达式匹配并转换镜像名称。如果私有注册表中的组织/项目结构与其镜像的注册表不同,这很有用。重写仅匹配和转换镜像名称,而不匹配标签。
例如,以下配置会将镜像 rancher/rke2-runtime:v1.30.1-rke2r1 从 registry.example.com:5000/mirrorproject/rancher-images/rke2-runtime:v1.30.1-rke2r1 处透明地拉取:
mirrors:
docker.io:
endpoint:
- "https://registry.example.com:5000"
rewrite:
"^rancher/(.*)": "mirrorproject/rancher-images/$1"
If you want to apply rewrites when pulling directly from a registry - when it is not being used as a mirror for a different upstream registry - you must provide a mirror endpoint that does not match the default endpoint. Mirror endpoints in registries.yaml that match the default endpoint are ignored; the default endpoint is always tried last with no rewrites, if fallback has not been disabled.
例如,如果您在 https://registry.example.com/ 处有一个注册表,并希望在明确拉取 registry.example.com/rancher/rke2-runtime:v1.30.1-rke2r1 时应用重写,您可以添加一个带有指定端口的镜像端点。由于镜像端点与默认端点不匹配 - "https://registry.example.com:443/v2" != "https://registry.example.com/v2" - 因此该端点被视为镜像,并会应用重写规则,尽管它实际上与默认端点相同。
mirrors:
registry.example.com
endpoint:
- "https://registry.example.com:443"
rewrite:
"^rancher/(.*)": "mirrorproject/rancher-images/$1"
请注意,在使用镜像和重写时,图像仍将以原始名称存储。例如,即使镜像是从名称不同的镜像端点拉取的,crictl image ls 也会显示 docker.io/rancher/rke2-runtime:v1.30.1-rke2r1 在节点上可用。
配置
configs 部分定义了每个镜像端点的 TLS 和凭证配置。对于每个镜像,您可以定义 auth 和/或 tls。
tls 部分包括:
| 指令 | 说明 |
|---|---|
|
将用于与注册表进行身份验证的客户端证书路径 |
|
将用于与注册表进行身份验证的客户端密钥路径 |
|
定义用于验证注册表服务器证书文件的 CA 证书路径 |
|
布尔值,定义是否应跳过注册表的 TLS 验证 |
auth 部分由用户名/密码或身份验证令牌组成:
| 指令 | 说明 |
|---|---|
|
私有注册表基本身份验证的用户名 |
|
私有注册表基本身份验证的用户密码 |
|
私有注册表基本身份验证的身份验证令牌 |
以下是使用私有注册表的不同模式的基本示例:
通配符支持
The "*" wildcard entry can be used in the mirrors and configs sections to provide default configuration for all registries. The default configuration will only be used if there is no specific entry for that registry. Note that the asterisk MUST be quoted.
在以下示例中,将为所有注册表使用本地注册表镜像。对于所有注册表,TLS 验证将被禁用,除了 docker.io。
mirrors:
"*":
endpoint:
- "https://registry.example.com:5000"
configs:
"docker.io":
"*":
tls:
insecure_skip_verify: true
使用 TLS
以下是当使用 TLS 时,您如何在每个节点上配置 /etc/rancher/rke2/registries.yaml 的示例。
-
使用身份验证
-
不使用身份验证
mirrors:
docker.io:
endpoint:
- "https://registry.example.com:5000"
configs:
"registry.example.com:5000":
auth:
username: xxxxxx # this is the registry username
password: xxxxxx # this is the registry password
tls:
cert_file: # path to the cert file used to authenticate to the registry
key_file: # path to the key file for the certificate used to authenticate to the registry
ca_file: # path to the ca file used to verify the registry's certificate
insecure_skip_verify: # may be set to true to skip verifying the registry's certificate
mirrors:
docker.io:
endpoint:
- "https://registry.example.com:5000"
configs:
"registry.example.com:5000":
tls:
cert_file: # path to the cert file used to authenticate to the registry
key_file: # path to the key file for the certificate used to authenticate to the registry
ca_file: # path to the ca file used to verify the registry's certificate
insecure_skip_verify: # may be set to true to skip verifying the registry's certificate
不使用 TLS
以下是当 不 使用 TLS 时,您如何在每个节点上配置 /etc/rancher/rke2/registries.yaml 的示例。
-
使用身份验证
-
不使用身份验证
mirrors:
docker.io:
endpoint:
- "http://registry.example.com:5000"
configs:
"registry.example.com:5000":
auth:
username: xxxxxx # this is the registry username
password: xxxxxx # this is the registry password
mirrors:
docker.io:
endpoint:
- "http://registry.example.com:5000"
如果使用不带 TLS 的明文 HTTP 的注册表,您需要将
http://指定为端点 URI 方案,否则将默认为https://。
为了使注册表更改生效,您需要在节点上启动 RKE2 之前配置此文件,或者在每个配置的节点上重新启动 RKE2。