K3S 私有镜像仓库配置
v1.0.0 开始可用
可以配置 Containerd 连接到私有镜像仓库,并使用它们在节点上拉取私有镜像。
启动时,K3s 会检查
/etc/rancher/k3s/
中是否存在registries.yaml
文件,并指示 containerd 使用文件中定义的镜像仓库。如果你想使用一个私有的镜像仓库,那么你需要在每个使用镜像仓库的节点上以 root 身份创建这个文件。
请注意,server 节点默认是可以调度的。如果你没有在 server 节点上设置污点,那么将在它们上运行工作负载,请确保在每个 server 节点上创建
registries.yaml
文件
镜像仓库配置文件
registries.yaml 由两大部分组成:
- mirrors
- configs
Mirrors
Mirrors 是一个用于定义专用镜像仓库的名称和 endpoint 的指令,例如
mirrors:
mycustomreg.com:
endpoint:
- "https://my.harbor.com"
每个 mirror 必须有一个名称和一组 endpoint。当从镜像仓库中拉取镜像时,containerd 会逐一尝试这些 endpoint URL,并使用第一个可用的 endpoin
重写
每个镜像都可以有一组重写。重写可以根据正则表达式来改变镜像的标签。
每个镜像都可以有一组重写。重写可以根据正则表达式来改变镜像的标签。
例如,以下配置将透明地从
my.harbor.com/mirrorproject/rancher-images/coredns-coredns:1.6.3
拉取镜像docker.io/rancher/coredns-coredns:1.6.3
:
mirrors:
docker.io:
endpoint:
- "https://my.harbor.com"
rewrite:
"^rancher/(.*)": "mirrorproject/rancher-images/$1"
镜像仍将以原始名称存储,所以
crictl image ls
将显示docker.io/rancher/coredns-coredns:1.6.3
在节点上是可用的,即使镜像是以不同的名字从镜像仓库中拉取的
Configs
Configs 部分定义了每个 mirror 的 TLS 和凭证配置。对于每个 mirror,你可以定义
auth
和/或tls
tls
部分包括:
指令 | 描述 |
---|---|
cert_file | 用来与镜像仓库进行验证的客户证书路径 |
key_file | 用来验证镜像仓库的客户端密钥路径 |
ca_file | 定义用于验证镜像仓库服务器证书文件的 CA 证书路径 |
insecure_skip_verify | 定义是否应跳过镜像仓库的 TLS 验证的布尔值 |
auth
部分由用户名/密码或身份验证令牌组成:
Directive | Description |
---|---|
username | 镜像仓库身份验证的用户名 |
password | 镜像仓库身份验证的用户密码 |
auth | 镜像仓库 auth 的认证 token |
使用 TLS
下面的例子展示了当你使用 TLS 时,如何在每个节点上配置
/etc/rancher/k3s/registries.yaml
有认证
mirrors:
docker.io:
endpoint:
- "https://my.harbor.com"
configs:
"my.harbor.com":
auth:
username: xxxxxx # 这是私有镜像仓库的用户名
password: xxxxxx # 这是私有镜像仓库的密码
tls:
cert_file: # 镜像仓库中使用的cert文件的路径。
key_file: # 镜像仓库中使用的key文件的路径。
ca_file: # 镜像仓库中使用的ca文件的路径。
无认证
mirrors:
docker.io:
endpoint:
- "https://my.harbor.com"
configs:
"my.harbor.com":
tls:
cert_file: # 镜像仓库中使用的cert文件的路径。
key_file: # 镜像仓库中使用的key文件的路径。
ca_file: # 镜像仓库中使用的ca文件的路径。
不使用 TLS
下面的例子展示了当你不使用 TLS 时,如何在每个节点上配置
/etc/rancher/k3s/registries.yaml
在没有 TLS 通信的情况下,需要为 endpoints 指定
http://
,否则将默认为 https。为了使镜像仓库更改生效,需要重新启动每个节点上的 K3s
有认证
mirrors:
docker.io:
endpoint:
- "http://my.harbor.com"
configs:
"my.harbor.com":
auth:
username: xxxxxx # 这是私有镜像仓库的用户名
password: xxxxxx # 这是私有镜像仓库的密码
无认证
mirrors:
docker.io:
endpoint:
- "http://my.harbor.com"
添加镜像到私有镜像仓库
1.获取 k3s-images.txt 文件
从 GitHub 上获取你正在使用的版本的 k3s-images.txt 文件
https://github.com/k3s-io/k3s/releases
https://github.com/k3s-io/k3s/releases/download/v1.23.6%2Bk3s1/k3s-images.txt
2.拉取k3s镜像
从 docker.io 中拉取 k3s-images.txt 文件中列出的 K3s 镜像
docker pull docker.io/rancher/coredns-coredns:1.6.3
3.重打标签
# 将镜像重新标记成私有镜像仓库
docker tag coredns-coredns:1.6.3 my.harbor.com/coredns-coredns
4.推送到私有仓库
docker tag coredns-coredns:1.6.3 my.harbor.com/coredns-coredns