Filebeat收集nginx容器日志并传到Elastic Cloud

Filebeat收集nginx容器日志并同步到Elastic Cloud

    • 1)Filebeat工作原理
    • 2)目标
    • 3)docker.elastic.co镜像加速
    • 4)下载配置文件
    • 5)启动nginx容器并加标签
    • 6)启动filebeat容器
    • 7)结果
    • 补充知识:Elastic Cloud设置和获取Cloud ID/password的方法

1)Filebeat工作原理

Filebeat是一个用于转发和集中日志数据的轻量级传送程序。作为代理安装在服务器上,Filebeat监视指定的日志文件或位置,收集日志事件,并将它们转发到Elasticsearch或Logstash进行索引。

Filebeat的工作原理
本文摘选自本人gitbook,地址:https://maxidea.gitbook.io/k8s-testing/
转载请标注出处。

2)目标

设置filebeat容器收集nginx容器的日志,并收集到Elastic Cloud上的Elasticsearch Service

3)docker.elastic.co镜像加速

由于官方filebeat镜像存放在官方仓库docker.elastic.co,这个仓库在国内访问速度很慢,所以我们直接用已经有阿里云镜像加速的dockerhub仓库地址。(具体加速配置可以参考https://developer.aliyun.com/mirror/docker-ce)
拉取dockerhub上filebeat镜像

1
docker pull store/elastic/filebeat:7.6.2

4)下载配置文件

下载官方的配置文件:

1
curl -L -O https://raw.githubusercontent.com/elastic/beats/7.6/deploy/docker/filebeat.docker.yml

filebeat.docker.yml文件内容,无需修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
filebeat.config:
  modules:
    path: ${path.config}/modules.d/*.yml
    reload.enabled: false

filebeat.autodiscover:
  providers:
    - type: docker
      hints.enabled: true

processors:
- add_cloud_metadata: ~

output.elasticsearch:
  hosts: '${ELASTICSEARCH_HOSTS:elasticsearch:9200}'
  username: '${ELASTICSEARCH_USERNAME:}'
  password: '${ELASTICSEARCH_PASSWORD:}'

5)启动nginx容器并加标签

filebeat.docker.yml文件配置默认为基于应用于容器的docker label来部署Beats模块,filebeat就能使用自动发现功能,所以我们启动nginx容器时需要加上label:

1
2
3
4
5
6
7
8
docker run \
  --label co.elastic.logs/enable=true \
  --label co.elastic.logs/module=nginx \
  --label co.elastic.logs/fileset.stdout=access \
  --label co.elastic.logs/fileset.stderr=error \
  --name nginx-app \
  -p 80:80 -d \
  nginx:alpine

6)启动filebeat容器

1
2
3
4
5
6
7
8
9
docker run -d \
  --name=filebeat \
  --user=root \
  --volume="$(pwd)/filebeat.docker.yml:/usr/share/filebeat/filebeat.yml:ro" \
  --volume="/var/lib/docker/containers:/var/lib/docker/containers:ro" \
  --volume="/var/run/docker.sock:/var/run/docker.sock:ro" \
  store/elastic/filebeat:7.6.2 filebeat -e -strict.perms=false \
  -E cloud.id=<Cloud ID from Elasticsearch Service> \
  -E cloud.auth=elastic:<elastic password>

这里由于我们还没有本地配置ElasticSearch,所以把参数-E output.elasticsearch.hosts=["elasticsearch:9200"] 改掉,暂时使用Elastic Cloud提供的Elasticsearch Service的代替,上面的用户名和密码请以你自己的Elastic Cloud代替。例如:

1
2
3
4
5
6
7
8
9
docker run -d \
   --name=filebeat \
   --user=root \
   --volume="$(pwd)/filebeat.docker.yml:/usr/share/filebeat/filebeat.yml:ro" \
   --volume="/var/lib/docker/containers:/var/lib/docker/containers:ro" \
   --volume="/var/run/docker.sock:/var/run/docker.sock:ro" \
   store/elastic/filebeat:7.6.2 filebeat -e -strict.perms=false \
   -E cloud.id=nginx-simon1:c291dGhlYXN0YXNpYS5henVyZS5lbGFzdGljLWNsb3VkLmNvbTo5MjQzJDgzMjZkYTcyMDRkNDQzODA4YzkyYjMzOTEwNzc0MjIzJGZlYzAyYzQ3NDI0NDQ4YjM5MWRlNGI5MjI5NTY2MThk \
   -E cloud.auth=elastic:HT0QWmB7yvJK4xjLgIs59Ruo

7)结果

确保nginx和filebeat两个容器都启动后,我们刷一下本地80端口访问量,然后打开Elastic Cloud上的Kibana-->Logs,能获取到nginx访问日志即代表配置正确:
在这里插入图片描述

补充知识:Elastic Cloud设置和获取Cloud ID/password的方法

Elastic Cloud是收费的SAAS服务,https://www.elastic.co/英文官方网站上提供免费的14天测试。
1)激活账号后登录https://cloud.elastic.co/home
2)点击“Start your free trail
3)填写“Create deployment”表单里的内容,例如:Select a cloud platform选择Azure,Select a region选择Southeast Asia (Singapore),其他保持默认。
4)点击Deploy后云端会自动开始准备环境,页面上会提示Save your Elasticsearch and Kibana password。大约三分钟后可以使用。
在这里插入图片描述
5)点击左上角Deployment的名字,在页面上获取对应的cloud ID
在这里插入图片描述
如果有任何疑问,欢迎留言和交流。