COOKBOOK-Kubernetes二进制高可用部署-Containerd

准备

1
2
3
4
5
6
7
8
9
10
# 安装runc工具
yum install -y runc

# 安装cni plugin
# 创建 cni plugin 目录
mkdir -p /opt/cni/bin
# 下载cni1.5.1版本
wget -P /usr/local/src https://github.com/containernetworking/plugins/releases/download/v1.5.1/cni-plugins-linux-amd64-v1.5.1.tgz
# 解压
tar -C /opt/cni/bin -zxvf /usr/local/src/cni-plugins-linux-amd64-v1.5.1.tgz

安装 containerd

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 下载containerd
wget -P /usr/local/src https://github.com/containerd/containerd/releases/download/v1.7.7/containerd-1.7.7-linux-amd64.tar.gz

# 解压
tar -C /usr/local/ -zxvf /usr/local/src/containerd-1.7.7-linux-amd64.tar.gz

# 创建 containerd 服务文件
# wget https://raw.githubusercontent.com/containerd/containerd/main/containerd.service
cat << EOF > /usr/lib/systemd/system/containerd.service
# Copyright The containerd Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target dbus.service

[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/local/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

# 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
EOF

配置国内代理源

推荐使用DaoCloud的代理源,稳定可靠:DaoCloud Mirror

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 创建 containerd 默认配置文件
mkdir -p /etc/containerd
containerd config default > /etc/containerd/config.toml

# 修改 /etc/containerd/config.toml 中的 [plugins."io.containerd.grpc.v1.cri".registry] 的 config_path 内容
# 改为 config_path = "/etc/containerd/certs.d"
sed -i '/\[plugins."io.containerd.grpc.v1.cri".registry\]/ {
n
s/\(config_path = \).*/\1"\/etc\/containerd\/certs.d"/
}' /etc/containerd/config.toml

# 验证修改是否成功,有输出为成功,无输出为失败
grep 'config_path = "/etc/containerd/certs.d"' -C 3 /etc/containerd/config.toml

# 配置 containerd 使用 cgroupfs Cgroup 驱动(需同 kubelet 配置参数一致)
sed -i 's/\(SystemdCgroup = \).*/\1false/' /etc/containerd/config.toml

# 验证修改是否成功,SystemdCgroup = true 时使用 Systemd Cgroup;SystemCgroup = false 时使用 Cgroupfs Cgroup
grep 'SystemdCgroup' -C 12 /etc/containerd/config.toml

# 创建 certs.d 目录
mkdir -p /etc/containerd/certs.d

# 配置 docker.io 代理源
mkdir /etc/containerd/certs.d/docker.io
cat << EOF > /etc/containerd/certs.d/docker.io/hosts.toml
server = "https://docker.io"

[host."docker.m.daocloud.io"]
capabilities = ["pull", "resolve"]
EOF

# 配置 registry.k8s.io 代理源
mkdir /etc/containerd/certs.d/registry.k8s.io
cat << EOF > /etc/containerd/certs.d/registry.k8s.io/hosts.toml
server = "https://registry.k8s.io"

[host."k8s.m.daocloud.io"]
capabilities = ["pull", "resolve"]
EOF

# 配置 quay.io 代理源
mkdir /etc/containerd/certs.d/quay.io
cat << EOF > /etc/containerd/certs.d/quay.io/hosts.toml
server = "https://quay.io"

[host."quay.m.daocloud.io"]
capabilities = ["pull", "resolve"]
EOF

# 配置 ghcr.io 代理源
mkdir /etc/containerd/certs.d/ghcr.io
cat << EOF > /etc/containerd/certs.d/ghcr.io/hosts.toml
server = "https://ghcr.io"

[host."ghcr.m.daocloud.io"]
capabilities = ["pull", "resolve"]
EOF

启动 containerd 服务

1
2
systemctl daemon-reload
systemctl start containerd && systemctl enable containerd

查看 containerd 配置是否生效

1
containerd config dump

安装 nerdctl

1
2
3
4
5
# 下载nerdctl
wget -P /usr/local/src https://github.com/containerd/nerdctl/releases/download/v1.7.7/nerdctl-1.7.7-linux-amd64.tar.gz

# 解压
tar -C /usr/local/bin/ -zxvf /usr/local/src/nerdctl-1.7.7-linux-amd64.tar.gz

(•̀ᴗ•́)و ̑̑

Share