非root用户使用podman容器
前提条件
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的数据目录
# 查看当前用户的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镜像
# 使用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
端口是通的
非根用户容器开机自启
创建普通用户的systemd目录
# 创建~/.config/systemd/user目录来存放service文件
mkdir ~/.config/systemd/user -p
给容器创建自启动service文件
# 给容器创建service文件
podman generate systemd -n --new -f httpdtest
将容器自启动service文件移动到用户的systemd目录
# 将service文件移动到普通用户的systemd目录~/.config/systemd/user/
[cesu-c8 devops ~]$ mv container-httpdtest.service ~/.config/systemd/user/
修复容器自启动service文件的SELinux安全上下文
系统如果开启了SELinux,需要修复下SELinux安全上下文
# 如果开启了SELinux,需要修复service文件的SELinux安全上下文
[cesu-c8 devops ~]$ restorecon -RvF ~/.config/systemd/user/container-httpdtest.service
查看用户的systemd权限
查看当前用户是否拥有执行systemd的权限
# 查看当前用户是否拥有执行systemd的权限
[cesu-c8 devops ~]$ loginctl
SESSION UID USER SEAT TTY
9 0 root
查看哪些用户可以执行systemd
# 查看哪些用户可以执行systemd
[cesu-c8 devops ~]$ loginctl list-users
UID USER
0 root
1001 devops
2 users listed.
查看当前用户的systemd配置信息
# 查看当前用户的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权限,
否则做的systemd操作是不会生效的
loginctl enable-linger
查看用户的systemd的权限
[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重新加载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
端口是通的

82

被折叠的 条评论
为什么被折叠?



