Podman(podmanager):
是一个功能齐全的容器引擎,它是一个简单的无需守护的用来管理镜像、容器的工具。 Podman提供了一个与Docker CLI兼容的操作方式,简单地说: alias docker=podman。 大多数Podman命令都可以普通用户运行,而无需其他额外的权限。容器(Container):
指的是针对应用所需的运行环境资源(依赖库/目录/网络/用户……等)进行整体封装的技术。封装好的镜像相比虚拟机的粒度要更细,可移植性强。每个容器采用沙箱机制,相互隔离。仓库:用来提供/存放镜像,有官方仓库(比如红帽的registry.redhat.io、刀客的docker.io),或自建私有仓库。
镜像:针对某个虚拟机或某个应用封装的独立环境,作为容器的模板。
容器:基于某个镜像启动的在内存中运行的实例。
传统虚拟化与容器技术对比。
一、安装环境
# yum module install -y container-tools //安装容器工具及其模块配置# yum install -y pdman-docker //安装docker兼容包(可选)
二、访问仓库
1)设置默认的仓库地址
# vim /etc/containers/registries.conf[registries.search]registries = ['registry.lab0.example.com'] //设置搜索镜像的默认仓库地址.. ..[registries.insecure]registries = ['registry.lab.example.com'] //添加不要求认证的仓库(如果有的话)地址
2)登录仓库
# podman login registry.lab.example.comUsername: zhsanPassword: ***********Login Succeeded!
3)搜索仓库中的镜像(比如nginx)
# podman search nginxINDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATEDexample.com registry.lab.example.com/library/nginx
三、管理镜像
1)下载镜像到本地
# podman pull registry.lab.example.com/library/nginx.. ..
2)查看镜像
# podman images //列出本地镜像REPOSITORY TAG IMAGE ID CREATED SIZEregistry.lab.example.com/nginx latest 4bb46517cac3 3 months ago 137 MB# podman inspect 4bb4 //查看xxxx镜像的详细信息.. ..
3)导出/备份镜像
# podman save nginx > /root/nginx.tar
4)导入镜像
# podman load -i /root/nginx.tar nginx-new:latest
5)删除镜像
# podman rmi xxxx //删除ID为xxxx的镜像# podman rmi -a //删除所有镜像
6)为ID以xxxx开头的镜像设置新标签
# podman tag xxxx hello-nginx:1.0
四、管理容器
1. 启动容器
1)在后台启动一个容器(-d 后台运行)
# podman run -d registry.lab.example.com/library/nginx80b22e7bd4d789773223f5afc85808ea472e82ec72f162903cd658ed6d98091c
2)新启动一个容器并执行其中的命令“cat /etc/os-release”,然后删除此容器
# podman run --rm registry.lab.example.com/library/nginx cat /etc/os-releasePRETTY_NAME="Debian GNU/Linux 10 (buster)"NAME="Debian GNU/Linux".. ..
3)启动一个容器,并进入容器内的/bin/bash命令行环境(-i 允许交互,-t 开启终端)
# podman run -it registry.lab.example.com/library/nginx /bin/bashroot@840b592a6d3f:/# nginx -v //检查nginx版本nginx version: nginx/1.19.2root@840b592a6d3f:/# ls /usr/share/nginx/html/ //检查网页目录50x.html index.htmlroot@840b592a6d3f:/# exit //退出容器exit#
4)在后台启动一个nginx容器,添加端口映射(-p 本地端口:容器端口)
[root@red ~]# podman run -d -p 8000:80 nginx2b9ef8c0864149e2cf7860e903e36ba9deaa1717863f172b2bf2e5c5f3f6600c [root@red ~]# podman ps //列出活动中的容器CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES2b9ef8c08641 registry.lab.example.com/library/nginx:latest nginx -g daemon o... 2 minutes ago Up 2 minutes ago 0.0.0.0:8000->80/tcp ecstatic_maxwell.. ..[root@red ~]# curl http://127.0.0.1:8000 //通过主机端口访问容器中的web站点<html><head><title>Welcome to nginx!title>.. ..
5)在后台启动一个nginx容器,将主机的/opt/webroot映射为此nginx容器的web目录(-v 本地目录:容器内目录):
# mkdir /opt/webroot //准备网页目录# echo "Podman Test" > /opt/webroot/index.html //准备默认测试网页# podman run -d -p 8001:80 -v /opt/webroot:/usr/share/nginx/html nginxba64a15abdce1dbd4ed834ad061efde2f7ea421a862076468cbbd694c587f8ca# curl http://127.0.0.1:8001 //测试结果Podman Test
2. 访问运行中的容器
1)连接到ID值以ba64开头的(或者以-l表示最近一个容器)容器的命令行
[root@red ~]# podman exec -it ba64 bashroot@ba64a15abdce:/# service nginx status [ ok ] nginx is running.root@ba64a15abdce:/# exitexit[root@red ~]#
2)检查容器的IP地址
[root@red ~]# podman inspect ba64 | grep IPAddress.. .. "SecondaryIPAddresses": null, "IPAddress": "10.88.0.6",.. ..
3)从主机向ID值为ba64的(或者以-l表示最近一个容器)容器传输文件
[root@red ~]# echo AAAA > /root/a.html //建立测试网页[root@red ~]# podman cp /root/a.html ba64:/usr/share/nginx/html/a.html //复制文件到容器[root@red ~]# curl http://127.0.0.1:8001/a.html //确认结果AAAA
4)通过映射端口访问容器中的Web服务
[root@red ~]# curl http://localhost:8001/ //浏览8001端口访问目标容器首页Podman Test[root@red ~]# curl http://localhost:8001/a.html //浏览指定页面AAAA
3. 关闭/杀死容器
1)关闭/杀死ID值为ba64的容器
[root@red ~]# podman stop ba64 //若要杀容器改用killba64a15abdce1dbd4ed834ad061efde2f7ea421a862076468cbbd694c587f8ca[root@red ~]# podman ps -a | grep ba64 //检查容器状态ba64a15abdce registry.lab.example.com/library/nginx:latest nginx -g daemon o... 47 minutes ago Exited (0) 25 seconds ago 0.0.0.0:8001->80/tcp dreamy_swirles
2)重新启动被关闭的ID值为ba64的容器
[root@red ~]# podman start ba64 //启用已关闭的xx容器ba64a15abdce1dbd4ed834ad061efde2f7ea421a862076468cbbd694c587f8ca[root@red ~]# podman ps -a | grep ba64 //检查容器状态ba64a15abdce registry.lab.example.com/library/nginx:latest nginx -g daemon o... 48 minutes ago Up 2 seconds ago 0.0.0.0:8001->80/tcp dreamy_swirles
3)强制删除ID值为ba64的容器
[root@red ~]# podman rm -f ba64 //删除已关闭的xx容器(如果不加-f,则需要先stop此容器)ba64a15abdce1dbd4ed834ad061efde2f7ea421a862076468cbbd694c587f8ca[root@red ~]# podman ps -a | grep ba64 //检查删除结果(无输出)[root@red ~]#
五、为容器设置systemd服务
1)启动一个容器,命名为myweb
[root@red ~]# podman run --name myweb -d -p 80:80 -v /opt/webroot:/usr/share/nginx/html nginx52e6996bef86c501731115216c84a2f48d1a03f8c1a2cad70d27e281bd642b18
2)为名称为myweb的容器创建对应的systemd服务配置
[root@red ~]# cd /etc/systemd/system/[root@red system]# podman generate systemd -n myweb --files/etc/systemd/system/container-myweb.service
3)更新systemd服务配置
[root@red system]# systemctl daemon-reload
4)配置congtainer-myweb服务开机自启
[root@red system]# systemctl enable container-mywebCreated symlink /etc/systemd/system/multi-user.target.wants/container-myweb.service → /etc/systemd/system/container-myweb.service.
5)关闭当前运行的容器
[root@red system]# podman stop 52e652e6996bef86c501731115216c84a2f48d1a03f8c1a2cad70d27e281bd642b18
6)重启主机后,检查是否可以访问此web
[root@red system]# reboot.. ..[root@server1 ~]# curl http://172.25.0.26/Podman Test
六、使用无根(rootless)环境
通过rootless无根模式,非特权用户也可以很方便的运行容器(允许开启1024以上端口),以提高服务管理的安全性。
!!!! 确认非特权用户的起始可用端口(需要时可更改)
# cat /proc/sys/net/ipv4/ip_unprivileged_port_start1024
1)配置仓库
[zaniu@red ~]$ mkdir -p ~/.config/containers[zaniu@red ~]$ vim ~/.config/containers/registries.conf //可以参考man containers-registries.confunqualified-search-registries = ['registry.lab.example.com'][[registry]] location = "registry.lab.example.com"insecure = trueblocked = false
2)下载(或导入)镜像
[zaniu@red ~]$ podman login registry.lab.example.com //登录仓库Username: adminPassword: Login Succeeded![zaniu@red ~]$ podman pull registry.lab.example.com/library/nginx //下载镜像到本地[zaniu@red ~]$ podman images //检查本地镜像REPOSITORY TAG IMAGE ID CREATED SIZEregistry.lab.example.com/library/nginx latest 4bb46517cac3 3 months ago 137 MB
2)启动一个名为xxnginx的容器(如果SELinux要求启用的话,可以通过:Z传递安全标签)
[zaniu@red ~]$ mkdir /home/zaniu/html[zaniu@red ~]$ echo zaniu > /home/zaniu//html/index.html[zaniu@red ~]$ podman run --name xxnginx -d -p 8080:80 -v /home/zaniu/html:/usr/share/nginx/html:Z nginx8fa1bc2ccd14ddc57e187ffe8e0035b6bfb1c3189460b3470b3935365f5d9a85[zaniu@red ~]$ curl http://127.0.0.1:8080zaniu
3)创建container-xxnginx服务配置
[zaniu@red ~]$ mkdir -p ~/.config/systemd/user //创建用户服务配置目录[zaniu@red ~]$ cd ~/.config/systemd/user[zaniu@red user]$ podman generate systemd --name xxnginx --files //生成container-xxnginx服务配置/home/zaniu/.config/systemd/user/container-xxnginx.service
4)更新用户服务配置,设置开机自启动
[zaniu@red user]$ systemctl --user daemon-reload //更新用户服务配置[zaniu@red user]$ systemctl --user enable container-xxnginx.service //配置自启动Created symlink /home/zaniu/.config/systemd/user/multi-user.target.wants/container-xxnginx.service → /home/zaniu/.config/systemd/user/container-xxnginx.service.[zaniu@red user]$ loginctl enable-linger //允许为未登录的用户启动/保持后台服务
5)测试用户服务控制
[zaniu@red user]$ podman stop xxnginx //停止原来手动运行的容器[zaniu@red user]$ systemctl --user start container-xxnginx.service //启动容器服务[zaniu@red user]$ systemctl --user status container-xxnginx.service //检查容器状态[zaniu@red user]$ systemctl --user stop container-xxnginx.service //停止容器服务
6)重启主机后,确认仍然可以访问web
[root@red system]# reboot.. ..[root@server1 ~]# curl http://172.25.0.26:8080/zaniu