Centos下使用containerd管理容器:5分鐘從docker轉型到containerd

2022-09-11 18:03:49

一.系統環境

伺服器版本 containerd軟體版本 CPU架構
CentOS Linux release 7.4.1708 (Core) 1.6.8 x86_64

二.前言

在 Kubernetes 的早期,我們只支援一個容器執行時。那個執行時是 Docker 引擎。那時,並沒有太多其他的選擇,而 Docker 是處理容器的主要工具,所以這不是一個有爭議的選擇。最終,我們開始新增更多的容器執行時,比如 rkt 和 hypernetes,很明顯 Kubernetes 使用者希望選擇最適合他們的執行時。因此,Kubernetes 需要一種方法來允許叢集操作員靈活地使用他們選擇的任何執行時。

釋出了容器執行時介面(CRI) 以實現這種靈活性。CRI 的引入對專案和使用者來說都很棒,但它確實引入了一個問題:Docker Engine 作為容器執行時的使用早於 CRI,並且 Docker Engine 不相容 CRI。為了解決這個問題,引入了一個小型軟體 shim (dockershim) 作為 kubelet 元件的一部分,專門用於填補 Docker Engine 和 CRI 之間的空白,允許叢集操作員繼續使用 Docker Engine 作為他們的容器執行時基本上不間斷。

然而,這個小軟體墊片從來沒有打算成為一個永久的解決方案。多年來,它的存在給 kubelet 本身帶來了許多不必要的複雜性。由於這個 shim,Docker 的一些整合實現不一致,導致維護人員的負擔增加,並且維護供應商特定的程式碼不符合我們的開源理念。為了減少這種維護負擔並朝著支援開放標準的更具共同作業性的社群邁進,引入了 KEP-2221,建議刪除 dockershim。隨著 Kubernetes v1.20 的釋出,正式棄用。
自 Kubernetes v1.24 起,Dockershim 已被刪除,這對專案來說是一個積極的舉措。

自 Kubernetes v1.24 起,K8S預設使用containerd作為容器執行時!可以理解為containerd是docker的替代品。

好訊息是containerd用法和docker類似,轉型相當簡單!

三.containerd

containerd 是一個行業標準的容器執行時,強調簡單性、健壯性和可移植性。它可作為 Linux 和 Windows 的守護行程使用,可以管理其主機系統的完整容器生命週期:影象傳輸和儲存、容器執行和監督、低階儲存和網路附件等。containerd 旨在嵌入到更大的系統中,而不是由開發人員或終端使用者直接使用。

K8S釋出CRI(Container Runtime Interface),統一了容器執行時介面,凡是支援CRI的容器執行時,皆可作為K8S的底層容器執行時。
如果你使用Docker作為K8S容器執行時的話,kubelet需要先要通過dockershim去呼叫Docker,再通過Docker去呼叫containerd。
如果你使用containerd作為K8S容器執行時的話,由於containerd內建了CRI外掛,kubelet可以直接呼叫containerd。使用containerd不僅效能提高了(呼叫鏈變短了),而且資源佔用也會變小(Docker不是一個純粹的容器執行時,具有大量其他功能)。

四.部署containerd

4.1 安裝containerd

安裝containerd

[root@k8sworker2 ~]# yum -y install containerd.io cri-tools 
已載入外掛:fastestmirror
base                                                                                                                                                                                      | 3.6 kB  00:00:00     
......
  驗證中      : containerd.io-1.4.12-3.1.el7.x86_64                                                                                                                                                          4/4 

更新完畢:
  containerd.io.x86_64 0:1.6.8-3.1.el7                                                                        cri-tools.x86_64 0:1.24.2-0                                                                       

完畢!

設定containerd開機自啟動並啟動containerd

[root@k8sworker2 ~]# systemctl enable containerd --now
Created symlink from /etc/systemd/system/multi-user.target.wants/containerd.service to /usr/lib/systemd/system/containerd.service.

