k8s 容器控制台日志收集

1、收集方案。

    通过DaemonSet方式部署filebeat 收集各节点日志。写入logstash中处理,最后写入es。

2、优势及缺点

            优点:   如果业务里面把日志全部往控制台输出,对于日志管理是非常的方便的。当删除容器了,日志文件也就没有了,所有不需要额外再写脚本全定时管理日志,

            缺点;  如果业务中有多种日志需要收集,当然也就可以通过logstash来做,只不过比较麻烦。还需要业务协调

 3.filebeat 配置

    创建configmap用于管理filbeat配置。

     

apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-config
  namespace: test
data:
  filebeat.yml: |
    filebeat.inputs:
    - type: log
      enabled: true
      paths:
        - /var/log/pods/*/*/*.log
      json.overwrite_keys: true
      symlinks: true
      json.message_key: log
      json.keys_under_root: true
      multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
      multiline.negate: true
      multiline.match: after
      json.add_error_key: true
    output.logstash:
      hosts: ["your_logstash"]

#paths中的正则会匹配k8s中所有的pod控制台日志。
#symlinks: true 文件是软件连接形式到docker目录中去的 所以这个开关需要开启Ture

4、filebeat DaemonSet yaml文件

apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: filebeat
  namespace: test
  labels:
    app: filebeat
    k8s-app: filebeat
    kubernetes.io/cluster-service: "true"
spec:
  template:
    metadata:
      labels:
        k8s-app: filebeat
        kubernetes.io/cluster-service: "true"
    spec:
      tolerations:
      - key: "node-role.kubernetes.io/master"
        operator: "Exists"
        effect: "NoSchedule"
      containers:
      - args:
          - '--path.config'
          - /etc/filebeat
        name: filebeat
        image: your images
        imagePullPolicy: Always
        resources:
          limits:
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 200Mi
        volumeMounts:
        - name: varlog
          mountPath: /var/
        - name: filebeat-config
          mountPath: /etc/filebeat/
      terminationGracePeriodSeconds: 30
      volumes:
      - name: varlog
        hostPath:
          path: /var/
      - name: varlibdockercontainers
      - name: filebeat-config
        configMap:
          name: filebeat-config

filebeat Dockerfile

from store/elastic/filebeat:7.3.0
USER root

#运行用户改成root不然会有权限问题

logstash 配置文件

input {
  beats {
    port => "5044"
  }
}

filter {
    ruby {
        code => "
####处理收集的文件的目录 根据目录名字输出到不同的索引 project = event.get('source').split('/') project.each do |temp2| if temp2.nil? then next end key = 'project' value = project[-2] if key.nil? then next end event.set(key, value) end " } } output { elasticsearch { hosts => ["es:9200"] index => "%{project}-%{+YYYY.MM}" } }

logstash Dockerfile

      

from docker.elastic.co/logstash/logstash:6.1.1
USER root
RUN yum -y install vim
RUN echo "xpack.monitoring.enabled: false" >>/usr/share/logstash/config/logstash.yml

#docker run -it -v your_config:/home/logstash.yml your_image -f /home/logstash.yml

总结 

         当创建一个pod时候,会自动收集这个pod的日志,并以项目名为索引写入es,无需在添加其他配。当然如果业务栈比较广,有java也有go等等语言的,日志格式就不是一个标准了,处理比较麻烦,对于logstash来说那都不是事,

ruby 插件支持ruby语法,处理起来很方便,多写几个if判断就行了哈哈。可以参考上面

        

 

posted @ 2019-09-09 15:42  Django_gege  阅读(3111)  评论(0编辑  收藏  举报