基础概念及es8集群安装
文档汇总
- 点击查看官方文档汇总
- 点击查看Elasticsearch Clients文档,如JAVA客户端文档
- 点击查看REST APIs文档
- 点击查看Elasticsearch配置项文档
基础概念
- 集群cluster
一个集群就是由一个或多个节点组织在一起,它们共同持有整个的数据,并一起提供索引和搜索功能。一个集群由一个唯一的名字标识,这个名字默认就是"elasticsearch"。这个名字是重要的,因为一个节点只能通过指定某个集群的名字,来加入这个集群 - 节点node
一个节点是集群中的一个服务器,作为集群的一部分,它存储数据,参与集群的索引和搜索功能。和集群类似,一个节点也是由一个名字来标识的,默认情况下,这个名字是一个随机的漫威漫画角色的名字,这个名字会在启动的时候赋予节点。这个名字对于管理工作来说挺重要的,因为在这个管理过程中,你会去确定网络中的哪些服务器对应于Elasticsearch集群中的哪些节点。
一个节点可以通过配置集群名称的方式来加入一个指定的集群。默认情况下,每个节点都会被安排加入到一个叫做"elasticsearch"的集群中,这意味着,如果你在你的网络中启动了若干个节点,并假定它们能够相互发现彼此,它们将会自动地形成并加入到一个叫做"elasticsearch"的集群中。
在一个集群里,只要你想,可以拥有任意多个节点。而且,如果当前你的网络中没有运行任何Elasticsearch节点。这时启动一个节点,会默认创建并加入一个叫做elasticsearch的集群。 - 结构
下载及官方文档
官方github点击下载对应所需ES版本zip压缩包
📌任意不懂内容可以直接在ES官网搜索就能找到对应文档,比如
搜索cluster.initial_master_nodes,就可以找到此配置相关内容
安装es8
-
解压对应安装包elasticsearch-8.x.x-zip,先启动初始化一下es,
执行bin目录下的elasticsearch.bat
初始化完成后,保存初始化完获取的信息后面需要用到
-> Elasticsearch security features have been automatically configured! -> Authentication is enabled and cluster connections are encrypted. -> Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`): 3maAuxJjHdgoSTgaH9+S -> HTTP CA certificate SHA-256 fingerprint: e7818ec3032eacef7dc911d97ca5998c5058987a44aecee3a6d128bed8e1315f -> Configure Kibana to use this cluster: * Run Kibana and click the configuration link in the terminal when Kibana starts. * Copy the following enrollment token and paste it into Kibana in your browser (valid for the next 30 minutes): eyJ2ZXIiOiI4LjQuMSIsImFkciI6WyIxOTIuMTY4LjUwLjE0Njo5MjAwIl0sImZnciI6ImU3ODE4ZWMzMDMyZWFjZWY3ZGM5MTFkOTdjYTU5OThjNTA1ODk4N2E0NGFlY2VlM2E2ZDEyOGJlZDhlMTMxNWYiLCJrZXkiOiItWm43em9ZQjIxWndudC0weXpRQjpCbjZjblpfV1NreVR0bHlRckQyR0FBIn0= -> Configure other nodes to join this cluster: * On this node: - Create an enrollment token with `bin/elasticsearch-create-enrollment-token -s node`. - Uncomment the transport.host setting at the end of config/elasticsearch.yml. - Restart Elasticsearch. * On other nodes: - Start Elasticsearch with `bin/elasticsearch --enrollment-token <token>`, using the enrollment token that you generated. --------
-
暂不关闭服务,修改一下elastic的随机密码,方便后面测试
bin/elasticsearch-reset-password -u elastic –i
修改为123456,修改后关闭服务 -
安装下载IK分词器拓展,需要注意版本和ES版本对应下载及官方文档
将分词器zip安装包右键解压到es安装目录下的plugins目录即可,如xx/plugins/elasticsearch-analysis-ik-8.x.x
可替代的中文分词包括HanLP分词
-
修改es的jvm内存配置,因为是在本机测试,到时会有多个服务启动,将内存配置改小,找到es安装目录下的config/jvm.options,添加或者修改以下配置项
-Xms256m -Xmx256m
-
修改es的配置文件,找到es安装目录下的/config/elasticsearch.yml,修改或新增以下内容
xpack.security.enabled: true xpack.security.enrollment.enabled: true xpack.security.http.ssl: enabled: true keystore.path: certs/http.p12 xpack.security.transport.ssl: enabled: true verification_mode: certificate keystore.path: certs/transport.p12 truststore.path: certs/transport.p12 http.host: 0.0.0.0 http.port: 9201 http.cors.enabled: true http.cors.allow-origin: "*" ingest.geoip.downloader.enabled: false cluster.name: my-es node.name: node-1 node.roles: [master,data] cluster.initial_master_nodes: ["node-1","node-2","node-3"] network.host: 127.0.0.1 transport.port: 9301 discovery.seed_hosts: ["127.0.0.1:9302","127.0.0.1:9303"]
注意查看原文件是否已有重复配置项,配置重复项启动服务时会闪退
xpack.security
相关配置是开启es的https证书安全认证http.host
配置允许访问的主机,0.0.0.0表示可以被任意远程主机访问http.port
当前节点提供对外HTTP访问的端口http.cors
相关配置开启跨域访问,配置es-head图形化界面ingest.geoip.downloader.enabled
表示是否启用geoip数据库的相关更新,否则启动时更新会有geoip相关报错,设置false重新启动后会自动删除.geoip_databases
索引cluster.name
表示配置集群名称,一个节点只有在与集群中的所有其他节点有相同的cluster.name时才能加入集群node.name
表示当前节点名称node.roles
表示当前节点允许充当的角色,master即有机会选举为主节点,data可充当数据操作节点,如CRUD、搜索和聚合cluster.initial_master_nodes
表示一个集群中符合主节点条件的节点初始集合discovery.seed_hosts
表示集群中符合条件的主节点地址列表。每个地址的格式都是host:port或host。如果没有给出端口,则通过按顺序检查以下设置来确定transport.profiles.default.port
transport.port
如果这两项都未设置,则默认端口为9300
network.host
设置此节点HTTP和数据传输的地址。节点将绑定到此地址,并将其用作发布地址,可以是IP地址、主机名或特殊值1,默认为local
transport.port
节点间的数据传输使用端口
📌更多配置项可以在官方文档中查看点击查看Elasticsearch配置项文档
-
此时es的单个节点配置已完成,将安装文件夹修改为cluster-node1,将cluster-node1复制出来两份,分别重命名为cluster-node2、cluster-node3,同时分别清空cluster-node2、cluster-node3下的logs夹内容,删除整个data文件夹,cluster-node1不需要进行上述操作
-
复制cluster-node1的配置文件,YAML 代码块,同样注意不要重复配置
- cluster-node2的配置文件修改为以下内容
xpack.security.enabled: true xpack.security.enrollment.enabled: true xpack.security.http.ssl: enabled: true keystore.path: certs/http.p12 xpack.security.transport.ssl: enabled: true verification_mode: certificate keystore.path: certs/transport.p12 truststore.path: certs/transport.p12 http.host: 0.0.0.0 http.port: 9202 http.cors.enabled: true http.cors.allow-origin: "*" ingest.geoip.downloader.enabled: false cluster.name: my-es node.name: node-2 node.roles: [master,data] cluster.initial_master_nodes: ["node-1","node-2","node-3"] network.host: 127.0.0.1 transport.port: 9302 discovery.seed_hosts: ["127.0.0.1:9301","127.0.0.1:9303"]
b. cluster-node3的配置文件修改为以下内容
xpack.security.enabled: true xpack.security.enrollment.enabled: true xpack.security.http.ssl: enabled: true keystore.path: certs/http.p12 xpack.security.transport.ssl: enabled: true verification_mode: certificate keystore.path: certs/transport.p12 truststore.path: certs/transport.p12 http.host: 0.0.0.0 http.port: 9203 http.cors.enabled: true http.cors.allow-origin: "*" ingest.geoip.downloader.enabled: false cluster.name: my-es node.name: node-3 node.roles: [master,data] cluster.initial_master_nodes: ["node-1","node-2","node-3"] network.host: 127.0.0.1 transport.port: 9303 discovery.seed_hosts: ["127.0.0.1:9301","127.0.0.1:9302"]
-
以此启动三个节点,cluster-node1、cluster-node2、cluster-node3
bin/elasticsearch.bat
,三个节点访问地址,任意一个浏览器访问,登录elastice/123456即可正常访问
cluster-node1:https://127.0.0.1:9201
cluster-node2:https://127.0.0.1:9202
cluster-node3:https://127.0.0.1:9203
到此es8集群安装结束
安装es-head图形化管理插件
- 下载es-head扩展包
- 安装到谷歌浏览器的扩展程序,步骤如下
a. 将下载的压缩包解压,得到crx文件
b. 将.crx修改为.zip,然后解压缩,将解压得到的多个文件(文件夹)移动至新文件夹;
c. 在浏览器扩展页面选择“加载解压缩的扩展”,找到上面解压的目录文件夹,选中确认即可完成安装。
文件夹不可删除,否则将导致插件不可用 - 浏览器点击es-head插件,打开管理页面,配置任意一个节点地址,输入密码,即可正常加载管理页面
如果点击"连接"没反应,先复制地址到浏览器访问一下,再回到当前页面继续连接
- 测试新建一个索引,如图
效果和前面结构分析一致,不同的分片分散在不同的节点
黑色加粗的为主分片,浅色黑边对应的是副本
local
系统上的任何环回地址,例如127.0.0.1
site
系统上的任何站点本地地址,例如192.168.0.1。
global
系统上任何全局范围的地址,例如8.8.8.8。
[networkInterface]
使用名为[networkInterface]的网络接口的地址。例如,如果您希望使用名为en0的接口的地址,则设置network.host:en0。
0.0.0.0
所有可用网络接口的地址。在一些系统中,这些特殊值解析为多个地址。如果是这样,Elasticsearch将选择其中一个作为其发布地址,并可能在每次节点重新启动时更改其选择。确保每个可能的地址都可以访问您的节点。注意:任何包含:的值(例如IPv6地址或某些特殊值)都必须引用"",因为:是YAML中的一个特殊字符。 ↩︎