Podmanが実行する公式のnginxドッカー画像を記録する方法

Podmanが実行する公式のnginxドッカー画像を記録する方法

次のコマンドを使用して、ルートの下のpodmanで公式のnginxイメージを起動しました。

sudo podman run --name nginx \
 ... \
 -v /var/log/nginx:/var/log/nginx \
 docker.io/library/nginx:latest

ロギングは正常に機能しますが、インターネットから取得したすべての設定を使用してホストでログを回転しようとすると失敗します。例:

/var/log/nginx/*.log {
 hourly
 missingok
 rotate 24
 compress
 delaycompress
 notifempty
 su root root
 create 0644
 sharedscripts
 postrotate
    podman exec nginx /bin/bash reset_log.sh
 endscript
}

Reset_log.shスクリプトには、新しいログを開始するためのよく知られたコマンドが含まれており、コンテナにログインして手動で実行すると、期待どおりに機能します。

 kill -USR1 `cat /var/run/nginx.pid`

ただし、これらすべてが期待どおりに機能しなくなり、access.log.1ファイルに引き続き記録されるか、まったく記録されません。 「su root root」や「create..」なしでバリエーションを試みましたが、成功しませんでした。

更新:追加の調査が必要な権限の問題があるようです。 logrotate -f -v /etc/logrotate.conf コマンドを手動で強制適用すると、期待どおりに機能します。 selinuxまたはapparmor(arch linux)がホストにインストールされていません。

systemd[1]: Starting Rotate log files...
conmon[4388]: conmon 32834b35446220b4e6d4 <nwarn>: runtime stderr: setns `mnt`: Operation not permitted
                                fail startup
conmon[4388]: conmon 32834b35446220b4e6d4 <error>: Failed to create container: exit status 1
logrotate[4377]: Error: crun: setns `mnt`: Operation not permitted: OCI permission denied
logrotate[4374]: error: error running shared postrotate script for '/var/log/nginx/*.log '
systemd[1]: logrotate.service: Main process exited, code=exited, status=1/FAILURE
systemd[1]: logrotate.service: Failed with result 'exit-code'.
systemd[1]: Failed to start Rotate log files.

ベストアンサー1

podman exec nginx ...最も簡単な解決策は、おそらく次のように使用を変更することですpodman kill

/var/log/nginx/*.log {
 hourly
 missingok
 rotate 24
 compress
 delaycompress
 notifempty
 su root root
 create 0644
 sharedscripts
 postrotate
    podman kill --signal USR1 nginx
 endscript
}

podman killこれは、指定された信号がnginxのコンテナのPID 1に送信されるために機能します。コンテナに入る必要もなく、/var/run/nginx.pidコンテナにまったく入る必要もないため、「OCI権限の拒否」エラーを引き起こす問題を回避できます。


基本的な問題は、(Rocky 9)logrotateで見られるさまざまな保護機能を使用してサービスが実行されていることです。/lib/systemd/system/logrotate.service

[Service]
Type=oneshot
ExecStart=/usr/sbin/logrotate /etc/logrotate.conf

# performance options
Nice=19
IOSchedulingClass=best-effort
IOSchedulingPriority=7

# hardening options
#  details: https://www.freedesktop.org/software/systemd/man/systemd.exec.html
#  no ProtectHome for userdir logs
#  no PrivateNetwork for mail deliviery
#  no NoNewPrivileges for third party rotate scripts
#  no RestrictSUIDSGID for creating setgid directories
LockPersonality=true
MemoryDenyWriteExecute=true
PrivateDevices=true
PrivateTmp=true
ProtectClock=true
ProtectControlGroups=true
ProtectHostname=true
ProtectKernelLogs=true
ProtectKernelModules=true
ProtectKernelTunables=true
ProtectSystem=full
RestrictNamespaces=true
RestrictRealtime=true

主な制限は、RestrictNamespaces=true次のオプションです。文書:

このデバイスのプロセスがLinuxネームスペース機能にアクセスできないように制限します... trueの場合、すべてのタイプのネームスペースへのアクセスは禁止されます。

とりわけ、これは代替インストール名前空間へのアクセスをブロックするため、機能podman execしません。 systemdオーバーライド単位を使用して制限を削除できますが、podman kill変更が少ないため、使用する方がきれいです。

おすすめ記事