[root@k8sworker2 ~]# systemctl status containerd
● containerd.service - containerd container runtime
   Loaded: loaded (/usr/lib/systemd/system/containerd.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2022-01-06 00:23:13 CST; 1min 4s ago
     Docs: https://containerd.io
 Main PID: 88228 (containerd)
   CGroup: /system.slice/containerd.service
           └─88228 /usr/bin/containerd

4.2 containerd組態檔

containerd的組態檔為/etc/containerd/config.toml

[root@k8sworker2 ~]# ll -h /etc/containerd/config.toml 
-rw-r--r-- 1 root root 886 11月 18 04:15 /etc/containerd/config.toml

containerd的預設組態檔/etc/containerd/config.toml 內容如下

[root@k8sworker2 ~]# cat /etc/containerd/config.toml
#   Copyright 2018-2020 Docker Inc.

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

disabled_plugins = ["cri"]

#root = "/var/lib/containerd"
#state = "/run/containerd"
#subreaper = true
#oom_score = 0

#[grpc]
#  address = "/run/containerd/containerd.sock"
#  uid = 0
#  gid = 0

#[debug]
#  address = "/run/containerd/debug.sock"
#  uid = 0
#  gid = 0
#  level = "info"

可以使用containerd config default > /etc/containerd/config.toml生成預設的組態檔,這個命令生成的組態檔內容很多

[root@k8sworker2 ~]# containerd config default
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@k8sworker2 ~]# containerd config dump
version = 2
root = "/var/lib/containerd"
state = "/run/containerd"
plugin_dir = ""
disabled_plugins = ["cri"]
required_plugins = []
oom_score = 0
imports = ["/etc/containerd/config.toml"]

