0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

KubernetesでPodごとに異なるFilebeatの設定を適用する方法

Last updated at Posted at 2024-12-18

はじめに

この記事では,FilebeatのAutodiscover機能を使用してKubernetes環境でPodごとに異なるFilebeatの設定を適用する方法について紹介します.

FilebeatのAutodiscoverとは

FilebeatのAutodiscover機能とは,動的環境で実行されているアプリケーションやサービスに応じて,Filebeatの設定を自動的に適用する機能です.

準備

以下のツールがインストールされていることを確認してください
・Kubernetesクラスタ
・Helm
・Elasticsearch
・Kibana

実装手順

1.環境準備

まず,リポジトリを追加します.

helm repo add elastic https://helm.elastic.co

Filebeat用のNamespaceを作成します.
今回はfbにして作業を行います.

kubectl create namespace fb

2.Autodiscoverの設定

Filebeatの設定を含めたvalues.yamlを作成します.

filebeatConfig:
  filebeat.yml: |
    filebeat.autodiscover:
      providers:
        - type: kubernetes
          hints.enabled: true
          templates:
            - condition:
                equals:
                  kubernetes.labels.app: "nginx"
              config:
                - type: container
                  paths:
                    - /var/log/containers/*${data.kubernetes.container.id}.log
                  fields:
                    index: test2
                  include_lines: ["^.*notice.*"]
            - condition:
                equals:
                  kubernetes.labels.app: "httpd"
              config:
                - type: container
                  paths:
                    - /var/log/containers/*${data.kubernetes.container.id}.log
                  fields:
                    index: test2
                  include_lines: ["[Thu Dec 19 09:13:42.683062 2024] [core:notice] [pid 1:tid 1] AH00094: Command line: 'httpd -D FOREGROUND'"] 
    output.logstash:
        hosts: ["ls-logstash:5044"]
    processors:
      - add_cloud_metadata: ~
      - add_host_metadata: ~

daemonset:
  enabled: true
  podTemplate:
    spec:
      containers:
        - name: filebeat
          # Readiness probe を削除
          readinessProbe: null

rbac:
  create: true
  serviceAccount:
    create: true
    name: filebeat-sa

設定の説明

・Autodiscoverの設定:
hints.enabled: trueを使用してPodのラベルから自動的にログの収集を設定します.
・nginxコンテナのログ設定:
文字列内に"notice"が含まれるログのみを収集
・Httpdコンテナログの設定:
[Thu Dec 19 09:13:42.683062 2024] [core:notice] [pid 1:tid 1] AH00094: Command line: 'httpd -D FOREGROUND'が含まれるログのみを収集

3.HelmでFilebeatのデプロイ

作成したyamlファイルを使ってFilebeatをデプロイします.

 helm install filebeat elastic/filebeat -n fb -f filebeat-values.yaml

FilebeatのPodの確認をします.

user@user-vm:~$ kubectl get po -n fb
NAME                      READY   STATUS    RESTARTS   AGE
filebeat-filebeat-bzd4m   1/1     Running   0          4m5s

4.Autodiscoverの動作確認

確認するため異なるラベルを持つテスト用のPodを作成します.
今回は,nginxとhttpsのラベルをもつPodをデプロイします.

apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    app: nginx
spec:
  containers:
    - name: nginx
      image: nginx:latest

---
apiVersion: v1
kind: Pod
metadata:
  name: httpd-pod
  labels:
    app: httpd
spec:
  containers:
    - name: httpd
      image: httpd:latest

デプロイします.

kubectl apply -f test-pod.yaml -n fb

Podを確認します.

user@user-vm:~$ kubectl get pods -n fb
NAME                      READY   STATUS    RESTARTS   AGE
filebeat-filebeat-n95wv   1/1     Running   0          102s
httpd-pod                 1/1     Running   0          93s
nginx-pod                 1/1     Running   0          93s

5.Podから出力されるログ

httpd Podの確認

user@user-vm:~$ kubectl logs httpd-pod -n fb
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.42.4.21. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.42.4.21. Set the 'ServerName' directive globally to suppress this message
[Thu Dec 19 09:13:42.682849 2024] [mpm_event:notice] [pid 1:tid 1] AH00489: Apache/2.4.62 (Unix) configured -- resuming normal operations
[Thu Dec 19 09:13:42.683062 2024] [core:notice] [pid 1:tid 1] AH00094: Command line: 'httpd -D FOREGROUND'

このログの中から,文字列内に"notice"が含まれるログを収集する.

nginx Podの確認

user@user-vm:~$ kubectl logs nginx-pod -n fb
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2024/12/19 09:13:42 [notice] 1#1: using the "epoll" event method
2024/12/19 09:13:42 [notice] 1#1: nginx/1.27.3
2024/12/19 09:13:42 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/12/19 09:13:42 [notice] 1#1: OS: Linux 6.8.0-51-generic
2024/12/19 09:13:42 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/12/19 09:13:42 [notice] 1#1: start worker processes
2024/12/19 09:13:42 [notice] 1#1: start worker process 29
2024/12/19 09:13:42 [notice] 1#1: start worker process 30
2024/12/19 09:13:42 [notice] 1#1: start worker process 31

このログの中の,[Thu Dec 19 09:13:42.683062 2024] [core:notice] [pid 1:tid 1] AH00094: Command line: 'httpd -D FOREGROUND'が含まれるログを収集する.

6.実行結果

Kibanaで確認!
"notice"が含まれるログの収集に成功した.
image.png

"[Thu Dec 19 09:13:42.683062 2024] [core:notice] [pid 1:tid 1] AH00094: Command line: 'httpd -D FOREGROUND'"が含まれるログの収集に成功した.
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?