参考: docker入门:单机elasticsearch安装记录,保证无坑_8月日更_小鲍侃java_InfoQ写作社区
新建文件夹
同上文所述相同,需要在宿主机上挂载配置文件与数据文件。
mkdir -p /Users/louye/data/learn-data/elastic/config mkdir -p /Users/louye/data/learn-data/elastic/data
修改配置文件
cd /Users/louye/data/learn-data/elastic/config vim elasticsearch.yml
network.host: 0.0.0.0 network.bind_host: 0.0.0.0 #外网可访问 http.cors.enabled: true http.cors.allow-origin: "*" xpack.security.enabled: true # 这条配置表示开启xpack认证机制 spring boot连接使用 xpack.security.transport.ssl.enabled: false
xpack.security 配置后,elasticsearch 需要账号密码使用,建议安排上。如果使用 springboot 查询,那一定要设置,否者会报错!
启动
docker run -p 9200:9200 --name elasticsearch --net elastic \ -e "discovery.type=single-node" \ -e ES_JAVA_OPTS="-Xms1g -Xmx2g" \ -v /Users/louye/data/learn-data/elastic/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ -v /Users/louye/data/learn-data/elastic/data:/usr/share/elasticsearch/data \ -v /Users/louye/data/learn-data/elastic/plugins:/usr/share/elasticsearch/plugins \ -d elasticsearch:8.2.3
初始化密码
docker exec -it elasticsearch /bin/bash bin/elasticsearch-setup-passwords interactive #user: elastic #password: 123456
kibana
启动异常
可以跳过
docker run -d --name kibana --net elastic -p 5601:5601 -e "ELASTICSEARCH_HOSTS=http://elasticsearch:9200" -e "ELASTICSEARCH_USERNAME=elastic" -e "ELASTICSEARCH_PASSWORD=123456" kibana:8.2.3
提示不能用默认的Elastic账号否则报错:
es新建账号
docker exec -it elasticsearch /bin/bash bin/elasticsearch-users useradd logadmin
输入密码
添加角色和授权
#增加授权: #superuser能正常打开es的9200端口,kibana_system配置后才可以正常对接kb和es bin/elasticsearch-users roles -a superuser logadmin bin/elasticsearch-users roles -a kibana_system logadmin #移除授权: bin/elasticsearch-users roles -r kibana_admin logadmin #查看授权: bin/elasticsearch-users roles -v logadmin
再次启动Kibana
docker run -d --name kibana --net elastic -p 5601:5601 -e "ELASTICSEARCH_HOSTS=http://elasticsearch:9200" -e "ELASTICSEARCH_USERNAME=logadmin" -e "ELASTICSEARCH_PASSWORD=123456" kibana:8.2.3
查看日志提示密码必须是字符串类型:
两种方式, 要么改密码改为字符串,不使用数字; 要么使用下面的格式,添加""的转义格式
docker run -d --name kibana --net elastic -p 5601:5601 -e "ELASTICSEARCH_HOSTS=http://elasticsearch:9200" -e "ELASTICSEARCH_USERNAME=logadmin" -e "ELASTICSEARCH_PASSWORD=\"123456\"" kibana:8.2.3