[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.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

4.3 設定containerd阿里雲映象加速器

修改組態檔,新增映象加速器

[root@k8sworker2 ~]# cat /etc/containerd/config.toml
disabled_plugins = ["restart"]
[plugins]
  [plugins.cri.registry.mirrors."docker.io"]
    endpoint = ["https://frz7i079.mirror.aliyuncs.com"]

重啟containerd

[root@k8sworker2 ~]# systemctl restart containerd

[root@k8sworker2 ~]# systemctl status containerd
● containerd.service - containerd container runtime
   Loaded: loaded (/usr/lib/systemd/system/containerd.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2022-01-06 00:34:18 CST; 6s ago
     Docs: https://containerd.io
  Process: 90199 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS)
 Main PID: 90201 (containerd)
   Memory: 19.6M
   CGroup: /system.slice/containerd.service
           └─90201 /usr/bin/containerd

五.管理containerd映象/容器

5.1 使用ctr管理容器

ctr是containerd自帶的用於管理容器的命令列工具,雖然該ctr工具與 containerd 捆綁在一起,但應注意該ctr工具僅用於偵錯 containerd,使用者體驗並不好,不推薦使用。

檢視名稱空間

[root@k8sworker2 ~]# ctr ns list
NAME LABELS 
moby        

檢視映象

[root@k8sworker2 ~]# ctr i list
REF TYPE DIGEST SIZE PLATFORMS LABELS 

5.2 使用nerdctl管理容器

5.2.1 安裝nerdctl

因為containerd自帶的命令列管理工具使用者體驗不好,所以Containerd 官方推出了一個新的 CLI 叫 nerdctl。nerdctl 的使用體驗和 docker 一樣順滑,使用最廣,推薦使用。

nerdctl是containerd的cli使用者端工具,與docker cli語法大部分相容,用法類似。需要兩個安裝包nerdctl-0.15.0-linux-amd64.tar.gz和cni-plugins-linux-amd64-v1.0.1.tgz ,兩個安裝包下載地址如下:

[root@k8sworker2 ~]# ll -h nerdctl-0.15.0-linux-amd64.tar.gz
-rw-r--r-- 1 root root 9.6M 1月   6 00:45 nerdctl-0.15.0-linux-amd64.tar.gz

[root@k8sworker2 ~]# ll -h cni-plugins-linux-amd64-v1.0.1.tgz 
-rw-r--r-- 1 root root 36M 1月   6 00:59 cni-plugins-linux-amd64-v1.0.1.tgz

分別進行解壓

[root@k8sworker2 ~]# tar xf nerdctl-0.15.0-linux-amd64.tar.gz -C /usr/local/bin/
[root@k8sworker2 ~]# ls /usr/local/bin/
containerd-rootless-setuptool.sh  containerd-rootless.sh  nerdctl

[root@k8sworker2 ~]# mkdir -p /opt/cni/bin
[root@k8sworker2 ~]# tar xf cni-plugins-linux-amd64-v1.0.1.tgz -C /opt/cni/bin/
[root@k8sworker2 ~]# ls /opt/cni/bin/
bandwidth  bridge  dhcp  firewall  host-device  host-local  ipvlan  loopback  macvlan  portmap  ptp  sbr  static  tuning  vlan  vrf

5.2.2 設定nerdctl命令tab鍵自動補全

先安裝必要的依賴包

[root@k8sworker2 ~]# yum install -y epel-release bash-completion

[root@k8sworker2 ~]# source /usr/share/bash-completion/bash_completion

修改組態檔

[root@k8sworker2 ~]# source <(nerdctl completion bash)

#新增source <(nerdctl completion bash)到/etc/profile裡
[root@k8sworker2 ~]# cat /etc/profile | head -3
# /etc/profile
source <(nerdctl completion bash)

使/etc/profile檔案生效

[root@k8sworker2 buildtest]# source /etc/profile

此時nerdctl r按tab鍵就可以自動補全了

[root@k8sworker2 ~]# nerdctl r
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.2.3 nerdctl常用命令

nerdctl的命令和docker命令類似

#檢視nerdctl版本
nerdctl version

#nerdctl run執行容器
nerdctl run -d -p 80:80 --name=nginx --restart=always nginx

#執行容器
nerdctl exec -it nginx /bin/sh

#列出正在執行的容器
nerdctl ps

#獲取容器的詳細資訊
nerdctl inspect nginx

#獲取容器紀錄檔
nerdctl logs -f nginx

#停止容器
nerdctl stop nginx

#刪除容器
nerdctl rm -f nginx

#映象列表
nerdctl images

#登入映象倉庫
nerdctl login --username xxx --password xxx  ip/主機名
nerdctl login --username xxx --password xxx  harbor.k8s.local

#登出退出登入
nerdctl logout

#拉取映象
nerdctl pull busybox

#推播映象
nerdctl push harbor.k8s.local/course/nginx:alpine

#給映象起別名
nerdctl tag nginx:alpine harbor.k8s.local/course/nginx:alpine

#匯出映象為tar包
nerdctl save -o busybox.tar busybox:latest
nerdctl save hub.c.163.com/library/nginx:latest >nginx.tar

#刪除映象
nerdctl rmi busybox

#匯入映象
nerdctl load -i busybox.tar.gz

#檢視資料卷
[root@k8sworker2 ~]# nerdctl volume list
VOLUME NAME    DIRECTORY

#檢視網路
[root@k8sworker2 ~]# nerdctl network list
NETWORK ID    NAME               FILE
0             bridge             
              k8s-pod-network    /etc/cni/net.d/10-calico.conflist
              host               
              none               

#檢視名稱空間
[root@k8sworker2 ~]# nerdctl namespace list
NAME        CONTAINERS    IMAGES    VOLUMES
buildkit    0             0         0
default     2             2         0
moby        0             0         0

5.2.4 使用nerdctl管理容器

檢視映象

[root@k8sworker2 ~]# nerdctl images
REPOSITORY    TAG    IMAGE ID    CREATED    PLATFORM    SIZE

拉取nginx映象

[root@k8sworker2 ~]# nerdctl pull nginx
docker.io/library/nginx:latest:                                                   resolved       |++++++++++++++++++++++++++++++++++++++| 
docker.io/library/nginx:latest:                                                   resolved       
......
|++++++++++++++++++++++++++++++++++++++| 
layer-sha256:a9edb18cadd1336142d6567ebee31be2a03c0905eeefe26cb150de7b0fbc520b:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:b4df32aa5a72e2a4316aad3414508ccd907d87b4ad177abd7cbd62fa4dab2a2f:    done           |++++++++++++++++++++++++++++++++++++++| 
elapsed: 30.9s                                                                    total:  54.1 M (1.8 MiB/s)                                       

[root@k8sworker2 ~]# nerdctl images 
REPOSITORY    TAG       IMAGE ID        CREATED           PLATFORM       SIZE
nginx         latest    0d17b565c37b    40 seconds ago    linux/amd64    146.2 MiB

檢視容器

#沒有容器執行
[root@k8sworker2 ~]# nerdctl ps
CONTAINER ID    IMAGE    COMMAND    CREATED    STATUS    PORTS    NAMES

使用Nginx映象建立一個容器,-p 80:80埠對映

[root@k8sworker2 ~]# nerdctl run -d --name=nginx --restart=always -p 80:80 nginx
5d1c3f1ca77f17d77f75895c29b6ce491d1921e61ad4f59f4ace3046436d10d4

[root@k8sworker2 ~]# nerdctl ps
CONTAINER ID    IMAGE                             COMMAND                   CREATED           STATUS    PORTS                 NAMES
5d1c3f1ca77f    docker.io/library/nginx:latest    "/docker-entrypoint.…"    17 seconds ago    Up        0.0.0.0:80->80/tcp    nginx    

存取nginx容器:物理機IP:80埠

[root@k8sworker2 ~]# curl http://192.168.110.139:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

檢視nerdctl資訊

[root@k8sworker2 ~]# nerdctl info
Client:
 Namespace:	default
 Debug Mode:	false

Server:
 Server Version: 1.4.12
 Storage Driver: overlayfs
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 1
 Plugins:
  Log: json-file
  Storage: native overlayfs
 Security Options:
  seccomp
   Profile: default
 Kernel Version: 3.10.0-693.el7.x86_64
 Operating System: CentOS Linux 7 (Core)
 OSType: linux
 Architecture: x86_64
 CPUs: 2
 Total Memory: 2.371GiB
 Name: k8sworker2
 ID: 160b58af-90bf-420f-99d3-ef2d4b9305ef

六.使用nerdctl構建映象

nerdctl構建映象和docker build類似,docker構建自定義映象可以檢視部落格《構建自定義映象並優化dockerfile檔案》https://www.cnblogs.com/renshengdezheli/p/16645144.html

注意:使用nerdctl構建映象需要提前安裝buildkit

設定Dockerfile檔案,此Dockerfile檔案的意思是構建一個可以ssh的centos映象,Dockerfile檔案的具體解釋請檢視部落格《構建自定義映象並優化dockerfile檔案》https://www.cnblogs.com/renshengdezheli/p/16645144.html

[root@k8sworker2 ~]# cat Dockerfile_ssh2
FROM hub.c.163.com/library/centos:latest
MAINTAINER LZ
RUN yum -y install openssh-server openssh-clients && \
    ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key && \
    ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key && \
    ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key && \
    echo qweasd | passwd --stdin root && \
    sed -i '/UseDNS/cUseDNS no' /etc/ssh/sshd_config && \
    yum -y install net-tools && \
    yum -y install iproute && \
    yum clean all
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]

