学习内容:

        1.Podman(Podmanger)是一个功能齐全的容器引擎,pod的英文意思是为豆荚,如图1.1所示,我们可以理解容器就如豆荚的外壳一样,用来“存放”一个个的镜像。Podman提供了一个与Docker CLI兼容的操作方式。简单地说:alias docker = podman。大多数Podman命令都可以用普通账户运行,而无需其他额外的权限。

kemp 容器化管理 容器管理是什么_kemp 容器化管理

 图1.1

        2.容器(Container)指的是针对应用所需的运行环境资源进行整体封装的技术。镜像在容器中一但封装好,相比于传统虚拟化技术活,颗粒度更细,可移植性更强。同时每个容器就是一个沙箱,互相隔离。

        3.仓库服务器、镜像、容器的关系

  • 仓库服务器:也叫注册表服务器,是用来提供/存放镜像的,有官方仓库(如红帽的registry.redhat.io、刀客的docker.io),或者自建私有仓库。
  • 镜像:针对某个虚拟机或某个应用封装的独立环境,作为容器的模板。
  • 容器:基于某个镜像启动的在内存中运行的实例。如图1.2是仓库、镜像、容器三者间的依赖关系。

 图1.2


实验输出:

一:安装环境

yum module install -y container-tools //安装容器工具及其配置模块

二:访问仓库

(1)设置默认的仓库地址(全局配置):可以使用官方仓库、第三方仓库或者私有仓库。

vim /etc/containers/registries.conf
[registries.search]
registries = ['registries.lab.example.com']  //设置搜索的镜像的默认仓库地址
···
[registries.insecure]
registries = ['registries.lab.example.com']  //允许访问不安全的仓库(比如HTTPS证书无效或过期等情况)

(2)登录仓库(如果需要的话,比如push上传镜像时)

podman  login  registry.lab.example.com
Username:  admin
Password: ***********
Login Succeeded!

 (3)搜索仓库中的镜像(比如nginx)

podman  search  nginx
INDEX         NAME                                     DESCRIPTION   STARS   OFFICIAL   AUTOMATED
example.com   registry.lab.example.com/library/nginx

三:管理镜像

(1)下载镜像到本地

podman  pull  registry.lab.example.com/library/nginx  //容器存储默认工作目录 /var/lib/containers/

(2)查看镜像

podman  images             //列出本地镜像
REPOSITORY                       TAG      IMAGE ID       CREATED        SIZE
registry.lab.example.com/nginx   latest   4bb46517cac3   3 months ago   137 MB

podman  image  inspect  4bb4       //查看xxxx镜像的详细配置信息

(3)导出/备份镜像

podman  save  nginx > /opt/nginx.tar

 (4)导入镜像

podman  load  -i  /opt/nginx.tar  nginx-new:latest

 (5)删除镜像

podman  rmi  xxxx       //删除ID为xxxx的镜像
podman  rmi  -a        //删除所有镜像

四 :管理容器

1.启动容器

(1)在后台启动一个容器(-d 后台运行)

# podman  run  -d  registry.lab.example.com/library/nginx
80b22e7bd4d789773223f5afc85808ea472e82ec72f162903cd658ed6d98091c

# podman  ps       //列出启用中的容器(结合-a选项可以列出所有)
.. ..
# podman  container  inspect  4bb4       //查看xxxx容器的详细信息

(2) 启动一个容器,并进入容器的bin/bash环境。

podman  run  -it  registry.lab.example.com/library/nginx  /bin/bash
root@840b592a6d3f:/# nginx  -v       //检查nginx版本
nginx version: nginx/1.19.2
root@840b592a6d3f:/# ls  /usr/share/nginx/html/      //检查网页目录
50x.html  index.html
root@840b592a6d3f:/# exit           //退出容器
exit

(3) 在后台启动一个容器,并且添加端口映射。

[root@red ~]# podman  run  -d  -p  8000:80  nginx
2b9ef8c0864149e2cf7860e903e36ba9deaa1717863f172b2bf2e5c5f3f6600c

[root@red ~]# podman  ps          //列出活动中的容器
CONTAINER ID  IMAGE                                          COMMAND               CREATED            STATUS                PORTS                 NAMES
2b9ef8c08641  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站点
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

(4) 在后台创建一个目录,将主机的/opt/nginx映射为此nginx的web目录。

mkdir  /opt/webroot         //准备网页目录
echo  "Podman Test" > /opt/webroot/index.html         //准备默认测试网页
podman   run  -d  -p  8001:80  -v  /opt/webroot:/usr/share/nginx/html   nginx
ba64a15abdce1dbd4ed834ad061efde2f7ea421a862076468cbbd694c587f8ca

curl  http://127.0.0.1:8001     //测试结果
Podman Test

2.访问运行中的容器。

(1)连接到ID值以ba64开头的(或者以-l表示最近一个容器)容器的命令行。

[root@red ~]# podman  exec  -it ba64  bash
root@ba64a15abdce:/# service  nginx  status       
[ ok ] nginx is running.
root@ba64a15abdce:/# exit
exit

(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       //若要杀容器改用kill
ba64a15abdce1dbd4ed834ad061efde2f7ea421a862076468cbbd694c587f8ca
[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   nginx
52e6996bef86c501731115216c84a2f48d1a03f8c1a2cad70d27e281bd642b18

 (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-myweb
Created symlink /etc/systemd/system/multi-user.target.wants/container-myweb.service → /etc/systemd/system/container-myweb.service.

 (5)关闭当前运行的容器

[root@red system]# podman  stop  52e6
52e6996bef86c501731115216c84a2f48d1a03f8c1a2cad70d27e281bd642b18

(6) 重启主机后,检查是否可以访问此web

[root@red system]# reboot
.. ..
[root@server1 ~]# curl http://172.25.0.26/
Podman Test

 


内容小结:

        有关于容器的内容到这里就结束了,就我个人而言,我还是不太熟悉。不管是命令还是对容器的理解,特别是有时候会混淆容器跟镜像的关系。因此还是要多练多去领悟。