文章目錄
-
-
-
-
- 1.podman介紹
- 2.與docker相比的優勢
- 3.相容性
- 4.背景服務單元檔案的優先級
- 5.podman基本操作
- 6.運作一個web容器
- 7.web容器設定開機自啟
-
-
-
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiIXZ05WZj91YpB3IwczX0xiRGZkRGZ0Xy9GbvNGL2EzXlpXazxSMJR0TxEFVPNTU6hFeGNDTwYVbiVHNHpleO1GTulzRilWO5xkNNh0YwIFSh9Fd4VGdsATMfd3bkFGazxyaHRGcWdUYuVzVa9GczoVdG1mWfVGc5RHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-AnYldnLlZTNkhDO3MjN5QzYhdTNhFGNhRzN4Q2MxEGO3M2NlV2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.webp)
1.podman介紹
podman之前是CRI-O項目的一部分,後被分離成獨立的項目libpod,libpod是一個建立容器pod的工具和庫,podman是個無守護程式容器引擎,以root使用者或無根模式運作,簡而言之podman提供了一個docker-CLI的指令行,管理着容器
2.與docker相比的優勢
docker劣勢一:
docker大家都知道,其守護程式在多個核心上占用差不多高達100%cpu資源,采用C/S模型
podman優勢一:
podman不需要守護程序,不需要root權限組,而且利用着使用者命名空間(namespace)模拟容器中的root運作,采用fork/exec模型。
fork/exec模型相比C/S模型優勢:
- 系統管理者知道某個容器由誰啟動
- 利用cgroup對podman做限制,對應着建立的容器也會受到限制
- systemd單元檔案的生成,可以管理着任務的啟動與關閉
- socket激活,将socker從systemd發送給podman容器使用
3.相容性
docker的功能大部分podman都是相容的,也可以使用别名(alias)來寫成docker的指令
4.背景服務單元檔案的優先級
/usr/lib/systemd/user:優先級最低,會被優先級高的同名 unit 覆寫
~/.local/share/systemd/user
/etc/systemd/user:全局共享的使用者級 unit[s]
~/.config/systemd/user:優先級最高
5.podman基本操作
安裝
#預設centos源
[[email protected] ~]# yum -y module install container-tools #容器工具基于子產品
[[email protected] ~]# yum -y install podman-docker #安裝docker相容包(可選)
版本
[[email protected] ~]# podman -v
podman version 3.3.0-dev
倉庫
官方倉庫:registry.access.redhat.com
第三方倉庫:docker.io
私有倉庫:registry.lab.example.com
指令幫助
[[email protected] ~]# podman help|head -15
Manage pods, containers and images
Usage:
podman [options] [command]
Available Commands:
attach Attach to a running container
auto-update Auto update containers according to their auto-update policy
build Build an image using instructions from Containerfiles
commit Create new image based on the changed container #基于修改的容器建立新的容器
container Manage containers
cp Copy files/folders between a container and the local filesystem
create Create but do not start a container
diff Display the changes to the object's file system
events Show podman events
....
鏡像加速器
修改配置檔案:/etc/containers/registries.conf 即可
注意:不能帶有httpds//:url格式
[[email protected] ~]# cp /etc/containers/registries.conf /backup/registries.conf.back #備份一下
[[email protected] ~]# vim /etc/containers/registries.conf
unqualified-search-registries = ["docker.io"] #非限定搜尋登記處
[[registry]]
prefix = "docker.io"
location = "x" #x是阿裡加速鏡像位址
拉取鏡像
6.運作一個web容器
背景啟動一個web容器,并通路容器内容
#準備html頁面内容
[root@192 ~]# cat /opt/webhtml/index.html
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambition
#運作一個守護web容器程序,将/opt/webhtml目錄内容映射到容器的/usr/share/nginx/html存放網頁的位置
[root@192 ~]# podman run -d --name web -p 8888:80 -v /opt/webhtml:/usr/share/nginx/html nginx
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c
[[email protected] ~]# curl 192.168.136.129:8888
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambition
#容器的ip
[[email protected] ~]# podman inspect web|grep IPAddress
"IPAddress": "10.88.0.6",
"IPAddress": "10.88.0.6",
#主控端的ip
[[email protected] ~]# ip r
192.168.136.0/24 dev ens33 proto kernel scope link src 192.168.136.129 metric 100
#由于進行了端口綁定,是以直接 curl 192.168.136.129:8888即可通路
進入背景web容器,檢視服務狀态
[[email protected] ~]# podman exec -it web bash
root@3528e6d5148b:/# service nginx status
[ ok ] nginx is running. #運作中
怎樣修改容器業務内容呢?很簡單
#修改主控端/opt/webhtml/index.html即可
[[email protected] ~]# cat /opt/webhtml/index.html
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS
RHCE RHCA
#進行通路
[[email protected] ~]# curl 192.168.136.129:8888
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS
RHCE RHCA
#進入容器檢視内容是否修改
[[email protected] ~]# podman exec -it web bash
root@3528e6d5148b:/# cat /usr/share/nginx/html/index.html
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS
RHCE RHCA
暫停與删除容器
#暫停
[[email protected] ~]# podman stop web
web
[[email protected] ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3528e6d5148b docker.io/library/nginx:latest nginx -g daemon o... 25 minutes ago Exited (0) 16 seconds ago 0.0.0.0:8888->80/tcp web
#删除
[[email protected] ~]# podman rm web
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c
#或強制删除運作中的容器
[[email protected] ~]# podman rm -f web
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c
7.web容器設定開機自啟
背景運作一個web容器
[[email protected] ~]# podman run --name web -d -p 8080:80 -v /opt/webhtml:/usr/shar/nginx/html nginx
910db3ab6bd1ef18e5fd0afe1844912f0b89334b7b8ab758353a948a1b55282a
基于web容器,在優先級一般的/etc/systemd/system内建立.service單元檔案
[root@192 ~]# cd /etc/systemd/system/
[[email protected] user]# podman generate systemd --
--container-prefix (Systemd unit name prefix for containers)
--files {生成.service檔案,而不是列印到标準輸出}
--format (Print the created units in specified format (json)) #以指定的格式列印單元檔案
--name (Use container/pod names instead of IDs) #建立新容器,而不是使用現有的容器
--new (Create a new container instead of starting an existing one)#(跳過标頭生成)
--no-header (Skip header generation)
--pod-prefix (Systemd unit name prefix for pods)
--restart-policy (Systemd restart-policy)
--separator (Systemd unit name separator between name/id and prefix)
--time (Stop timeout override)
[root@192 system]# podman generate systemd --name web --files --new
/etc/systemd/system/container-web.service
檢視生成的單元檔案
[root@192 system]# cat container-web.service
# container-web.service
# autogenerated by Podman 3.3.0-dev #podman 3.3.0-dev自動生成
# Tue Aug 17 13:03:13 CST 2021 #8月17日星期二13:03:13 CST 2021
[Unit] #單元
Description=Podman container-web.service #描述
Documentation=man:podman-generate-systemd(1) #幫助以及生成的系統
Wants=network-online.target #網絡
After=network-online.target
RequiresMountsFor=%t/containers #前面不重要直接跳過
[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure #故障時重新啟動
TimeoutStopSec=70 #逾時時間
ExecStart=/usr/bin/podman run --sdnotify=conmon --cgroups=no-conmon --rm --replace --name web -d -p 8080:80 -v /opt/webhtml:/usr/shar/nginx/html nginx #執行開始為/usr/bin/podman 運作剛才建立的容器
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target default.target
删除剛才的容器
[[email protected] ~]# podman rm web
910db3ab6bd1ef18e5fd0afe1844912f0b89334b7b8ab758353a948a1b55282a
[[email protected] ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
設定開機自啟
[root@192 ~]# systemctl daemon-reload
[root@192 ~]# systemctl enable --now container-web.service
Created symlink /etc/systemd/system/multi-user.target.wants/container-web.service → /etc/systemd/system/container-web.service.
Created symlink /etc/systemd/system/default.target.wants/container-web.service → /etc/systemd/system/container-web.service.
[root@192 user]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b0c7709cb00e docker.io/library/nginx:latest nginx -g daemon o... 15 seconds ago Up 16 seconds ago 0.0.0.0:8080->80/tcp web
無根root模式設定容器和上面這種方式大同小異
使用systemctl指令帶上 --user 即可
#需要運作loginctl enable-linger指令,使使用者服務在伺服器啟動時自動啟動即可
[[email protected] ~]$ loginctl enable-linger