背景
由于项目中需要用到es存储geo信息,所以就在测试机器上使用docker快速搭建了一台单节点的es。
拉取镜像
选择这个版本的原因,看官网上介绍此版本比较稳定,其次是es的依赖发生了变化,和es8版本依赖是一样的,具体依赖下面会有介绍。
docker pull elasticsearch:7.17.6
# 查看本地es镜像
docker images | grep elasticsearch
部署并运行容器
docker run -m 716m -d --name es-test -p 9200:9200 -v /home/docker/es/config/es-single.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /home/docker/es/data:/usr/share/elasticsearch/data --net mynet --restart=always -e "discovery.type=single-node" elasticsearch:7.17.6
上面的命令可以解释一下:
-m :设置容器使用内存最大值;
-d :后台运行容器,并返回容器ID;
--name : 容器命名;
-p :端口映射 第一个端口是宿主机端口,第二个端口是容器端口;
-v : 把es的配置文件映射到宿主机的指定目录了;
--net :桥接名,这个需要提前创建,加到同一个网络桥接里的容器可以互相ping同;
--restart : 容器重启模式;
-e :设置环境变量;
elasticsearch:7.17.6 :如果只是写elasticsearch,tag就是latest;
可以添加下面这些参数到es-single.yml
vim /home/docker/es/config/es-single.yml
es-single.yml 写入以下内容
cluster.name: elasticsearch-single
node.name: es-single-node-1
network.bind_host: 0.0.0.0
如果没有创建net,需要先创建; 如果已经创建则跳过
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
这个时候可以docker ps一下,可以在宿主机可以看到容器了。
然后现在我们设置启动密码功能。
在es-single.yml中追加如下配置启动密码xpack功能
xpack.security.enabled: true
xpack.license.self_generated.type: basic
xpack.security.transport.ssl.enabled: true
然后重启es让配置文件生效,再到es包下bin目录执行以下命令
exec -it es-test ls /usr/share/elasticsearch/bin/elasticsearch-setup-passwords interactive
执行上面的命令后,就会有很多角色要设置对应的密码,
future versions of Elasticsearch will require Java 11; your Java version from [/opt/jdk1.8.0_281/jre] does not meet this requirement
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
passwords must be at least [6] characters long
Try again.
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana_system]:
Reenter password for [kibana_system]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
1234Changed password for user [apm_system]
56Changed password for user [kibana_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
设置好上面命令后就可以使用了。
常用命令
docker ps # 列出正在运行的容器
docker ps -a # 列出所有容器,包括未运行的
docker logs es-test # 查看某个容器的日志
docker exec -it es-test /bin/bash # 进入到容器内
es简单操作
建index
curl -H "Content-Type: application/json" -XPUT 宿主机ip:9200/regions?pretty -d '{"settings": {"number_of_shards": 1,"number_of_replicas": 1}, "mappings": {"properties": {"name":{"type": "text"},"gender":{"type": "keyword"},"age":{"type": "integer"},"bodyType":{"type": "integer"},"relationShip":{"type": "integer"},"smoke":{"type": "integer"},"drink":{"type": "integer"},"location": {"type": "geo_point"}}}}' --user elastic:es123456
其他用户的密码均是 es123456
删除index
curl -XDELETE 宿主机ip:9200/regions --user elastic:es123456
插入数据
curl -H "Content-Type: application/json" -XPUT 宿主机ip:9200/regions/_doc/1 -d '{"name":"广州","gender":"2","age":25,"bodyType":1,"relationShip":1,"smoke":1,"drink":1,"location": {"lat":23.101479, "lon":112.270719}}' --user elastic:es123456
查询数据
查询方式一
curl -H "Content-Type: application/json" -XGET 宿主机ip:9200/regions/_search -d '{"query":{"term":{"name":"广州"}}}' --user elastic:es123456
查询方式二
curl -H "Content-Type: application/json" -XGET 宿主机ip:9200/regions/_search -d '{"query":{"bool":{"filter":{"geo_distance":{"distance": "1km","location": {"lat": 23.101479,"lon": 112.270719}}}}},"sort":[{"_geo_distance":{"unit":"km","location":{"lat":23.131304,"lon":113.262402},"order":"asc"}}]}' --user elastic:es123456
查询方式三
curl -H "Content-Type: application/json" -XGET 宿主机ip:9200/regions/_search -d '{"query":{"bool":{"filter":{"geo_distance":{"distance": "1km","location": {"lat": 23.101479,"lon": 112.270719}}}}}}' --user elastic:es123456
查询方式四 - 根据id查找
curl -H "Content-Type: application/json" -XGET 宿主机ip:9200/regions/_doc/1 --user elastic:es123456
查看健康状态
curl -X GET "宿主机ip:9200/_cat/health?v" --user elastic:es123456
单节点情况下集群状态是黄色的,可以忽略不管,也可以执行下面命令设置不分片
curl -X PUT "宿主机ip:9200/_settings" -H 'Content-Type: application/json' -d' {"number_of_replicas":0}'
# 返回
{"acknowledged":true}
查看节点
curl -X GET “宿主机ip:9200/_cat/nodes?v” --user elastic:es123456
查看索引
curl -X GET “宿主机ip:9200/_cat/indices?v” --user elastic:es123456
pom
另外如果是一名Java开发师,如果要用这个相关api,依赖如下:文章来源:https://www.toymoban.com/news/detail-406894.html
<!-- elasticsearch -->
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>7.16.3</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>7.16.3</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.0.1</version>
</dependency>
结语
欢迎交流,欢迎指正与批评。文章来源地址https://www.toymoban.com/news/detail-406894.html
到了这里,关于Docker安装Elasticsearch并启动密码xpack功能的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!