構建映象

#構建映象失敗,報錯依賴於buildkit
[root@k8sworker2 ~]# nerdctl build -t centos:ssh . -f Dockerfile_ssh2
FATA[0000] `buildctl` needs to be installed and `buildkitd` needs to be running, see https://github.com/moby/buildkit: exec: "buildctl": executable file not found in $PATH 

6.1 安裝buildkit

為了能使用nerdctl構建映象,需要先安裝buildkit
buildkit-v0.9.3.linux-amd64.tar.gz下載網址:https://github.com/moby/buildkit/releases/tag/v0.9.3

準備好buildkit安裝包並解壓

[root@k8sworker2 ~]# ll -h buildkit-v0.9.3.linux-amd64.tar.gz 
-rw-r--r-- 1 root root 46M 1月   6 22:11 buildkit-v0.9.3.linux-amd64.tar.gz

#解壓
[root@k8sworker2 ~]# tar xf buildkit-v0.9.3.linux-amd64.tar.gz -C /usr/local/bin/
[root@k8sworker2 ~]# ls /usr/local/bin/
bin  containerd-rootless-setuptool.sh  containerd-rootless.sh  nerdctl

[root@k8sworker2 ~]# ls /usr/local/bin/bin/
buildctl   buildkit-qemu-aarch64  buildkit-qemu-i386    buildkit-qemu-mips64el  buildkit-qemu-riscv64  buildkit-runc
buildkitd  buildkit-qemu-arm      buildkit-qemu-mips64  buildkit-qemu-ppc64le   buildkit-qemu-s390x
[root@k8sworker2 ~]# mv /usr/local/bin/bin/* /usr/local/bin/

