配置 containerd 镜像仓库完全攻略


更多奇技淫巧欢迎订阅博客:https://fuckcloudnative.io

前言

Kubernetes 在 Changelog 中宣布自 ????Kubernetes 1.20 之后将弃用 Docker 作为容器运行时之后,containerd 成为下一个容器运行时的热门选项。虽然 containerd 很早就已经是 Docker 的一部分,但是纯粹使用 containerd 还是给大家带来了诸多困扰,本文将介绍如何使用 containerd 配置镜像仓库和加速器。

本文将以 K3s 为例对 containerd 进行配置,如果您的环境未使用 K3s 而是使用的 Kubernetes,你也可以参考本文来配置 containerd 的镜像仓库,因为 containerd 的配置是通用的。

关于 K3s 和 containerd

K3s 是一个轻量级 Kubernetes 发行版,二进制大小小于 100MB,所需内存不到 Kubernetes 的一半。K3s 为了降低资源消耗,将默认的 runtime 修改为 containerd,同时也内置了 Kubernetes CLI 工具 crictl 和 ctr。

K3s 默认的 containerd 配置文件目录为 /var/lib/rancher/k3s/agent/etc/containerd/config.toml,但直接操作 containerd 的配置文件去设置镜像仓库或加速器相比于操作 docker 要复杂许多。K3s 为了简化配置 containerd 镜像仓库的复杂度,K3s 会在启动时检查/etc/rancher/k3s/中是否存在 registries.yaml 文件,如果存在该文件,就会根据 registries.yaml 的内容转换为 containerd 的配置并存储到 /var/lib/rancher/k3s/agent/etc/containerd/config.toml,从而降低了配置 containerd 镜像仓库的复杂度。

使用 K3s 配置私有镜像仓库

K3s 镜像仓库配置文件由两大部分组成:mirrors 和 configs:

  • Mirrors 是一个用于定义专用镜像仓库的名称和 endpoint 的指令

  • Configs 部分定义了每个 mirror 的 TLS 和证书配置。对于每个 mirror,你可以定义 auth 和/或 tls

containerd 使用了类似 K8S 中 svc 与 endpoint 的概念,svc 可以理解为访问名称,这个名称会解析到对应的 endpoint 上。也可以理解 mirror 配置就是一个反向代理,它把客户端的请求代理到 endpoint 配置的后端镜像仓库。mirror 名称可以随意填写,但是必须符合 IP 或域名的定义规则。并且可以配置多个 endpoint,默认解析到第一个 endpoint,如果第一个 endpoint 没有返回数据,则自动切换到第二个 endpoint,以此类推。

比如以下配置示例:

mirrors:
  "172.31.6.200:5000":
    endpoint:
      - "http://172.31.6.200:5000"
  "rancher.ksd.top:5000":
    endpoint:
      - "http://172.31.6.200:5000"
  "docker.io":
    endpoint:
      - "https://fogjl973.mirror.aliyuncs.com"
      - "https://registry-1.docker.io"

可以通过 crictl pull 172.31.6.200:5000/library/alpinecrictl pull rancher.ksd.top:5000/library/alpine 获取到镜像,但镜像都是从同一个仓库获取到的。

root@rancher-server:/etc/rancher/k3s# systemctl restart k3s.service
root@rancher-server:/etc/rancher/k3s# crictl pull 172.31.6.200:5000/library/alpine
Image is up to date for sha256:a24bb4013296f61e89ba57005a7b3e52274d8edd3ae2077d04395f806b63d83e
root@rancher-server:/etc/rancher/k3s# crictl pull rancher.ksd.top:5000/library/alpine
Image is up to date for sha256:a24bb4013296f61e89ba57005a7b3e52274d8edd3ae2077d04395f806b63d83e
root@rancher-server:/etc/rancher/k3s#

非安全(http)私有仓库配置

配置非安全(http)私有仓库,只需要在 endpoint 中指定 http 协议头的地址即可。

在没有 TLS 通信的情况下,需要为 endpoints 指定 http://,否则将默认为 https。

无认证

如果你使用的是非安全(http)私有仓库,那么可以通过下面的参数来配置 K3s 连接私有仓库:

$ cat >> /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
  "172.31.6.200:5000":
    endpoint:
      - "http://172.31.6.200:5000"
EOF
$ systemctl restart k3s

然后可以通过 crictl 去 pull 镜像:

root@ip-172-31-13-117:~# crictl pull 172.31.6.200:5000/my-ubuntu
Image is up to date for sha256:9499db7817713c4d10240ca9f5386b605ecff7975179f5a46e7ffd59fff462ee

接下来,在看一下 containerd 的配置,可以看到文件末尾追加了如下配置:

