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判斷就行了哈哈。可以參考上面
