说明

k8s v1.24.0 版本之后,删除了docker的代码,运行时推荐使用containerd.悲伤的是没有docker build命令,无法build镜像,需要使用 nerdctl + buildkit组合实现build功能.
nerdctl 不会从本地寻找镜像,例如已经存在 test:1.0.0 的image,如果dockerfile里 from test:1.0.0 docker可以正常从本地获取镜像,nerdctl会从公网查找镜像,需要把镜像发布到registry内网仓库,通过https访问获取.
crictl images是k8s的使用的镜像,和nerdctl images不是一个,需要单独清理.

安装containerd和配置

yum安装

1
2
3
4
5
6
# 安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
yum install -y yum-utils device-mapper-persistent-data lvm2
# 设置 yum 源
# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install containerd -y

二进制安装

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
### 下载
wget https://hub.fastgit.xyz/containerd/containerd/releases/download/v1.6.6/containerd-1.6.6-linux-amd64.tar.gz 

### 解压
tar -zxvf containerd-1.6.6-linux-amd64.tar.gz

### copy到 /usr/bin/
cp -rf ./bin/* /usr/bin/

### 生成默认的配置文件
mkdir /etc/containerd
containerd config default > /etc/containerd/config.toml

镜像加速,修改/etc/containerd/config.toml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
    [plugins."io.containerd.grpc.v1.cri".registry]
      config_path = ""

      [plugins."io.containerd.grpc.v1.cri".registry.auths]

      [plugins."io.containerd.grpc.v1.cri".registry.configs]
	    ### 跳过tls验证
        [plugins."io.containerd.grpc.v1.cri".registry.configs."registry.jiagou.com:5000".tls]
          insecure_skip_verify = true

      [plugins."io.containerd.grpc.v1.cri".registry.headers]

      [plugins."io.containerd.grpc.v1.cri".registry.mirrors]

        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."registry.jiagou.com:5000"]
          endpoint = ["https://registry.jiagou.com:5000"]
        ### 表示需要配置 mirror 的镜像仓库原镜像仓库,endpoint表示提供 mirror 的镜像加速服务.
        [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
          endpoint = ["https://xuachqgw.mirror.aliyuncs.com"]

编写服务脚本 /usr/lib/systemd/system/containerd.service

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target

[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/bin/containerd

Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
# Comment TasksMax if your systemd version does not supports it.
# Only systemd 226 and above support this version.
TasksMax=infinity
OOMScoreAdjust=-999

[Install]
WantedBy=multi-user.target

注册服务: systemctl enable containerd
启动服务: systemctl start containerd

安装buildkit

1
2
3
4
5
6
7
8
mkdir -p /usr/local/buildkit/
cd /usr/local/buildkit/
wget https://hub.fastgit.xyz/moby/buildkit/releases/download/v0.10.3/buildkit-v0.10.3.linux-amd64.tar.gz

tar -zxvf buildkit-v0.10.3.linux-amd64.tar.gz 

### 创建软连接
ln -s /usr/local/buildkit/bin/buildctl /usr/local/bin/buildctl

配置/etc/buildkit/buildkitd.toml,非root用户配置文件是~/.config/buildkit/buildkitd.toml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[worker.oci]
  enabled = false

[worker.containerd]
  enabled = true
  # 设置默认命名空间
  #namespace = "default"

   
# optionally mirror configuration can be done by defining it as a registry.
[registry."registry.jiagou.com:5000"]
  ###允许http访问
  http = true
  ###忽略检查https证书,用于自签证书
  insecure=true

编写服务脚本 /etc/systemd/system/buildkit.service

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[Unit]
Description=BuildKit
Documentation=https://github.com/moby/buildkit

[Service]
Type=notify
NotifyAccess=all
ExecStart=/usr/local/buildkit/bin/buildkitd --oci-worker=false --containerd-worker=true

[Install]
WantedBy=multi-user.target

注册服务: systemctl enable buildkit
启动服务: systemctl start buildkit

安装nerdctl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
wget https://hub.fastgit.xyz/containerd/nerdctl/releases/download/v0.21.0/nerdctl-0.21.0-linux-amd64.tar.gz

tar -zxvf nerdctl-0.21.0-linux-amd64.tar.gz

cp -rf ./nerdctl /usr/local/bin/nerdctl
### 伪装成 docker命令
ln -s /usr/local/bin/nerdctl /usr/local/bin/docker

##使用 docker pull 和 docker push 时如果出现X509异常,可以添加 --insecure-registry 参数,使用自签证书.
## docker --insecure-registry  pull/push

安装配置registry

nerdctl 默认不解析本地已经存在的镜像,会从公网搜索,所以还是要使用镜像仓库,默认使用registry,配置https证书.

生成SANs https证书

高版本的Chrome浏览器会要求设置subjectAltName,如果没有设置SAN会报证书错误
参考openssl配置文件,Linux服务器上通常在/etc/pki/tls/openssl.cnf
新建文件jiagou.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
[ req ]
default_bits = 4096
distinguished_name = req_distinguished_name
req_extensions = v3_req

[req_distinguished_name]
countryName = country - CN
stateOrProvinceName = province - henan
localityName = city - zhengzhou
organizationName = company name - jiaogu
commonName = domain name or ip - *.jiagou.com

[ v3_req ]
subjectAltName = @alt_names

[ alt_names ]
DNS.1=*.jiagou.com #可以使用通配符
#IP.1=xxx.xxx.xxx.xxx

使用openssl生成证书

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#1.生成根证书密钥
openssl genrsa -out ca.key 4096   #建议长度为4096,1024长度已经被列为不安全.
#2.生成自签名根证书
openssl req -new -x509 -days 36500 -key ca.key -out ca.crt
#3.生成证书密钥
openssl genrsa -out jiagou.key 4096
#向根证书请求签名一个新的证书,由于用户信任了你的根证书,所以根证书签名的其它证书也会被信任
#4.生成csr 注意要使用sha256算法(推荐是sha256算法,默认算法浏览器会报弱加密算法错误)
openssl req -new -key jiagou.key -out jiagou.csr -config ./jiagou.conf -sha256
#5.使用根证书按照csr给证书签名,生成新证书jiagou.crt
openssl x509 -req -days 36500 -in jiagou.csr -CA ca.crt -CAkey ca.key -set_serial 01 -out jiagou.crt -extfile ./jiagou.conf -extensions v3_req

#6.查看证书信息
openssl x509 -text -in jiagou.crt

配置registry.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
    spec:
      containers:
      - name: registry
        image: registry:2.8.1
        ###
        imagePullPolicy: IfNotPresent
        env:
        ###设置时区###
        - name: TZ
          value: Asia/Shanghai  

        ###配置生成的域名证书,文件外部挂载
        - name: REGISTRY_HTTP_TLS_CERTIFICATE
          value: /certs/jiagou.crt                     
        - name: REGISTRY_HTTP_TLS_KEY
          value: /certs/jiagou.key       

本地解析

修改/etc/hosts,增加本地的域名解析

1
10.98.239.102 registry.jiagou.com