kubernete 日志收集之 efk(es+Fluentd+Kibana)
作者:互联网
简介
EFK工作示意
-
Elasticsearch
一个开源的分布式、Restful 风格的搜索和数据分析引擎,它的底层是开源库Apache Lucene。它可以被下面这样准确地形容:- 一个分布式的实时文档存储,每个字段可以被索引与搜索;
- 一个分布式实时分析搜索引擎;
- 能胜任上百个服务节点的扩展,并支持 PB 级别的结构化或者非结构化数据。
-
Fluentd
一个针对日志的收集、处理、转发系统。通过丰富的插件系统,可以收集来自于各种系统或应用的日志,转化为用户指定的格式后,转发到用户所指定的日志存储系统之中。
Fluentd 通过一组给定的数据源抓取日志数据,处理后(转换成结构化的数据格式)将它们转发给其他服务,比如 Elasticsearch、对象存储、kafka等等。Fluentd 支持超过300个日志存储和分析服务,所以在这方面是非常灵活的。主要运行步骤如下- 首先 Fluentd 从多个日志源获取数据
- 结构化并且标记这些数据
- 然后根据匹配的标签将数据发送到多个目标服务
-
Kibana
Kibana是一个开源的分析和可视化平台,设计用于和Elasticsearch一起工作。可以通过Kibana来搜索,查看,并和存储在Elasticsearch索引中的数据进行交互。也可以轻松地执行高级数据分析,并且以各种图标、表格和地图的形式可视化数据。
部署es服务
部署分析
- es生产环境是部署es集群,通常会使用statefulset进行部署;演示环境可以单点部署,生产环境必须集群部署
- 数据存储挂载主机路径
- 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
部署分析
- kibana需要暴漏web页面给前端使用,因此使用ingress配置域名来实现对kibana的访问
- kibana为无状态应用,直接使用Deployment来启动
- 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