サイトアイコン デーコムラボ

Kubernetes 1.20.14 に elasticsearch 7.17 / kibana 7.17 / fluentd daemonset をインストールしてみた

第二システム部の小浜です。

今回はKubernetes環境にelasticsearchとkibana、ログ回収用のfluentd daemonsetをインストールしてみました。

概要

前提とする環境

今回は以下の環境で行いました。

前準備:ネームスペースの作成

今回はネームスペース log-ns にインストールしていくので、ネームスペースを作成します。

export NAMESPACE_NAME=log-ns
kubectl create namespace $NAMESPACE_NAME

elasticsearch のインストール

elasticsearch は、公式 helm からインストールします。

# elasticsearch install
helm repo add elastic https://helm.elastic.co
helm install \
    --namespace $NAMESPACE_NAME  elasticsearch elastic/elasticsearch  \
    --set antiAffinity="best effort" \
    --wait

kibana のインストール

kibana は、公式 helm からインストールします。

今回は nginx ingress 経由で kibana の Web画面にアクセスするため、
ingress用の設定を指定して helm からインストールしています。
アクセス用のホスト名は「kibana.area54.local」としています。

# kibana install
helm repo add elastic https://helm.elastic.co
# 設定ファイルを作成
cat > kibana-values.yaml << "EOF"
ingress:
  enabled: true
  className: "nginx"
  pathtype: ImplementationSpecific
  annotations: {}
  # kubernetes.io/ingress.class: nginx
  # kubernetes.io/tls-acme: "true"
  hosts:
    - host: kibana.area54.local
      paths:
        - path: /
  #tls: []
  #  - secretName: chart-example-tls
  #    hosts:
  #      - chart-example.local
EOF
helm template elastic/kibana --values kibana-values.yaml
helm install \
    --namespace $NAMESPACE_NAME  kibana elastic/kibana \
    --values kibana-values.yaml \
    --wait

/etc/hostsに追加

「kibana.area54.local」というFQDNで実際に通信できるようにするため、
/etc/hostsにIPアドレスとFQDNを追記します。

# add /etc/hosts
echo "192.168.54.77  kibana.area54.local" | sudo tee -a /etc/hosts

fluentd daemonset のインストール

fluentd は、公式 yaml から fluentd daemonset (rbac型, elasticsearchに送信) をインストールします。

YAMLファイルを一部編集しています。

ログ転送先のelasticsearchのホスト名を「elasticsearch-master.log-ns.svc.cluster.local」としています。

ログ取得元のDockerコンテナのログファイル位置指定「/var/lib/docker/containers」としています。

# fluentd daemonset install (RBAC設定)
# https://github.com/fluent/fluentd-kubernetes-daemonset/blob/master/fluentd-daemonset-elasticsearch-rbac.yaml
#
cat > fluentd-daemonset-elasticsearch-rbac.yaml << "EOF"
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: fluentd
  namespace: kube-system

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: fluentd
rules:
- apiGroups:
  - ""
  resources:
  - pods
  - namespaces
  verbs:
  - get
  - list
  - watch

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: fluentd
roleRef:
  kind: ClusterRole
  name: fluentd
  apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
  name: fluentd
  namespace: kube-system

---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: fluentd
  namespace: kube-system
  labels:
    k8s-app: fluentd-logging
    version: v1
spec:
  selector:
    matchLabels:
      k8s-app: fluentd-logging
      version: v1
  template:
    metadata:
      labels:
        k8s-app: fluentd-logging
        version: v1
    spec:
      serviceAccount: fluentd
      serviceAccountName: fluentd
      #tolerations:
      #- key: node-role.kubernetes.io/master
      #  effect: NoSchedule
      containers:
      - name: fluentd
        image: fluent/fluentd-kubernetes-daemonset:v1-debian-elasticsearch
        env:
          - name:  FLUENT_ELASTICSEARCH_HOST
            value: "elasticsearch-master.log-ns.svc.cluster.local"
          - name:  FLUENT_ELASTICSEARCH_PORT
            value: "9200"
          - name: FLUENT_ELASTICSEARCH_SCHEME
            value: "http"
          # Option to configure elasticsearch plugin with self signed certs
          # ================================================================
          - name: FLUENT_ELASTICSEARCH_SSL_VERIFY
            value: "false"
          # Option to configure elasticsearch plugin with tls
          # ================================================================
          - name: FLUENT_ELASTICSEARCH_SSL_VERSION
            value: "TLSv1_2"
          # X-Pack Authentication
          # =====================
          - name: FLUENT_ELASTICSEARCH_USER
            value: ""
          - name: FLUENT_ELASTICSEARCH_PASSWORD
            value: ""
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: varlog
          mountPath: /var/log
        # When actual pod logs in /var/lib/docker/containers, the following lines should be used.
        - name: dockercontainerlogdirectory
          mountPath: /var/lib/docker/containers
          readOnly: true
        # When actual pod logs in /var/log/pods, the following lines should be used.
        #- name: dockercontainerlogdirectory
        #  mountPath: /var/log/pods
        #  readOnly: true
      terminationGracePeriodSeconds: 30
      volumes:
      - name: varlog
        hostPath:
          path: /var/log
      # When actual pod logs in /var/lib/docker/containers, the following lines should be used.
      - name: dockercontainerlogdirectory
        hostPath:
          path: /var/lib/docker/containers
      # When actual pod logs in /var/log/pods, the following lines should be used.
      #- name: dockercontainerlogdirectory
      #  hostPath:
      #    path: /var/log/pods
EOF

kubectl apply -f fluentd-daemonset-elasticsearch-rbac.yaml

テストアクセス

ブラウザで「http://kibana.area54.local/」にアクセスします。

Kinabaの画面が表示されれば成功です。

削除

削除する場合は、以下のコマンドを実行します。

helm delete  --namespace $NAMESPACE_NAME  elasticsearch
helm delete  --namespace $NAMESPACE_NAME  kibana
kubectl delete -f fluentd-daemonset-elasticsearch-rbac.yaml
kubectl delete -f fluentd-daemonset-elasticsearch.yaml
モバイルバージョンを終了