搭建Elasticsearch集群

2021-03-22 09:43:37 浏览数 (1)

为什么要搭建集群?

凡事都要讲究个为什么。在搭建集群之前,我们首先先问一句,为什么我们需要搭建集群?它有什么优势呢?

高可用

Elasticsearch 作为一个搜索引擎,我们对它的基本要求就是存储海量数据并且可以在非常短的时间内查询到我们想要的信息。所以第一步我们需要保证的就是 Elasticsearch 的高可用性,什么是高可用性呢?它通常是指,通过设计减少系统不能提供服务的时间。假设系统一直能够提供服务,我们说系统的可用性是 100%。如果系统在某个时刻宕掉了,比如某个网站在某个时间挂掉了,那么就可以它临时是不可用的。所以,为了保证 Elasticsearch 的高可用性,我们就应该尽量减少 Elasticsearch 的不可用时间。

  那么怎样提高 Elasticsearch 的高可用性呢?这时集群的作用就体现出来了。假如 Elasticsearch 只放在一台服务器上,即单机运行,假如这台主机突然断网了或者被攻击了,那么整个 Elasticsearch 的服务就不可用了。但如果改成 Elasticsearch 集群的话,有一台主机宕机了,还有其他的主机可以支撑,这样就仍然可以保证服务是可用的。

### 存储空间

既然是群集,那么存储空间肯定也是联合起来的,假如一台主机的存储空间是固定的,那么集群它相对于单个主机也有更多的存储空间,可存储的数据量也更大。所以综上所述,我们需要一个集群!

Elasticsearch集群搭建

下载Elasticsearch

Linux系统可以使用如下命令。

代码语言:javascript复制
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.11.1-linux-x86_64.tar.gz
tar -zxvf  elasticsearch-7.11.1-linux-x86_64.tar.gz

Windows系统可以到官网下载:

代码语言:javascript复制
https://www.elastic.co/cn/downloads/elasticsearch

我们将解压出来的文件复制三份出来elasticsearch1、elasticsearch2、elasticsearch3,启动三个节点搭建集群。

修改配置文件

依次打开三个elasticsearch中config目录下的下elasticsearch.yml配置文件

代码语言:javascript复制
vim config/elasticsearch.yml

需要修改的位置如下

代码语言:javascript复制
#配置文件中未改动的位置就不展示了


#第一个配置文件改动如下
#集群名称(必须一样)
cluster.name: my-application
#节点名称(必须不一样)
node.name: node-1
#是否可以选举为主节点
node.master: true
#是否存储数据
node.data: true
#本机的IP地址
network.host: 0.0.0.0
#发布地址
network.publish_host: 127.0.0.1
#服务的端口号(在本地配置多个时,请注意修改为不一样的端口)
http.port: 9200
#服务发现端口
transport.tcp.port: 9300
#集群发现IP集合
discovery.seed_hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
#集群选举设置
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
#开启跨域访问
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "Authorization"



#第二个配置文件改动如下
cluster.name: my-application
node.name: node-2
node.master: true
node.data: true
network.host: 0.0.0.0
network.publish_host: 127.0.0.1
http.port: 9201
transport.tcp.port: 9301
discovery.seed_hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "Authorization"



#第三个配置文件改动如下
cluster.name: my-application
node.name: node-3
node.master: true
node.data: true
network.host: 0.0.0.0
network.publish_host: 127.0.0.1
http.port: 9202
transport.tcp.port: 9302
discovery.seed_hosts: ["127.0.0.1:9300","127.0.0.1:9301","127.0.0.1:9302"]
cluster.initial_master_nodes: ["node-1","node-2","node-3"]
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-headers: "Authorization"

启动集群

依次启动三个elasticsearch节点

代码语言:javascript复制
bin/elasticsearch

执行以下命令,观察集群状态。

代码语言:javascript复制
http://localhost:9200/_cluster/stats?pretty

集群状态如下。

