基于K8S实现EFK日志收集

介绍

在日志收集系统中,我们会想到Logstash,因为它是ELK stack中的重要成员,但是在测试过程中发现,Logstash是基于JDK的,在没有产生日志的情况单纯启动Logstash就大概要消耗500M内存,在每个Pod中都启动一个日志收集组件的情况下,使用logstash有点浪费系统资源,经人推荐我们选择使用Filebeat替代,经测试单独启动Filebeat容器大约会消耗12M内存,比起logstash相当轻量级.
这边我们使用elasticsearch+filebeat+kibana收集pod指定目录日志,filebeat用于收集日志,es用于存储,kibana用于展示。

部署

安装compose
curl -L "https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-composechmod a+x /usr/local/bin/docker-compose
ES和kibana部署
version: '3.9'services:  elasticsearch:    image: elasticsearch:7.13.3    container_name: elasticsearch    volumes:      - ./data/local/elasticsearch/data:/usr/share/elasticsearch/data  #这里将elasticsearch的数据文件映射本地,以保证下次如果删除>了容器还有数据    environment:      - discovery.type=single-node      - bootstrap.memory_lock=true      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"    ports:      - "9200:9200"        kibana:    image: kibana:7.13.3    container_name: kibana    depends_on:      - elasticsearch     environment:      ELASTICSEARCH_HOSTS: http://ip:9200 #设置elasticsearch      I18N_LOCALE: zh-CN    ports:      - 5601:5601

创建一个configmap,存储filebeat配置文件

apiVersion: v1kind: ConfigMapmetadata:  name: filebeatconfdata:  filebeat.yml: |-    filebeat.inputs:    - input_type: log      paths:        - /data/logs/*.log         # 收集日志的路径        - /data/logs/*/*         # 收集日志的路径              tail_files: true      fields:        pod_name: '${podName}'        pod_ip: '${podIp}'        pod_deploy_name: '${podDeployName}'        pod_namespace: '${podNamespace}'      fields_under_root: true    setup.ilm.enabled: false    setup.template.name: "${podNamespace}"    setup.template.pattern: "${podNamespace}-*"    output.elasticsearch:        hosts: ["192.168.1.239:9200"]    # elasticsearch 的ip+port      index: "${podNamespace}-%{+yyyy.MM.dd}"     # 索引名称定义

可以定义多个路径,根据自己的需求修改

配置deployment

---kind: DeploymentapiVersion: apps/v1metadata:  name: java-1-dev  namespace: dev  annotations: {}spec:  replicas: 1  selector:    matchLabels:      app: java-1-dev  template:    metadata:      creationTimestamp: null      labels:        app: java-1-dev      annotations:        kubectl.kubernetes.io/restartedAt: '2023-07-18T11:27:26+08:00'    spec:      volumes:        - name: data-log          hostPath:            path: /tmp/hostpath            type: ''        - name: logpath          emptyDir: {}        - name: local-time          hostPath:            path: /usr/share/zoneinfo/Asia/Shanghai            type: ''        - name: filebeatconf          configMap:            name: filebeatconf            items:              - key: filebeat.yml                path: filebeat.yml            defaultMode: 420      containers:        - name: filebeat          image: 'registry.cn-beijing.aliyuncs.com/dotbalo/filebeat:7.10.2'          env:            - name: podIp              valueFrom:                fieldRef:                  apiVersion: v1                  fieldPath: status.podIP            - name: podName              valueFrom:                fieldRef:                  apiVersion: v1                  fieldPath: metadata.name            - name: podNamespace              valueFrom:                fieldRef:                  apiVersion: v1                  fieldPath: metadata.namespace            - name: podDeployName              value: java-1-dev            - name: TZ              value: Asia/Shanghai          resources:            limits:              cpu: 200m              memory: 300Mi            requests:              cpu: 10m              memory: 100Mi          volumeMounts:            - name: data-log              mountPath: /data/logs/   #将服务日志挂载到filebeat,filebeat从这边路径下收集日志            - name: filebeatconf              mountPath: /usr/share/filebeat/filebeat.yml              subPath: filebeat.yml          terminationMessagePath: /dev/termination-log          terminationMessagePolicy: File          imagePullPolicy: IfNotPresent          securityContext:            runAsUser: 0        - name: java-1-dev          image: '192.168.1.123:9010/java/java-1-dev:dev-30'          ports:            - containerPort: 80              protocol: TCP          env:            - name: HOST_IP              valueFrom:                fieldRef:                  apiVersion: v1                  fieldPath: status.hostIP            - name: NAMESPACE              valueFrom:                fieldRef:                  apiVersion: v1                  fieldPath: metadata.namespace            - name: POD_IP              valueFrom:                fieldRef:                  apiVersion: v1                  fieldPath: status.podIP            - name: POD_NAME              valueFrom:                fieldRef:                  apiVersion: v1                  fieldPath: metadata.name          resources: {}          volumeMounts:            - name: data-log              mountPath: /logs   #服务中的日志路径          terminationMessagePath: /dev/termination-log          terminationMessagePolicy: File          imagePullPolicy: IfNotPresent      restartPolicy: Always      terminationGracePeriodSeconds: 30      dnsPolicy: ClusterFirst      securityContext: {}      schedulerName: default-scheduler  strategy:    type: RollingUpdate    rollingUpdate:      maxUnavailable: 25%      maxSurge: 25%  revisionHistoryLimit: 10  progressDeadlineSeconds: 600

按自己需求修改服务日志路径,重点处都有添加注释.

用kibana查看日志

阅读全文
下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.shuli.cc/?p=15627,转载请注明出处。
0

评论0

显示验证码
没有账号?注册  忘记密码?