1.下载nerdctl工具软件,要full版本的
wget https://github.com/containerd/nerdctl/releases/download/v0.21.0/nerdctl-full-0.21.0-linux-amd64.tar.gz
2.解压缩到指定路径
# tar -zxvf nerdctl-full-0.21.0-linux-amd64.tar.gz -C /usr/local
# ll /usr/local/lib/systemd/system/*.service
-rw-r--r-- 1 root root 1331 6月 19 00:17 /usr/local/lib/systemd/system/buildkit.service
-rw-r--r-- 1 root root 1270 6月 19 00:11 /usr/local/lib/systemd/system/containerd.service
-rw-r--r-- 1 root root 312 6月 19 00:17 /usr/local/lib/systemd/system/stargz-snapshotter.service
# cp /usr/local/lib/systemd/system/*.service /etc/systemd/system/
# systemctl daemon-reload
3.安装网络插件CNI
wget https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz
mkdir -p /opt/cni/bin/ # 这个路径要跟containerd配置文件中的 bin_dir = "/opt/cni/bin" 保持一致
tar -zxvf cni-plugins-linux-amd64-v1.1.1.tgz -C /opt/cni/bin/
默认插件网络文件路径是:conf_dir = "/etc/cni/net.d"
但是这个路径下没有文件,当使用nerdctl启动一个容器的时候,就会自动生成一个nerdctl-bridge.conflist文件,内容是:
# cat nerdctl-bridge.conflist
{
"cniVersion": "1.0.0",
"name": "bridge",
"nerdctlID": 0,
"nerdctlLabels": {},
"plugins": [
{
"type": "bridge",
"bridge": "nerdctl0",
"isGateway": true,
"ipMasq": true,
"hairpinMode": true,
"ipam": {
"ranges": [
[
{
"gateway": "10.4.0.1",
"subnet": "10.4.0.0/24"
}
]
],
"routes": [
{
"dst": "0.0.0.0/0"
}
],
"type": "host-local"
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
},
{
"type": "firewall",
"ingressPolicy": "same-bridge"
},
{
"type": "tuning"
}
]
}
同时查看网卡,会多出来一个nerdctl0,这个就是容器网络使用的网关:
# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:4f:48:21 brd ff:ff:ff:ff:ff:ff
inet 192.168.20.202/24 brd 192.168.20.255 scope global noprefixroute ens32
valid_lft forever preferred_lft forever
inet6 fe80::9b37:d439:e478:a189/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::45cc:6455:6bb0:4d86/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::eff4:d67d:257f:ead/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: nerdctl0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 1e:b9:dc:b3:6d:d5 brd ff:ff:ff:ff:ff:ff
inet 10.4.0.1/24 brd 10.4.0.255 scope global nerdctl0
valid_lft forever preferred_lft forever
inet6 fe80::1cb9:dcff:feb3:6dd5/64 scope link
valid_lft forever preferred_lft forever
4: vethe3b22ddf@if2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master nerdctl0 state UP group default
link/ether de:87:93:de:3e:8c brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::dc87:93ff:fede:3e8c/64 scope link
valid_lft forever preferred_lft forever
4.nerdctl命令补全
# yum install -y epel-release bash-completion
# source /usr/share/bash-completion/bash_completion
# source <(nerdctl completion bash)
# echo "source <(nerdctl completion bash)" >> ~/.bashrc
# source ~/.bashrc
# nerdctl r
rename (rename a container)
restart (Restart one or more running containers)
rmi (Remove one or more images)
rm (Remove one or more containers)
run (Run a command in a new container. Optionally specify "ipfs://" or "ipns://" scheme to pull image from IPFS.)
5.启动 containerd 、 buildkitd 和 stargz-snapshotter服务
# 启动 containerd && 验证服务状态
systemctl enable --now containerd && systemctl restart containerd && systemctl status containerd
# 启用 BuildKit 为了构建 Dockerfile,您需要使用以下命令启用 BuildKit。
systemctl enable --now buildkit && systemctl restart buildkit && systemctl status buildkit
# 懒加载镜像
systemctl enable --now stargz-snapshotter && systemctl restart stargz-snapshotter && systemctl status stargz-snapshotter
6.查看nerdctl工具执行结果及其版本,至此安装完毕
[root@localhost ~]# nerdctl --version
nerdctl version 0.21.0
[root@localhost ~]# nerdctl version
Client:
Version: v0.21.0
OS/Arch: linux/amd64
Git commit: 9ddf5226eabcbb7b4b43987f3b0f8d53d86d3bca
Server:
containerd:
Version: v1.6.6 =>这个指的是containerd版本
GitCommit: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
7.配置containerd
参考配置文件内容如下:
# mkdir -p /etc/containerd && cd /etc/containerd
# cat 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 = 0
level = ""
uid = 0
[grpc]
address = "/run/containerd/containerd.sock"
gid = 0
max_recv_message_size = 16777216
max_send_message_size = 16777216
tcp_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 = 0
mutation_threshold = 100
pause_threshold = 0.02
schedule_delay = "0s"
startup_delay = "100ms"
[plugins."io.containerd.grpc.v1.cri"]
device_ownership_from_security_context = false
disable_apparmor = false
disable_cgroup = false
disable_hugetlb_controller = true
disable_proc_mount = false
disable_tcp_service = true
enable_selinux = false
enable_tls_streaming = false
enable_unprivileged_icmp = false
enable_unprivileged_ports = false
ignore_image_defined_volumes = false
max_concurrent_downloads = 3
max_container_log_line_size = 16384
netns_mounts_under_state_dir = false
restrict_oom_score_adj = false
sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6"
selinux_category_range = 1024
stats_collect_period = 10
stream_idle_timeout = "4h0m0s"
stream_server_address = "127.0.0.1"
stream_server_port = "0"
systemd_cgroup = false
tolerate_missing_hugetlb_controller = true
unset_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 = 1
[plugins."io.containerd.grpc.v1.cri".containerd]
default_runtime_name = "runc"
disable_snapshot_annotations = false
discard_unpacked_layers = false
ignore_rdt_not_enabled_errors = false
no_pivot = false
snapshotter = "stargz"
[plugins."io.containerd.grpc.v1.cri".containerd.default_runtime]
base_runtime_spec = ""
cni_conf_dir = ""
cni_max_conf_num = 0
container_annotations = []
pod_annotations = []
privileged_without_host_devices = false
runtime_engine = ""
runtime_path = ""
runtime_root = ""
runtime_type = ""
[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 = 0
container_annotations = []
pod_annotations = []
privileged_without_host_devices = false
runtime_engine = ""
runtime_path = ""
runtime_root = ""
runtime_type = "io.containerd.runc.v2"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
BinaryName = ""
CriuImagePath = ""
CriuPath = ""
CriuWorkPath = ""
IoGid = 0
IoUid = 0
NoNewKeyring = false
NoPivotRoot = false
Root = ""
ShimCgroup = ""
SystemdCgroup = false
[plugins."io.containerd.grpc.v1.cri".containerd.untrusted_workload_runtime]
base_runtime_spec = ""
cni_conf_dir = ""
cni_max_conf_num = 0
container_annotations = []
pod_annotations = []
privileged_without_host_devices = false
runtime_engine = ""
runtime_path = ""
runtime_root = ""
runtime_type = ""
[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".registry.mirrors."docker.io"]
endpoint = ["https://docker.mirrors.ustc.edu.cn","http://hub-mirror.c.163.com"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"]
endpoint = ["https://gcr.mirrors.ustc.edu.cn"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"]
endpoint = ["https://gcr.mirrors.ustc.edu.cn/google-containers/"]
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."quay.io"]
endpoint = ["https://quay.mirrors.ustc.edu.cn"]
[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"]
sampling_ratio = 1.0
service_name = "containerd"
[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"]
no_shim = false
runtime = "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"]
rdt_config_file = ""
[plugins."io.containerd.snapshotter.v1.aufs"]
root_path = ""
[plugins."io.containerd.snapshotter.v1.btrfs"]
root_path = ""
[plugins."io.containerd.snapshotter.v1.devmapper"]
async_remove = false
base_image_size = ""
discard_blocks = false
fs_options = ""
fs_type = ""
pool_name = ""
root_path = ""
[plugins."io.containerd.snapshotter.v1.native"]
root_path = ""
[plugins."io.containerd.snapshotter.v1.overlayfs"]
root_path = ""
upperdir_label = false
[plugins."io.containerd.snapshotter.v1.zfs"]
root_path = ""
[plugins."io.containerd.tracing.processor.v1.otlp"]
endpoint = ""
insecure = false
protocol = ""
[proxy_plugins]
[proxy_plugins.stargz]
type = "snapshot"
address = "/run/containerd-stargz-grpc/containerd-stargz-grpc.sock"
[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.shim.cleanup" = "5s"
"io.containerd.timeout.shim.load" = "5s"
"io.containerd.timeout.shim.shutdown" = "3s"
"io.containerd.timeout.task.state" = "2s"
[ttrpc]
address = ""
gid = 0
uid = 0
8.添加harbor私有仓库
# mkdir -p /etc/containerd/certs.d/www.myharbor.com
# cp ca.cert /etc/nerdctl/certs.d/www.myharbor.com
# systemctl restart containerd
# echo "192.168.20.250 www.myharbor.com" >> /etc/hosts
# nerdctl login -u test -p Test123456 www.myharbor.com
# nerdctl pull www.myharbor.com/mytest/busybox:v0.3
# nerdctl image ls
9.使用Dockerfile文件构建镜像
# mkdir test && cd test
# vim Dockerfile
FROM jeanblanchard/alpine-glibc:latest
MAINTAINER sandu <1103324414@qq.com>
RUN echo "https://mirrors.aliyun.com/alpine/latest-stable/main/" > /etc/apk/repositories
RUN echo "https://mirrors.aliyun.com/alpine/latest-stable/community/" >> /etc/apk/repositories
RUN ["apk","update"]
RUN ["apk","add","curl"]
RUN ["apk","add","openjdk8-jre"]
# nerdctl build -t alpine-glibc:20220629 .
10.日常操作
- 镜像操作
# 拉取
nerdctl pull www.myharbor.com/mytest/busybox:v0.3
nerdctl -n k8s.io pull www.myharbor.com/mytest/busybox:v0.3
# 查看
nerdctl -n default image ls
nerdctl -n k8s.io image ls
nerdctl -n default inspect www.myharbor.com/mytest/busybox:v0.3
# 删除
nerdctl -n default rmi www.myharbor.com/mytest/busybox:v0.3 => 命令空间k8s.io中的仍在
# 重新tag镜像
nerdctl tag alpine-glibc:20220629 www.myharbor.com/mytest/alpine-glibc:20220629
# 推送镜像到harbor仓库
nerdctl push www.myharbor.com/mytest/alpine-glibc:20220629
- 容器操作
# 创建运行容器
nerdctl -n default run -d --name nginx -p 80:80 nginx:alpine
nerdctl -n k8s.io run -d --privileged --restart=always --name rancher -p 80:80 -p 443:443 docker.io/cnrancher/rancher:v2.4.17-ent
# 查看容器
nerdctl -n k8s.io ps -a
nerdctl -n k8s.io inpsect rancher
# 进入容器内部
nerdctl -n k8s.io exec rancher -- sh
# 停止容器
nerdctl -n k8s.io stop rancher
# 删除容器
nerdctl -n k8s.io rm rancher