之前使用docker配置的可以查看这个笔记https://sulao.cn/post/786.html进行配置
现在使用containerd以后需要在config.toml中配置仓库的相关信息
修改/etc/containerd/config.toml文件
找到[plugins."io.containerd.grpc.v1.cri".registry]的配置
然后添加的下面这样的
[plugins."io.containerd.grpc.v1.cri".registry] [plugins."io.containerd.grpc.v1.cri".registry.mirrors] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."192.168.1.2"] endpoint = ["http://192.168.1.2"]
192.168.1.2就是我的仓库地址,如果有端口可以直接加在后面,例如192.168.1.2就直接改成192.168.1.2:5000
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."192.168.1.2:5000"] endpoint = ["http://192.168.1.2:5000"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."192.168.1.2"]就是镜像仓库源地址
endpoint = ["http://192.168.1.2"]是镜像仓库代理地址
当在需要配置账户密码的场景下时,如果需要跳过安全验证,还需要添加tls,并配置auth认证的账户密码
[plugins."io.containerd.grpc.v1.cri".registry] [plugins."io.containerd.grpc.v1.cri".registry.mirrors] [plugins."io.containerd.grpc.v1.cri".registry.mirrors."192.168.1.2"] endpoint = ["http://192.168.1.2"] [plugins."io.containerd.grpc.v1.cri".registry.configs] [plugins."io.containerd.grpc.v1.cri".registry.configs."192.168.1.2".tls] insecure_skip_verify = true [plugins."io.containerd.grpc.v1.cri".registry.configs."192.168.1.2".auth] username = "admin" password = "admin密码"
最后重启containerd使其生效
systemctl daemon-reload systemctl restart containerd.service
再尝试拉取镜像
ctr -n k8s.io image pull 192.168.1.2/orionx/native:tensorflow2.4.1-hvd0.21.3-cu11.1-cudnn8-py3.6-ubuntu18.04 --plain-http --user admin:admin密码
如果直接修改不好可以使用以下脚本进行配置然后修改
#!/usr/bin/python3 #coding: utf-8 import toml import sys import os import time import shutil import logging logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(filename)s[line:%(lineno)d] %(message)s', datefmt='%Y-%m-%d') config_path = r"/etc/containerd/" config_name = "config.toml" mirrors = "192.168.1.2" username = "" password = "" def modify_config(config, mirrors, username, password): config["plugins"]["io.containerd.grpc.v1.cri"]["registry"]["mirrors"][mirrors] = { "endpoint": ["http://{}".format(mirrors)] } if username: config["plugins"]["io.containerd.grpc.v1.cri"]["registry"]["configs"][mirrors] = { "tls": { "insecure_skip_verify": True } } config["plugins"]["io.containerd.grpc.v1.cri"]["registry"]["configs"][mirrors]["auth"] = { "username": username, "password": password } return config if __name__ == "__main__": if not os.path.exists(os.path.join(config_path, config_name)): sys.exit("不能找到container配置文件 {}".format(os.path.join(config_path, config_name))) try: with open(os.path.join(config_path, config_name), "r") as f: config = toml.load(f) logging.debug("打印当前config配置: {}".format(config)) new_config = modify_config(config) logging.debug("打印修改后的config配置: {}".format(new_config)) except Exception as e: sys.exit("修改container配置文件失败, {}".format(e)) logging.info("containerd配置修改完成, 开始备份{}文件...".format(config_name)) shutil.copyfile(os.path.join(config_path, config_name), os.path.join(config_path, config_name+"_"+time.strftime("%Y%m%d%H%M%S", time.localtime()))) logging.info("将修改后的container配置写回 {} 文件".format(config_name)) try: with open(os.path.join(config_path, config_name), "w") as f: toml.dump(config, f) except Exception as e: sys.exit("写回containerd配置失败, {}".format(e)) logging.info("修改containerd配置操作完成!!!")