赞
踩
podman
如果要使用普通用户
,shell不能是nologin。
必须要有ssh或者终端登录。
# 创建普通权限新用户devops
[cesu-c8 root ~]# useradd devops
# 给devops用户设置密码
[cesu-c8 root ~]# echo devops|passwd --stdin devops
Changing password for user devops.
passwd: all authentication tokens updated successfully.
# 使用devops用户ssh登录到主机
ssh devops@podmanhost
root下已经使用podman拉取了httpd镜像,在devops用户下是看不到root拉取到镜像的。
# 使用devops用户查看镜像,发现没有镜像
[cesu-c8 devops ~]$ podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
# 查看当前用户的podman配置文件路径
[cesu-c8 devops ~]$ podman info|grep -E 'configFile|graphRoot|volumePath'
configFile: /home/devops/.config/containers/storage.conf
graphRoot: /home/devops/.local/share/containers/storage
volumePath: /home/devops/.local/share/containers/storage/volumes
# 使用devops用户拉取httpd镜像
[cesu-c8 devops ~]$ podman pull httpd
# 查看devops用户拉取的镜像列表
[cesu-c8 devops ~]$ podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/httpd latest 1132a4fc88fa 11 days ago 148 MB
# 查看httpd镜像的镜像id
[cesu-c8 devops ~]$ podman images -q
1132a4fc88fa
# 根据镜像id查看httpd镜像的存储目录
[cesu-c8 devops ~]$ locate -b /home/ 1132a4fc88fa
/home/devops/.local/share/containers/storage/overlay-images/1132a4fc88faaf5c19959f03535c1356d3004ced1978cb9c3f32e73d9c139532
非root用户只能映射1024以上的端口,1024以下的端口只能root用户映射。
# 容器名httpdtest,映射到本地81端口,目录挂载到/web [cesu-c8 devops ~]$ podman run --name httpdtest -dti -p 1081:80 -v ~/web:/usr/local/apache2/htdocs httpd da3f671d340d4972c848e2de3b085e15f9f7da7b342e9c0fa586aefb21877183 # 给~/web目录下添加一个index.html文件 [cesu-c8 devops ~]$ echo "端口是通的">>~/web/index.html [cesu-c8 devops ~]$ cat web/index.html 端口是通的 # 查看容器 [cesu-c8 devops ~]$ podman ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES da3f671d340d docker.io/library/httpd:latest httpd-foreground 4 seconds ago Up 4 seconds ago 0.0.0.0:1081->80/tcp httpdtest # 查看httpd服务是否正常 [cesu-c8 devops ~]$ curl localhost:1081 端口是通的
# 创建~/.config/systemd/user目录来存放service文件
mkdir ~/.config/systemd/user -p
# 给容器创建service文件
podman generate systemd -n --new -f httpdtest
# 将service文件移动到普通用户的systemd目录~/.config/systemd/user/
[cesu-c8 devops ~]$ mv container-httpdtest.service ~/.config/systemd/user/
系统如果开启了SELinux,需要修复下SELinux安全上下文
# 如果开启了SELinux,需要修复service文件的SELinux安全上下文
[cesu-c8 devops ~]$ restorecon -RvF ~/.config/systemd/user/container-httpdtest.service
# 查看当前用户是否拥有执行systemd的权限
[cesu-c8 devops ~]$ loginctl
SESSION UID USER SEAT TTY
9 0 root
# 查看哪些用户可以执行systemd
[cesu-c8 devops ~]$ loginctl list-users
UID USER
0 root
1001 devops
2 users listed.
# 查看当前用户的systemd配置信息 [cesu-c8 devops ~]$ loginctl show-user EnableWallMessages=no NAutoVTs=6 KillUserProcesses=no RebootToFirmwareSetup=no IdleHint=no IdleSinceHint=0 IdleSinceHintMonotonic=0 DelayInhibited=sleep InhibitDelayMaxUSec=5s HandlePowerKey=poweroff HandleSuspendKey=suspend HandleHibernateKey=hibernate HandleLidSwitch=suspend HandleLidSwitchDocked=ignore HoldoffTimeoutUSec=30s IdleAction=ignore IdleActionUSec=30min PreparingForShutdown=no PreparingForSleep=no Docked=yes RemoveIPC=no RuntimeDirectorySize=79671296 InhibitorsMax=8192 NCurrentInhibitors=1 SessionsMax=8192 NCurrentSessions=2
如果用户没有systemd权限,需要执行这个命令开启systemd权限,
否则做的systemd操作是不会生效的
loginctl enable-linger
[cesu-c8 devops ~]$ loginctl user-status devops
devops (1001)
Since: Tue 2021-11-02 19:58:07 CST; 1h 37min ago
State: active
Sessions: *7
# 普通用户执行systemd重新加载systemd配置
systemctl --user daemon-reload
# 普通用户设置service文件开机自启
[cesu-c8 devops ~]$ systemctl --user enable --now container-httpdtest.service
# 查看容器自启动服务状态
[cesu-c8 devops ~]$ systemctl --user status container-httpdtest.service
container-httpdtest.service - Podman container-httpdtest.service
Loaded: loaded (/home/devops/.config/systemd/user/container-httpdtest.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2021-11-02 21:47:44 CST; 2s ago
Docs: man:podman-generate-systemd(1)
Process: 4418 ExecStart=/usr/bin/podman run --conmon-pidfile /run/user/1001/container-httpdtest.pid --cidfile /run/user/1001/container-httpdtest.ctr-id --cgroups=no-conmon --replace --name h>
Process: 4416 ExecStartPre=/bin/rm -f /run/user/1001/container-httpdtest.pid /run/user/1001/container-httpdtest.ctr-id (code=exited, status=0/SUCCESS)
Main PID: 4471 (conmon)
# 切换到root下执行reboot
# 查看systemd的服务状态
[cesu-c8 devops ~]$ systemctl --user status container-httpdtest.service
● container-httpdtest.service - Podman container-httpdtest.service
Loaded: loaded (/home/devops/.config/systemd/user/container-httpdtest.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2021-11-02 21:55:10 CST; 7s ago
Docs: man:podman-generate-systemd(1)
# 查看容器状态是否在运行
[cesu-c8 devops ~]$ podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3f1d855dd9dc docker.io/library/httpd:latest httpd-foreground 22 seconds ago Up 23 seconds ago 0.0.0.0:1081->80/tcp httpdtest
# 验证服务服务是否可以访问
[cesu-c8 devops ~]$ curl localhost:1081
端口是通的
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。