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のログをFilebeatで収集しElasticsearchへ送ろう

Last updated at Posted at 2025-06-04

はじめに

Kubernetesの環境では、Podのログを効率よく収集し分析することが重要です。本記事では、Filebeatを活用してログを収集し、Elasticsearchに送る手順を解説します。
前提条件としてElasticsearchとLogstashとKibanaは構築を終えています。

  • こちらが構成図です
    構成図.png

環境

  • Kubernetesクラスタ(ディストリビューション:K3s、バージョン:v1.29.3)
  • Filebeat(バージョン:8.5.1)
  • Elasticsearch(バージョン:8.5.1)
  • Kibana (バージョン:7.17.3)

Filebeatの設定

Kubernetes環境でFilebeatをデプロイし、ログを収集できるようにします。

Filebeatのインストール

Elastic社が提供するHelmチャートを利用すると、簡単にFilebeatを導入できます。

1.helmコマンドをインストールします

curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash

▼出力例▼

c0a22103@c0a22103-practice:~$ curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--    100 11913  100 11913    0     0  54608      0 --:--:-- --:--:-- --:--:-- 54646
Downloading https://get.helm.sh/helm-v3.18.4-linux-amd64.tar.gz
Verifying checksum... Done.
Preparing to install helm into /usr/local/bin
[sudo] password for c0a22103:
helm installed into /usr/local/bin/helm

2.ネームスペースを作成します

kubectl create ns elastic

▼出力例▼

c0a22103@c0a22103-practice:~$ kubectl create ns elastic
namespace/elastic created

3.Filebeatの設定ファイルを作成します
Filebeatの設定を変更し、Kubernetesのコンテナログを収集するようにします。

vi fb-values.yaml
fb-values.yaml
daemonset:
  filebeatConfig:
    filebeat.yml: |
      logging.level: info
      filebeat.autodiscover:
        providers:
          - type: kubernetes
            node: ${NODE_NAME}
            hints.enabled: true
            hints.default_config:
              type: container
              paths:
                - /var/log/containers/*${data.kubernetes.container.id}.log
              fields:
                index: logstash
      output.logstash:
        hosts: ["ls-master:30544"]
      setup.template:
        name: "k8s"
        pattern: "k8s-*"
        enabled: false
      setup.ilm.enabled: false
  extraEnvs:
    - name: ELASTICSEARCH_USERNAME
      value: "elastic"
    - name: ELASTICSEARCH_PASSWORD
      value: "PASSWORD"
  resources:
    limits:
      memory: 500Mi
    requests:
      memory: 500Mi
  secretMounts: []

4.elasticリポジトリをインストールしてアップロードします

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

▼出力例▼

c0a22103@c0a22103-practice:~/Qiita$ helm repo add elastic https://helm.elastic.co
"elastic" has been added to your repositories
helm repo update

▼出力例▼

c0a22103@c0a22103-practice:~/Qiita$ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "elastic" chart repository
Update Complete. ⎈Happy Helming!⎈

5.YAMLファイルの設定でFilebeatをインストールします

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

▼出力例▼

c0a22103@c0a22103-practice:~/Qiita$ helm install fb elastic/filebeat -n elastic -f fb-values.yaml
NAME: fb
LAST DEPLOYED: Fri Jul 11 02:28:09 2025
NAMESPACE: elastic
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
1. Watch all containers come up.
  $ kubectl get pods --namespace=elastic -l app=fb-filebeat -w

6.Filebeatがインストールできたか確認します

  • 下記のコマンドでFilebeatのPodが動作しているか、ステータスを確認できます
kubectl get pods -n elastic

▼出力例▼

c0a22103@c0a22103-practice:~/Qiita$ kubectl get pods -n elastic
NAME                READY   STATUS    RESTARTS   AGE
fb-filebeat-fr2rg   1/1     Running   0          93s
  • 下記のコマンドでHelmを使ってインストールしたアプリケーションの状態を確認できます
helm list -n elastic

▼出力例▼

c0a22103@c0a22103-practice:~/Qiita$ helm list -n elastic
NAME    NAMESPACE       REVISION        UPDATED                                 STATUS          CHART           APP VERSION
fb      elastic         1               2025-07-11 02:28:09.857103126 +0000 UTC deployed        filebeat-8.5.1  8.5.1

Kibanaの動作確認をする

実際にKibanaで可視化した画面がこちらです
image.png

まとめ

本記事では、Kubernetes環境でFilebeatを用いてログを収集し、Elasticsearchへ送信する方法を紹介しました。これにより、クラスタのログを効率よく管理し、分析できるようになります。

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?