spring boot集成elk 7.9.1
es
第一次运行es
docker run -d \
--name elasticsearch \
--env ES_JAVA_OPTS=-"Xms1024m -Xmx1024m" \
--env "discovery.type=single-node" \
--env TZ=Asia/Shanghai \
-p 9200:9200/tcp \
-p 9300:9300/tcp \
--restart=always \
elasticsearch:7.9.1
将容器内的文件复制到主机上
# 这里要提前新建文件夹 否则第一次cp的是容器文件夹下的内容 而不是容器文件夹
mkdir /opt/elasticsearch/
docker cp elasticsearch:/usr/share/elasticsearch/config /opt/elasticsearch/
mv /opt/elasticsearch/config /opt/elasticsearch/conf
docker cp elasticsearch:/usr/share/elasticsearch/data /opt/elasticsearch/
docker cp elasticsearch:/usr/share/elasticsearch/logs /opt/elasticsearch/
删除上面容器 正式运行
docker rm -f elasticsearch
docker run -d \
--name elasticsearch \
--env ES_JAVA_OPTS="-Xms1024m -Xmx1024m" \
--env "discovery.type=single-node" \
--env TZ=Asia/Shanghai \
-p 9200:9200/tcp \
-p 9300:9300/tcp \
--restart=always \
-v /opt/elasticsearch/conf:/usr/share/elasticsearch/config \
-v /opt/elasticsearch/data:/usr/share/elasticsearch/data \
-v /opt/elasticsearch/logs:/usr/share/elasticsearch/logs \
elasticsearch:7.9.1
修改elasticsearch.yml 配置跨域相关 否则es-head无法连接
cluster.name: "docker-cluster"
network.host: 0.0.0.0
# 增加下面两行
http.cors.enabled: true
http.cors.allow-origin: "*"
安装ik分词器
# 进入elasticsearch容器
docker exec -it elasticsearch /bin/bash
# 下载并安装ik分词器
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.9.1/elasticsearch-analysis-ik-7.9.1.zip
# 退出容器
exit
# 重启elasticsearch容器
docker restart elasticsearch
装分词器前
装完分词器测试
es-head
第一次运行
docker run -d \
--name elastic-head \
--env TZ=Asia/Shanghai \
-p 9100:9100/tcp \
--restart=always \
mobz/elasticsearch-head:5
将容器内的文件复制到主机上
mkdir -p /opt/elastic-head/_site
docker cp elastic-head:/usr/src/app/_site/vendor.js /opt/elastic-head/_site
删除上面容器 正式运行
docker rm -f elastic-head
docker run -d \
--name elastic-head \
--env TZ=Asia/Shanghai \
-p 9100:9100/tcp \
--restart=always \
-v /opt/elastic-head/_site/vendor.js:/usr/src/app/_site/vendor.js \
mobz/elasticsearch-head:5
修改vendor.js 否则es-head查询的时候会报“Content-Type header [application/x-www-form-urlencoded] is not supported” 406
# 将6886行和7573行 application/x-www-form-urlencoded 改成 application/json;charset=UTF-8
logstash
第一次运行logstash
docker run -d \
--name logstash \
--env TZ=Asia/Shanghai \
-p 4560:4560/tcp \
-p 9600:9600/tcp \
--restart=always \
logstash:7.9.1
将容器内的文件复制到主机上
mkdir /opt/logstash
docker cp logstash:/usr/share/logstash/config /opt/logstash/
mv /opt/logstash/config /opt/logstash/conf
docker cp logstash:/usr/share/logstash/data /opt/logstash/
chmod -R 777 /opt/logstash
删除上面容器 正式运行
docker rm -f logstash
docker run -d \
--name logstash \
--privileged \
--env TZ=Asia/Shanghai \
-p 4560:4560/tcp \
-p 9600:9600/tcp \
--restart=always \
-v /opt/logstash/conf:/usr/share/logstash/config \
-v /opt/logstash/data:/usr/share/logstash/data \
logstash:7.9.1
logstash.yml
http.host: "0.0.0.0"
xpack.monitoring.elasticsearch.hosts: [ "http://192.168.0.82:9200" ]
path.config: /usr/share/logstash/config/*.conf
logstash-springboot.conf
input {
tcp {
port => 4560
codec => "json_lines"
}
}
output {
elasticsearch {
hosts => ["192.168.0.82:9200"]
index => ["springboot-logstash-%{+YYYY.MM.dd}"]
}
stdout { codec => rubydebug }
}
kibana
第一次运行
docker run -d \
--name kibana \
--env ELASTICSEARCH_URL=http://192.168.0.82:9200 \
--env TZ=Asia/Shanghai \
-p 5601:5601/tcp \
--restart=always \
kibana:7.9.1
将容器内的文件复制到主机上
mkdir -p /opt/kibana/conf
docker cp kibana:/usr/share/kibana/config/kibana.yml /opt/kibana/conf
删除上面容器 正式运行
docker rm -f kibana
docker run -d \
--name kibana \
--env ELASTICSEARCH_URL=http://192.168.0.82:9200 \
--env TZ=Asia/Shanghai \
-p 5601:5601/tcp \
--restart=always \
-v /opt/kibana/conf/kibana.yml:/usr/share/kibana/config/kibana.yml \
kibana:7.9.1
server.name: kibana
server.host: "0"
# 设置Elasticsearch集群的主机地址和端口号
elasticsearch.hosts: [ "http://192.168.0.82:9200" ]
monitoring.ui.container.elasticsearch.enabled: true
# 设置界面语言,将界面语言设置成中文
i18n.locale: "zh-CN"
二、代码集成测试
elk demo代码
点击创建索引模式
参考文档
-
elasticsearch-head 无法连接elasticsearch的原因和解决 https://blog.csdn.net/qq_41264674/article/details/118494191
-
Docker部署单点ElasticSearch(ES)、中文分词器以及可视化插件elasticsearch-head、kibana https://blog.csdn.net/junehappylove/article/details/105676411
日拱一卒 功不唐捐