Transport log-entity via conveyor. Inspired by AliyunContainerService/log-pilot.
why conveyor?
- 高度可定制
- 提供更多实现细节,使用原生 filebeat 构建
- 支持 Elasticsearch7.x
conveyor 是采一个负责采集 docker 容器日志的组件,使用 Porter 将特定容器产生的日志输出到指定后端,如 kafka/elasticsearch/redis/...。目前 porter 有:
- filebeat-porter.
- TODO...
GOPATH mode
$ go get -u github.com/chenjiandongx/conveyor/...
GOMODULE mode
require (
github.com/chenjiandongx/conveyor
)
package main
import (
"github.com/chenjiandongx/conveyor"
)
func main() {
porter := conveyor.NewFileBeatPorter(nil)
cy := conveyor.NewConveyor("")
cy.RegisterPorter(porter)
cy.Run()
}
Porter Interface/ ContainerInfo Struct
type Porter interface {
List(containers []*ContainerInfo) error
Create(container *ContainerInfo) error
Delete(container *ContainerInfo) error
Run()
}
type ContainerInfo struct {
ID string
Name string
Env map[string]string
Labels map[string]string
LogPath []string
}
容器启动的时候会优先读取 /etc/filebeat/filebeat.yaml 和 /etc/filebeat/configs/config.tmpl 两个配置文件,不存在则使用默认配置。${your_varname}
均为可选参数,非必须。
$ docker run -d --restart=always --name conveyor \
-v /var/run/docker.sock:/var/run/docker.sock
-v /:/host:ro
-v ${your_filebeat_data_dir}:/etc/filebeat/data
-v ${your_filebeat_base_confile_file}:/etc/filebeat/filebeat.yaml
-v ${your_filebeat_custom_confile_tmpl}:/etc/filebeat/configs/config.tmpl
-e CONVEYOR_NAME=${your_conveyor_name}
chenjiandongx/conveyor:latest
默认 /etc/filebeat/filebeat.yaml
# 标准 filebeat 配置文件
filebeat.config.inputs:
enabled: true
path: /etc/filebeat/configs/*.yaml
reload.enabled: true
reload.period: 10s
output.console:
pretty: true
默认 /etc/filebeat/configs/config.tmpl
# 标准 golang 模板语言
- type: log
paths:
- "/tmp/tmp.log"
{{- range . }}
- type: log
paths:
{{- range .LogPath }}
- "{{ . }}"
{{- end }}
fields:
{{- range $k, $v := .Labels }}
{{ $k }}: {{ $v }}
{{- end }}
{{- end }}
启动容器后向 nginx 发送请求再查看 conveyor 的日志,可以看到日志被输出到标准输出。
$ docker run -d -e CONVEYOR_ENABLED=true -e CONVEYOR_PATH="stdout" --name ngx nginx
容器环境变量
CONVEYOR_NAME
: conveyor 实例名称。默认为 ""CONVEYOR_ENABLED
: 是否开启日志追踪,"true" 时开启。默认为 ""CONVEYOR_FILED
: filebeat.inputs.fields 字段,支持,
分割,如 CONVEYOR_FILED="app=nginx,env=dev"。默认为 ""CONVEYOR_PATH
: 用户自定义追踪日志路径,支持;
分割,"stdout" 代表采集容器的标准输出。同时也支持指定索引(仅适用于 ES 输出端),索引以:
分割,CONVEYOR_PATH="tmp-index:/tmp/logs/*.log;stdout-index:stdout"
表示讲追踪标准日志输出以及/tmp/logs/*.log
路径下的日志,前者在 ES 中的前缀为tmp-index
后者为stdout-index
。
使用 DaemonSet 形式部署 conveyor,生产环境请将 /etc/filebeat/data 目录使用 hostpath 挂载出来,该目录记录着 filebeat 的消费进度。
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: conveyor
spec:
selector:
matchLabels:
name: conveyor
template:
metadata:
labels:
name: conveyor
spec:
containers:
- name: conveyor
image: chenjiandongx/conveyor:latest
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
# 挂载 docker.sock 文件,监听 dockerd 事件
- name: docker-sock
mountPath: /var/run/docker.sock
# 挂载 / 路径,只读权限,用于日志收集
- name: docker-log
mountPath: /host
readOnly: true
- name: filebeat-data
mountPath: /etc/filebeat/data
- name: filebeat-config
mountPath: /etc/filebeat/filebeat.yaml
subPath: filebeat.yaml
volumes:
- name: docker-sock
hostPath:
path: /var/run/docker.sock
- name: docker-log
hostPath:
path: /
- name: filebeat-data
hostPath:
path: /var/filebeat-data
- name: filebeat-config
configMap:
name: filebeat-config
---
apiVersion: v1
kind: ConfigMap
metadata:
name: filebeat-config
data:
filebeat.yaml: |
setup.ilm.enabled: false
setup.template.enabled: true
setup.template.name: filebeat
setup.template.pattern: filebeat-*
filebeat.config.inputs:
enabled: true
path: /etc/filebeat/configs/*.yaml
reload.enabled: true
reload.period: 10s
# elasticsearch 输出端示例
output.elasticsearch:
hosts: ["http://elasticsearch-svc:9200"]
index: "filebeat-%{[fields.index]}-%{+yyyy.MM.dd}"
部署 nginx depolyments 实例
apiVersion: apps/v1
kind: Deployment
metadata:
name: ngx
spec:
replicas: 4
selector:
matchLabels:
run: ngx
template:
metadata:
labels:
run: ngx
spec:
containers:
- image: nginx
# 可另外挂载空白卷追踪自定义日志文件
volumeMounts:
- mountPath: /tmp/logs
name: tmp-log
name: ngx
env:
- name: CONVEYOR_ENABLED
value: "true"
# 定义自定义日志路径
- name: CONVEYOR_PATH
value: "tmp-index:/tmp/logs/*.log;stdout-index:stdout"
# 声明空白卷
volumes:
- emptyDir: {}
name: tmp-log
MIT ©chenjiandongx