github 地址:https://github.com/AliyunContainerService/log-pilot
介紹
log-pilot是一個(gè)很棒的 docker 日志工具〗拔洌可以從dockerlog-pilot主機(jī)收集日志并將它們發(fā)送到您的集中式日志系統(tǒng)晓勇,例如 elasticsearch掌呜、graylog2添怔、awsog 等歪脏。log-pilot不僅可以收集 docker stdout钞啸,還可以收集 docker 容器內(nèi)的日志文件襟齿。
系統(tǒng)架構(gòu)
準(zhǔn)備工作
- 已經(jīng)成功部署了一個(gè)kubernetes集群
部署Elasticsearch
為了方便測(cè)試梯澜,在單機(jī)部署了一個(gè)Elasticsearch+Kibana
# 部署Elasticsearch
docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:6.8.23
# 部署Kibana (ELASTICSEARCH_URL請(qǐng)換成自己本地ip)
docker run --name kibana -e ELASTICSEARCH_URL=http://192.168.0.71:9200 -p 5601:5601 -d kibana:6.8.23
成功訪問(wèn)Kibana
部署log-pilot
注意:ELASTICSEARCH_HOST 寞冯、ELASTICSEARCH_PORT 需要換成剛剛創(chuàng)建Elasticsearch地址
如果是集群,ELASTICSEARCH_HOSTS 變量寫(xiě)多個(gè)地址即可晚伙,中間用逗號(hào)隔開(kāi)
pilot-elasticsearch-kubernetes.yml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: log-pilot
labels:
k8s-app: log-pilot
spec:
updateStrategy:
type: RollingUpdate
selector:
matchLabels:
k8s-app: log-pilot
template:
metadata:
labels:
k8s-app: log-pilot
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: log-pilot
image: registry.cn-hangzhou.aliyuncs.com/acs/log-pilot:0.9.5-fluentd
env:
- name: "LOGGING_OUTPUT"
value: "elasticsearch"
- name: "ELASTICSEARCH_HOST"
value: "192.168.0.71" #changeme
- name: "ELASTICSEARCH_PORT"
value: "9200" #changeme
- name: "NODE_NAME"
valueFrom:
fieldRef:
fieldPath: spec.nodeName
volumeMounts:
- name: sock
mountPath: /var/run/docker.sock
- name: root
mountPath: /host
readOnly: true
- name: pos
mountPath: /pilot/pos
- name: localtime
mountPath: /etc/localtime
securityContext:
capabilities:
add:
- SYS_ADMIN
#terminationGracePeriodSeconds: 30
volumes:
- name: sock
hostPath:
path: /var/run/docker.sock
- name: root
hostPath:
path: /
- name: pos
emptyDir: {}
- name: localtime
hostPath:
path: /etc/localtime
在k8s集群部署
kubectl apply -f pilot-elasticsearch-kubernetes.yml
部署成功
部署一個(gè)測(cè)試服務(wù)
tomcat.yml
apiVersion: v1
kind: Pod
metadata:
name: tomcat
spec:
tolerations:
- key: "node-role.kubernetes.io/master"
effect: "NoSchedule"
containers:
- name: tomcat
image: "tomcat:7.0"
env:
- name: aliyun_logs_catalina
value: "stdout" # 告訴 log-pilot 這個(gè)容器要收集 stdout 日志吮龄。
- name: aliyun_logs_access
value: "/usr/local/tomcat/logs/catalina.*.log" # 則表示要收集容器內(nèi) /usr/local/tomcat/logs/ 目錄下所有名字匹配 catalina.*.log的文件日志。
volumeMounts:
- name: tomcat-log
mountPath: /usr/local/tomcat/logs
volumes:
- name: tomcat-log
emptyDir: {}
啟動(dòng)服務(wù)后咆疗,會(huì)自動(dòng)按天創(chuàng)建兩個(gè)索引
接下來(lái)就可以愉快的玩耍了@熘恪!午磁!