作者简介

王海龙,Rancher中国社区技术经理,负责Rancher中国技术社区的维护和运营。拥有6年的云计算领域经验,经历了OpenStack到Kubernetes的技术变革,无论底层操作系统Linux,还是虚拟化KVM或是Docker容器技术都有丰富的运维和实践经验。

前 言

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 镜像仓库配置文件由两大部分组成:mirrorsconfigs:

  • 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 连接私有仓库:

root@ip-172-31-13-117:~# 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 连接私有仓库:

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 配置文件末尾追加了如下配置:

[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
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 证书的校验。

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
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
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 配置文件末尾追加了如下配置:

[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 配置多个仓库地址来实现 加速地址+默认仓库地址。如下配置示例:

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"]

完整配置示例

  "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私有镜像仓库配置:

https://docs.rancher.cn/docs/k3s/installation/private-registry/_index

Containerd配置镜像仓库:

https://github.com/containerd/cri/blob/master/docs/registry.md

配置 containerd 镜像仓库完全攻略的更多相关文章

  1. Maven配置国内镜像仓库

    eclipse 位置

  2. docker配置私有镜像仓库-registry和hyper/docker-registry-web

    1.前言️​ Docker hub是远程仓库,是国外的,push pull速度特别慢,尤其是网速不好的时候,页面都点不进去,官网 但是可以配置阿里云镜像加速哦: 因此搭建一个私有的镜像仓库用于管理我们 ...

  3. [摸鱼] 配置的vim的使用攻略!

    vim使用攻略 <>=f 折叠与缩进 开:<>[^fuck]cc 关:<>cu 缩进一块使用V选中,按>> [Ctrl]V 以列为单位选 za,打开或关 ...

  4. Kubernetes 配置私有镜像仓库时,没有权限访问的问题

    使用 K8S 部署服务时,如果指定的镜像地址是内部镜像仓库,那么在下载镜像的时候可能会报权限错误.这是由于在 K8S 中部署服务时,K8S 需要到 Harbor 中进行一次验证,这个验证与节点中使用 ...

  5. Portainer安装,配置自定义镜像仓库拉取镜像

    Portainer介绍 Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容器控制台 ...

  6. [Clojure] 包管理器leiningen配置国内镜像仓库

    clojure用到的包管理站主要有两个,一个是解决java类库依赖的maven,一个是clojar 很多人都知道maven有阿里云提供的镜像站,可是clojar呢?幸运的是中科大为我们提供了cloja ...

  7. maven配置阿里镜像仓库

    打开maven的配置文件(windows机器一般在maven安装目录的conf/settings.xml),在<mirrors></mirrors>标签中添加mirror子节点 ...

  8. maven 配置国内镜像仓库加速获取jar包的配置方法

    在 maven 的 conf/settings.xml 中配置 <mirrors> <!-- mirror | Specifies a repository mirror site ...

  9. [摸鱼] 配置的tmux的使用攻略!

    o~/.tmux.conf <>=ctrl b <>$ 重命名 <>% 水平切割 <>" 垂直切割 <>o 下一个窗口 <& ...

随机推荐

  1. Spring Data JPA的基本学习之了解

    Spring Data JPA 是 什 么 可以理解为JPA规范的再次封装抽象,底层还是使用了Hibernate的JPA技术实现,引用JPQL(Java Persistence Query Langu ...

  2. 【程序包管理】篇章2:rpm程序包来源合法和完整性验正

    来源合法性验正: 数字签名   私钥签名完整性    哈希 注意: 1.如果是网站下载的程序包的话,没有公钥,就无法进行来源合法性验证,所以最好使用系统自带的rpm程序包或去可靠的网站下载程序包.[如 ...

  3. 小团队产品研发管理V0.0.1

    序言 之前做研发的时候非常鄙视管理,觉得管理的那些人就知道搞政治,后来做了开发主管,以及到部门经理之后,管的人多了发现管理真是门大学问,真的应该每个人都要学习一些基本管理知识,特别是刚入社会的打工人. ...

  4. xxfpmW 的诞生过程

    最近因为在win 服务器搭建php服务,发现php-cgi.exe 很容易崩溃,看cpu和硬盘都没有暴涨,也不知道啥原因,网上查发现有一款xxfpm 小应用可以解决这个问题,但这个应用是2011年开发 ...

  5. kaggle入门题Titanic

    集成开发环境:Pycharm python版本:2.7(anaconda库) 用到的库:科学计算库numpy,数据分析包pandas,画图包matplotlib,机器学习库sklearn 大体步骤分为 ...

  6. [leetcode]114. Flatten Binary Tree to Linked List由二叉树构建链表

    /* 先序遍历构建链表,重新构建树 */ LinkedList<Integer> list = new LinkedList<>(); public void flatten( ...

  7. [leetcode350]Intersection of Two Arrays II求数组交集

    List<Integer> res = new ArrayList<>(); Arrays.sort(nums1); Arrays.sort(nums2); int i1 = ...

  8. 求你别再用swagger了,给你推荐几个在线文档生成神器

    前言 最近公司打算做一个openapi开放平台,让我找一款好用的在线文档生成工具,具体要求如下: 必须是开源的 能够实时生成在线文档 支持全文搜索 支持在线调试功能 界面优美 说实话,这个需求看起来简 ...

  9. 当会打王者荣耀的AI学会踢足球,一不小心拿下世界冠军!

    难得的元旦小假期,没有什么比得上在慵懒的冬日艳阳下放松自己,拿起手机,叫上了许久未一起作战的小伙伴,到王者荣耀中激战了一番,仿佛又回到了当年那个年轻的自己. 厉害不,毕竟当年DD也是王者五十星的水平, ...

  10. 2. C++中的引用

    1.  引用的基本使用 作用:给变量起别名 语法:数据类型  &别名=原名 注意: 别名数据类型与原名数据类型一致. 引用必须初始化. 引用一旦初始化后,就不可以更改(只能作为一个变量的别名) ...