root@ip-172-31-13-117:~# cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml
[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."172.31.6.200:5000"]
  endpoint = ["http://172.31.6.200:5000"]

[plugins.cri.registry.mirrors."rancher.ksd.top:5000"]
  endpoint = ["http://172.31.6.200:5000"]

有认证

如果你的非安全(http)私有仓库带有认证,那么可以通过下面的参数来配置 k3s 连接私有仓库:

$ cat >> /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
  "35.182.134.80":
    endpoint:
      - "http://35.182.134.80"
configs:
  "35.182.134.80":
    auth:
      username: admin # this is the registry username
      password: Harbor12345 # this is the registry password
EOF
$ systemctl restart k3s

通过 crictl 去 pull 镜像:

root@ip-172-31-13-117:~# crictl pull 35.182.134.80/ksd/ubuntu:16.04
Image is up to date for sha256:9499db7817713c4d10240ca9f5386b605ecff7975179f5a46e7ffd59fff462ee

Containerd 配置文件末尾追加了如下配置:

root@ip-172-31-13-117:~# cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml
[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."35.182.134.80"]
  endpoint = ["http://35.182.134.80"]

[plugins.cri.registry.configs."35.182.134.80".auth]
  username = "admin"
  password = "Harbor12345"

安全(https)私有仓库配置

以下示例均启用了认证,所以每个示例都配置了 configs.auth,如果实际环境未配置认证,删除 configs.auth 配置即可。

使用授信 ssl 证书

与非安全(http)私有仓库配置类似,只需要配置 endpoint 对应的仓库地址为 https 即可。

root@ip-172-31-13-117:~# cat >> /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
  "harbor.kingsd.top":
    endpoint:
      - "https://harbor.kingsd.top"
configs:
  "harbor.kingsd.top":
    auth:
      username: admin # this is the registry username
      password: Harbor12345 # this is the registry password
EOF
root@ip-172-31-13-117:~# systemctl restart k3s

通过 crictl 去 pull 镜像:


root@ip-172-31-13-117:~# crictl pull harbor.kingsd.top/ksd/ubuntu:16.04
Image is up to date for sha256:9499db7817713c4d10240ca9f5386b605ecff7975179f5a46e7ffd59fff462ee

Containerd 配置文件末尾追加了如下配置:

root@ip-172-31-13-117:~# cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml
[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."harbor.kingsd.top"]
  endpoint = ["https://harbor.kingsd.top"]

[plugins.cri.registry.configs."harbor.kingsd.top".auth]
  username = "admin"
  password = "Harbor12345"

使用自签 ssl 证书

如果后端仓库使用的是自签名的 ssl 证书,那么需要配置 CA 证书 用于 ssl 证书的校验。

root@ip-172-31-13-117:~# cat >> /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
  "harbor-ksd.kingsd.top":
    endpoint:
      - "https://harbor-ksd.kingsd.top"
configs:
  "harbor-ksd.kingsd.top":
    auth:
      username: admin # this is the registry username
      password: Harbor12345 # this is the registry password
    tls:
      ca_file: /opt/certs/ca.crt
EOF
root@ip-172-31-13-117:~# systemctl restart k3s

通过 crictl 去 pull 镜像:

root@ip-172-31-13-117:~# crictl pull harbor-ksd.kingsd.top/ksd/ubuntu:16.04
Image is up to date for sha256:9499db7817713c4d10240ca9f5386b605ecff7975179f5a46e7ffd59fff462ee

Containerd 配置文件末尾追加了如下配置:

root@ip-172-31-13-117:~# cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml
[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."harbor-ksd.kingsd.top"]
  endpoint = ["https://harbor-ksd.kingsd.top"]

[plugins.cri.registry.configs."harbor-ksd.kingsd.top".auth]
  username = "admin"
  password = "Harbor12345"

[plugins.cri.registry.configs."harbor-ksd.kingsd.top".tls]
  ca_file = "/opt/certs/ca.crt"

ssl 双向认证

如果镜像仓库配置了双向认证,那么需要为 containerd 配置 ssl 证书用于 镜像仓库对 containerd 做认证。

root@ip-172-31-13-117:~# cat >> /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
  "harbor-ksd.kingsd.top":
    endpoint:
      - "https://harbor-ksd.kingsd.top"
configs:
  "harbor-ksd.kingsd.top":
    auth:
      username: admin # this is the registry username
      password: Harbor12345 # this is the registry password
    tls:
      ca_file: /opt/certs/ca.crt # path to the ca file used in the registry
      cert_file: /opt/certs/harbor-ksd.kingsd.top.cert # path to the cert file used in the registry
      key_file: /opt/certs/harbor-ksd.kingsd.top.key # path to the key file used in the registry
