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設定を適用する方法

Posted at

はじめに

この記事では,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
                  include_lines: ["^info"] # infoログのみ収集
            - condition:
                equals:
                  kubernetes.labels.app: "httpd"
              config:
                - type: container
                  paths:
                    - /var/log/containers/*${data.kubernetes.container.id}.log
                  include_lines: ["^debug"] # debugログのみ収集
    output.logstash:
      hosts: ["ls-master:30714"]
    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コンテナのログ設定:
infoログのみを収集
・Httpdコンテナログの設定:
debugログのみを収集

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.実行結果

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.0.27. 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.0.27. Set the 'ServerName' directive globally to suppress this message
[Wed Dec 18 06:23:16.787830 2024] [mpm_event:notice] [pid 1:tid 1] AH00489: Apache/2.4.62 (Unix) configured -- resuming normal operations
[Wed Dec 18 06:23:16.787929 2024] [core:notice] [pid 1:tid 1] AH00094: Command line: 'httpd -D FOREGROUND'

infoレベルのログが収集される設定.

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/18 06:23:13 [notice] 1#1: using the "epoll" event method
2024/12/18 06:23:13 [notice] 1#1: nginx/1.27.3
2024/12/18 06:23:13 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2024/12/18 06:23:13 [notice] 1#1: OS: Linux 5.15.0-128-generic
2024/12/18 06:23:13 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/12/18 06:23:13 [notice] 1#1: start worker processes
2024/12/18 06:23:13 [notice] 1#1: start worker process 30
2024/12/18 06:23:13 [notice] 1#1: start worker process 31

noticeレベルのログが収集される設定.

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?