はじめに
前回、以下の記事でElastic AgentをデーモンセットとしてKubernetesにインストールし、コンテナログに出力されているアプリケーション系ログを収集する方法を紹介しました。
https://qiita.com/nobuhikosekiya/items/1ee046d7a601d2570f61
今回はNGINXのログのを収集します。Elastic Agentのインストールに関しては前回の記事を参照ください。
利用するNGINXデプロイメント
今回はこのようなNGINXをデプロイしました。
# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
app.kubernetes.io/name: myservice
app.kubernetes.io/version: v0.1.2
app.kubernetes.io/instance: myservice-abcxzy
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 80
NGINXのコンテナの中に入って、今回の収集対象のログを見てみます。stdoutとstderrに出るようになっており、結果コンテナログとして出力されるようになっています。
# ls -l /var/log/nginx/
total 0
lrwxrwxrwx 1 root root 11 Jan 31 23:54 access.log -> /dev/stdout
lrwxrwxrwx 1 root root 11 Jan 31 23:54 error.log -> /dev/stderr
コンテナ内部からcurlでhttp://localhost:80/
などでリクエストを送るとログを出力させることができます。このようなコンテナログが出ます。
$ kubectl logs nginx-deployment-6f85dfcdcc-wppcc
127.0.0.1 - - [02/Feb/2024:07:10:58 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.88.1" "-"
2024/02/02 07:11:01 [error] 30#30: *2 open() "/usr/share/nginx/html/hello" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /hello HTTP/1.1", host: "localhost"
127.0.0.1 - - [02/Feb/2024:07:11:01 +0000] "GET /hello HTTP/1.1" 404 153 "-" "curl/7.88.1" "-"
127.0.0.1 - - [02/Feb/2024:07:11:02 +0000] "GET /hello HTTP/1.1" 404 153 "-" "curl/7.88.1" "-"
2024/02/02 07:11:02 [error] 29#29: *3 open() "/usr/share/nginx/html/hello" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /hello HTTP/1.1", host: "localhost"
2024/02/02 07:11:03 [error] 29#29: *4 open() "/usr/share/nginx/html/hello" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /hello HTTP/1.1", host: "localhost"
127.0.0.1 - - [02/Feb/2024:07:11:03 +0000] "GET /hello HTTP/1.1" 404 153 "-" "curl/7.88.1" "-"
127.0.0.1 - - [02/Feb/2024:07:11:06 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.88.1" "-"
NGINX IntegrationのAssetのインストール
前処理として、NGINX IntegrationのAssetsをインストールします。NGINX Integrationの中のSettingsタブのInstall Nginx assets
からインストールできます。
このようにダッシュボードやDiscoverでログ表示する際のSaved searchesが使えるようになります。
さらに、裏ではこのようにnginxのログのIndex Templateが作成されています。これでlogs-nginx.access-xxxという名前パターンのインデックスに送られたログは、自動的にNGINX Integrationで最適化(ログのパーシングなど)されたログが格納されます。後続のステップで設定するDataセット名はこのネーミングルールに合うように注意していきます。
NGINXアクセスログの収集設定
Kubernetes Container Logsを検索バーで検索し、以下の画面を開きます。Integration policiesタブを開くと、前回の記事で作成したアプリケーションログを収集設定hello-java
が見えます。Addボタンで新規を追加します。
Integration nameはログの種類を表すnginx.access
にしました。
- Conditionには、
${kubernetes.container.name} == "nginx-container"
として、今回のNGINXコンテナ名で条件付けします。 - Dataset nameは、
nginx.access
です。これはさきほど有効にしたNGINX IntegrationのDataset名なので、この値である必要があります。このログはlog-nginx.access-defaultのデータストリームに送られます。 - Container parser's stream configurationは
stdout
を設定します。
この設定を適用するElastic Agentは、前回の記事でインストールしたElastic Agentにしたいので、Existing hostsから前回使ったAgent policyを選択してください。
Saveボタンを押すと、設定が反映されます。
NGINXエラーログの収集設定
同じようにエラーログの分も以下の設定で作成します。
- Integration name:
nginx.error
- Condition:
${kubernetes.container.name} == "nginx-container"
- Dataset name:
nginx.error
- Container parser's stream configuration:
stderr
結果、このようにログの種類毎にIntegration設定ができている状況です。
ログの確認
Discover画面の右上のOpenからNGINX用のログの検索設定を開きます。
おわり
やり方さえわかれば、結構簡単にできますね。このやり方の情報がどこにもなかったので、今回記事を作成しました。お役に立てれば幸いです。