cenos7虚拟机环境下docker中配置Elasticsearch7.x集群(2个节点)

安装docker,pull镜像什么的就不说了,直接开始

  1. 首先查询本机的IP(此步骤非常重要)
ip addr
  1. 得到下图所示的IP地址,记住途中的docker0的IP地址:172.17.0.1
    在这里插入图片描述
  2. 然后创建两个es容器
docker run -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -d -p 9201:9201 --name ES01 你的es镜像ID
docker run -e ES_JAVA_OPTS="-Xms512m -Xmx512m" -d -p 9202:9202 --name ES02 你的es镜像ID
  1. 修改这两个es容器的配置文件(修改两个容器的命令是一样的,但是配置文件不一样)
#首先需要进入容器内部,才能修改容器的配置文件(若容器无法正常启动,请自行搜索docker容器外部修改配置文件)
docker exec -ti ES01 /bin/bash
vi config/elasticsearch.yml
#修改完毕之后退出当前容器
exit

#以下是配置文件
#ES01的配置文件
cluster.name: "es"
node.name: master
node.master: true
node.data: true
network.host: 0.0.0.0
http.port: 9201
transport.tcp.port: 9301
discovery.seed_hosts: ["127.0.0.1:9301","127.0.0.1:9302"]
cluster.initial_master_nodes: ["127.0.0.1:9301"]
http.cors.enabled: true
http.cors.allow-origin: "*"

#ES02的配置文件
cluster.name: "es"
node.name: 9202
node.master: false
node.data: true
network.host: 0.0.0.0
http.port: 9202
discovery.seed_hosts: ["127.0.0.1:9301","127.0.0.1:9302"]
cluster.initial_master_nodes: ["127.0.0.1:9301"]
http.cors.enabled: true
http.cors.allow-origin: "*"
  1. 在浏览器中输入 centos虚拟机IP:9201,会返回一串json数据,注意"cluster_uuid": "na"这个数据,他是代表es服务虽然正常启动但是不能正常提供服务。如果能够正常提供服务,那么此处的 "na"将会是一个全局唯一的字符串
{
   "name": "master",
   "cluster_name": "es",
   "cluster_uuid": "CIT7b1flSc2ztzAd5JxUaw",
   "version": {
   		"number": "7.4.2",
   		"build_flavor": "default",
   		"build_type": "docker",
   		"build_hash": "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
   		"build_date": "2019-10-28T20:40:44.881551Z",
   		"build_snapshot": false,
   		"lucene_version": "8.2.0",
   		"minimum_wire_compatibility_version": "6.8.0",
   		"minimum_index_compatibility_version": "6.0.0-beta1"
   },
   "tagline": "You Know, for Search"
}
  1. 然后查看容器的日志,会发现具有警告信息
    在这里插入图片描述
    大体翻译过来就是,没有找到地址为127.0.0.1:9301的主节点,但是发现了地址为172.0.0.3:9201的主节点,地址为172.0.0.3:9302辅节点。看到这个172.0.0.3的时候忽然发现跟ip地址中docker的ip地址很像,于是我想到,docker可能已经为es分配好了IP地址,不能用本机的127.0.0.1地址。我就尝试着将配置文件中的127.0.0.1改成172.0.0.3,然后重启ES01和ES02。
docker restart ES01 ES02
  1. 然后再次在浏览器中访问 centos虚拟机IP:9201,然后会返回一串json数据,此时会发现uuid处生成了正常的字符串,这就代表它可以正常提供服务。
{
   "name": "master",
   "cluster_name": "es",
   "cluster_uuid": "CIT7b1flSc2ztzAd5JxUaw",
   "version": {
   		"number": "7.4.2",
   		"build_flavor": "default",
   		"build_type": "docker",
   		"build_hash": "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
   		"build_date": "2019-10-28T20:40:44.881551Z",
   		"build_snapshot": false,
   		"lucene_version": "8.2.0",
   		"minimum_wire_compatibility_version": "6.8.0",
   		"minimum_index_compatibility_version": "6.0.0-beta1"
   },
   "tagline": "You Know, for Search"
}
  1. 使用postman发送请求测试一下,请求方式为put,请求url为http://你的虚拟机IP:9201/blogs,请求体数据格式选择json,请求数据为
 {
   "settings" : {
      "number_of_shards" : 3,
      "number_of_replicas" : 1
   }
}
  1. 请求结果为
    在这里插入图片描述
  2. 然后使用浏览器访问 你的虚拟机IP地址:9201/_/_cluster/health,又返回了一串json数据,会发现status是green,这代表着集群正常工作。
{
   "cluster_name": "es",
   "status": "green",
   "timed_out": false,
   "number_of_nodes": 2,
   "number_of_data_nodes": 2,
   "active_primary_shards": 3,
   "active_shards": 6,
   "relocating_shards": 0,
   "initializing_shards": 0,
   "unassigned_shards": 0,
   "delayed_unassigned_shards": 0,
   "number_of_pending_tasks": 0,
   "number_of_in_flight_fetch": 0,
   "task_max_waiting_in_queue_millis": 0,
   "active_shards_percent_as_number": 100
}
  1. 上一步返回的数据中,如果active_shards以及unassigned_shards都为3,那么就代表集群中的某一个节点挂掉了,也就是说明ES02没有正常工作,建议查看日志文件来确定问题所在。
发布了4 篇原创文章 · 获赞 0 · 访问量 180

猜你喜欢

转载自blog.csdn.net/qq_40378165/article/details/103217206