文章目录
1. 需求背景
2. 部署
2.1 目录说明
2.2 配置文件
2.2.1 docker-compose.yml
2.2.2 elasticsearch.yml(容器内 config/elasticsearch.yml)
2.2.3 kibana.yml
2.2.4 logstash.yml (容器内 config/logstash.yml)
2.2.5 logstash.conf (取存日志到es)
2.3 设置 & 配置密码
2.3.1 elasticsearch
2.3.2 kibana.yml 设置连接es密码,并重启容器
2.3.3 logstash.yml 设置连接es密码,并重启容器
2.4 遇到的坑
2.4.1 宿主机权限不足
3. 总结
1. 需求背景
Elasticsearch、Logstash、Kibana三个中间件在有点规模的项目中都会使用到,尤其是涉及到站内搜索、日志采集等需求时。在开发和测试环境,我们需要快速部署和管理给开发人员使用,那么 docker-compose一键部署最适合不过了。
2. 部署
端口说明
Elasticsearch:9200(HTTP通信) 和 9300(TCP通信)
Kibana:5601
Logstash:5044 和 9600 (TCP通信)
2.1 目录说明
tree -L 3
.
├── config
│ ├── kibana.yml
│ └── logstash.conf
├── docker-compose.yml
└── elasticsearch
└── data
└── nodes
执行命令:
创建elk的容器网络:
docker network create elk_net
mkdir -p elasticsearch/data/ && chmod 777 elasticsearch/data/
2.2 配置文件
2.2.1 elasticsearch.yml
(容器内 config/elasticsearch.yml)
cluster.name: "docker-cluster"
network.host: 0.0.0.0
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: Authorization
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
2.2.2 kibana.yml
server.host: "0.0.0.0"
server.shutdownTimeout: "5s"
elasticsearch.hosts: [ "http://elasticsearch:9200" ]
monitoring.ui.container.elasticsearch.enabled: true
elasticsearch.username: "elastic"
elasticsearch.password: "123456"
2.2.3 logstash.yml
(容器内 config/logstash.yml)
http.host: "0.0.0.0"
xpack.monitoring.elasticsearch.hosts: [ "http://elasticsearch:9200" ]
xpack.monitoring.enabled: true
xpack.monitoring.elasticsearch.username: logstash_system
xpack.monitoring.elasticsearch.password: 123456
2.2.4 logstash.conf
(取存日志到es)
input {
tcp {
mode => "server"
host => "0.0.0.0"
port => 4560
codec => json_lines
}
}
input {
file {
path => "/home/nginx/logs/access.log"
}
}
filter {
mutate {
}
}
output {
elasticsearch {
hosts => ["http://elasticsearch:9200"]
user => "elastic"
password => "123456"
index => "java1-logstash"
}
}
filter {
mutate {
add_field => [ "[fields][path]", "%{[path]}"]
add_field => [ "message]", "%{[message]}"]
}
}
2.2.5 docker-compose.yml
version: '3'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:7.16.2
container_name: elasticsearch_server
restart: always
environment:
- discovery.type=single-node
- discovery.zen.minimum_master_nodes=1
- ES_JAVA_OPTS=-Xm512m -Xmx1g
volumes:
- ./elasticsearch/data:/usr/share/elasticsearch/data
- ./config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
ports:
- 9200:9200
- 9300:9300
networks:
elk_net: # 指定使用的网络
aliases:
- elasticsearch # 该容器的别名,在 elk_net 网络中的其他容器可以通过别名 elasticsearch 来访问到该容器
kibana:
image: docker.elastic.co/kibana/kibana:7.16.2
container_name: kibana_server
ports:
- "5601:5601"
restart: always
networks:
elk_net:
aliases:
- kibana
environment:
- ELASTICSEARCH_URL=http://elasticsearch:9200
- SERVER_NAME=kibana
# 如需具体配置,可以创建./config/kibana.yml,并映射
volumes:
- ./config/kibana.yml:/usr/share/kibana/config/kibana.yml
depends_on:
- elasticsearch
logstash:
image: docker.elastic.co/logstash/logstash:7.16.2
container_name: logstash_server
restart: always
environment:
- LS_JAVA_OPTS=-Xmx256m -Xms256m
volumes:
- ./config/logstash.conf:/etc/logstash/conf.d/logstash.conf
- ./config/logstash.yml:/etc/logstash/conf.d/logstash.yml
ports:
- "5044:5044"
- "4560:4560"
- "5000:5000/tcp"
- "5000:5000/udp"
- "9600:9600"
networks:
elk_net:
aliases:
- logstash
depends_on:
- elasticsearch
entrypoint:
- logstash
- -f
- /etc/logstash/conf.d/logstash.conf
logging:
driver: "json-file"
options:
max-size: "200m"
max-file: "3"
networks:
elk_net:
external:
name: elk_net
2.3 设置 & 配置密码
2.3.1 elasticsearch
使用上述配置重启容器后,进入容器执行以下命令,可配置elastic、kibana、logstash_system 等账号的密码: elasticsearch-setup-passwords interactive
elasticsearch-setup-passwords interactive
设置完成后,登录Kibana的账户就是 kibana , elasticsearch的账户为 elastic 。
2.3.2 kibana.yml 设置连接es密码,并重启容器
elasticsearch.username: "elastic"
elasticsearch.password: "123456"
2.3.3 logstash.yml 设置连接es密码,并重启容器
xpack.monitoring.enabled: true
xpack.monitoring.elasticsearch.username: logstash_system
xpack.monitoring.elasticsearch.password: 123456
2.4 遇到的坑
2.4.1 宿主机权限不足
./elasticsearch/data目录权限不够会有以下异常:
AccessDeniedException[/usr/share/elasticsearch/data/nodes];",
解决:chmod 777 elasticsearch/data/
3. 总结
梳理好部署目录,创建对应的文件,直接docker-compose up -d 启动。
内网使用Elasticsearch、Logstash时,可以不设置密码,但kibana需要设置(可以通过yml配置或者nginx代理实现)。
生产环境可以使用公有云上的 ES产品,开发和测试环境可以一键部署。
————————————————
版权声明:本文为CSDN博主「林凡尘coding」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/oschina_41731918/article/details/123098391