監査ロギングの構成
K3s は、監査ロギングを提供する Kubernetes ディストリビューションであり、デフォルトでは無効になっています。 管理者は、 K3s-based IBM® Security Edge Gateway クラスターで監査ロギングを有効にすることができます。
始める前に
このタスクについて
監査ロギングに必要な構成には、以下のものが含まれます。
- イベントがログに記録される監査ポリシー定義ファイル。
- ログを収集して保管するためのバックエンド構成を監査します。
sudo mkdir -p /var/lib/rancher/k3s/server/manifests /var/log/kubernetes/audit手順
- 監査ポリシー・ファイルを作成し、ポリシー・ルールを定義します。
以下の例では、デプロイメント作成のみを記録し、
request/response本文は記録しません。
監査ポリシーの作成方法について詳しくは、 Kubernetes の資料を参照してください。cat >/var/lib/rancher/k3s/server/manifests/policy.yaml <<EOF # Log only deployment creations at the Metadata level. apiVersion: audit.k8s.io/v1 kind: Policy rules: - level: Metadata verbs: ["create"] resources: - group: "apps" resources: ["deployments"] EOF - 使用可能な 2 つの監査バックエンドから目的の監査バックエンドを選択します。
- ログ・バックエンド: イベントをファイル・システムに書き込みます。
- Webhookバックエンド:外部 HTTP APIにイベントを送信します。
systemdユニット構成で構成されます。 - 使用する監査バックエンドに応じて、以下のいずれかの手順を実行します。ローカル・ファイル・システム・ログ・バックエンドを使用して監査ロギングを有効にするには、以下のように、関連する構成を K3s
systemd開始引数に追加し、構成を再ロードして、 K3s サービスを再始動する必要があります。k3s.service構成ファイルのServiceセクションの下に、ExecStart内の以下の追加オプションがあります。vi /etc/systemd/system/k3s.service以下の 2 つのオプションは必須であり、両方とも
--kube-apiserver-arg=option-name=option-valueによって渡す必要があります。audit-policy-fileは、最初のステップで説明されているように、監査ポリシーのパスです。audit-log-pathは、出力監査ログのパスです。
以下のオプションの引数を使用して、ログ・ローテーションを制御できます。
audit-log-maxsizeは、ログ・ローテーションの前の最大サイズ (MB) です。audit-log-maxbackupは、保持される循環ログの最大数です。audit-log-maxageは、ローテーションされた古いファイルを保持する最大日数です。
以下の図は、例を示しています。
systemdユニットの構成が完了したら、以下のコマンドを使用してサービスを再ロードして再始動します。systemctl daemon-reload systemctl restart k3s出力監査イベントは、サービス開始引数で指定された場所にある JSONline ファイルに保管されます。 以下の例は、この手順で説明する監査ポリシーを使用して新しい Edge Gateway をペアリングした後の出力を示しています。{"kind":"Event","apiVersion":"audit.k8s.io/v1","level":"Metadata","auditID":"b20ac0e0-bd02-4b93-90fd-dd24b73c9330","stage":"RequestReceived","requestURI":"/apis/apps/v1/namespaces/cfd3995c-c011-4e1f-aa05-8c6787d1861b/deployments","verb":"create","user":{"username":"system:admin","groups":["system:masters","system:authenticated"]},"sourceIPs":["127.0.0.1"],"userAgent":"okhttp/3.12.12","objectRef":{"resource":"deployments","namespace":"cfd3995c-c011-4e1f-aa05-8c6787d1861b","apiGroup":"apps","apiVersion":"v1"},"requestReceivedTimestamp":"2022-11-30T07:52:23.440012Z","stageTimestamp":"2022-11-30T07:52:23.440012Z"} {"kind":"Event","apiVersion":"audit.k8s.io/v1","level":"Metadata","auditID":"b20ac0e0-bd02-4b93-90fd-dd24b73c9330","stage":"ResponseComplete","requestURI":"/apis/apps/v1/namespaces/cfd3995c-c011-4e1f-aa05-8c6787d1861b/deployments","verb":"create","user":{"username":"system:admin","groups":["system:masters","system:authenticated"]},"sourceIPs":["127.0.0.1"],"userAgent":"okhttp/3.12.12","objectRef":{"resource":"deployments","namespace":"cfd3995c-c011-4e1f-aa05-8c6787d1861b","name":"deployment-synchronizer","apiGroup":"apps","apiVersion":"v1"},"responseStatus":{"metadata":{},"code":201},"requestReceivedTimestamp":"2022-11-30T07:52:23.440012Z","stageTimestamp":"2022-11-30T07:52:23.446914Z","annotations":{"authorization.k8s.io/decision":"allow","authorization.k8s.io/reason":""}} {"kind":"Event","apiVersion":"audit.k8s.io/v1","level":"Metadata","auditID":"c10353df-095a-4454-96c5-ca028988911a","stage":"RequestReceived","requestURI":"/apis/apps/v1/namespaces/cfd3995c-c011-4e1f-aa05-8c6787d1861b/deployments","verb":"create","user":{"username":"system:admin","groups":["system:masters","system:authenticated"]},"sourceIPs":["127.0.0.1"],"userAgent":"okhttp/3.12.12","objectRef":{"resource":"deployments","namespace":"cfd3995c-c011-4e1f-aa05-8c6787d1861b","apiGroup":"apps","apiVersion":"v1"},"requestReceivedTimestamp":"2022-11-30T07:52:23.521285Z","stageTimestamp":"2022-11-30T07:52:23.521285Z"} {"kind":"Event","apiVersion":"audit.k8s.io/v1","level":"Metadata","auditID":"c10353df-095a-4454-96c5-ca028988911a","stage":"ResponseComplete","requestURI":"/apis/apps/v1/namespaces/cfd3995c-c011-4e1f-aa05-8c6787d1861b/deployments","verb":"create","user":{"username":"system:admin","groups":["system:masters","system:authenticated"]},"sourceIPs":["127.0.0.1"],"userAgent":"okhttp/3.12.12","objectRef":{"resource":"deployments","namespace":"cfd3995c-c011-4e1f-aa05-8c6787d1861b","name":"deployment-operator","apiGroup":"apps","apiVersion":"v1"},"responseStatus":{"metadata":{},"code":201},"requestReceivedTimestamp":"2022-11-30T07:52:23.521285Z","stageTimestamp":"2022-11-30T07:52:23.531923Z","annotations":{"authorization.k8s.io/decision":"allow","authorization.k8s.io/reason":""}}詳しくは、 Kubernetes の資料を参照してください。
リモート Webhook ログ・バックエンドを使用している場合は、以下のように、Webhook 構成を作成し、関連する構成を K3s systemd 開始引数に追加し、構成を再ロードして、 K3s サービスを再始動する必要があります。webhook構成ファイルを作成します。 このファイルには、 HTTP 接続用のアドレスと認証情報が含まれており、kubectlの設定( $HOME/.kube/config )と類似しています。 以下の例では、基本認証を使用してループバック・バックエンドを定義します。cat >/var/lib/rancher/k3s/server/manifests/webhook.yaml <<EOF apiVersion: v1 kind: Config preferences: {} clusters: # address and port of remote server - name: example-audit-backend cluster: server: http://localhost:8888 users: # client credential - name: example-user user: username: example-username password: example-password contexts: # link the remote server address and credential - name: example-context context: cluster: example-audit-backend user: example-user current-context: example-context EOF詳しくは、 Kubernetes の資料を参照してください。
k3s.service構成ファイルのServiceセクションの下に、ExecStart内の以下の追加オプションがあります。vi /etc/systemd/system/k3s.service以下の 2 つのオプションは必須であり、両方とも
--kube-apiserver-arg=option-name=option-valueによって渡す必要があります。audit-policy-fileは、最初のステップで説明されているように、監査ポリシーのパスです。audit-webhook-config-fileは、前のステップで説明したwebhook構成のパスです。
systemdユニット構成を編集した後、サービスを再ロードして再始動します。systemctl daemon-reload systemctl restart k3s出力監査イベントは、JSONのリストとして HTTP のPOSTボディにバッチで送信されます。 以下は、この手順と
webhook要求本体で説明されている監査ポリシーを使用して新しい Edge Gateway をペアリングした後の出力例です。{ "kind":"EventList", "apiVersion":"audit.k8s.io/v1", "metadata":{}, "items": [ {"level":"Metadata","auditID":"86fcb3ac-9ae2-4f8f-a102-9efa0747f513","stage":"RequestReceived","requestURI":"/apis/apps/v1/namespaces/cfd3995c-c011-4e1f-aa05-8c6787d1861b/deployments","verb":"create","user":{"username":"system:admin","groups":["system:masters","system:authenticated"]},"sourceIPs":["127.0.0.1"],"userAgent":"okhttp/3.12.12","objectRef":{"resource":"deployments","namespace":"cfd3995c-c011-4e1f-aa05-8c6787d1861b","apiGroup":"apps","apiVersion":"v1"},"requestReceivedTimestamp":"2022-12-02T09:02:10.502140Z","stageTimestamp":"2022-12-02T09:02:10.502140Z"}, {"level":"Metadata","auditID":"86fcb3ac-9ae2-4f8f-a102-9efa0747f513","stage":"ResponseComplete","requestURI":"/apis/apps/v1/namespaces/cfd3995c-c011-4e1f-aa05-8c6787d1861b/deployments","verb":"create","user":{"username":"system:admin","groups":["system:masters","system:authenticated"]},"sourceIPs":["127.0.0.1"],"userAgent":"okhttp/3.12.12","objectRef":{"resource":"deployments","namespace":"cfd3995c-c011-4e1f-aa05-8c6787d1861b","name":"deployment-synchronizer","apiGroup":"apps","apiVersion":"v1"},"responseStatus":{"metadata":{},"code":201},"requestReceivedTimestamp":"2022-12-02T09:02:10.502140Z","stageTimestamp":"2022-12-02T09:02:10.511688Z","annotations":{"authorization.k8s.io/decision":"allow","authorization.k8s.io/reason":""}}, {"level":"Metadata","auditID":"4366e4f2-f437-44f7-bc8d-97dec21b54ef","stage":"RequestReceived","requestURI":"/apis/apps/v1/namespaces/cfd3995c-c011-4e1f-aa05-8c6787d1861b/deployments","verb":"create","user":{"username":"system:admin","groups":["system:masters","system:authenticated"]},"sourceIPs":["127.0.0.1"],"userAgent":"okhttp/3.12.12","objectRef":{"resource":"deployments","namespace":"cfd3995c-c011-4e1f-aa05-8c6787d1861b","apiGroup":"apps","apiVersion":"v1"},"requestReceivedTimestamp":"2022-12-02T09:02:10.583000Z","stageTimestamp":"2022-12-02T09:02:10.583000Z"}, {"level":"Metadata","auditID":"4366e4f2-f437-44f7-bc8d-97dec21b54ef","stage":"ResponseComplete","requestURI":"/apis/apps/v1/namespaces/cfd3995c-c011-4e1f-aa05-8c6787d1861b/deployments","verb":"create","user":{"username":"system:admin","groups":["system:masters","system:authenticated"]},"sourceIPs":["127.0.0.1"],"userAgent":"okhttp/3.12.12","objectRef":{"resource":"deployments","namespace":"cfd3995c-c011-4e1f-aa05-8c6787d1861b","name":"deployment-operator","apiGroup":"apps","apiVersion":"v1"},"responseStatus":{"metadata":{},"code":201},"requestReceivedTimestamp":"2022-12-02T09:02:10.583000Z","stageTimestamp":"2022-12-02T09:02:10.612983Z","annotations":{"authorization.k8s.io/decision":"allow","authorization.k8s.io/reason":""}} ] }詳しくは、 Kubernetes の資料を参照してください。