【网址收藏】podman安装及使用简单介绍
什么是 Podman ?
Podman 是一個(gè)開源的容器運(yùn)行時(shí)項(xiàng)目,可在大多數(shù) Linux 平臺(tái)上使用。Podman 提供與 Docker 非常相似的功能。正如前面提到的那樣,它不需要在你的系統(tǒng)上運(yùn)行任何守護(hù)進(jìn)程,并且它也可以在沒有 root 權(quán)限的情況下運(yùn)行。
Podman 可以管理和運(yùn)行任何符合 OCI(Open Container Initiative)規(guī)范的容器和容器鏡像。Podman 提供了一個(gè)與 Docker 兼容的命令行前端來管理 Docker 鏡像。
Podman 官網(wǎng)地址:https://podman.io/
Podman 項(xiàng)目地址:https://github.com/containers/libpod
安裝 Podman
Podman 目前已支持大多數(shù)發(fā)行版本通過軟件包來進(jìn)行安裝,下面我們來舉幾個(gè)常用發(fā)行版本的例子。
Fedora / CentOS
$ sudo yum -y install podman
Ubuntu
$ sudo apt-get update -qq
$ sudo apt-get install -qq -y software-properties-common uidmap
$ sudo add-apt-repository -y ppa:projectatomic/ppa
$ sudo apt-get update -qq
$ sudo apt-get -qq -y install podman
MacOS
$ brew cask install podman
RHEL 7
$ sudo subscription-manager repos --enable=rhel-7-server-extras-rpms
$ sudo yum -y install podman
Arch Linux
$ sudo pacman -S podman
更多系統(tǒng)的安裝方法,可參考官方文檔:https://github.com/containers/libpod/blob/master/install.md
使用 Podman
使用 Podman 非常的簡單,Podman 的指令跟 Docker 大多數(shù)都是相同的。下面我們來看幾個(gè)常用的例子:
運(yùn)行一個(gè)容器
$ podman run -dt -p 8080:8080/tcp
-e HTTPD_VAR_RUN=/var/run/httpd
-e HTTPD_MAIN_CONF_D_PATH=/etc/httpd/conf.d
-e HTTPD_MAIN_CONF_PATH=/etc/httpd/conf
-e HTTPD_CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/httpd/
registry.fedoraproject.org/f27/httpd /usr/bin/run-httpd
列出運(yùn)行的容器
$ podman ps -a
分析一個(gè)運(yùn)行的容器
$ podman inspect -l | grep IPAddress":
“SecondaryIPAddresses”: null,
“IPAddress”: “”,
查看一個(gè)運(yùn)行中容器的日志
$ sudo podman logs --latest
10.88.0.1 - - [07/Feb/2018:15:22:11 +0000] “GET / HTTP/1.1” 200 612 “-” “curl/7.55.1” “-”
10.88.0.1 - - [07/Feb/2018:15:22:30 +0000] “GET / HTTP/1.1” 200 612 “-” “curl/7.55.1” “-”
10.88.0.1 - - [07/Feb/2018:15:22:30 +0000] “GET / HTTP/1.1” 200 612 “-” “curl/7.55.1” “-”
10.88.0.1 - - [07/Feb/2018:15:22:31 +0000] “GET / HTTP/1.1” 200 612 “-” “curl/7.55.1” “-”
10.88.0.1 - - [07/Feb/2018:15:22:31 +0000] “GET / HTTP/1.1” 200 612 “-” “curl/7.55.1” “-”
查看一個(gè)運(yùn)行容器中的進(jìn)程資源使用情況
$ sudo podman top <container_id>
UID PID PPID C STIME TTY TIME CMD
0 31873 31863 0 09:21 ? 00:00:00 nginx: master process nginx -g daemon off;
101 31889 31873 0 09:21 ? 00:00:00 nginx: worker process
停止一個(gè)運(yùn)行中的容器
$ sudo podman stop --latest
刪除一個(gè)容器
$ sudo podman rm --latest
以上這些特性基本上都和 Docker 一樣,Podman 除了兼容這些特性外,還支持了一些新的特性。
給容器設(shè)置一個(gè)檢查點(diǎn)
$ sudo podman container checkpoint <container_id>
需要 CRIU 3.11 以上版本支持,CRIU 項(xiàng)目地址:https://criu.org/
根據(jù)檢查點(diǎn)位置恢復(fù)容器
$ sudo podman container restore <container_id>
遷移容器
Podman 支持將容器從一臺(tái)機(jī)器遷移到另一臺(tái)機(jī)器。
首先,在源機(jī)器上對(duì)容器設(shè)置檢查點(diǎn),并將容器打包到指定位置。
$ sudo podman container checkpoint <container_id> -e /tmp/checkpoint.tar.gz
$ scp /tmp/checkpoint.tar.gz <destination_system>:/tmp
其次,在目標(biāo)機(jī)器上使用源機(jī)器上傳輸過來的打包文件對(duì)容器進(jìn)行恢復(fù)。
$ sudo podman container restore -i /tmp/checkpoint.tar.gz
配置別名
如果習(xí)慣了使用 Docker 命令,可以直接給 Podman 配置一個(gè)別名來實(shí)現(xiàn)無縫轉(zhuǎn)移。你只需要在 .bashrc 下加入以下行內(nèi)容即可:
$ echo “alias docker=podman” >> .bashrc
$ source .bashrc
Podman 如何實(shí)現(xiàn)開機(jī)重啟容器
由于 Podman 不再使用守護(hù)進(jìn)程管理服務(wù),所以不能通過守護(hù)進(jìn)程去實(shí)現(xiàn)自動(dòng)重啟容器的功能。那如果要實(shí)現(xiàn)開機(jī)自動(dòng)重啟容器,又該如何實(shí)現(xiàn)呢?
其實(shí)方法很簡單,現(xiàn)在大多數(shù)系統(tǒng)都已經(jīng)采用 Systemd 作為守護(hù)進(jìn)程管理工具。這里我們就可以使用 Systemd 來實(shí)現(xiàn) Podman 開機(jī)重啟容器,這里我們以啟動(dòng)一個(gè) Nginx 容器為例子。
首先,我們先運(yùn)行一個(gè) Nginx 容器。
$ sudo podman run -t -d -p 80:80 --name nginx nginx
然后,在建立一個(gè) Systemd 服務(wù)配置文件。
$ vim /etc/systemd/system/nginx_container.service
[Unit]
Description=Podman Nginx Service
After=network.target
After=network-online.target
[Service]
Type=simple
ExecStart=/usr/bin/podman start -a nginx
ExecStop=/usr/bin/podman stop -t 10 nginx
Restart=always
[Install]
WantedBy=multi-user.target
接下來,啟用這個(gè) Systemd 服務(wù)。
$ sudo systemctl daemon-reload
$ sudo systemctl enable nginx_container.service
$ sudo systemctl start nginx_container.service
服務(wù)啟用成功后,我們可以通過 systemctl status 命令查看到這個(gè)服務(wù)的運(yùn)行狀況。
$ sudo systemctl status nginx_container.service
● nginx_container.service - Podman Nginx Service
Loaded: loaded (/etc/systemd/system/nginx_container.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2019-08-20 20:59:26 UTC; 1min 41s ago
Main PID: 845 (podman)
Tasks: 16 (limit: 4915)
Memory: 37.6M
CGroup: /system.slice/nginx_container.service
└─845 /usr/bin/podman start -a nginx
Aug 20 20:59:26 Ubuntu-dev.novalocal systemd[1]: Started Podman Nginx Service.
之后每次系統(tǒng)重啟后 Systemd 都會(huì)自動(dòng)啟動(dòng)這個(gè)服務(wù)所對(duì)應(yīng)的容器。
其它相關(guān)工具
Podman 只是 OCI 容器生態(tài)系統(tǒng)計(jì)劃中的一部分,主要專注于幫助用戶維護(hù)和修改符合 OCI 規(guī)范的容器鏡像。其它的組件還有 Buildah、Skopeo 等。
參考鏈接:
再見 Docker,是時(shí)候擁抱下一代容器工具了
總結(jié)
以上是生活随笔為你收集整理的【网址收藏】podman安装及使用简单介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ventory制作U盘启动盘
- 下一篇: helm3安装