创建一个elasticsearch集群

安装elasticsearch

安装elasticsearch很简单,基本上不需要怎么安装,下载压缩包解压后,进入目录直接在bash中运行bin/目录下的elasticsearch即可启动一个elasticsearch服务。

配置集群

配置集群需要修改config目录下的elasticsearch.yml文件;

  • 设置集群的名称,master和slave 都设置成同一个名称,才能组成集群

  • 给当前节点指定一个名称,我取名为master,并且设置node.master 为 true

    # ---------------------------------- Cluster -----------------------------------
    #
    # Use a descriptive name for your cluster:
    #
    #cluster.name: my-application
    # 集群名称
    cluster.name: seagm
    # ------------------------------------ Node ------------------------------------
    #
    # Use a descriptive name for the node:
    #
    #node.name: node-1
    # master名称
    node.name: master
    node.master: true
    #
    # Add custom attributes to the node:
    #
    #node.attr.rack: r1
    #
  • 把IP绑定为127.0.0.1

  • elasticsearch的默认端口是 9200, 这里我暂不修改。等会儿slave中需要修改,因为slave和master放在同一台电脑上不能同时使用9200

# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
network.host: 127.0.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
  • 最后这个参数,我忘了是不是默认的了。先贴出来
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: "*"

以上是master的配置 配置好后,bash运行./bin/elasticsearch 然后稍后片刻,等elasticsearch运行之后,可在流览器中输入localhost:9200 看到如下输出

{
  "name" : "master",
  "cluster_name" : "seagm",
  "cluster_uuid" : "WmBVxtB-QsqsRQK_Srkijg",
  "version" : {
    "number" : "6.4.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "04711c2",
    "build_date" : "2018-09-26T13:34:09.098244Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

  • 接下来,把刚刚配置好的整个elasticsearch目录复制一份,修改目录名称为slave-1,然后进入到slave-1/config中修改elasticsearch.yml
cluster.name: seagm
node.name: slave1

network.host: 127.0.0.1
http.port: 8200

discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
  • 指定cluster.name 与上面的主节点cluster.name相同(这个是必须的)
  • 指定node.name 为slave1(这个可以随便取)
  • network.host 需要与主节点绑定的ip相同
  • http.port: 这个需要重指定一个端口号,原因上面已经说了
  • 最后那一句。。忘了是什么意思了..
  • 总之,这样配置好后,就可以通过./bin/elasticsearch启动这个slave1了。

启动之后,可以在浏览器中单独访问这个节点localhost:8200,可以看到输出:

{
  "name" : "slave1",
  "cluster_name" : "seagm",
  "cluster_uuid" : "WmBVxtB-QsqsRQK_Srkijg",
  "version" : {
    "number" : "6.4.2",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "04711c2",
    "build_date" : "2018-09-26T13:34:09.098244Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

那么怎么在主节点上看到子节点呢?

访问:localhost:9200/_cat/nodes

127.0.0.1 29 95 11 3.60   mdi * master
127.0.0.1 32 95 11 3.60   mdi - slave1