EOF
root@ip-172-31-13-117:~# systemctl restart k3s

通过 crictl 去 pull 镜像:

root@ip-172-31-13-117:~# crictl pull harbor-ksd.kingsd.top/ksd/ubuntu:16.04
Image is up to date for sha256:9499db7817713c4d10240ca9f5386b605ecff7975179f5a46e7ffd59fff462ee

Containerd 配置文件末尾追加了如下配置:

root@ip-172-31-13-117:~# cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml
[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."harbor-ksd.kingsd.top"]
  endpoint = ["https://harbor-ksd.kingsd.top"]

[plugins.cri.registry.configs."harbor-ksd.kingsd.top".auth]
  username = "admin"
  password = "Harbor12345"

[plugins.cri.registry.configs."harbor-ksd.kingsd.top".tls]
  ca_file = "/opt/certs/ca.crt"
  cert_file = "/opt/certs/harbor-ksd.kingsd.top.cert"
  key_file = "/opt/certs/harbor-ksd.kingsd.top.key"

加速器配置

Containerd 与 docker 都有默认仓库,均为 docker.io 。如果配置中未指定 mirror 为 docker.io,containerd 后会自动加载 docker.io 配置。与 docker 不同的是,containerd 可以修改 docker.io 对应的 endpoint(默认为 https://registry-1.docker.io),而 docker 无法修改。

Docker 中可以通过 registry-mirrors 设置镜像加速地址。如果 pull 的镜像不带仓库地址(项目名+镜像名:tag),则会从默认镜像仓库去拉取镜像。如果配置了镜像加速地址,会先访问镜像加速仓库,如果没有返回数据,再访问默认的镜像仓库。

Containerd 目前没有直接配置镜像加速的功能,但 containerd 中可以修改 docker.io 对应的 endpoint,所以可以通过修改 endpoint 来实现镜像加速下载。因为 endpoint 是轮询访问,所以可以给 docker.io 配置多个仓库地址来实现 加速地址+默认仓库地址。如下配置示例:

$ cat >> /etc/rancher/k3s/registries.yaml <<EOF
mirrors:
  "docker.io":
    endpoint:
      - "https://fogjl973.mirror.aliyuncs.com"
      - "https://registry-1.docker.io"
EOF

$ systemctl restart k3s

Containerd 配置文件末尾追加了如下配置:

root@ip-172-31-13-117:~# cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml
[plugins.cri.registry.mirrors]

[plugins.cri.registry.mirrors."docker.io"]
  endpoint = ["https://fogjl973.mirror.aliyuncs.com", "https://registry-1.docker.io"]

完整配置示例

mirrors:
  "192.168.50.119":
    endpoint:
      - "http://192.168.50.119"
  "docker.io":
    endpoint:
      - "https://fogjl973.mirror.aliyuncs.com"
      - "https://registry-1.docker.io"
configs:
  "192.168.50.119":
    auth:
      username: '' # this is the registry username
      password: '' # this is the registry password
    tls:
      cert_file: '' # path to the cert file used in the registry
      key_file: '' # path to the key file used in the registry
      ca_file: '' # path to the ca file used in the registry
  "docker.io":
    auth:
      username: '' # this is the registry username
      password: '' # this is the registry password
    tls:
      cert_file: '' # path to the cert file used in the registry
      key_file: '' # path to the key file used in the registry
      ca_file: '' # path to the ca file used in the registry

参考

  • K3s 私有镜像仓库配置[1]

  • Containerd 配置镜像仓库[2]

参考资料

[1]

K3s 私有镜像仓库配置: https://docs.rancher.cn/docs/k3s/installation/private-registry/_index

[2]

Containerd 配置镜像仓库: https://github.com/containerd/cri/blob/master/docs/registry.md


你可能还喜欢

点击下方图片即可阅读

容器网络一直在颤抖,罪魁祸首竟然是 ipvs 定时器

云原生是一种信仰 ????

码关注公众号

后台回复◉k8s◉获取史上最方便快捷的 Kubernetes 高可用部署工具,只需一条命令,连 ssh 都不需要!

点击 "阅读原文" 获取更好的阅读体验!

❤️给个「在看」,是对我最大的支持❤️
<think>好的,用户问的是关于containerd配置镜像仓库的问题。我需要先回忆一下containerd的基本知识。containerd是容器运行时,负责管理容器的生命周期,而镜像仓库配置通常涉及如何让containerd从私有或自定义的仓库拉取镜像。 首先,用户可能是在使用containerd时遇到了镜像拉取的问题,比如无法从私有仓库拉取,或者需要配置镜像加速器。常见的场景包括配置自签名的仓库、设置认证信息或者修改镜像仓库的地址。 接下来,我需要考虑containerd配置文件位置。通常,配置文件在/etc/containerd/config.toml,但不同系统可能路径不同,比如Minikube中的路径可能不同。用户可能需要生成默认配置,如果文件不存在的话,可以用containerd config default来生成。 然后,配置镜像仓库的关键部分在于registry.mirrors和registry.configs。mirrors用于指定镜像仓库的替代地址,比如将docker.io的镜像转到国内镜像源加速下载。configs则用于配置TLS和认证信息,特别是对于私有仓库,可能需要禁用TLS验证或提供用户名密码。 例如,配置一个私有仓库的示例如下:在config.toml中,添加[plugins."io.containerd.grpc.v1.cri".registry]下的mirrors和configs部分。对于自签名的仓库,可能需要设置insecure_skips_verify为true,同时配置auth,使用base64编码的用户名和密码。 需要注意的是,修改配置后必须重启containerd服务,比如用systemctl restart containerd。如果是Kubernetes环境,比如使用kubeadm,可能还需要调整每个节点的配置。 用户可能会遇到的问题包括配置格式错误,比如TOML语法错误,或者路径不正确。另外,私有仓库的认证信息是否正确,TLS证书是否被信任也是常见的问题点。 最后,可能需要指导用户如何验证配置是否生效,比如使用crictl pull命令测试拉取镜像,或者查看日志排查问题。此外,提醒用户如果是containerd作为Kubernetes的运行时,可能需要额外的配置步骤,比如在kubelet中指定配置路径。 总结来说,回答需要分步骤说明配置文件的位置、如何修改mirrors和configs,提供示例,并强调重启服务以及验证配置的步骤。同时,注意用户可能的使用场景,如私有仓库或镜像加速,给出相应的配置方法。</think>Containerd 配置镜像仓库主要通过修改 `config.toml` 文件实现,以下是详细配置步骤和注意事项: --- ### 1. 定位配置文件 默认路径:`/etc/containerd/config.toml` (如果是 Kubernetes 集群中的节点,可能需要通过 `crictl info` 查看实际路径) --- ### 2. 关键配置项 ```toml [plugins."io.containerd.grpc.v1.cri".registry] # 镜像仓库映射(加速/替换) [plugins."io.containerd.grpc.v1.cri".registry.mirrors] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"] endpoint = ["https://registry-1.docker.io"] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."my-registry.local:5000"] endpoint = ["http://my-registry.local:5000"] # 私有仓库认证配置 [plugins."io.containerd.grpc.v1.cri".registry.configs] [plugins."io.containerd.grpc.v1.cri".registry.configs."my-registry.local:5000".auth] username = "user" password = "pass" [plugins."io.containerd.grpc.v1.cri".registry.configs."my-registry.local:5000".tls] insecure_skip_verify = true # 跳过TLS验证 ``` --- ### 3. 典型场景配置示例 #### 场景 1:配置镜像加速器(如阿里云) ```toml [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"] endpoint = ["https://registry.cn-hangzhou.aliyuncs.com"] ``` #### 场景 2:自签名私有仓库 ```toml [plugins."io.containerd.grpc.v1.cri".registry.mirrors."my-registry:5000"] endpoint = ["http://my-registry:5000"] [plugins."io.containerd.grpc.v1.cri".registry.configs."my-registry:5000".tls] insecure_skip_verify = true ``` #### 场景 3:带认证的私有仓库 ```toml [plugins."io.containerd.grpc.v1.cri".registry.configs."registry.example.com".auth] username = "robot$user" password = "your_token_here" auth = "dXNlcm5hbWU6cGFzc3dvcmQ=" # base64(username:password) ``` --- ### 4. 操作步骤 1. 备份原配置:`cp /etc/containerd/config.toml /etc/containerd/config.toml.bak` 2. 修改配置后重启服务:`systemctl restart containerd` 3. 验证配置: ```bash crictl pull my-registry.local:5000/nginx:alpine ctr images ls | grep nginx ``` --- ### 5. 注意事项 - 修改配置后必须重启 containerd 服务 - TLS 证书问题常见错误:`x509: certificate signed by unknown authority` - Kubernetes 环境需确保所有节点配置同步 - 使用 `ctr` 命令测试时需添加 `--hosts-dir=/etc/containerd/certs.d` 参数 --- ### 附:证书配置目录结构 ``` /etc/containerd/certs.d/ └── my-registry.local:5000 ├── hosts.toml └── ca.crt ``` 通过以上配置可以实现镜像仓库的定制化管理和加速访问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值