代码语言:javascript复制
{
  "_nodes" : {
    "total" : 3,
    "successful" : 3,
    "failed" : 0
  },
  "cluster_name" : "my-application",
  "cluster_uuid" : "28M3hT4mRGeFI1fcSlgvwA",
  "timestamp" : 1614613614376,
  "status" : "green",
  "indices" : {
    "count" : 0,
    "shards" : { },
    "docs" : {
      "count" : 0,
      "deleted" : 0
    },
    "store" : {
      "size_in_bytes" : 0,
      "reserved_in_bytes" : 0
    },
    "fielddata" : {
      "memory_size_in_bytes" : 0,
      "evictions" : 0
    },
    "query_cache" : {
      "memory_size_in_bytes" : 0,
      "total_count" : 0,
      "hit_count" : 0,
      "miss_count" : 0,
      "cache_size" : 0,
      "cache_count" : 0,
      "evictions" : 0
    },
    "completion" : {
      "size_in_bytes" : 0
    },
    "segments" : {
      "count" : 0,
      "memory_in_bytes" : 0,
      "terms_memory_in_bytes" : 0,
      "stored_fields_memory_in_bytes" : 0,
      "term_vectors_memory_in_bytes" : 0,
      "norms_memory_in_bytes" : 0,
      "points_memory_in_bytes" : 0,
      "doc_values_memory_in_bytes" : 0,
      "index_writer_memory_in_bytes" : 0,
      "version_map_memory_in_bytes" : 0,
      "fixed_bit_set_memory_in_bytes" : 0,
      "max_unsafe_auto_id_timestamp" : -9223372036854775808,
      "file_sizes" : { }
    },
    "mappings" : {
      "field_types" : [ ]
    },
    "analysis" : {
      "char_filter_types" : [ ],
      "tokenizer_types" : [ ],
      "filter_types" : [ ],
      "analyzer_types" : [ ],
      "built_in_char_filters" : [ ],
      "built_in_tokenizers" : [ ],
      "built_in_filters" : [ ],
      "built_in_analyzers" : [ ]
    },
    "versions" : [ ]
  },
  "nodes" : {
    "count" : {
      "total" : 3,
      "coordinating_only" : 0,
      "data" : 3,
      "data_cold" : 3,
      "data_content" : 3,
      "data_hot" : 3,
      "data_warm" : 3,
      "ingest" : 3,
      "master" : 3,
      "ml" : 3,
      "remote_cluster_client" : 3,
      "transform" : 3,
      "voting_only" : 0
    },
    "versions" : [
      "7.11.1"
    ],
    "os" : {
      "available_processors" : 24,
      "allocated_processors" : 24,
      "names" : [
        {
          "name" : "Mac OS X",
          "count" : 3
        }
      ],
      "pretty_names" : [
        {
          "pretty_name" : "Mac OS X",
          "count" : 3
        }
      ],
      "mem" : {
        "total_in_bytes" : 51539607552,
        "free_in_bytes" : 15520432128,
        "used_in_bytes" : 36019175424,
        "free_percent" : 30,
        "used_percent" : 70
      }
    },
    "process" : {
      "cpu" : {
        "percent" : 11
      },
      "open_file_descriptors" : {
        "min" : 355,
        "max" : 357,
        "avg" : 355
      }
    },
    "jvm" : {
      "max_uptime_in_millis" : 772083,
      "versions" : [
        {
          "version" : "1.8.0_192",
          "vm_name" : "Java HotSpot(TM) 64-Bit Server VM",
          "vm_version" : "25.192-b12",
          "vm_vendor" : "Oracle Corporation",
          "bundled_jdk" : true,
          "using_bundled_jdk" : false,
          "count" : 3
        }
      ],
      "mem" : {
        "heap_used_in_bytes" : 728808848,
        "heap_max_in_bytes" : 3113877504
      },
      "threads" : 98
    },
    "fs" : {
      "total_in_bytes" : 250790436864,
      "free_in_bytes" : 32642080768,
      "available_in_bytes" : 16309493760
    },
    "plugins" : [ ],
    "network_types" : {
      "transport_types" : {
        "security4" : 3
      },
      "http_types" : {
        "security4" : 3
      }
    },
    "discovery_types" : {
      "zen" : 3
    },
    "packaging_types" : [
      {
        "flavor" : "default",
        "type" : "tar",
        "count" : 3
      }
    ],
    "ingest" : {
      "number_of_pipelines" : 2,
      "processor_stats" : {
        "gsub" : {
          "count" : 0,
          "failed" : 0,
          "current" : 0,
          "time_in_millis" : 0
        },
        "script" : {
          "count" : 0,
          "failed" : 0,
          "current" : 0,
          "time_in_millis" : 0
        }
      }
    }
  }
}

0 人点赞