Podman – Compose (YAML)

Loading

測試環境為 CentOS 8 x86_64 虛擬機.

使用一般使用者 ben (非 sudo user)登入並操作.

下面使用 Nginx (http server) 為範例.

[ben@localhost ~]$ podman search nginx
docker.io  docker.io/library/nginx                               Official build of Nginx.                         14935   [OK]      
...
[root@localhost ~]# podman pull docker.io/library/nginx 
Trying to pull docker.io/library/nginx:latest...
Getting image source signatures
Copying blob 8283eee92e2f done  
Copying blob febe5bd23e98 done  
Copying blob 69692152171a done  
Copying blob 30afc0b18f67 done  
Copying blob 351ad75a6cfa done  
Copying blob 596b1d696923 done  
Copying config d1a364dc54 done  
Writing manifest to image destination
Storing signatures
d1a364dc548d5357f0da3268c888e1971bbdb957ee3f028fe7194f1d61c6fdee
[root@localhost ~]# podman images
REPOSITORY               TAG     IMAGE ID      CREATED     SIZE
docker.io/library/nginx  latest  d1a364dc548d  6 days ago  137 MB

CMD

下面範例會用本地端 ~/docker-nginx/html 資料夾去取代 Container 內的 /usr/share/nginx/html 資料夾,先建立該資料夾.

[ben@localhost ~]$ mkdir -p ~/docker-nginx/html
[ben@localhost ~]$ vi ~/docker-nginx/html/index.html
Local index.html

使用指令的方式來建立 Nginx 的 Container .

[ben@localhost ~]$ podman run --pod new:mypod --name docker-nginx -p 8080:80 -d -v ~/docker-nginx/html:/usr/share/nginx/html nginx
0f22d07fed4f10787e6e62980007a6b257b16638377012ba7904ebe6367b6080

使用參數:

  • –pod=name
    Run container in an existing pod. If you want Podman to make the pod for you, prefix the pod name with new:.
    上面範例將容器放入 singular pod 中,這些容器可以直接使用 localhost 進行溝通.

    Podman pod 中的所有 Container 共享相同的 network namespace ( namespace 命名空間是 Linux 核心 2.6.24 之後的一個功能,它可以隔離和虛擬化 processes 相關系統資源,虛擬化資源包括 process IDs, hostnames, user IDs, network access, interprocess communication, 以及 filesystems . ) ,這意味著 Containers 將使用相同的 IP 地址 , MAC 地址 與 Port mapping 網路埠映射.

    參數 –pod new:mypod 指定了一個新的 pod 名稱為 mypod (也可以是先建立 podman pod create –name testpod),從以下指令可以看到.

    [ben@localhost ~]$ podman pod ls
    POD ID        NAME    STATUS    CREATED             INFRA ID      # OF CONTAINERS
    d3aae47d8cc6  mypod   Degraded  About a minute ago  ade5ec81c311  2
    
  • –name=name
    Assign a name to the container.
  • –publish, -p=ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort
    Publish a container’s port, or range of ports, to the host.
  • –detach, -d=true|false
    Detached mode: run the container in the background and print the new container ID.
  • –volume, -v[=[[SOURCE-VOLUME|HOST-DIR:]CONTAINER-DIR[:OPTIONS]]]
    Create a bind mount. If you specify /HOST-DIR:/CONTAINER-DIR, Podman bind mounts host-dir in the host to CONTAINER-DIR in the Podman container.
    用本地端 ~/docker-nginx/html 資料夾去取代 Container 內的 /usr/share/nginx/html 資料夾.

檢視一下剛剛建立的 pod 與 container .

[ben@localhost ~]$ podman ps -a
CONTAINER ID  IMAGE                                         COMMAND               CREATED         STATUS             PORTS                 NAMES
6c0d2f9b37a2  registry.access.redhat.com/ubi8/pause:latest                        17 seconds ago  Up 18 seconds ago  0.0.0.0:8080->80/tcp  2ca433d84b42-infra
0f22d07fed4f  docker.io/library/nginx:latest                nginx -g daemon o...  17 seconds ago  Up 18 seconds ago  0.0.0.0:8080->80/tcp  docker-nginx
[ben@localhost ~]$ podman port -a
0f22d07fed4f	80/tcp -> 0.0.0.0:8080
6c0d2f9b37a2	80/tcp -> 0.0.0.0:8080
[ben@localhost ~]$ podman pod list
POD ID        NAME    STATUS   CREATED         INFRA ID      # OF CONTAINERS
2ca433d84b42  mypod   Running  31 seconds ago  6c0d2f9b37a2  2

