介绍
容器化应用服务包含多个层级:systemd 单元、包装命令、镜像引用、容器运行时以及应用输出。错误的镜像标签会导致服务在应用启动前就发生故障。
在本实验中,rhce-container-app.service 使用 Podman 运行一个 UBI 10 最小化容器。正确的镜像已经拉取完毕,但服务环境变量文件指向了一个错误的标签。你将诊断故障、修复镜像标签、重启服务并验证容器的运行状态。
确认容器服务故障
在此步骤中,你将在更改容器配置之前检查失败的 systemd 单元。
进入项目目录并创建工作区。
cd /home/labex/project
mkdir -p container-service
检查服务状态。
systemctl is-failed rhce-container-app.service || true
保存选定的服务属性。
systemctl show rhce-container-app.service -p ActiveState -p Result -p ExecMainStatus | tee container-service/service-state.txt
读取最近的服务日志。
sudo journalctl -u rhce-container-app.service -n 20 --no-pager
该服务在应用创建就绪文件(ready file)之前就已失败。
检查镜像与环境变量文件
在此步骤中,你将对比本地可用的镜像与服务所使用的镜像标签。
列出本地镜像。
sudo podman images | tee container-service/images.txt
读取服务环境变量文件。
sudo cat /etc/rhce-container/app.env | tee container-service/env-before.txt
显示包装命令。
sudo sed -n '1,16p' /usr/local/bin/rhce-container-run
本地镜像标签为 latest,但服务指向的是 missing。
修复镜像标签
在此步骤中,你将仅更新服务环境变量文件,使其使用已拉取的镜像标签。
替换错误的标签。
sudo sed -i 's/:missing/:latest/' /etc/rhce-container/app.env
保存修复后的环境配置。
sudo cat /etc/rhce-container/app.env | tee container-service/env-after.txt
现在服务指向了本地存在的镜像。
重启容器服务
在此步骤中,你将清除失败状态并重启由 systemd 管理的容器。
重置失败状态。
sudo systemctl reset-failed rhce-container-app.service
重启服务。
sudo systemctl restart rhce-container-app.service
保存活动状态。
systemctl is-active rhce-container-app.service | tee container-service/service-final.txt
由于 Podman 现在可以使用正确的镜像,服务应该保持活动状态。
验证运行中的容器
在此步骤中,你将检查 Podman 和应用就绪文件。
列出正在运行的容器。
sudo podman ps --filter name=rhce-container-app --format "{{.Names}} {{.Status}}" | tee container-service/container-state.txt
读取由容器创建的就绪文件。
cat /srv/rhce-container/ready.txt | tee container-service/ready.txt
容器化服务正在运行,并已生成预期的输出。
总结
你诊断了一个由 systemd 管理的 Podman 服务故障,对比了可用镜像与配置的镜像标签,修复了环境变量文件,重启了服务,并验证了容器的运行状态及应用就绪文件。