[root@k8sworker2 ~]# ls /usr/local/bin/
bin       buildkitd              buildkit-qemu-arm   buildkit-qemu-mips64    buildkit-qemu-ppc64le  buildkit-qemu-s390x  containerd-rootless-setuptool.sh  nerdctl
buildctl  buildkit-qemu-aarch64  buildkit-qemu-i386  buildkit-qemu-mips64el  buildkit-qemu-riscv64  buildkit-runc        containerd-rootless.sh

設定buildkit使用systemd啟動

[root@k8sworker2 buildtest]# 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

確保/usr/local/bin/buildkitd這個可執行檔案存在

[root@k8sworker2 ~]# ls /usr/local/bin/buildkitd
/usr/local/bin/buildkitd

重啟載入組態檔

[root@k8sworker2 ~]# systemctl daemon-reload

設定buildkit開機自啟動並現在啟動buildkit

[root@k8sworker2 ~]# systemctl enable buildkit --now
Created symlink from /etc/systemd/system/multi-user.target.wants/buildkit.service to /etc/systemd/system/buildkit.service.

[root@k8sworker2 ~]# systemctl status buildkit
● buildkit.service - BuildKit
   Loaded: loaded (/etc/systemd/system/buildkit.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2022-01-06 22:18:45 CST; 9s ago
     Docs: https://github.com/moby/buildkit
 Main PID: 93007 (buildkitd)
   Memory: 10.6M
   CGroup: /system.slice/buildkit.service
           └─93007 /usr/local/bin/buildkitd --oci-worker=false --containerd-worker=true

可以檢視buildkit的紀錄檔

[root@k8sworker2 ~]# journalctl -u buildkit
-- Logs begin at 三 2022-01-05 10:28:14 CST, end at 四 2022-01-06 22:19:31 CST. --
1月 06 22:18:45 k8sworker2 systemd[1]: Started BuildKit.
1月 06 22:18:45 k8sworker2 systemd[1]: Starting BuildKit...
1月 06 22:18:45 k8sworker2 buildkitd[93007]: time="2022-01-06T22:18:45+08:00" level=warning msg="using host network as the default"
1月 06 22:18:45 k8sworker2 buildkitd[93007]: time="2022-01-06T22:18:45+08:00" level=warning msg="git source cannot be enabled: failed to find git binary: exec: \"git\
1月 06 22:18:45 k8sworker2 buildkitd[93007]: time="2022-01-06T22:18:45+08:00" level=info msg="found worker \"bb19mi8dhi1sk5r4qn6wxic8e\", labels=map[org.mobyproject.b
1月 06 22:18:45 k8sworker2 buildkitd[93007]: time="2022-01-06T22:18:45+08:00" level=info msg="found 1 workers, default=\"bb19mi8dhi1sk5r4qn6wxic8e\""
1月 06 22:18:45 k8sworker2 buildkitd[93007]: time="2022-01-06T22:18:45+08:00" level=warning msg="currently, only the default worker can be used."
1月 06 22:18:45 k8sworker2 buildkitd[93007]: time="2022-01-06T22:18:45+08:00" level=info msg="running server on /run/buildkit/buildkitd.sock"

6.2 構建映象

設定Dockerfile檔案,此Dockerfile檔案的意思是構建一個可以ssh的centos映象

[root@k8sworker2 ~]# cat Dockerfile_ssh2
FROM hub.c.163.com/library/centos:latest
MAINTAINER LZ
RUN yum -y install openssh-server openssh-clients && \
    ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key && \
    ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key && \
    ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key && \
    echo qweasd | passwd --stdin root && \
    sed -i '/UseDNS/cUseDNS no' /etc/ssh/sshd_config && \
    yum -y install net-tools && \
    yum -y install iproute && \
    yum clean all
EXPOSE 22
CMD ["/usr/sbin/sshd","-D"]

使用nerdctl根據Dockerfile_ssh2檔案構建映象

[root@k8sworker2 ~]# nerdctl build -t centos:ssh . -f Dockerfile_ssh2
[+] Building 35.1s (6/6) FINISHED                                                                                                                                                                                
 => [internal] load build definition from Dockerfile_ssh2                                                                                                                                                   0.0s
 => => transferring dockerfile: 557B                                                                                                                                                                        0.0s
 => [internal] load .dockerignore                                                                                                                                                                           0.0s
 => => transferring context: 2B                                                                                                                                                                             0.0s
 => [internal] load metadata for hub.c.163.com/library/centos:latest                                                                                                                                        1.0s
 => CACHED [1/2] FROM hub.c.163.com/library/centos:latest@sha256:ab7e9c357fa8e5c822dd22615d3f704090780df1e089ac4ff8c6098f26a71fef                                                                           0.0s
 => => resolve hub.c.163.com/library/centos:latest@sha256:ab7e9c357fa8e5c822dd22615d3f704090780df1e089ac4ff8c6098f26a71fef                                                                                  0.2s
 => [2/2] RUN yum -y install openssh-server openssh-clients &&     ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key &&     ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key &&     ssh-keygen -t ed25519   27.3s
 => exporting to oci image format                                                                                                                                                                           6.6s 
 => => exporting layers                                                                                                                                                                                     2.0s 
 => => exporting manifest sha256:d250fe183c437af049cf0c03e616c543574f889d6f7c271c37918104537503ba                                                                                                           0.0s 
 => => exporting config sha256:06aa9d95ed27cbb080d669c66131ae9975910efd34de434fc407b08da66e2a59                                                                                                             0.0s 
 => => sending tarball                                                                                                                                                                                      4.6s 
unpacking docker.io/library/centos:ssh (sha256:d250fe183c437af049cf0c03e616c543574f889d6f7c271c37918104537503ba)...done                             

可以看到映象已經構建完畢,centos:ssh就是新構建的映象

[root@k8sworker2 ~]# nerdctl images
REPOSITORY    TAG       IMAGE ID        CREATED           PLATFORM       SIZE
centos        ssh       d250fe183c43    16 seconds ago    linux/amd64    232.0 MiB
nginx         latest    0d17b565c37b    6 hours ago       linux/amd64    146.2 MiB

使用新構建的映象centos:ssh建立容器

[root@k8sworker2 ~]# nerdctl run -d --name=centos-ssh --restart=always centos:ssh
baf0b7d8f067a12eb67576dbaf9249d8134f04a46df6d36b7c3d213065cdf44e

檢視正在執行的容器

[root@k8sworker2 ~]# nerdctl ps
CONTAINER ID    IMAGE                             COMMAND                   CREATED          STATUS    PORTS                 NAMES
5d1c3f1ca77f    docker.io/library/nginx:latest    "/docker-entrypoint.…"    6 hours ago      Up        0.0.0.0:80->80/tcp    nginx         
baf0b7d8f067    docker.io/library/centos:ssh      "/usr/sbin/sshd -D"       5 seconds ago    Up                              centos-ssh    

檢視容器centos-ssh的IP地址,IP地址為:10.4.0.6

[root@k8sworker2 ~]# nerdctl exec -it centos-ssh ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    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: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 66:ff:8f:43:5c:af brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.4.0.6/24 brd 10.4.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::64ff:8fff:fe43:5caf/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever

使用ssh連線容器centos-ssh,root密碼為:qweasd

[root@k8sworker2 ~]# ssh [email protected]
The authenticity of host '10.4.0.6 (10.4.0.6)' can't be established.
ECDSA key fingerprint is SHA256:gaD8Yb7WsdXxXtAx1Iwz2zkvzhYPb4TqluriL39OzxM.
ECDSA key fingerprint is MD5:e8:32:27:d1:75:58:8f:b3:5b:71:43:e8:fd:cc:7d:ee.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.4.0.6' (ECDSA) to the list of known hosts.
[email protected]'s password: 

#成功ssh進入容器
[root@baf0b7d8f067 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    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: eth0@if9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 66:ff:8f:43:5c:af brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 10.4.0.6/24 brd 10.4.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::64ff:8fff:fe43:5caf/64 scope link tentative dadfailed 
       valid_lft forever preferred_lft forever

[root@baf0b7d8f067 ~]# exit
logout
Connection to 10.4.0.6 closed.