您好,登錄后才能下訂單哦!
這篇文章主要介紹“nerdctl怎么使用”,在日常操作中,相信很多人在nerdctl怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”nerdctl怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
雖然 Docker 能干的事情,現在 Containerd
都能干,但 Containerd
還有一個非常明顯的缺陷:CLI 不夠友好。它無法像 Docker 和 Podman 一樣通過一條簡單的命令啟動一個容器,它的兩個 CLI 工具 ctr 和 crictl 都無法實現這么一件非常簡單的需求,而這個需求是大多數人都需要的,我總不能為了在本地測試容器而專門部署一個 Kubernetes 集群吧?
ctr 的設計對人類不太友好,例如缺少以下這些和 Docker 類似的功能:
docker run -p <PORT>
docker run --restart=always
通過憑證文件 ~/.docker/config.json
來拉取鏡像
docker logs
除此之外還有一個 CLI 工具叫 crictl
,和 ctr
一樣不太友好。
為了解決這個痛點,Containerd 官方推出了一個新的 CLI 叫 nerdctl。nerdctl 的使用體驗和 docker 一樣順滑,例如:
???? → nerdctl run -d -p 8080:80 --name=nginx --restart=always nginx
nerdctl
的目標并不是單純地復制 docker 的功能,它還實現了很多 docker 不具備的功能,例如延遲拉取鏡像(lazy-pulling)、鏡像加密(imgcrypt)等。
延遲拉取鏡像功能可以參考這篇文章:Containerd 使用 Stargz Snapshotter 延遲拉取鏡像。
雖然這些功能預計最終也會在 Docker 中實現,但可能需要幾個月甚至幾年的時間,因為 Docker 目前的設計只使用一小部分 Containerd
子系統。將來 Docker 有可能重構代碼以使用完整的 Containerd
,但目前還沒看到什么實質性進展。所以 Containerd
社區決定創建一個新的 CLI 來更友好地使用 Containerd
。
你可以從 nerdctl 的 release 中下載最新的可執行文件,每一個版本都有兩種可用的發行版:
nerdctl-<VERSION>-linux-amd64.tar.gz
: 只包含 nerdctl。
nerdctl-full-<VERSION>-linux-amd64.tar.gz
: 包含了 nerdctl 和相關依賴組件(containerd, runc, CNI, …)。
如果你已經安裝了 Containerd,只需要選擇前一個發行版,否則就選擇完整版。
安裝好 nerdctl 后,就可以使用 nerdctl 來運行容器了:
???? → nerdctl run -d -p 80:80 --name=nginx --restart=always nginx:alpine docker.io/library/nginx:alpine: resolved |++++++++++++++++++++++++++++++++++++++| index-sha256:d33e9e24389d7d8b90fe2bcc2dd1bc09b4d235e916ba9d5d9a71cf52e340edb6: done |++++++++++++++++++++++++++++++++++++++| manifest-sha256:c1f4e1974241c3f9ddb2866b2bf8e7afbceaa42dae82aabda5e946d03f054ed2: done |++++++++++++++++++++++++++++++++++++++| config-sha256:bfad9487e175364fd6315426feeee34bf5e6f516d2fe6a4e9b592315e330828e: done |++++++++++++++++++++++++++++++++++++++| layer-sha256:29d3f97df6fd99736a0676f9e57e53dfa412cf60b26d95008df9da8197f1f366: done |++++++++++++++++++++++++++++++++++++++| layer-sha256:9aae54b2144e5b2b00c610f8805128f4f86822e1e52d3714c463744a431f0f4a: done |++++++++++++++++++++++++++++++++++++++| layer-sha256:a5f0adaddd5456b7c5a3753ab541b5fad750f0a6499a15f63571b964eb3e2616: done |++++++++++++++++++++++++++++++++++++++| layer-sha256:5df810e1c460527fe400cdd2cab62228f5fb3da0f2dce86a6a6c354972f19b6e: done |++++++++++++++++++++++++++++++++++++++| layer-sha256:345aee38d3533398e0eb7118e4323a8970f7615136f2170dfb2b0278bbd9099d: done |++++++++++++++++++++++++++++++++++++++| layer-sha256:e6a4c36d7c0e358e5fc02ccdac645b18b85dcfec09d4fb5f8cbdc187ce9467a0: done |++++++++++++++++++++++++++++++++++++++| elapsed: 5.7 s total: 9.4 Mi (1.6 MiB/s) 27b55e0b18b10c4c8f34e3ba709614e7b1760a75db061d2ce5183e8b1101ce09
查看創建的容器:
???? → nerdctl ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3b5faa266a43 docker.io/library/nginx:alpine "/docker-entrypoint.…" 3 minutes ago Up 0.0.0.0:80->80/tcp nginx
和 Docker 一樣,Containerd 也有一個子命令 network
:
???? → nerdctl network ls NETWORK ID NAME FILE 0 bridge k8s-pod-network /etc/cni/net.d/10-calico.conflist host none
來看下默認的 bridge 配置:
???? → nerdctl network inspect bridge [ { "CNI": { "cniVersion": "0.4.0", "name": "bridge", "nerdctlID": 0, "plugins": [ { "type": "bridge", "bridge": "nerdctl0", "isGateway": true, "ipMasq": true, "hairpinMode": true, "ipam": { "type": "host-local", "routes": [ { "dst": "0.0.0.0/0" } ], "ranges": [ [ { "subnet": "10.4.0.0/24", "gateway": "10.4.0.1" } ] ] } }, { "type": "portmap", "capabilities": { "portMappings": true } }, { "type": "firewall" }, { "type": "tuning" } ] }, "NerdctlID": 0 } ]
可以看到 network
子命令背后還是 CNI 在運作,與 docker network 子命令原理不同。
nerdctl
也可以和 buildkit
結合使用來構建容器鏡像,需要先下載 buildkit 的可執行文件:
???? → wget https://github.com/moby/buildkit/releases/download/v0.8.2/buildkit-v0.8.2.darwin-amd64.tar.gz
將其解壓到 $PATH
中:
???? → tar -C /usr/local/ -zxvf buildkit-v0.8.2.linux-amd64.tar.gz
編寫 systemd unit 文件:
# /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
啟用 buildkit.service 并設置開機自動運行:
???? → systemctl enable --now buildkit.service
下面以 KubeSphere 項目為例,展示如何使用 nerdctl
來構建鏡像。
首先克隆 KubeSphere 官方倉庫:
???? → git clone --depth=1 https://github.com.cnpmjs.org/kubesphere/kubesphere.git
進入倉庫目錄,編譯二進制文件:
???? → cd kubesphere ???? → make ks-apiserver
將二進制文件拷貝到 Dockerfile
目錄:
???? → cp bin/cmd/ks-apiserver build/ks-apiserver
進入 Dockerfile
目錄,修改 Dockerfile:
# Copyright 2020 The KubeSphere Authors. All rights reserved. # Use of this source code is governed by an Apache license # that can be found in the LICENSE file. FROM alpine:3.11 ARG HELM_VERSION=v3.5.2 RUN apk add --no-cache ca-certificates # install helm RUN wget https://get.helm.sh/helm-${HELM_VERSION}-linux-amd64.tar.gz && \ tar xvf helm-${HELM_VERSION}-linux-amd64.tar.gz && \ rm helm-${HELM_VERSION}-linux-amd64.tar.gz && \ mv linux-amd64/helm /usr/bin/ && \ rm -rf linux-amd64 # To speed up building process, we copy binary directly from make # result instead of building it again, so make sure you run the # following command first before building docker image # make ks-apiserver # COPY ks-apiserver /usr/local/bin/ EXPOSE 9090 CMD ["sh"]
構建鏡像:
???? → cd build/ks-apiserver ???? → nerdctl build -t ks-apiserver . [+] Building 22.6s (9/9) FINISHED => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 812B 0.0s => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load metadata for docker.io/library/alpine:3.11 1.0s => [1/4] FROM docker.io/library/alpine:3.11@sha256:bf5fa774f08a9ed2cb301e522b769d43d48124315a4ec50eae3228d03b9dc558 7.9s => => resolve docker.io/library/alpine:3.11@sha256:bf5fa774f08a9ed2cb301e522b769d43d48124315a4ec50eae3228d03b9dc558 0.0s => => sha256:9b794450f7b6db7c944ba1f4161edb68cb535052fe7db8ac06e613516c4a658d 2.10MB / 2.82MB 21.4s => => extracting sha256:9b794450f7b6db7c944ba1f4161edb68cb535052fe7db8ac06e613516c4a658d 0.1s => [internal] load build context 1.0s => => transferring context: 115.87MB 1.0s => [2/4] RUN apk add --no-cache ca-certificates 2.7s => [3/4] RUN wget https://get.helm.sh/helm-v3.5.2-linux-amd64.tar.gz && tar xvf helm-v3.5.2-linux-amd64.tar.gz && rm helm-v3.5.2-linux-amd64.tar.gz && mv linux-amd64 4.7s => [4/4] COPY ks-apiserver /usr/local/bin/ 0.2s => exporting to oci image format 5.9s => => exporting layers 4.6s => => exporting manifest sha256:d7eb2a90496678d11ac5c363b7743ffe2b8e23e7071b94556a5e3231f50f5a6e 0.0s => => exporting config sha256:8eb6a5187ce958e76c8d37e18221d88f25b48dd7e6672021d0fce21bb071f284 0.0s => => sending tarball 1.3s unpacking docker.io/library/ks-apiserver:latest (sha256:d7eb2a90496678d11ac5c363b7743ffe2b8e23e7071b94556a5e3231f50f5a6e)...done unpacking overlayfs@sha256:d7eb2a90496678d11ac5c363b7743ffe2b8e23e7071b94556a5e3231f50f5a6e (sha256:d7eb2a90496678d11ac5c363b7743ffe2b8e23e7071b94556a5e3231f50f5a6e)...done
查看構建好的鏡像:
???? → nerdctl images REPOSITORY TAG IMAGE ID CREATED SIZE alpine 3.11 bf5fa774f08a 3 seconds ago 2.7 MiB ks-apiserver latest d7eb2a904966 6 minutes ago 57.7 MiB
到此,關于“nerdctl怎么使用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。