其他分享
首页 > 其他分享> > kubernete 日志收集之 efk(es+Fluentd+Kibana)

kubernete 日志收集之 efk(es+Fluentd+Kibana)

作者:互联网

简介

EFK工作示意
image

部署es服务

部署分析

  1. es生产环境是部署es集群,通常会使用statefulset进行部署;演示环境可以单点部署,生产环境必须集群部署
  2. 数据存储挂载主机路径
  3. es默认使用elasticsearch用户启动进程,es的数据目录是通过宿主机的路径挂载,因此目录权限被主机的目录权限覆盖,因此可以利用init container容器在es进程启动之前把目录的权限修改掉,注意init container要用特权模式启动。

部署 es 集群版

es 三个节点
efk/elasticsearch.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    addonmanager.kubernetes.io/mode: Reconcile
    k8s-app: elasticsearch
    version: v7.4.2
  name: elasticsearch
  namespace: monitoring
spec:
  replicas: 3
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: elasticsearch
      version: v7.4.2
  serviceName: elasticsearch
  template:
    metadata:
      labels:
        k8s-app: elasticsearch
        version: v7.4.2
    spec:
      nodeSelector:
        log: es	## 指定部署在哪个节点。需根据环境来修改
      containers:
      - env:
        - name: NAMESPACE
          valueFrom:
            fieldRef:
              apiVersion: v1
              fieldPath: metadata.namespace
        - name: discovery.zen.minimum_master_nodes
          value: "2"
        - name: cluster.name
          value: es-cluster
        - name: discovery.seed_hosts
          value: elasticsearch-0.elasticsearch,elasticsearch-1.elasticsearch,elasticsearch-2.elasticsearch
        - name: cluster.initial_master_nodes
          value: elasticsearch-0,elasticsearch-1,elasticsearch-2
        - name: network.host
          value: "0.0.0.0"
        - name: ES_JAVA_OPTS
          value: "-Xms5g -Xmx5g"
        image: mrliulei/elasticsearch:v7.4.2
        name: elasticsearch
        ports:
        - containerPort: 9200
          name: db
          protocol: TCP
        - containerPort: 9300
          name: transport
          protocol: TCP
        volumeMounts:
        - mountPath: /usr/share/elasticsearch/data
          name: elasticsearch
      dnsConfig:
        options:
        - name: single-request-reopen
      initContainers:
      - command:
        - /sbin/sysctl
        - -w
        - vm.max_map_count=262144
        image: alpine:3.6
        imagePullPolicy: IfNotPresent
        name: elasticsearch-init
        resources: {}
        securityContext:
          privileged: true
      - name: fix-permissions
        image: alpine:3.6
        command: ["sh", "-c", "chown -R 1000:1000 /usr/share/elasticsearch/data"]
        securityContext:
          privileged: true
        volumeMounts:
        - name: elasticsearch
          mountPath: /usr/share/elasticsearch/data
      volumes:
      - name: elasticsearch
        hostPath:
          path: /esdata
---
apiVersion: v1
kind: Service
metadata:
  labels:
    k8s-app: elasticsearch
  name: elasticsearch
  namespace: monitoring
spec:
  ports:
  - port: 9200
    protocol: TCP
    name: db
  - port: 9300
    protocol: TCP
    name: transport
  selector:
    k8s-app: elasticsearch
  type: ClusterIP
# 检查集群状态
# 登录 es 的pod 检查集群状态
kubectl  -n monitoring exec -it elasticsearch-0 bash

curl http://elasticsearch:9200/_cat/health?v
epoch      timestamp cluster    status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1658396628 09:43:48  es-cluster green           3         3     10   5    0    0        0             0                  -                100.0%

curl http://localhost:9200/_cat/health?v

curl http://elasticsearch:9200/_cluster/state?pretty

curl http://localhost:9200/_cluster/state?pretty

部署kibana

部署分析

  1. kibana需要暴漏web页面给前端使用,因此使用ingress配置域名来实现对kibana的访问
  2. kibana为无状态应用,直接使用Deployment来启动
  3. kibana需要访问es,直接利用k8s服务发现访问此地址即可,http://elasticsearch:9200

部署并验证

资源文件 efk/kibana.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kibana
  namespace: monitoring
  labels:
    app: kibana
spec:
  selector:
    matchLabels:
      app: kibana
  template:
    metadata:
      labels:
        app: kibana
    spec:
      containers:
      - name: kibana
        image: mrliulei/kibana:v7.4.2
        resources:
          limits:
            cpu: 1000m
          requests:
            cpu: 100m
        env:
          - name: ELASTICSEARCH_URL
            value: elasticsearch:9200
        ports:
        - containerPort: 5601
---
apiVersion: v1
kind: Service
metadata:
  name: kibana
  namespace: monitoring
  labels:
    app: kibana
spec:
  ports:
  - port: 5601
    protocol: TCP
    targetPort: 5601
  type: ClusterIP
  selector:
    app: kibana
---
# 这里我使用的是 之前 创建出来的 ingress ,所以这里的ingress我注视掉了;
# apiVersion: extensions/v1beta1
# kind: Ingress
# metadata:
#   name: kibana
#   namespace: monitoring
# spec:
#   rules:
#   - host: kibana.devops.cn
#     http:
#       paths:
#       - path: /
#         backend:
#           serviceName: kibana
#           servicePort: 5601

标签:Fluentd,name,app,kibana,efk,elasticsearch,Kibana,es,9200
来源: https://www.cnblogs.com/Star-Haitian/p/16502875.html