公司App需要上线一个新的搜索服务,比起之前其他开发过的其他ElasticSearch相关服务,从来没有自己动手开启过7版本的xpack功能,要么是云部署要么就没有开启或者是旧版本不一样。
首先Elastic Stack默认是没有安全特性的,API都可以随便访问,官方通过Xpack来加强安全性,之前Xpack是一个收费包,现在Xpack中的部分功能已经免费了,先说下什么是Xpack
X-Pack是一个Elastic Stack的扩展,将安全,警报,监视,报告和图形功能包含在一个易于安装的软件包中。
1 首先先配置elasticsearch的xpack功能
修改elasticsearch目录下的elasticsearch.yml文件,添加下面两条,开启xpack基本版本和安全相关功能
xpack.security.enabled: true
xpack.license.self_generated.type: basic
重启Elasticsearch后通过curl发现
curl http://localhost:9200 无效了失败
2 设置密码
./elasticsearch-setup-passwords interactive
会通过交互式设置elasticsearch logstash kibnana 的密码
3 设置kibnana的密码
当完成如上过程后Kibnana不能被正常访问,需要设置密码
修改Kibnan的文件yml文件
xpack.monitoring.ui.container.elasticsearch.enabled: true
xpack.security.enabled: true
elasticsearch.username: "kibana"
elasticsearch.password: "123qweASD"
4 设置logstash的密码
如果使用logstash做日志收集等功能,需要修改logstash配置,如下:
input {
beats {
port => 5044
}
}
output {
stdout {
codec => rubydebug
}
elasticsearch {
hosts => ["192.168.3.181:9200","192.168.3.182:9200","192.168.3.183:9200"]
user => "elastic"
password => "123456"
}
}
下面介绍下在容器或者说集群编排下怎么开启Xpack并做相应的设置,这里以docker-compose单节点elk为例
version: "3.3"
networks:
macco-elk-network:
driver: bridge
services:
elasticsearch:
image: registry.cn-hangzhou.aliyuncs.com/macco/elasticsearch:7.6.2
networks:
macco-elk-network:
aliases:
- elasticsearch
ports:
- "9200:9200"
- "9300:9300"
environment:
- discovery.type=single-node
- ELASTIC_SECURITY=true
- ELASTIC_PASSWORD=xxx
volumes:
- /external-data/elk/es/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- /external-data/elk/es/data:/usr/share/elasticsearch/data
- /external-data/elk/es/backup:/backups
restart: always
kibana:
image: docker.elastic.co/kibana/kibana:7.6.2
environment:
- ELASTICSEARCH_HOSTS=http://elasticsearch:9200
- ELASTICSEARCH_USERNAME=kibnan
- ELASTICSEARCH_PASSWORD=xxxx
- xpack.security.enabled=true
volumes:
- /external-data/elk/kibana/config/kibana.yml:/usr/share/kibana/config/kibana.yml
networks:
macco-elk-network:
aliases:
- kibana
ports:
- "5601:5601"
restart: always
depends_on:
- elasticsearch
logstash:
image: docker.elastic.co/logstash/logstash:7.6.2
networks:
macco-elk-network:
aliases:
- logstash
environment:
- TZ=Asia/Shanghai
ports:
- "4567:4567"
- "5044:5044"
- "9600:9600"
volumes:
- /external-data/elk/logstash:/elk/logstash
- /external-data/elk/logstash/logstash.yml:/usr/share/logstash/config/logstash.yml
command: logstash -f /elk/logstash/logstash.conf
restart: always
depends_on:
- elasticsearch
本文为Lokie.Wang原创文章,转载无需和我联系,但请注明来自lokie博客http://lokie.wang