Podman 的基本设置和使用
文章目录
Podman 的基本设置和使用
Podman 是作为 libpod 库的一部分提供的实用程序。它可用于创建和维护容器。以下是Podman 执行一些基本命令
运行示例容器
这个示例容器将运行一个非常基本的 httpd 服务器,它只服务于它的索引页面。
[root@centos8 ~]# podman run -dt -p 8080:8080/tcp -e HTTPD_VAR_RUN=/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/f29/httpd /usr/bin/run-httpd
Trying to pull registry.fedoraproject.org/f29/httpd:latest...
Getting image source signatures
Copying blob d77ff9f653ce done
Copying blob aaf5ad2e1aa3 done
Copying blob 7692efc5f81c done
Copying config 25c76f9dcd done
Writing manifest to image destination
Storing signatures
ae8da53bef36aa8580d6fac936e03ea7ce97db737bb17f469a12cd09673be8d7
因为容器在分离模式下运行,由命令中的-dpodman run表示,所以 Podman 将在运行后打印容器 ID。请注意,我们使用端口转发来访问 HTTP 服务器。为了成功运行,至少需要 slirp4netns v0.3.0。
列出正在运行的容器
Podman ps命令用于列出创建和运行的容器。
[root@centos8 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae8da53bef36 registry.fedoraproject.org/f29/httpd:latest /usr/bin/run-http... 2 minutes ago Up 2 minutes ago 0.0.0.0:8080->8080/tcp brave_pascal
参数:
-a 或 --all //列出所有容器,包括未运行
-s 或 --size //查看本地机上所有运行实例的大小
-q 或 --quiet //查看容器id
检查正在运行的容器
您可以“检查”正在运行的容器以获取有关其自身的元数据和详细信息。我们甚至可以使用 inspect 子命令来查看分配给容器的 IP 地址。如果容器在无根模式下运行,就不会分配 IP 地址,并且该值将在 inspect 的输出中列为“无”。
[root@centos8 ~]# podman inspect -l | grep -i ipaddress
"IPAddress": "10.88.0.2",
"IPAddress": "10.88.0.2",
参数:
-l 或--latest 指定最近创建容器
测试 httpd 服务器
[root@centos8 ~]# curl 10.88.0.2:8080
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test Page for the Apache HTTP Server on Fedora</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
…………
<p><a href="https://httpd.apache.org/"><img src="/icons/apache_pb2.gif" alt="[ Powered by Apache ]"/></a> <a href="https://getfedora.org/"><img src="/icons/poweredby.png" alt="[ Powered by Fedora ]" width="88" height="31" /></a></p>
</div>
</div>
</div>
</div>
</body>
</html>
web页面查看结果
通过宿主机加上映射出来的端口进行访问
查看容器的日志
[root@centos8 ~]# podman logs -l
=> sourcing 10-set-mpm.sh ...
=> sourcing 20-copy-config.sh ...
=> sourcing 40-ssl-certs.sh ...
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.2. Set the 'ServerName' directive globally to suppress this message
查看容器的进程
您可以使用top观察容器中的 httpd pid 。
[root@centos8 ~]# podman top -l
USER PID PPID %CPU ELAPSED TTY TIME COMMAND
default 1 0 0.000 8m38.517640656s pts/0 0s httpd -D FOREGROUND
default 23 1 0.000 8m38.517848044s pts/0 0s /usr/bin/coreutils --coreutils-prog-shebang=cat /usr/bin/cat
default 24 1 0.000 8m38.517882067s pts/0 0s /usr/bin/coreutils --coreutils-prog-shebang=cat /usr/bin/cat
default 25 1 0.000 8m38.517909468s pts/0 0s /usr/bin/coreutils --coreutils-prog-shebang=cat /usr/bin/cat
default 26 1 0.000 8m38.517934836s pts/0 0s /usr/bin/coreutils --coreutils-prog-shebang=cat /usr/bin/cat
default 27 1 0.000 8m38.517966074s pts/0 0s httpd -D FOREGROUND
default 28 1 0.000 8m38.517993576s pts/0 0s httpd -D FOREGROUND
default 30 1 0.000 8m38.518020777s pts/0 0s httpd -D FOREGROUND
default 44 1 0.000 8m38.518047006s pts/0 0s httpd -D FOREGROUND
检查点容器
检查点容器会停止容器,同时将容器中所有进程的状态写入磁盘。有了这个,容器可以稍后恢复并在与检查点完全相同的时间点继续运行。此功能需要在系统上安装 CRIU 3.11 或更高版本。此功能不支持为无根;因此,如果您想尝试它,您需要以 root 身份重新创建您的容器,使用相同的命令但使用 sudo。
[root@centos8 ~]# podman container checkpoint brave_pascal
ae8da53bef36aa8580d6fac936e03ea7ce97db737bb17f469a12cd09673be8d7
[root@centos8 ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae8da53bef36 registry.fedoraproject.org/f29/httpd:latest /usr/bin/run-http... 25 minutes ago Exited (0) 20 seconds ago 0.0.0.0:8080->8080/tcp brave_pascal
## 这就相当于做了个快照
恢复容器
仅对以前设置检查点的容器才能恢复容器。恢复的容器将继续在与检查点完全相同的时间点运行。就相当于恢复到检查点当时的状态
[root@centos8 ~]# podman container restore brave_pascal
ae8da53bef36aa8580d6fac936e03ea7ce97db737bb17f469a12cd09673be8d7
[root@centos8 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae8da53bef36 registry.fedoraproject.org/f29/httpd:latest /usr/bin/run-http... 26 minutes ago Up 26 minutes ago 0.0.0.0:8080->8080/tcp brave_pascal
恢复后,容器将像检查点之前一样再次响应请求。
[root@centos8 ~]# curl 10.88.0.2:8080
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test Page for the Apache HTTP Server on Fedora</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css">
…………
<p><a href="https://httpd.apache.org/"><img src="/icons/apache_pb2.gif" alt="[ Powered by Apache ]"/></a> <a href="https://getfedora.org/"><img src="/icons/poweredby.png" alt="[ Powered by Fedora ]" width="88" height="31" /></a></p>
</div>
</div>
</div>
</div>
</body>
</html>
迁移容器
要将容器从一台主机实时迁移到另一台主机,容器会在迁移的源系统上设置检查点,转移到目标系统,然后在目标系统上恢复。传输检查点时,可以指定输出文件。
在源系统上:
[root@wjh ~]# podman container checkpoint brave_pascal -e /tmp/algg.tar.gz
ae8da53bef36aa8580d6fac936e03ea7ce97db737bb17f469a12cd09673be8d7
传输检查点
[root@wjh ~]# scp /tmp/algg.tar.gz 192.168.137.134:/tmp
The authenticity of host '192.168.137.133 (192.168.137.133)' can't be established.
ECDSA key fingerprint is SHA256:u3A9HCdz3qc1ZfyEQNpSYXCaJOq8Gt1t0g7nfv8eh0o.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '192.168.137.133' (ECDSA) to the list of known hosts.
root@192.168.137.133's password:
algg.tar.gz 100% 2121KB 91.6MB/s 00:00
在目标系统上:
[root@wjh ~]# podman container restore -i /tmp/algg.tar.gz
Trying to pull registry.fedoraproject.org/f29/httpd:latest...
Getting image source signatures
Copying blob d77ff9f653ce done
Copying blob aaf5ad2e1aa3 done
Copying blob 7692efc5f81c done
Copying config 25c76f9dcd done
Writing manifest to image destination
Storing signatures
ae8da53bef36aa8580d6fac936e03ea7ce97db737bb17f469a12cd09673be8d7
访问一下
停止容器
[root@wjh ~]# podman stop -l
ae8da53bef36aa8580d6fac936e03ea7ce97db737bb17f469a12cd09673be8d7
[root@wjh ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae8da53bef36 registry.fedoraproject.org/f29/httpd:latest /usr/bin/run-http... 4 minutes ago Exited (0) 6 seconds ago 0.0.0.0:8080->8080/tcp brave_pascal
删除容器
[root@wjh ~]# podman rm -l
ae8da53bef36aa8580d6fac936e03ea7ce97db737bb17f469a12cd09673be8d7