log-Pilot是一个智能容器日志采集工具,它不仅能够高效便捷地将容器日志采集输出到多种存储日志后端,同时还能够动态地发现和采集容器内部的日志文件,更多咨询可以移步这里。
log-Pilot目前支持两种工具对日志进行收集,Fluentd Plugin 和 Filebeat Plugin。
Log-Pilot支持容器事件管理,它能够动态地监听容器的事件变化,然后依据容器的标签来进行解析,生成日志采集配置文件,然后交由采集插件来进行日志采集。
在Kubernetes下,Log-Pilot可以依据环境变量 aliyun_logs_$name = $path 动态地生成日志采集配置文件,其中包含两个变量:
- $name是我们自定义的一个字符串,它在不同的场景下指代不同的含义,在本场景中,将日志采集到ElasticSearch的时候,这个$name表示的是Index。
- 另一个是$path,支持两种输入形式,stdout和容器内部日志文件的路径,对应日志标准输出和容器内的日志文件。
- 第一种约定关键字stdout表示的是采集容器的标准输出日志,如本例中我们要采集tomcat容器日志,那么我们通过配置标签
aliyun.logs.catalina=stdout来采集tomcat标准输出日志。 - 第二种是容器内部日志文件的路径,也支持通配符的方式,通过配置环境变量
aliyun_logs_access=/usr/local/tomcat/logs/*.log来采集tomcat容器内部的日志。当然如果你不想使用aliyun这个关键字,Log-Pilot也提供了环境变量PILOT_LOG_PREFIX可以指定自己的声明式日志配置前缀,比如PILOT_LOG_PREFIX: "aliyun,custom"。
- 第一种约定关键字stdout表示的是采集容器的标准输出日志,如本例中我们要采集tomcat容器日志,那么我们通过配置标签
部署实例:
apiVersion: apps/v1kind: DaemonSetmetadata:name: log-pilotlabels:app: log-pilotnamespace: kube-opsspec:selector:matchLabels:app: log-pilotupdateStrategy:type: RollingUpdatetemplate:metadata:labels:app: log-pilotannotations:scheduler.alpha.kubernetes.io/critical-pod: ''spec:tolerations:- key: node-role.kubernetes.io/mastereffect: NoSchedulecontainers:- name: log-pilotimage: registry.cn-hangzhou.aliyuncs.com/acs/log-pilot:0.9.7-filebeatresources:limits:memory: 500Mirequests:cpu: 200mmemory: 200Mienv:- name: "NODE_NAME"valueFrom:fieldRef:fieldPath: spec.nodeName# 日志收集前缀- name: PILOT_LOG_PREFIXvalue: aliyun- name: "LOGGING_OUTPUT"value: "elasticsearch"# 请确保集群到ES网络可达- name: "ELASTICSEARCH_HOSTS"value: "elasticsearch:9200"# 配置ES访问权限#- name: "ELASTICSEARCH_USER"# value: "{es_username}"#- name: "ELASTICSEARCH_PASSWORD"# value: "{es_password}"volumeMounts:- name: sockmountPath: /var/run/docker.sock- name: rootmountPath: /hostreadOnly: true- name: varlibmountPath: /var/lib/filebeat- name: varlogmountPath: /var/log/filebeat- name: localtimemountPath: /etc/localtimereadOnly: truelivenessProbe:failureThreshold: 3exec:command:- /pilot/healthzinitialDelaySeconds: 10periodSeconds: 10successThreshold: 1timeoutSeconds: 2securityContext:capabilities:add:- SYS_ADMINterminationGracePeriodSeconds: 30volumes:- name: sockhostPath:path: /var/run/docker.sock- name: roothostPath:path: /- name: varlibhostPath:path: /var/lib/filebeattype: DirectoryOrCreate- name: varloghostPath:path: /var/log/filebeattype: DirectoryOrCreate- name: localtimehostPath:path: /etc/localtim
创建pod测试
apiVersion: v1kind: Podmetadata:name: tomcatspec:containers:- name: tomcatimage: "tomcat:8.0"env:# 1、stdout为约定关键字,表示采集标准输出日志# 2、配置标准输出日志采集到ES的catalina索引下- name: aliyun_logs_catalinavalue: "stdout"# 1、配置采集容器内文件日志,支持通配符# 2、配置该日志采集到ES的access索引下- name: aliyun_logs_accessvalue: "/usr/local/tomcat/logs/catalina.*.log"# 容器内文件日志路径需要配置emptyDirvolumeMounts:- name: tomcat-logmountPath: /usr/local/tomcat/logsvolumes:- name: tomcat-logemptyDir: {}
然后我们查看索引会看到access-和catalina-的索引
# curl -XGET 'localhost:9200/_cat/indices?v&pretty'health status index uuid pri rep docs.count docs.deleted store.size pri.store.sizegreen open access-2020.06.23 0LS6STfpQ4yHt7makuSI1g 5 1 40 0 205.5kb 102.5kbgreen open logstash-2020.06.23 HR62innTQi6HjObIzf6DHw 5 1 99 0 296kb 148kbgreen open catalina-2020.06.23 dSFGcZlPS6-wieFKrOWV-g 5 1 40 0 227.1kb 133.3kbgreen open .kibana H-TAto8QTxmi-jI_4mIUrg 1 1 2 0 20.4kb 10.2kbgreen open logstash-2020.06.22 8-IFAOj_SqiipqOXN6Soxw 5 1 43784 0 30.6mb 15.3mb
然后到页面添加索引即可。
当然日志的输出除了直接输出到es外还可以输出到其他地方,如果是使用filebeat则可以点击这里进行查看。如果是fluentd,则点击这里。
自制log-pilot
FROM golang:1.9-alpine3.6 as builderENV PILOT_DIR /go/src/github.com/AliyunContainerService/log-pilotARG GOOS=linuxARG GOARCH=amd64#RUN set -ex && apk add --no-cache make gitWORKDIR $PILOT_DIRCOPY . $PILOT_DIRRUN go installFROM alpine:3.6ENV FILEBEAT_VERSION=7.4.1COPY assets/glibc/glibc-2.26-r0.apk /tmp/RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \apk update && \apk add python && \apk add ca-certificates && \apk add wget && \update-ca-certificates && \wget http://acs-logging.oss-cn-hangzhou.aliyuncs.com/beats/filebeat/filebeat-${FILEBEAT_VERSION}-linux-x86_64.tar.gz -P /tmp/ && \mkdir -p /etc/filebeat /var/lib/filebeat /var/log/filebeat && \tar zxf /tmp/filebeat-${FILEBEAT_VERSION}-linux-x86_64.tar.gz -C /tmp/ && \cp -rf /tmp/filebeat-${FILEBEAT_VERSION}-linux-x86_64/filebeat /usr/bin/ && \cp -rf /tmp/filebeat-${FILEBEAT_VERSION}-linux-x86_64/fields.yml /etc/filebeat/ && \cp -rf /tmp/filebeat-${FILEBEAT_VERSION}-linux-x86_64/kibana /etc/filebeat/ && \cp -rf /tmp/filebeat-${FILEBEAT_VERSION}-linux-x86_64/module /etc/filebeat/ && \cp -rf /tmp/filebeat-${FILEBEAT_VERSION}-linux-x86_64/modules.d /etc/filebeat/ && \apk add --allow-untrusted /tmp/glibc-2.26-r0.apk && \rm -rf /var/cache/apk/* /tmp/filebeat-${FILEBEAT_VERSION}-linux-x86_64.tar.gz /tmp/filebeat-${FILEBEAT_VERSION}-linux-x86_64 /tmp/glibc-2.26-r0.apkCOPY --from=builder /go/bin/log-pilot /pilot/pilotCOPY assets/entrypoint assets/filebeat/ assets/healthz /pilot/RUN chmod +x /pilot/pilot /pilot/entrypoint /pilot/healthz /pilot/config.filebeatHEALTHCHECK CMD /pilot/healthzVOLUME /var/log/filebeatVOLUME /var/lib/filebeatWORKDIR /pilot/ENV PILOT_TYPE=filebeatENTRYPOINT ["/pilot/entrypoint"]
