简介
开始学es,我习惯边学边记,总结出现的问题和解决方法。本文是在两台linux虚拟机下,安装了三个节点。本次搭建es同时实践了两种模式——单机模式和分布式模式。条件允许的话,可以在多台机器上配置es节点,如果你机器性能有限,那么可以在一台虚拟机上完成多节点的配置。
如图,是本次3个节点的分布。
虚拟机主机名 | IP | es节点 |
---|
master | 192.168.137.100 | node1、node3 |
slave | 192.168.137.101 | node2 |
一、下载及配置
1.几个基本名词
index: es里的index相当于一个数据库。
type: 相当于数据库里的一个表。
id: 唯一,相当于主键。
node:节点是es实例,一台机器可以运行多个实例,但是同一台机器上的实例在配置文件中要确保http和tcp端口不同(下面有讲)。
cluster:代表一个集群,集群中有多个节点,其中有一个会被选为主节点,这个主节点是可以通过选举产生的,主从节点是对于集群内部来说的。
shards:代表索引分片,es可以把一个完整的索引分成多个分片,这样的好处是可以把一个大的索引拆分成多个,分布到不同的节点上,构成分布式搜索。分片的数量只能在索引创建前指定,并且索引创建后不能更改。
replicas:代表索引副本,es可以设置多个索引的副本,副本的作用一是提高系统的容错性,当个某个节点某个分片损坏或丢失时可以从副本中恢复。二是提高es的查询效率,es会自动对搜索请求进行负载均衡。
2.下载
下载后,放到你的目录下并解压. 因为我们要配置包含三个节点的集群,可以先将其重命名为elasticsearch-node1。比如我的是 /home/zkpk/elasticsearch-node1。
3.修改配置文件
(1) 初步修改
打开/home/zkpk/elasticsearch-node1/config目录下的elasticsearch.yml 文件 ,修改以下属性值并取消该行的注释:
<code class="hljs vala has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">cluster.name: elasticsearch
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#这是集群名字,我们 起名为 elasticsearch。es启动后会将具有相同集群名字的节点放到一个集群下。</span>
node.name: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"es-node1"</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#节点名字。</span>
covery.zen.minimum_master_nodes: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#指定集群中的节点中有几个有master资格的节点。对于大集群可以写3个以上。</span>
discovery.zen.ping.timeout: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">40</span>s
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#默认是3s,这是设置集群中自动发现其它节点时ping连接超时时间,为避免因为网络差而导致启动报错,我设成了40s。</span>
discovery.zen.ping.multicast.enabled: <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">false</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#设置是否打开多播发现节点,默认是true。</span>
network.bind_host: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">192.168</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.137</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.100</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#设置绑定的ip地址,这是我的master虚拟机的IP。</span>
network.publish_host: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">192.168</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.137</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.100</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#设置其它节点和该节点交互的ip地址。</span>
network.host: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">192.168</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.137</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.100</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#同时设置bind_host和publish_host上面两个参数。</span>
discovery.zen.ping.unicast.hosts: [<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"192.168.137.100"</span>, <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"192.168.137.101"</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"192.168.137.100:9301"</span>]
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#discovery.zen.ping.unicast.hosts:["节点1的 ip","节点2 的ip","节点3的ip"]</span>
指明集群中其它可能为master的节点ip,以防es启动后发现不了集群中的其他节点。第一对引号里是node1,默认端口是<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9300</span>,第二个引号里是 node3,因为它俩在一台机器上,所以指定了<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">9301</span>端口,第三个是 node2 ,在另外一台机器上。</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li></ul>
(2) 进一步修改
拷贝 elasticsearch-node1 整个文件夹,两份,一份elasticsearch-node2,一份elasticsearch-node3.
将elasticsearch-node2 文件夹copy到另外一台IP为192.168.137.101的机器上。而在 192.168.137.100 机器上有 node1和node3.
对于node3: node3和node1在一台机器上,node1的配置文件里端口默认分别是9300和9200,所以要改一下node3配置文件里的端口,elasticsearch.yml 文件修改如下:
<code class="hljs http has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-attribute" style="box-sizing: border-box;">node.name</span>: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"es-node3"</span>
<span class="hljs-attribute" style="box-sizing: border-box;">transport.tcp.port</span>: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">9301</span>
<span class="hljs-attribute" style="box-sizing: border-box;">http.port</span>: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">9201</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
对于node2: 对 elasticsearch.yml 修改如下
<code class="hljs http has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-attribute" style="box-sizing: border-box;">node.name</span>: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"es-node2"</span>
<span class="hljs-attribute" style="box-sizing: border-box;">network.bind_host</span>: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">192.168.137.101</span>
<span class="hljs-attribute" style="box-sizing: border-box;">network.publish_host</span>: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">192.168.137.101</span>
<span class="hljs-attribute" style="box-sizing: border-box;">network.host</span>: <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">192.168.137.101</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
注意:
1.对于单机多节点的es集群,一定要注意修改 transport.tcp.port 和http.port 的默认值保证节点间不冲突。
2. 出现找不到同一集群中的其他节点的情况,检查下 discovery.zen.ping.unicast.hosts 是否已设置。
二、运行 & 关闭 elasticsearch
1.运行elasticsearch :
编辑 /home/zkpk/elasticsearch-1.7.3/bin/elasticsearch.in.sh, 设置 ES_MIN_MEM和ES_MAX_MEM,确保二者数值一致,或者可以在启动es时指定,
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">[zkpk<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@master</span> ~]<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>cd ~<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/elasticsearch-node1/bin</span>
[zkpk<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@master</span> bin]<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>./elasticsearch -<span class="hljs-constant" style="box-sizing: border-box;">Xms512m</span> -<span class="hljs-constant" style="box-sizing: border-box;">Xmx512m</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>
若想让es后台运行,则
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">[zkpk<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@master</span> bin]<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>./elasticsearch -d -<span class="hljs-constant" style="box-sizing: border-box;">Xms512m</span> -<span class="hljs-constant" style="box-sizing: border-box;">Xmx512m</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
2.关闭elasticsearch:
前台运行:可以通过”CTRL+C”组合键来停止运行
后台运行,可以通过”kill -9 进程号”停止.也可以通过REST API接口:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">curl <span class="hljs-attribute" style="box-sizing: border-box;">-XPOST</span> http:<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//主机IP:9200/_cluster/nodes/_shutdown</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
来关闭整个集群,通过:
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">curl <span class="hljs-attribute" style="box-sizing: border-box;">-XPOST</span> http:<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//主机IP:9200/_cluster/nodes/节点标示符(如es-node1)/_shutdown</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
来关闭单个节点.
三、插件及其安装
BigDesk Plugin : 对集群中es状态进行监控。
Elasticsearch Head Plugin: 对ES进行各种操作,如查询、删除、浏览索引等。
1.安装head插件
进入到节点elasticsearch-node1/bin路径,并安装插件。
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">[zkpk<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@master</span> bin]<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>./plugin -install mobz/elasticsearch-head</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
2. 安装bigdesk
[zkpk@master bin]$ ./plugin -install lukas-vlcek/bigdesk
让我们看下es页面吧~~
打开head浏览,浏览器输入http://192.168.137.100:9200/_plugin/head/ ,如图,
每个小方块就是索引分片,可以看到每个索引被分成几个分片,每个分片还有它的备份分片,然后存储在三个节点上。粗框的是主分片,细框的是备份分片。
四、添加索引
现在我们来添加一个索引记录吧~
1.可以在命令窗口通过命令来添加
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">curl <span class="hljs-attribute" style="box-sizing: border-box;">-XPUT</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'http://主机IP:9200/dept/employee/32'</span> <span class="hljs-attribute" style="box-sizing: border-box;">-d</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'{ "empname": "emp32"}'</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
见 http://www.oschina.net/translate/elasticsearch-getting-started?cmp
2.我们可以在页面上通过JSON添加
(1)点击 复合查询[+] ,我们可以在 megacorp 索引 (相当于数据库名)的 employee 类型(相当于表名)下新增一个id为2的人的信息。
点击下方的 提交请求 按钮,页面右方有回馈信息,“created”代表是否为新建。添加成功。
点击 浏览数据 ,在左侧 索引 下选择 megacorp,如图,
可以看到,一条id为2的记录被添加了。
(2)下面我们修改id为2 的人的年龄为15,把about 信息去掉,并且加一项兴趣。
提交后,右侧有反馈信息,“created”为 false,因为我们这次不是新建而是修改。
返回浏览数据,id为2 的记录,年龄、兴趣等均已发生变化。
所有评论(0)