面向?qū)ο缶幊?/a>詳解">JavaScript面向?qū)ο缶幊?/a>詳解
1888
2025-04-03
一 背景
之前使用Docker,但是在一些場景Docker不是很適用,Docker是一個C/S架構(gòu),運行容器需要Daemon,但是一下簡單測試或者CI/CD中,沒有Daemon,或者沒有root權(quán)限,此時就可以使用其他的一些遵循OCI接口規(guī)范的工具,例如紅帽的podman,其是fork/exec模型,直接通過 OCI runtime(默認也是 runc)來啟動容器,無需Daemon后臺進程,所以利用podman啟動的容器是podman的子進程,
二 概念
Podman 是 Libpod 的一部分,它的定義可以簡單用這個命令表示:alias docker=podman
Libpod 是一個創(chuàng)建容器 pod 的工具和庫,它包含 pod 管理工具 Podman,Podman 管理 pod、容器、容器鏡像和容器卷。
在較高的層面上,Libpod 和 Podman 的作用范圍如下:
支持多種鏡像格式,包括 OCI 和 Docker。
支持多種方式下載鏡像,包括信任和鏡像驗證。
容器鏡像管理,管理鏡像層、覆蓋文件系統(tǒng)等。
全面管理容器生命周期。
支持 pod 管理容器組。
pos 和容器的資源隔離。
與 CRI-O 集成以共享容器和后端代碼。
支持 Fedora、RHEL 與 Ubuntu 等的不同版本。
允許 Podman CLI 使用 Varlink 后端連接到遠程 Podman 實例。
將 Libpod 集成到 CRI-O 中以替換其現(xiàn)有的容器管理后端。
進一步改進 Podman pod 命令
不需要 root(rootless)容器的進一步改進
2.1 OCI
因為它們(包括Docker)都遵循OCI (Open Container Initiative)下的相同規(guī)范。它們包含了容器運行時、容器分發(fā)和容器鏡像的規(guī)范,其中涵蓋了使用容器所需的所有特性。
有了OCI,你可以選擇一套最符合你需求的工具,同時你仍然可以享受跟Docker一樣使用相同的API和CLI命令。
2.2 容器引擎
目前已經(jīng)有許多容器引擎,但Docker最突出的競爭對手是由紅帽開發(fā)的Podman。與Docker不同,Podman不需要Daemon來運行,也不需要root特權(quán),這是Docker長期以來一直關(guān)注的問題。基于它的名字,Podman不僅可以運行容器,還可以運行pods。
LXD——LXC (Linux Containers)是一個容器管理器(守護進程)。該工具提供了運行系統(tǒng)容器的能力,這些系統(tǒng)容器提供了更類似于VM的容器環(huán)境。它位于非常狹窄的空間,沒什么用戶,所以除非你有非常具體的實例,否則最好還是使用Docker或Podman。
CRI-O——當你Google什么是CRI-O你可能會發(fā)現(xiàn)它被描述為容器引擎。不過,實際上它只是容器運行時。其實它既不是引擎,也不適合“正常”使用。我的意思是,它是專門為Kubernetes運行時(CRI)而構(gòu)建的,而不是為最終用戶使用的。
Rkt——rkt(“火箭”)是由CoreOS開發(fā)的容器引擎。這里提到這個項目只是為了完整性,因為這個項目已經(jīng)結(jié)束,開發(fā)也停止了——所以也就沒必要再使用了。
Podman 原來是 CRI-O 項目的一部分,后來被分離成一個單獨的項目叫 libpod。Podman 的使用體驗和 Docker 類似,不同的是 Podman 沒有 daemon。以前使用 Docker CLI 的時候,Docker CLI 會通過 gRPC API 去跟 Docker Engine 說「我要啟動一個容器」,然后 Docker Engine 才會通過 OCI Container runtime(默認是 runc)來啟動一個容器。這就意味著容器的進程不可能是 Docker CLI 的子進程,而是 Docker Engine 的子進程。
Podman 比較簡單粗暴,它不使用 Daemon,而是直接通過 OCI runtime(默認也是 runc)來啟動容器,所以容器的進程是 podman 的子進程。這比較像 Linux 的 fork/exec 模型,而 Docker 采用的是 C/S(客戶端/服務(wù)器)模型。與 C/S 模型相比,fork/exec 模型有很多優(yōu)勢,比如:
系統(tǒng)管理員可以知道某個容器進程到底是誰啟動的。
如果利用 cgroup 對 podman 做一些限制,那么所有創(chuàng)建的容器都會被限制。
SD_NOTIFY : 如果將 podman 命令放入 systemd 單元文件中,容器進程可以通過 podman 返回通知,表明服務(wù)已準備好接收任務(wù)。
socket 激活 : 可以將連接的 socket 從 systemd 傳遞到 podman,并傳遞到容器進程以便使用它們。
廢話不多說,下面我們直接進入實戰(zhàn)環(huán)節(jié),本文將手把手教你如何用 podman 來部署靜態(tài)博客,并通過 Sidecar 模式將博客所在的容器加入到 Envoy mesh 之中。
三 實操
3.1 安裝
Centos 8 默認使用podman
$ sudo yum -y install podman
配置加速
改名并備份好文件:/etc/containers/registries.conf
再新建一個空的 registries.conf 文件,插入如下內(nèi)容
unqualified-search-registries = ["docker.io"] [[registry]] prefix = "docker.io" location = "5980zxy5.mirror.aliyuncs.com"
$ brew install podman
啟用podman服務(wù)
$ podman images ls Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: Get "http://d/v3.4.0/libpod/_ping": dial unix ///var/folders/wn/367g1v9n1bv0sg1k8qldzym80000gn/T/podman-run--1/podman/podman.sock: connect: no such file or directory # 需要啟動machine $ podman machine init Downloading VM image: fedora-coreos-34.20211004.2.0-qemu.x86_64.qcow2.xz: done Extracting compressed file $ podman machine start INFO[0000] waiting for clients... INFO[0000] listening tcp://0.0.0.0:7777 INFO[0000] new connection from to /var/folders/wn/367g1v9n1bv0sg1k8qldzym80000gn/T/podman/qemu_podman-machine-default.sock Waiting for VM ... 2Machine "podman-machine-default" started successfully
注意:再次使用的微vm使用的鏡像,不是宿主機的鏡像。
3.2 基礎(chǔ)命令
Manage pods, containers and images Usage: podman [options] [command] Available Commands: attach Attach to a running container build Build an image using instructions from Containerfiles commit Create new image based on the changed container container Manage containers create Create but do not start a container diff Display the changes to the object's file system events Show podman events exec Run a process in a running container export Export container's filesystem contents as a tar archive generate Generate structured data based on containers and pods. healthcheck Manage health checks on containers help Help about any command history Show history of a specified image image Manage images images List images in local storage import Import a tarball to create a filesystem image info Display podman system information init Initialize one or more containers inspect Display the configuration of object denoted by ID kill Kill one or more running containers with a specific signal load Load image(s) from a tar archive login Login to a container registry logout Logout of a container registry logs Fetch the logs of one or more containers manifest Manipulate manifest lists and image indexes network Manage networks pause Pause all the processes in one or more containers play Play a pod and its containers from a structured file. pod Manage pods port List port mappings or a specific mapping for the container ps List containers pull Pull an image from a registry push Push an image to a specified destination restart Restart one or more containers rm Remove one or more containers rmi Removes one or more images from local storage run Run a command in a new container save Save image(s) to an archive search Search registry for image start Start one or more containers stats Display a live stream of container resource usage statistics stop Stop one or more containers system Manage podman tag Add an additional name to a local image top Display the running processes of a container unpause Unpause the processes in one or more containers untag Remove a name from a local image version Display the Podman Version Information volume Manage volumes wait Block on one or more containers Options: -c, --connection string Connection to use for remote Podman service --help Help for podman --identity string path to SSH identity file, (CONTAINER_SSHKEY) --log-level string Log messages above specified level (debug, info, warn, error, fatal, panic) (default "error") --url string URL to access Podman service (CONTAINER_HOST) (default "unix:/var/folders/wn/367g1v9n1bv0sg1k8qldzym80000gn/T/podman-run--1/podman/podman.sock") -v, --version version for podman
3.3 運行一個基礎(chǔ)命令
$ podman --remote info host: arch: amd64 buildahVersion: 1.15.1 cgroupVersion: v1 conmon: package: conmon-2.0.20-2.module_tl3+88+de755738.x86_64 path: /usr/bin/conmon version: 'conmon version 2.0.20, commit: a9ae6b9ffaadd564332ddf93b4641d48137430bf' cpus: 2 distribution: distribution: '"tencentos"' version: "3.1" eventLogger: file hostname: xuel-terraform-cvm-0 idMappings: gidmap: null uidmap: null kernel: 5.4.119-19-0006 linkmode: dynamic memFree: 2587340800 memTotal: 3851665408 ociRuntime: name: runc package: runc-1.0.0-68.rc92.module_tl3+88+de755738.x86_64 path: /usr/bin/runc version: 'runc version spec: 1.0.2-dev' os: linux remoteSocket: path: /run/podman/podman.sock rootless: false slirp4netns: executable: "" package: "" version: "" swapFree: 0 swapTotal: 0 uptime: 8m 7.8s registries: search: - registry.access.redhat.com - registry.redhat.io - docker.io store: configFile: /etc/containers/storage.conf containerStore: number: 0 paused: 0 running: 0 stopped: 0 graphDriverName: overlay graphOptions: overlay.mountopt: nodev,metacopy=on graphRoot: /var/lib/containers/storage graphStatus: Backing Filesystem: extfs Native Overlay Diff: "false" Supports d_type: "true" Using metacopy: "true" imageStore: number: 0 runRoot: /var/run/containers/storage volumePath: /var/lib/containers/storage/volumes version: APIVersion: 1 Built: 1618972697 BuiltTime: Wed Apr 21 10:38:17 2021 GitCommit: "" GoVersion: go1.14.12 OsArch: linux/amd64 Version: 2.0.5
# 拉取容器 $ podman pull nginx:latest # 運行容器 $ podman run -itd -p 80:80 nginx:latest 6ccd9783fe8b868ef273618570bfff5d40abea9c45e252e5033a2f4b3f6725f8 # 查看容器 $ podman ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6ccd9783fe8b docker.io/library/nginx:latest nginx -g daemon o... 9 seconds ago Up 8 seconds ago 0.0.0.0:80->80/tcp affectionate_rubin # 查看容器日志 $ podman logs 6ccd9783fe8b /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh 10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf 10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh /docker-entrypoint.sh: Configuration complete; ready for start up 2021/10/08 06:28:55 [notice] 1#1: using the "epoll" event method
podman 基本上和docker命令是一致的,如果熟悉docker,可以將podman,alias為docker來使用
$ alias docker='podman' $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6ccd9783fe8b docker.io/library/nginx:latest nginx -g daemon o... 3 minutes ago Up 3 minutes ago 0.0.0.0:80->80/tcp affectionate_rubin $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/library/nginx latest f8f4ffc8092c 9 days ago 138 MB
docker 項目容器操作再次就不詳細演示。
3.4 運行pod
podman顧名思義,其可以對接K8s,實現(xiàn)直接創(chuàng)建Pod,
$ docker pod --help Manage pods Description: Pods are a group of one or more containers sharing the same network, pid and ipc namespaces. Usage: podman pod [command] Available Commands: create Create a new empty pod exists Check if a pod exists in local storage inspect Displays a pod configuration kill Send the specified signal or SIGKILL to containers in pod pause Pause one or more pods prune Remove all stopped pods and their containers ps List pods restart Restart one or more pods rm Remove one or more pods start Start one or more pods stats Display a live stream of resource usage statistics for the containers in one or more pods stop Stop one or more pods top Display the running processes of containers in a pod unpause Unpause one or more pods
運行pod依賴與pause鏡像,需要先拉取下來
# 如果不能訪問k8s.gcr.io,可以從其他源拉取再修改tag $ docker pull mirrorgooglecontainers/pause:3.1 $ docker tag docker.io/mirrorgooglecontainers/pause:3.1 k8s.gcr.io/pause:3.1
啟動pod
# 創(chuàng)建pod $ docker pod create --name mynginxpod f26515e8caa7c5d84b15cef6702d1ba6f83be3e6760c96f0619f9f60ac5df1e0 # 查看pod $ docker pod ls POD ID NAME STATUS CREATED # OF CONTAINERS INFRA ID 0560f1744dee mynginxpod Created 2 seconds ago 1 375497f7b909 # 在pod中運行容器 $ docker run -d --pod mynginxpod nginx:latest 9f9696382299d0eef711c8ec68baa02b49635e7b9a87f22d1d951e9a326b41a9 $ docker pod ls POD ID NAME STATUS CREATED # OF CONTAINERS INFRA ID 0560f1744dee mynginxpod Running 44 seconds ago 2 375497f7b909 # 查看pod中所有容器 $ docker ps -pa CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES POD ID PODNAME 375497f7b909 docker.io/mirrorgooglecontainers/pause:3.1 About an hour ago Up About an hour ago 0560f1744dee-infra 0560f1744dee mynginxpod 9f9696382299 docker.io/library/nginx:latest nginx -g daemon o... About an hour ago Up About an hour ago pensive_montalcini 0560f1744dee mynginxpod # 查看資源使用情況 $ docker pod top mynginxpod USER PID PPID %CPU ELAPSED TTY TIME COMMAND 0 1 0 0.000 1h9m55.211491679s ? 0s /pause root 1 0 0.000 1h9m55.212004807s ? 0s nginx: master process nginx -g daemon off; nginx 30 1 0.000 1h9m55.212053645s ? 0s nginx: worker process nginx 31 1 0.000 1h9m55.212089686s ? 0s nginx: worker process
3.5 導(dǎo)出資源清單
$ docker generate kube mynginxpod > mynginxpod.yaml $ cat mynginxpod.yaml # Generation of Kubernetes YAML is still under development! # # Save the output of this file and use kubectl create -f to import # it into Kubernetes. # # Created with podman-2.0.5 apiVersion: v1 kind: Pod metadata: creationTimestamp: "2021-10-08T08:14:23Z" labels: app: mynginxpod name: mynginxpod spec: containers: - command: - nginx - -g - daemon off; env: - name: PATH value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin - name: TERM value: xterm - name: PKG_RELEASE value: 1~buster - name: container value: podman - name: NGINX_VERSION value: 1.21.3 - name: NJS_VERSION value: 0.6.2 - name: HOSTNAME value: mynginxpod image: docker.io/library/nginx:latest name: pensivemontalcini resources: {} securityContext: allowPrivilegeEscalation: true capabilities: {} privileged: false readOnlyRootFilesystem: false seLinuxOptions: {} workingDir: / status: {} --- metadata: creationTimestamp: null spec: {} status: loadBalancer: {}
該文件為兼容k8s的pod資源清單文件,可以通過該文件直接創(chuàng)建pod
$ docker pod ls POD ID NAME STATUS CREATED # OF CONTAINERS INFRA ID 0560f1744dee mynginxpod Running About an hour ago 2 375497f7b909 # 刪除pod $ docker pod rm -f mynginxpod 0560f1744dee0492627ea28e5f19664dcee33e51a1ebf2243a6be9b5ab2f6331 $ docker pod ls POD ID NAME STATUS CREATED # OF CONTAINERS INFRA ID $ docker play kube mynginxpod.yaml Trying to pull docker.io/library/nginx:latest... Getting image source signatures Copying blob 4ce73aa6e9b0 skipped: already exists Copying blob 44ac32b0bba8 skipped: already exists Copying blob bbe0b7acc89c skipped: already exists Copying blob 07aded7c29c6 skipped: already exists Copying blob 91d6e3e593db skipped: already exists Copying blob 8700267f2376 [--------------------------------------] 0.0b / 0.0b Copying config f8f4ffc809 done Writing manifest to image destination Storing signatures Pod: 3ca51af554315e1c828f81ca29a5dbaf3a256a5037170b4378ef0bc8ad1824c5 Container: 3d5c5a674f6140d86138e288db73eee1594de7598da2dd982f29299cbe97ecef $ docker pod ls POD ID NAME STATUS CREATED # OF CONTAINERS INFRA ID 3ca51af55431 mynginxpod Running 44 seconds ago 2 ba4e1570dc74
podman 由兩部分組成,一個是 podman CLI,還有一個是 container runtime,container runtime 由 conmon 來負責,主要包括監(jiān)控、日志、TTY 分配以及類似 out-of-memory 情況的雜事。也就是說,conmon 是所有容器的父進程。
3.6 運行兩個容器
$ docker pod ls POD ID NAME STATUS CREATED # OF CONTAINERS INFRA ID 3ca51af55431 mynginxpod Running 17 minutes ago 2 ba4e1570dc74 # 同一個pod中啟動三個容器 $ docker run -d --pod mynginxpod tomcat:latest 08b77aeb728668d2ecea9cffe86d03e1177c903fcfcee8c91fee5816e3172d22 $ docker pod ls POD ID NAME STATUS CREATED # OF CONTAINERS INFRA ID 3ca51af55431 mynginxpod Running 17 minutes ago 3 ba4e1570dc74
四 相關(guān)概念
4.1 容器引擎
Container Engine是一種工具,它為處理鏡像和容器提供用戶界面,遠程倉庫提取鏡像并將其擴展到磁盤。它看起來也是運行容器,但實際上它的工作是創(chuàng)建容器清單和帶有鏡像層的目錄。然后它將它們傳遞到容器運行時,如runC或Crun
Docker:
Podman:
LXD——LXC (Linux Containers)是一個容器管理器(守護進程)。該工具提供了運行系統(tǒng)容器的能力,這些系統(tǒng)容器提供了更類似于VM的容器環(huán)境。它位于非常狹窄的空間,沒什么用戶,所以除非你有非常具體的實例,否則最好還是使用Docker或Podman。
CRI-O——當你Google什么是CRI-O你可能會發(fā)現(xiàn)它被描述為容器引擎。不過,實際上它只是容器運行時。其實它既不是引擎,也不適合“正常”使用。我的意思是,它是專門為Kubernetes運行時(CRI)而構(gòu)建的,而不是為最終用戶使用的。
Rkt——rkt(“火箭”)是由CoreOS開發(fā)的容器引擎。這里提到這個項目只是為了完整性,因為這個項目已經(jīng)結(jié)束,開發(fā)也停止了——所以也就沒必要再使用了。
4.2 鏡像構(gòu)建
Docker
Buildah紅帽開發(fā),目前已經(jīng)集成在podman中,無守護程序和無根的,并遵循OCI的鏡像標準,所以它能保證所構(gòu)建的鏡像和Docker構(gòu)建的是一樣的
Kaniko谷歌開發(fā),Kaniko也是從Dockerfile構(gòu)建容器鏡像,跟Buildah類似,也不需要守護進程。與Buildah的主要區(qū)別在于,Kaniko更專注于在Kubernetes中構(gòu)建鏡像。Kaniko使用gcr.io/ Kaniko -project/executor作為鏡像運行。這對于Kubernetes來說是行得通的,但是對于本地構(gòu)建來說不是很方便,并且在某種程度上違背了它的初衷,因為我們得先使用Docker來運行Kaniko鏡像,然后再去構(gòu)建鏡像。也就是說,如果正在為Kubernetes集群中構(gòu)建鏡像的工具進行選型(例如在CI/CD Pipeline中),那么Kaniko可能是一個不錯的選擇,因為它是無守護程序的,而且(可能)更安全。
4.3 容器運行時
runC:是基于OCI容器運行時規(guī)范創(chuàng)建的,且最流行的容器運行時。Docker(通過containerd)、Podman和crio使用它,所以幾乎所有東西都依賴于LXD。它幾乎是所有產(chǎn)品/工具的默認首選項,所以即使你在閱讀本文后放棄Docker,但你仍然會用到runC。
CRI-O:因為它被構(gòu)建為用于Kubernetes節(jié)點上的運行時,可以看到它被描述為“Kubernetes需要的所有運行時,僅此而已”。因此,除非你正在設(shè)置Kubernetes集群(或OpenShift集群——CRI-O已經(jīng)是默認首選項了),否則不大可能會接觸到這個。
containerd:它是一個守護進程,充當各種容器運行時和操作系統(tǒng)的API。在后臺,它依賴于runC,是Docker引擎的默認運行時。谷歌Kubernetes引擎(GKE)和IBM Kubernetes服務(wù)(IKS)也在使用。它是Kubernetes容器運行時接口的一個部署(與CRI-O相同),因此它是Kubernetes集群運行時的一個很好的備選項。
Kata:kata containers是由OpenStack基金會管理,但獨立于OpenStack項目之外的容器項目。kata containers整合了Intel的 Clear Containers 和 Hyper.sh 的 runV,能夠支持不同平臺的硬件 (x86-64,arm等),并符合OCI(Open Container Initiative)規(guī)范,同時還可以兼容k8s的 CRI(Container Runtime Interface)接口規(guī)范。項目包含幾個配套組件,即Runtime,Agent, Proxy,Shim等。項目已于6月份release了1.0版本。Kata最大的亮點是解決了傳統(tǒng)容器共享內(nèi)核的安全和隔離問題
參考鏈接
https://podman.io/
https://blog.csdn.net/alex_yangchuansheng/article/details/102618128
https://github.com/containers/podman/blob/main/docs/tutorials/mac_win_client.md
Docker Kubernetes 容器
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應(yīng)法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔相應(yīng)法律責任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實后本網(wǎng)站將在24小時內(nèi)刪除侵權(quán)內(nèi)容。