YAML

剛剛參數很多,可以把它寫成 Kubernetes YAML 檔案,可以使用指令 #podman generate kube pod_name/pod_id 來將現有的環境轉成 YAML 檔案.

[ben@localhost ~]$ podman generate kube mypod -f docker-nginx.yaml

使用參數 :

  • –filename, -f=filename
    Output to the given file, instead of STDOUT.

格式如下(可以對照一下剛剛的指令就會知道相對應的欄位).

[ben@localhost ~]$ cat docker-nginx.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-3.1.0-dev
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-06-02T11:59:25Z"
  labels:
    app: mypod
  name: mypod
spec:
  containers:
  - args:
    - nginx
    - -g
    - daemon off;
    command:
    - /docker-entrypoint.sh
    env:
    - name: PATH
      value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    - name: TERM
      value: xterm
    - name: container
      value: podman
    - name: NGINX_VERSION
      value: 1.21.0
    - name: NJS_VERSION
      value: 0.5.3
    - name: PKG_RELEASE
      value: 1~buster
    image: docker.io/library/nginx:latest
    name: docker-nginx
    ports:
    - containerPort: 80
      hostPort: 8080
      protocol: TCP
    resources: {}
    securityContext:
      allowPrivilegeEscalation: true
      capabilities:
        drop:
        - CAP_MKNOD
        - CAP_AUDIT_WRITE
      privileged: false
      readOnlyRootFilesystem: false
      seLinuxOptions: {}
    volumeMounts:
    - mountPath: /usr/share/nginx/html
      name: home-ben-docker-nginx-html-host
    workingDir: /
  dnsConfig: {}
  restartPolicy: Never
  volumes:
  - hostPath:
      path: /home/ben/docker-nginx/html
      type: Directory
    name: home-ben-docker-nginx-html-host
status: {}

可以使用這個 YAML 檔案來建立 pod 與 Container,先把現有環境的 pod 與 Container 移除.

[ben@localhost ~]$ podman stop docker-nginx
docker-nginx
[ben@localhost ~]$ podman rm docker-nginx
0f22d07fed4f10787e6e62980007a6b257b16638377012ba7904ebe6367b6080
[ben@localhost ~]$ podman pod rm mypod
2ca433d84b420dd0ae88c9b32a035392805fbfeed6d5d837f4ad31850b8b6ab5

現在可以使用指令 #podman play kube YAML 檔案來建立 pod 與 Container .

[ben@localhost ~]$ podman play kube docker-nginx.yaml 
Trying to pull docker.io/library/nginx:latest...
Getting image source signatures
Copying blob 351ad75a6cfa skipped: already exists  
Copying blob 69692152171a skipped: already exists  
Copying blob 30afc0b18f67 skipped: already exists  
Copying blob 596b1d696923 skipped: already exists  
Copying blob febe5bd23e98 skipped: already exists  
Copying blob 8283eee92e2f [--------------------------------------] 0.0b / 0.0b
Copying config d1a364dc54 done  
Writing manifest to image destination
Storing signatures
Pod:
29c0f7cd4b158f3a3e610815a873a2686c8b7deb1d26705eac3240ece80e7aca
Container:
659a9b77d939a4e0c11cbaa821d45f712a49015d483b5207071cc618b12b6a10

可以看到 pod 與 container 都建立好了.

[ben@localhost ~]$ podman ps -a
CONTAINER ID  IMAGE                                         COMMAND               CREATED         STATUS             PORTS                 NAMES
fc371cc54372  registry.access.redhat.com/ubi8/pause:latest                        35 seconds ago  Up 27 seconds ago  0.0.0.0:8080->80/tcp  29c0f7cd4b15-infra
659a9b77d939  docker.io/library/nginx:latest                nginx -g daemon o...  27 seconds ago  Up 27 seconds ago  0.0.0.0:8080->80/tcp  mypod-docker-nginx
[ben@localhost ~]$ podman pod list
POD ID        NAME    STATUS   CREATED         INFRA ID      # OF CONTAINERS
29c0f7cd4b15  mypod   Running  41 seconds ago  fc371cc54372  2
沒有解決問題,試試搜尋本站其他內容

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料