Linux使用cgroup进行资源的隔离控制
Centos启动时使用systemd(即我们使用的systemctl,Centos7之前是使用/etc/rc.d/init.d)进行初始化系统,会生成一个cgroup manager去管理cgroupfs。如果让Containerd直接去管理cgroupfs,又会生成一个cgroup manager。一个系统有两个cgroup manager很不稳定。所以我们需要配置Containerd直接使用systemd去管理cgroupfs
下载并解压
[root@k8s-master ~]# wget https://github.com/containerd/containerd/releases/download/v1.4.12/cri-containerd-cni-1.4.12-linux-amd64.tar.gz
[root@k8s-master ~]# mkdir cri-containerd-cni
[root@k8s-master ~]# tar -zxvf cri-containerd-cni-1.4.12-linux-amd64.tar.gz -C cri-containerd-cni
将解压的文件,复制到系统的配置目录和执行目录
[root@k8s-master ~]# cp -a cri-containerd-cni/etc/systemd/system/containerd.service /etc/systemd/system
[root@k8s-master ~]# cp -a cri-containerd-cni/etc/crictl.yaml /etc
[root@k8s-master ~]# cp -a cri-containerd-cni/etc/cni /etc
[root@k8s-master ~]# cp -a cri-containerd-cni/usr/local/sbin/runc /usr/local/sbin
[root@k8s-master ~]# cp -a cri-containerd-cni/usr/local/bin/* /usr/local/bin
[root@k8s-master ~]# cp -a cri-containerd-cni/opt/* /opt
启动containerd
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl enable containerd --now
Created symlink from /etc/systemd/system/multi-user.target.wants/containerd.service to /etc/systemd/system/containerd.service.
[root@k8s-master ~]#
获取默认配置文件
[root@k8s-master ~]# mkdir /etc/containerd
[root@k8s-master ~]# containerd config default | tee /etc/containerd/config.toml
version = 2
root = "/var/lib/containerd"
state = "/run/containerd"
plugin_dir = ""
disabled_plugins = []
required_plugins = []
oom_score = 0
[grpc]
address = "/run/containerd/containerd.sock"
tcp_address = ""
tcp_tls_cert = ""
tcp_tls_key = ""
uid = 0
gid = 0
max_recv_message_size = 16777216
max_send_message_size = 16777216
[ttrpc]
address = ""
uid = 0
gid = 0
[debug]
address = ""
uid = 0
gid = 0
level = ""
[metrics]
address = ""
grpc_histogram = false
[cgroup]
path = ""
[timeouts]
"io.containerd.timeout.shim.cleanup" = "5s"
"io.containerd.timeout.shim.load" = "5s"
"io.containerd.timeout.shim.shutdown" = "3s"
"io.containerd.timeout.task.state" = "2s"
[plugins]
[plugins."io.containerd.gc.v1.scheduler"]
pause_threshold = 0.02
deletion_threshold = 0
mutation_threshold = 100
schedule_delay = "0s"
startup_delay = "100ms"
[plugins."io.containerd.grpc.v1.cri"]
disable_tcp_service = true
stream_server_address = "127.0.0.1"
stream_server_port = "0"
stream_idle_timeout = "4h0m0s"
enable_selinux = false
selinux_category_range = 1024
sandbox_image = "k8s.gcr.io/pause:3.2"
stats_collect_period = 10
systemd_cgroup = false
enable_tls_streaming = false
max_container_log_line_size = 16384
disable_cgroup = false
disable_apparmor = false
restrict_oom_score_adj = false
max_concurrent_downloads = 3
disable_proc_mount = false
unset_seccomp_profile = ""
tolerate_missing_hugetlb_controller = true
disable_hugetlb_controller = true
ignore_image_defined_volumes = false
[plugins."io.containerd.grpc.v1.cri".containerd]
snapshotter = "overlayfs"
default_runtime_name = "runc"
no_pivot = false
disable_snapshot_annotations = true
discard_unpacked_layers = false
[plugins."io.containerd.grpc.v1.cri".containerd.default_runtime]
runtime_type = ""
runtime_engine = ""
runtime_root = ""
privileged_without_host_devices = false
base_runtime_spec = ""
[plugins."io.containerd.grpc.v1.cri".containerd.untrusted_workload_runtime]
runtime_type = ""
runtime_engine = ""
runtime_root = ""
privileged_without_host_devices = false
base_runtime_spec = ""
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes]
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]
runtime_type = "io.containerd.runc.v2"
runtime_engine = ""
runtime_root = ""
privileged_without_host_devices = false
base_runtime_spec = ""
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
[plugins."io.containerd.grpc.v1.cri".cni]
bin_dir = "/opt/cni/bin"
conf_dir = "/etc/cni/net.d"
max_conf_num = 1
conf_template = ""
[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".image_decryption]
key_model = ""
[plugins."io.containerd.grpc.v1.cri".x509_key_pair_streaming]
tls_cert_file = ""
tls_key_file = ""
[plugins."io.containerd.internal.v1.opt"]
path = "/opt/containerd"
[plugins."io.containerd.internal.v1.restart"]
interval = "10s"
[plugins."io.containerd.metadata.v1.bolt"]
content_sharing_policy = "shared"
[plugins."io.containerd.monitor.v1.cgroups"]
no_prometheus = false
[plugins."io.containerd.runtime.v1.linux"]
shim = "containerd-shim"
runtime = "runc"
runtime_root = ""
no_shim = false
shim_debug = false
[plugins."io.containerd.runtime.v2.task"]
platforms = ["linux/amd64"]
[plugins."io.containerd.service.v1.diff-service"]
default = ["walking"]
[plugins."io.containerd.snapshotter.v1.devmapper"]
root_path = ""
pool_name = ""
base_image_size = ""
async_remove = false
[root@k8s-master ~]#
修改config.toml配置文件,修改说明如下
[root@k8s-master ~]# cat /etc/containerd/config.toml
......省略部分......
enable_selinux = false
selinux_category_range = 1024
#sandbox_image = "k8s.gcr.io/pause:3.2"
# 注释上面那行,添加下面这行
sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2"
stats_collect_period = 10
systemd_cgroup = false
......省略部分......
privileged_without_host_devices = false
base_runtime_spec = ""
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
# 添加下面这行
SystemdCgroup = true
[plugins."io.containerd.grpc.v1.cri".cni]
bin_dir = "/opt/cni/bin"
......省略部分......
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
#endpoint = ["https://registry-1.docker.io"]
# 注释上面那行,添加下面三行
endpoint = ["https://docker.mirrors.ustc.edu.cn"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"]
endpoint = ["https://registry.cn-hangzhou.aliyuncs.com/google_containers"]
[plugins."io.containerd.grpc.v1.cri".image_decryption]
key_model = ""
......省略部分......
[root@k8s-master ~]#
然后重启containerd
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl restart containerd
nerdctl是containerd原生的命令行管理工具。和Docker的命令行兼容
nerdctl管理的容器、镜像与Kubernetes的容器、镜像是完全隔离的,不能互通。目前只能通过crictl命令行工具来查看、拉取Kubernetes的容器、镜像
nerdctl和containerd的版本对应关系,参考:https://github.com/containerd/nerdctl/blob/v0.6.1/go.mod
下载解压,然后复制到执行目录下,即可使用
[root@k8s-master ~]# wget https://github.com/containerd/nerdctl/releases/download/v0.6.1/nerdctl-0.6.1-linux-amd64.tar.gz
[root@k8s-master ~]#
[root@k8s-master ~]# mkdir nerdctl
[root@k8s-master ~]#
[root@k8s-master ~]# tar -zxvf nerdctl-0.6.1-linux-amd64.tar.gz -C nerdctl
nerdctl
containerd-rootless-setuptool.sh
containerd-rootless.sh
[root@k8s-master ~]#
[root@k8s-master ~]# cp -a nerdctl/nerdctl /usr/bin
[root@k8s-master ~]#
[root@k8s-master ~]# nerdctl images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@k8s-master ~]#
注意: nerdctl run -p containerPort:hostPort
的端口映射无效,只能通过nerdctl run -net host
来实现
crictl是Kubernetes用于管理Containerd上的镜像和容器的一个命令行工具,主要用于Debug
下载解压
[root@k8s-master ~]# wget https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.24.0/crictl-v1.24.0-linux-amd64.tar.gz
[root@k8s-master ~]# tar -zxvf crictl-v1.24.0-linux-amd64.tar.gz -C /usr/local/bin
crictl
[root@k8s-master ~]#
创建crictl的配置文件
[root@k8s-master ~]# cat > /etc/crictl.yaml <
使用crictl
[root@k8s-master ~]# crictl images
IMAGE TAG IMAGE ID SIZE
[root@k8s-master ~]#
使用nerdctl build
进行Dockerfile的镜像构建时,报缺少buildkit,所以需要进行安装
buildkit和containerd的版本对应关系,参考:https://github.com/moby/buildkit/blob/v0.8.3/go.mod
[root@k8s-master ~]# mkdir buildkit
[root@k8s-master ~]#
[root@k8s-master ~]# cd buildkit/
[root@k8s-master buildkit]#
[root@k8s-master buildkit]# wget https://github.com/moby/buildkit/releases/download/v0.8.3/buildkit-v0.8.3.linux-amd64.tar.gz
[root@k8s-master buildkit]#
[root@k8s-master buildkit]# tar -zxvf buildkit-v0.8.3.linux-amd64.tar.gz
bin/
bin/buildctl
bin/buildkit-qemu-aarch64
bin/buildkit-qemu-arm
bin/buildkit-qemu-i386
bin/buildkit-qemu-ppc64le
bin/buildkit-qemu-riscv64
bin/buildkit-qemu-s390x
bin/buildkit-runc
bin/buildkitd
[root@k8s-master buildkit]#
[root@k8s-master buildkit]# cp -a bin /usr/local
[root@k8s-master buildkit]# cat /etc/systemd/system/buildkit.service
[Unit]
Description=BuildKit
Documentation=https://github.com/moby/buildkit
[Service]
ExecStart=/usr/local/bin/buildkitd --oci-worker=false --containerd-worker=true
[Install]
WantedBy=multi-user.target
[root@k8s-master buildkit]#
[root@k8s-master buildkit]# systemctl enable buildkit --now
Created symlink from /etc/systemd/system/multi-user.target.wants/buildkit.service to /etc/systemd/system/buildkit.service.
[root@k8s-master buildkit]#