前两天刚写了个博客,docker 安装 ELK,但是那次是从docker hub下载的镜像,不过看到docker hub上写着镜像已经弃用,所以我选择到docker.elastic.io下载最新的镜像尝试安装。最新的镜像内置了x-pack插件,用作安全验证。
docker pull docker.elastic.co/elasticsearch/elasticsearch:5.6.1
docker tag docker.elastic.co/elasticsearch/elasticsearch:5.6.1 10.45.53.221:5000/elasticsearch
docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --name es 10.45.53.221:5000/elasticsearch
docker cp 容器id:/usr/share/elasticsearch/config /home/docker/config/es
docker cp 容器id:/usr/share/elasticsearch/data /home/docker/data/es
docker stop es
docker rm es
如果需要更改配置,可以直接修改config目录下的 elasticsearch.yml 文件,然后启动es
docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" --restart always --name es -v /home/docker/config/es:/usr/share/elasticsearch/config -v /home/docker/data/es:/usr/share/elasticsearch/data 10.45.53.221:5000/elasticsearch
docker pull docker.elastic.co/logstash/logstash:5.6.1
docker tag docker.elastic.co/logstash/logstash:5.6.1 10.45.53.221:5000/logstash
docker run -d --name logstash 10.45.53.221:5000/logstash
docker pull docker.elastic.co/kibana/kibana:5.6.2
docker tag docker.elastic.co/kibana/kibana:5.6.2 10.45.53.221:5000/kibana
docker run --name kibana -p 5601:5601 -d 10.45.53.221:5000/kibana
curl -XPUT -u elastic '192.168.187.108:9200/_xpack/security/user/elastic/_password' -H "Content-Type: application/json" -d '{ "password" : "123456"}'
curl -XPUT -u elastic '192.168.187.108:9200/_xpack/security/user/logstash_system/_password' -H "Content-Type: application/json" -d '{ "password" : "123456" }'
curl -XPUT -u elastic '192.168.187.108:9200/_xpack/security/user/kibana/_password' -H "Content-Type: application/json" -d '{ "password" : "123456" }'
docker cp 容器id:/usr/share/logstash/config /home/docker/config/logstash/config
docker cp 容器id:/usr/share/logstash/pipeline /home/docker/config/logstash/pipeline
docker stop logstash
docker rm logstash
http.host: "0.0.0.0"
path.config: /usr/share/logstash/pipeline
xpack.monitoring.elasticsearch.url: http://192.168.187.108:9200
xpack.monitoring.elasticsearch.username: logstash_system
xpack.monitoring.elasticsearch.password: "123456"
input{
kafka{
group_id => "log-api-1"
topics => ["log-api"]
bootstrap_servers => "192.168.187.108:9092,192.168.187.109:9092,192.168.187.110:9092"
}
}
filter {
grok {
match => { "message" => "%{DATA:timestamp}\|%{DATA:serverHost}\|%{IP:clientIp}\|%{DATA:auth}\|%{DATA:uri}\|%{DATA:userAgent}\|%{NUMBER:resultCode}\|%{DATA:resultMsg}\|%{NUMBER:costime}\|"}
}
date {
match => [ "timestamp", "yyyy-MM-dd HH:mm:ss" ]
target => "@timestamp"
}
geoip {
source => "clientIp"
}
if [userAgent] != "-" {
useragent {
target => "ua"
source => "userAgent"
}
}
mutate {
convert => ["costime", "integer"]
convert => ["resultCode", "integer"]
}
json {
source => "auth"
target => "auth_content"
remove_field => ["auth"]
}
}
output {
elasticsearch{
hosts => ["192.168.187.108:9200"]
index => "logstash-api-%{+YYYY.MM.dd}"
user => "elastic"
password => "123456"
}
stdout{
codec => rubydebug
}
}
docker run -d --name logstash -v /home/docker/config/logstash/config:/usr/share/logstash/config -v /home/docker/config/logstash/pipeline:/usr/share/logstash/pipeline 10.45.53.221:5000/logstash
docker cp 容器id:/usr/share/kibana/config /home/docker/config/kibana/config
docker stop kibana
docker rm kibana
server.name: kibana
server.host: "0"
elasticsearch.url: http://192.168.187.108:9200
elasticsearch.username: elastic
elasticsearch.password: "123456"
xpack.monitoring.ui.container.elasticsearch.enabled: true
docker run --name kibana -v /home/docker/config/kibana/config:/usr/share/kibana/config -p 5601:5601 -d 10.45.53.221:5000/kibana
这时通过kibana默认端口5601访问kibana,http://host:5601,会出现以下界面,这时候用kibana用户登录
这里碰到一个问题,然后解决了一段时间,那就是用kibana用户登录,创建index的时候,我logstash中设置的索引,不能创建,提示错误信息为 :
Unable to fetch mapping. Do you have indices matching the pattern?
curl -u elastic '192.168.187.108:9200/_cat/indices?v'
这就奇怪了,然后查看kibana日志,就是这个日志害人不前,用docker logs查看kibana日志,用json工具解析日志如下:
按这个提示,是 http 404 错误,那么难道是没找到这个url?看到404都会这么想的,当然里面也有状态码为200,我由于看到里面是用get请求,所以我直接用浏览器访问,发现的确是200和404,不过这个404和我们通常访问html页面还不一样。所以这个404不是真正的404,由于之前用docker hub的安装都可以,所以怀疑是不是加上x-pack后出的问题。接下来又是看官方文档,通过更改elasticsearch.ym中的配置,禁用x-pack,来试试。竟然成功了,看来就是权限的问题。
最后上一个超级管理员的界面吧
在这个界面可以对用户以及权限进行管理