CentOS7安装使用containerd
一,安装
1.1、安装containerd
下载
https://github.com/containerd/containerd/releases/download/v1.7.24/cri-containerd-cni-1.7.24-linux-amd64.tar.gz
wget https://github.com/containerd/containerd/releases/download/v1.7.24/cri-containerd-cni-1.7.24-linux-amd64.tar.gz
解压
tar xf cri-containerd-cni-1.7.24-linux-amd64.tar.gz
cp usr/local/bin/containerd /usr/local/bin/
配置开机启动(使用默认配置)
cp etc/systemd/system/containerd.service /usr/lib/systemd/system/
检查是否安装成功
containerd --help
NAME:containerd - __ _ ___________ ____ / /_____ _(_)___ ___ _________/ // ___/ __ \/ __ \/ __/ __ `/ / __ \/ _ \/ ___/ __ /
/ /__/ /_/ / / / / /_/ /_/ / / / / / __/ / / /_/ /
\___/\____/_/ /_/\__/\__,_/_/_/ /_/\___/_/ \__,_/high performance container runtimeUSAGE:containerd [global options] command [command options] [arguments...]VERSION:v1.7.24DESCRIPTION:containerd is a high performance container runtime whose daemon can be started
by using this command. If none of the *config*, *publish*, *oci-hook*, or *help* commands
are specified, the default action of the **containerd** command is to start the
containerd daemon in the foreground.A default configuration is used if no TOML configuration is specified or located
at the default file location. The *containerd config* command can be used to
generate the default configuration for containerd. The output of that command
can be used and modified as necessary as a custom configuration.COMMANDS:config Information on the containerd configpublish Binary to publish events to containerdoci-hook Provides a base for OCI runtime hooks to allow arguments to be injected.help, h Shows a list of commands or help for one commandGLOBAL OPTIONS:--config value, -c value Path to the configuration file (default: "/etc/containerd/config.toml")--log-level value, -l value Set the logging level [trace, debug, info, warn, error, fatal, panic]--address value, -a value Address for containerd's GRPC server--root value containerd root directory--state value containerd state directory--help, -h show help--version, -v print the version
创建配置文件夹
mkdir /etc/containerd/
生成默认配置
containerd config default > /etc/containerd/config.toml
查看配置文件
cat /etc/containerd/config.toml
disabled_plugins = []
imports = []
oom_score = 0
plugin_dir = ""
required_plugins = []
root = "/var/lib/containerd"
state = "/run/containerd"
temp = ""
version = 2[cgroup]path = ""[debug]address = ""format = ""gid = 0level = ""uid = 0[grpc]address = "/run/containerd/containerd.sock"gid = 0max_recv_message_size = 16777216max_send_message_size = 16777216tcp_address = ""tcp_tls_ca = ""tcp_tls_cert = ""tcp_tls_key = ""uid = 0[metrics]address = ""grpc_histogram = false[plugins][plugins."io.containerd.gc.v1.scheduler"]deletion_threshold = 0mutation_threshold = 100pause_threshold = 0.02schedule_delay = "0s"startup_delay = "100ms"[plugins."io.containerd.grpc.v1.cri"]cdi_spec_dirs = ["/etc/cdi", "/var/run/cdi"]device_ownership_from_security_context = falsedisable_apparmor = falsedisable_cgroup = falsedisable_hugetlb_controller = truedisable_proc_mount = falsedisable_tcp_service = truedrain_exec_sync_io_timeout = "0s"enable_cdi = falseenable_selinux = falseenable_tls_streaming = falseenable_unprivileged_icmp = falseenable_unprivileged_ports = falseignore_deprecation_warnings = []ignore_image_defined_volumes = falseimage_pull_progress_timeout = "5m0s"image_pull_with_sync_fs = falsemax_concurrent_downloads = 3max_container_log_line_size = 16384netns_mounts_under_state_dir = falserestrict_oom_score_adj = falsesandbox_image = "registry.k8s.io/pause:3.8"selinux_category_range = 1024stats_collect_period = 10stream_idle_timeout = "4h0m0s"stream_server_address = "127.0.0.1"stream_server_port = "0"systemd_cgroup = falsetolerate_missing_hugetlb_controller = trueunset_seccomp_profile = ""[plugins."io.containerd.grpc.v1.cri".cni]bin_dir = "/opt/cni/bin"conf_dir = "/etc/cni/net.d"conf_template = ""ip_pref = ""max_conf_num = 1setup_serially = false[plugins."io.containerd.grpc.v1.cri".containerd]default_runtime_name = "runc"disable_snapshot_annotations = truediscard_unpacked_layers = falseignore_blockio_not_enabled_errors = falseignore_rdt_not_enabled_errors = falseno_pivot = falsesnapshotter = "overlayfs"[plugins."io.containerd.grpc.v1.cri".containerd.default_runtime]base_runtime_spec = ""cni_conf_dir = ""cni_max_conf_num = 0container_annotations = []pod_annotations = []privileged_without_host_devices = falseprivileged_without_host_devices_all_devices_allowed = falseruntime_engine = ""runtime_path = ""runtime_root = ""runtime_type = ""sandbox_mode = ""snapshotter = ""[plugins."io.containerd.grpc.v1.cri".containerd.default_runtime.options][plugins."io.containerd.grpc.v1.cri".containerd.runtimes][plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc]base_runtime_spec = ""cni_conf_dir = ""cni_max_conf_num = 0container_annotations = []pod_annotations = []privileged_without_host_devices = falseprivileged_without_host_devices_all_devices_allowed = falseruntime_engine = ""runtime_path = ""runtime_root = ""runtime_type = "io.containerd.runc.v2"sandbox_mode = "podsandbox"snapshotter = ""[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]BinaryName = ""CriuImagePath = ""CriuPath = ""CriuWorkPath = ""IoGid = 0IoUid = 0NoNewKeyring = falseNoPivotRoot = falseRoot = ""ShimCgroup = ""SystemdCgroup = false[plugins."io.containerd.grpc.v1.cri".containerd.untrusted_workload_runtime]base_runtime_spec = ""cni_conf_dir = ""cni_max_conf_num = 0container_annotations = []pod_annotations = []privileged_without_host_devices = falseprivileged_without_host_devices_all_devices_allowed = falseruntime_engine = ""runtime_path = ""runtime_root = ""runtime_type = ""sandbox_mode = ""snapshotter = ""[plugins."io.containerd.grpc.v1.cri".containerd.untrusted_workload_runtime.options][plugins."io.containerd.grpc.v1.cri".image_decryption]key_model = "node"[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][plugins."io.containerd.grpc.v1.cri".registry.headers][plugins."io.containerd.grpc.v1.cri".registry.mirrors][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.internal.v1.tracing"][plugins."io.containerd.metadata.v1.bolt"]content_sharing_policy = "shared"[plugins."io.containerd.monitor.v1.cgroups"]no_prometheus = false[plugins."io.containerd.nri.v1.nri"]disable = truedisable_connections = falseplugin_config_path = "/etc/nri/conf.d"plugin_path = "/opt/nri/plugins"plugin_registration_timeout = "5s"plugin_request_timeout = "2s"socket_path = "/var/run/nri/nri.sock"[plugins."io.containerd.runtime.v1.linux"]no_shim = falseruntime = "runc"runtime_root = ""shim = "containerd-shim"shim_debug = false[plugins."io.containerd.runtime.v2.task"]platforms = ["linux/amd64"]sched_core = false[plugins."io.containerd.service.v1.diff-service"]default = ["walking"][plugins."io.containerd.service.v1.tasks-service"]blockio_config_file = ""rdt_config_file = ""[plugins."io.containerd.snapshotter.v1.aufs"]root_path = ""[plugins."io.containerd.snapshotter.v1.blockfile"]fs_type = ""mount_options = []root_path = ""scratch_file = ""[plugins."io.containerd.snapshotter.v1.btrfs"]root_path = ""[plugins."io.containerd.snapshotter.v1.devmapper"]async_remove = falsebase_image_size = ""discard_blocks = falsefs_options = ""fs_type = ""pool_name = ""root_path = ""[plugins."io.containerd.snapshotter.v1.native"]root_path = ""[plugins."io.containerd.snapshotter.v1.overlayfs"]mount_options = []root_path = ""sync_remove = falseupperdir_label = false[plugins."io.containerd.snapshotter.v1.zfs"]root_path = ""[plugins."io.containerd.tracing.processor.v1.otlp"][plugins."io.containerd.transfer.v1.local"]config_path = ""max_concurrent_downloads = 3max_concurrent_uploaded_layers = 3[[plugins."io.containerd.transfer.v1.local".unpack_config]]differ = ""platform = "linux/amd64"snapshotter = "overlayfs"[proxy_plugins][stream_processors][stream_processors."io.containerd.ocicrypt.decoder.v1.tar"]accepts = ["application/vnd.oci.image.layer.v1.tar+encrypted"]args = ["--decryption-keys-path", "/etc/containerd/ocicrypt/keys"]env = ["OCICRYPT_KEYPROVIDER_CONFIG=/etc/containerd/ocicrypt/ocicrypt_keyprovider.conf"]path = "ctd-decoder"returns = "application/vnd.oci.image.layer.v1.tar"[stream_processors."io.containerd.ocicrypt.decoder.v1.tar.gzip"]accepts = ["application/vnd.oci.image.layer.v1.tar+gzip+encrypted"]args = ["--decryption-keys-path", "/etc/containerd/ocicrypt/keys"]env = ["OCICRYPT_KEYPROVIDER_CONFIG=/etc/containerd/ocicrypt/ocicrypt_keyprovider.conf"]path = "ctd-decoder"returns = "application/vnd.oci.image.layer.v1.tar+gzip"[timeouts]"io.containerd.timeout.bolt.open" = "0s""io.containerd.timeout.metrics.shimstats" = "2s""io.containerd.timeout.shim.cleanup" = "5s""io.containerd.timeout.shim.load" = "5s""io.containerd.timeout.shim.shutdown" = "3s""io.containerd.timeout.task.state" = "2s"[ttrpc]address = ""gid = 0uid = 0
修改配置文件
vim /etc/containerd/config.toml
更改沙箱镜像地址
#sandbox_image = "registry.k8s.io/pause:3.8"sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.8"
配置私有镜像仓库Harbor服务器地址
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]#以下内容为私有Harbor仓库地址[plugins."io.containerd.grpc.v1.cri".registry.mirrors."192.168.74.129:5000"]endpoint = ["http://192.168.74.129:5000"]
设为开机启动、并检查状态
#开机启动
systemctl enable containerd
#立即启动
systemctl start containerd
#查看状态
systemctl status containerd
1.2、安装ctr工具
cp usr/local/bin/ctr /usr/bin/
1.3、安装runC
下载runC
https://objects.githubusercontent.com/github-production-release-asset-2e65be/36960321/003e9b27-6b65-4b53-ab1f-1cdaeba4e640?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20250123%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250123T050501Z&X-Amz-Expires=300&X-Amz-Signature=437d4621e4b6eb9c1f2c13931cbb157f077530ed5a7bae7ae8ae580e053661ec&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Drunc.amd64&response-content-type=application%2Foctet-stream
wget https://objects.githubusercontent.com/github-production-release-asset-2e65be/36960321/003e9b27-6b65-4b53-ab1f-1cdaeba4e640?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20250123%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250123T050501Z&X-Amz-Expires=300&X-Amz-Signature=437d4621e4b6eb9c1f2c13931cbb157f077530ed5a7bae7ae8ae580e053661ec&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Drunc.amd64&response-content-type=application%2Foctet-stream
安装runC
mv runc.amd64 /usr/sbin/runc
分配权限
chmod +x /usr/sbin/runc
检查版本
runc -v
二,containerd基础命令使用
2.1、镜像管理命令
-
查看镜像
- 命令:
ctr images ls
或ctr i ls
- 描述:列出当前可用的镜像。
- 命令:
-
拉取镜像
- 命令:
ctr images pull
- 描述:从远程仓库拉取镜像到本地。
- 常用参数:
-k
,跳过TLS验证(如果仓库不支持HTTPS)。 - 示例:
ctr images pull docker.io/library/nginx:latest
- 命令:
-
导出镜像
- 命令:
ctr images export
- 描述:将镜像导出到文件中。
- 常用参数:目标文件名。
- 示例:
ctr images export nginx.tar docker.io/library/nginx:latest
- 命令:
-
导入镜像
- 命令:
ctr images import
- 描述:将文件中的镜像导入到本地。
- 常用参数:镜像文件路径。
- 示例:
ctr images import nginx.tar
- 命令:
2.2、容器管理命令
-
查看容器
- 命令:
ctr containers ls
或ctr c ls
- 描述:列出当前所有容器。
- 命令:
-
创建容器
- 命令:
ctr container create
- 描述:创建一个新的容器,但容器不会立即启动。
- 常用参数:
--image
或-i
:指定容器镜像。--name
:指定容器名称。
- 示例:
ctr container create docker.io/library/nginx:latest mynginx
- 命令:
-
启动容器
- 命令:
ctr task start
- 描述:启动一个静态容器(之前通过
ctr container create
创建的容器)。 - 常用参数:容器ID或名称。
- 示例:
ctr task start mynginx
- 命令:
-
在容器中执行命令
- 命令:
ctr task exec
- 描述:在运行的容器中执行命令。
- 常用参数:
--exec-id
:执行ID(可以自定义)。- 容器ID或名称:要执行的命令(如
/bin/sh
)。
- 示例:
ctr task exec --exec-id 1 mynginx -t /bin/sh
- 命令:
-
停止并删除容器
- 通常使用
ctr task kill
发送信号来停止容器,然后使用ctr container delete
删除容器。 ctr task kill
的-s
参数可以指定发送的信号(默认为SIGKILL)。- 示例:
- 停止容器:
ctr task kill mynginx
- 删除容器:
ctr container delete mynginx
- 停止容器:
- 通常使用
2.3、其他常用命令
-
查看containerd版本
- 命令:
containerd --version
- 命令:
-
启动containerd服务
- 命令:
systemctl start containerd && systemctl enable containerd && systemctl status containerd
- 命令:
-
配置containerd
- 可以通过修改
/etc/containerd/config.toml
文件来配置containerd,例如配置镜像仓库加速地址等。
- 可以通过修改
三、containerd连接私有镜像仓库
3.1、修改配置
vim /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".registry.configs][plugins."io.containerd.grpc.v1.cri".registry.configs."192.168.74.129:5000".auth]username = "admin"password = "Harbor12345"
# 注释上面那行,添加下面两行[plugins."io.containerd.grpc.v1.cri".registry.mirrors."192.168.74.129:5000"]endpoint = ["http://192.168.74.129:5000"]
192.168.74.129:5000是我自己搭建的Harbor私有仓库
Harbor私有仓库搭建请参考:搭建Docker私有仓库Harbor_dockerhub私人仓库管理-CSDN博客
3.2、推送镜像到私有仓库
更改镜像名称
ctr images tag docker.io/nginx:alpine3.20 192.168.74.129:5000/test_obj/docker.io/nginx:alpine3.20
推送镜像
#推送镜像
ctr images push --plain-http -u admin:Harbor12345 192.168.74.129:5000/test_obj/docker.io/nginx:alpine3.20
-u admin:Harbor12345,私有仓库的用户名和密码
--plain-http:私有仓库仅支持http方式(ctr 拉取、推送 默认是https)
四、启动镜像并访问
先安装runc-v2程序
cp usr/local/bin/containerd-shim-runc-v2 /usr/local/bin/
关闭防火墙,方便其他设别访问端口服务
systemctl stop firewalld && systemctl disable firewalld
4.1、启动容器
ctr run -d --net-host 192.168.74.129:5000/test_obj/docker.io/nginx:alpine3.20
局域网其他设备此时就能访问这个nginx服务了
4.2、进入容器内部
ctr task exec --exec-id 75085 -t my-nginx sh
75085 是taskid 可以通过 ctr task ls 查看
进入容器内部后 可查看文件信息
4.3、停止容器
停止容器
ctr t rm my-nginx
删除容器
ctr c rm my-nginx