Podman開機(jī)自啟容器實(shí)現(xiàn)過程及與Docker對比
1.podman介紹
podman之前是CRI-O項(xiàng)目的一部分,后被分離成獨(dú)立的項(xiàng)目libpod,libpod是一個創(chuàng)建容器pod的工具和庫,podman是個無守護(hù)程序容器引擎,以root用戶或無根模式運(yùn)行,簡而言之podman提供了一個docker-CLI的命令行,管理著容器
2.與docker相比的優(yōu)勢
docker劣勢一:
docker大家都知道,其守護(hù)程序在多個核心上占用差不多高達(dá)100%cpu資源,采用C/S模型
podman優(yōu)勢一:
podman不需要守護(hù)進(jìn)程,不需要root權(quán)限組,而且利用著用戶命名空間(namespace)模擬容器中的root運(yùn)行,采用fork/exec模型。
fork/exec模型相比C/S模型優(yōu)勢:
- 系統(tǒng)管理員知道某個容器由誰啟動
- 利用cgroup對podman做限制,對應(yīng)著創(chuàng)建的容器也會受到限制
- systemd單元文件的生成,可以管理著任務(wù)的啟動與關(guān)閉
- socket激活,將socker從systemd發(fā)送給podman容器使用
3.兼容性
docker的功能大部分podman都是兼容的,也可以使用別名(alias)來寫成docker的命令
4.后臺服務(wù)單元文件的優(yōu)先級
/usr/lib/systemd/user:優(yōu)先級最低,會被優(yōu)先級高的同名 unit 覆蓋 ~/.local/share/systemd/user
/etc/systemd/user:全局共享的用戶級 unit[s]
~/.config/systemd/user:優(yōu)先級最高
5.podman基本操作
安裝
#默認(rèn)centos源 [root@slave02 ~]# yum -y module install container-tools #容器工具基于模塊 [root@slave02 ~]# yum -y install podman-docker #安裝docker兼容包(可選)
版本
[root@slave02 ~]# podman -v podman version 3.3.0-dev
倉庫
官方倉庫:registry.access.redhat.com
第三方倉庫:docker.io
私有倉庫:registry.lab.example.com
命令幫助
[root@slave02 ~]# 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 #基于修改的容器創(chuàng)建新的容器 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格式
[root@slave02 ~]# cp /etc/containers/registries.conf /backup/registries.conf.back #備份一下 [root@slave02 ~]# vim /etc/containers/registries.conf unqualified-search-registries = ["docker.io"] #非限定搜索登記處 [[registry]] prefix = "docker.io" location = "x" #x是阿里加速鏡像地址
拉取鏡像
[root@slave02 ~]# podman pull nginx
6.運(yùn)行一個web容器
后臺啟動一個web容器,并訪問容器內(nèi)容
#準(zhǔn)備html頁面內(nèi)容 [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 #運(yùn)行一個守護(hù)web容器進(jìn)程,將/opt/webhtml目錄內(nèi)容映射到容器的/usr/share/nginx/html存放網(wǎng)頁的位置 [root@192 ~]# podman run -d --name web -p 8888:80 -v /opt/webhtml:/usr/share/nginx/html nginx 3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c [root@podman ~]# 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 [root@podman ~]# podman inspect web|grep IPAddress "IPAddress": "10.88.0.6", "IPAddress": "10.88.0.6", #宿主機(jī)的ip [root@podman ~]# ip r 192.168.136.0/24 dev ens33 proto kernel scope link src 192.168.136.129 metric 100 #由于進(jìn)行了端口綁定,所以直接 curl 192.168.136.129:8888即可訪問
進(jìn)入后臺web容器,查看服務(wù)狀態(tài)
[root@podman ~]# podman exec -it web bash root@3528e6d5148b:/# service nginx status [ ok ] nginx is running. #運(yùn)行中
修改容器業(yè)務(wù)內(nèi)容
#修改宿主機(jī)/opt/webhtml/index.html即可 [root@podman ~]# 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 #進(jìn)行訪問 [root@podman ~]# 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 #進(jìn)入容器查看內(nèi)容是否修改 [root@podman ~]# 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
暫停與刪除容器
#暫停 [root@podman ~]# podman stop web web [root@podman ~]# 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 #刪除 [root@podman ~]# podman rm web 3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c #或強(qiáng)制刪除運(yùn)行中的容器 [root@podman ~]# podman rm -f web 3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c
7.web容器設(shè)置開機(jī)自啟
后臺運(yùn)行一個web容器
[root@podman ~]# podman run --name web -d -p 8080:80 -v /opt/webhtml:/usr/shar/nginx/html nginx 910db3ab6bd1ef18e5fd0afe1844912f0b89334b7b8ab758353a948a1b55282a
基于web容器,在優(yōu)先級一般的/etc/systemd/system內(nèi)
創(chuàng)建.service單元文件
[root@192 ~]# cd /etc/systemd/system/
[root@podman user]# podman generate systemd --
--container-prefix (Systemd unit name prefix for containers)
--files {生成.service文件,而不是打印到標(biāo)準(zhǔn)輸出}
--format (Print the created units in specified format (json)) #以指定的格式打印單元文件
--name (Use container/pod names instead of IDs) #創(chuàng)建新容器,而不是使用現(xiàn)有的容器
--new (Create a new container instead of starting an existing one)#(跳過標(biāo)頭生成)
--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) #幫助以及生成的系統(tǒng) Wants=network-online.target #網(wǎng)絡(luò) 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 #執(zhí)行開始為/usr/bin/podman 運(yùn)行剛才創(chuàng)建的容器 Type=notify NotifyAccess=all [Install] WantedBy=multi-user.target default.target
刪除剛才的容器
[root@podman ~]# podman rm web 910db3ab6bd1ef18e5fd0afe1844912f0b89334b7b8ab758353a948a1b55282a [root@podman ~]# podman ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
設(shè)置開機(jī)自啟
[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模式設(shè)置容器和上面這種方式大同小異
使用systemctl命令帶上 --user 即可
#需要運(yùn)行l(wèi)oginctl enable-linger命令,使用戶服務(wù)在服務(wù)器啟動時自動啟動即可 [containers@serverb ~]$ loginctl enable-linger
以上就是Podman開機(jī)自啟容器實(shí)現(xiàn)過程的詳細(xì)內(nèi)容,更多關(guān)于Podman開機(jī)自啟容器的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Containerd容器運(yùn)行yum安裝與二進(jìn)制安裝
這篇文章主要為大家介紹了Containerd容器運(yùn)行yum安裝與二進(jìn)制安裝,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
kubernetes需要默認(rèn)的serviceaccount的原因解析
這篇文章主要介紹了kubernetes為何需要默認(rèn)的serviceaccount,ServiceAccount 是 Kubernetes 中的一種重要概念,它的實(shí)際使用場景包括很多,本文給大家講解的非常詳細(xì),需要的朋友可以參考下2023-04-04
KVM虛擬化技術(shù)之virt-manager使用及KVM虛擬化平臺網(wǎng)絡(luò)模型介紹
這篇文章主要介紹了KVM虛擬化技術(shù)之virt-manager使用及KVM虛擬化平臺網(wǎng)絡(luò)模型介紹,需要的朋友可以參考下2016-10-10
Kubernetes 1.28.2集群安裝過程中的關(guān)鍵步驟詳解(最新推薦)
Kubernetes K8s是Google開源的容器編排平臺,最初由Borg項(xiàng)目發(fā)展而來,K8s 1.28版本引入了非正常節(jié)點(diǎn)關(guān)閉恢復(fù)、內(nèi)置Sidecar容器支持、Job優(yōu)化、Proxy改進(jìn)和調(diào)度框架優(yōu)化等功能,本文介紹Kubernetes 1.28.2集群安裝過程中的關(guān)鍵步驟,感興趣的朋友一起看看吧2025-03-03
k8s跨服務(wù)調(diào)用入門到實(shí)戰(zhàn)示例詳解
這篇文章主要為大家介紹了k8s跨服務(wù)調(diào)用入門到實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
kubelet為cadvisor添加namespace/pod/container標(biāo)簽示例詳解
這篇文章主要為大家介紹了kubelet為cadvisor添加namespace/pod/container標(biāo)簽示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

