Filebeat收集nginx容器日志并同步到Elastic Cloud
https://www.elastic.co/guide/en/beats/filebeat/
Filebeat是一个用于转发和集中日志数据的轻量级传送程序。作为代理安装在服务器上,Filebeat监视指定的日志文件或位置,收集日志事件,并将它们转发到Elasticsearch或Logstash进行索引。

注意:由于官方docker.elastic.co的国内访问速度很慢,所以我们直接用已经有加速的dockerhub仓库地址。
拉取Filebeat镜像文件(dockerhub官方镜像版本,我们在第一章里用了阿里云镜像对其进行加速)
docker pull store/elastic/filebeat:7.6.2
下载官方的配置文件:
filebeat.docker.yml文件内容:
filebeat.docker.yml文件配置默认为基于应用于容器的docker label来部署Beats模块,filebeat就能使用自动发现功能,所以我们启动nginx容器时需要加上label:
启动filebeat容器:
这里由于我们还没有本地配置ElasticSearch,所以把参数-E output.elasticsearch.hosts=["elasticsearch:9200"]改掉,暂时使用Elastic Cloud提供的Elasticsearch Service的代替,上面的用户名和密码请以你自己的Elastic Cloud代替。例如:
确保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

Last updated
Was this helpful?