elasticsearch专题

ElasticSearch高可用集群安装记录

2022-04-16  本文已影响0人  前浪浪奔浪流

涉及版本声明:

Linux: CentOS 7.9 64位
Jdk: 1.8.0_11
ElasticSearch: elasticsearch-7.16.2
下载地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.16.2-x86_64.rpm
Ik-Analysis: elasticsearch-analysis-ik-7.16.2
下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
analysis-pinyin: elasticsearch-analysis-pinyin-7.16.2
下载地址:https://github.com/medcl/elasticsearch-analysis-pinyin/releases
elasticsearch-head: elasticsearch-head5.0.0
下载地址:https://codeload.github.com/mobz/elasticsearch-head/tar.gz/refs/tags/v5.0.0
Elasticsearch与jdk版本对应关系

image.png

一、 安装环境准备

主机规划:
192.168.100.181 es01
192.168.100.182 es02
192.168.100.183 es03
配置:
2 VCPU、2G内存、20G硬盘

修改服务器名称:

hostnamectl set-hostname es01
hostnamectl set-hostname es02
hostnamectl set-hostname es03

系统配置:

需要修改几个参数,不然启动会报错

vim /etc/security/limits.conf
在末尾追加以下内容
* soft nofile 65536
* hard nofile 65536
* soft nproc 2048
* hard nproc 2048
* soft memlock unlimited
* hard memlock unlimited

对于systemd service的资源限制,现在放在 /etc/systemd/system.conf

vim /etc/systemd/system.conf
在末尾追加以下内容
DefaultLimitNOFILE=65536
DefaultLimitNPROC=32000
DefaultLimitMEMLOCK=infinity

否则elasticsearch启动时会报错:
memory locking requested for elasticsearch process but memory is not locked

这个问题的解决办法是修改

vim /usr/lib/systemd/system/elasticsearch.service
在文件中添加
LimitMEMLOCK=infinity
保存后退出
systemctl daemon-reload

二、安装配置

1、安装JDK
使用脚本安装jdk1.8版本 
chmod +x jdkinstall.sh
sh jdkinstall.sh
source /etc/profile
java -version
image.png
2,安装elasticsearch
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.16.2-x86_64.rpm

rpm -ivh elasticsearch-7.16.2-x86_64.rpm
ll /etc/elasticsearch/
image.png
2.1配置elasticsearch
创建elasticsearch data的存放目录,并修改该目录的属主属组
# mkdir -p /data/es-data   (自定义用于存放data数据的目录)
# chown -R elasticsearch:elasticsearch /data/es-data
修改elasticsearch的日志属主属组
# chown -R elasticsearch:elasticsearch /var/log/elasticsearch/
修改elasticsearch的配置文件
# vim /etc/elasticsearch/elasticsearch.yml
找到配置文件中的cluster.name,打开该配置并设置集群名称
17行
cluster.name: my-els
找到配置文件中的node.name,打开该配置并设置节点名称
node.name: es01      //192.168.100.181
node.name: es02      //192.168.100.182
node.name: es03      //192.168.100.183

修改data存放的路径
path.data: /data/es-data

修改logs日志的路径
path.logs: /var/log/elasticsearch/

锁定物理内存地址,防止elasticsearch内存被交换出去,也就是避免es使用swap交换分区,频繁的交换,会导致IOPS变高;
bootstrap.memory_lock: true

监听的网络地址
network.host: 0.0.0.0

开启监听的端口
http.port: 9200

增加新的参数,这样head插件可以访问es
http.cors.enabled: true    
http.cors.allow-origin: "*"
bootstrap.system_call_filter: false

集群发现
在启动此节点时,传递要执行发现的主机的初始列表:
discovery.seed_hosts: ["192.168.100.181", "192.168.100.182","192.168.100.183"]
初始用来引导集群的符合主节点条件的节点列表:
cluster.initial_master_nodes: ["192.168.100.181", "192.168.100.182","192.168.100.183"]
保存退出
ps -ef|grep java
elastic+ 25475     1  0 4月15 ?       00:11:13 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -XX:+ShowCodeDetailsInExceptionMessages -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dio.netty.allocator.numDirectArenas=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Dlog4j2.formatMsgNoLookups=true -Djava.locale.providers=SPI,COMPAT --add-opens=java.base/java.io=ALL-UNNAMED -Xms512m -Xmx512m -XX:+UseG1GC -Djava.io.tmpdir=/tmp/elasticsearch-730528740427080331 -XX:+HeapDumpOnOutOfMemoryError -XX:+ExitOnOutOfMemoryError -XX:HeapDumpPath=/var/lib/elasticsearch -XX:ErrorFile=/var/log/elasticsearch/hs_err_pid%p.log -Xlog:gc*,gc+age=trace,safepoint:file=/var/log/elasticsearch/gc.log:utctime,pid,tags:filecount=32,filesize=64m -XX:MaxDirectMemorySize=268435456 -XX:G1HeapRegionSize=4m -XX:InitiatingHeapOccupancyPercent=30 -XX:G1ReservePercent=15 -Des.path.home=/usr/share/elasticsearch -Des.path.conf=/etc/elasticsearch -Des.distribution.flavor=default -Des.distribution.type=rpm -Des.bundled_jdk=true -cp /usr/share/elasticsearch/lib/* org.elasticsearch.bootstrap.Elasticsearch -p /var/run/elasticsearch/elasticsearch.pid --quiet

可以看到elasticsearch启动所使用的java路径:/usr/share/elasticsearch/jdk/bin/java
查看java版本
[root@es01 home]# /usr/share/elasticsearch/jdk/bin/java -version
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment Temurin-17.0.1+12 (build 17.0.1+12)
OpenJDK 64-Bit Server VM Temurin-17.0.1+12 (build 17.0.1+12, mixed mode, sharing)

elasticsearch使用的内存大小为2G,我们把它改小点:
修改参数:
vim /etc/elasticsearch/jvm.options
-Xms512m
-Xmx512m
启动
systemctl start elasticsearch.service

**查看状态**

systemctl status elasticsearch.service
image.png image.png

日志位置(日志的名称是以集群名称命名的):

ll /var/log/elasticsearch/
总用量 1760
-rw-r--r-- 1 elasticsearch elasticsearch 814198 4月  16 16:21 gc.log
-rw-r--r-- 1 elasticsearch elasticsearch   2031 4月   9 17:48 gc.log.00
-rw-r--r-- 1 elasticsearch elasticsearch  64859 4月   9 17:55 gc.log.01
-rw-r--r-- 1 elasticsearch elasticsearch   2036 4月  14 17:13 gc.log.02
-rw-r--r-- 1 elasticsearch elasticsearch 142865 4月  14 17:14 gc.log.03
-rw-r--r-- 1 elasticsearch elasticsearch   2036 4月  14 17:19 gc.log.04
-rw-r--r-- 1 elasticsearch elasticsearch  76996 4月  14 17:20 gc.log.05
-rw-r--r-- 1 elasticsearch elasticsearch   2036 4月  14 17:26 gc.log.06
-rw-r--r-- 1 elasticsearch elasticsearch 539889 4月  15 16:58 gc.log.07
-rw-r--r-- 1 elasticsearch elasticsearch   2036 4月  15 17:00 gc.log.08
-rw-r--r-- 1 elasticsearch elasticsearch  61206 4月  14 17:11 gc.log.0.current
-rw-r--r-- 1 elasticsearch elasticsearch   5388 4月  14 17:10 my-els-2022-04-09-1.json.gz
-rw-r--r-- 1 elasticsearch elasticsearch   5039 4月  14 17:10 my-els-2022-04-09-1.log.gz
-rw-r--r-- 1 elasticsearch elasticsearch   7727 4月  15 00:34 my-els-2022-04-14-1.json.gz
-rw-r--r-- 1 elasticsearch elasticsearch   6883 4月  15 00:34 my-els-2022-04-14-1.log.gz
-rw-r--r-- 1 elasticsearch elasticsearch  10013 4月  16 01:02 my-els-2022-04-15-1.json.gz
-rw-r--r-- 1 elasticsearch elasticsearch   9412 4月  16 01:02 my-els-2022-04-15-1.log.gz
-rw-r--r-- 1 elasticsearch elasticsearch      0 4月   9 17:48 my-els_audit.json
-rw-r--r-- 1 elasticsearch elasticsearch   5148 4月  15 17:04 my-els_deprecation.json
-rw-r--r-- 1 elasticsearch elasticsearch   3166 4月  15 17:04 my-els_deprecation.log
-rw-r--r-- 1 elasticsearch elasticsearch      0 4月   9 17:48 my-els_index_indexing_slowlog.json
-rw-r--r-- 1 elasticsearch elasticsearch      0 4月   9 17:48 my-els_index_indexing_slowlog.log
-rw-r--r-- 1 elasticsearch elasticsearch      0 4月   9 17:48 my-els_index_search_slowlog.json
-rw-r--r-- 1 elasticsearch elasticsearch      0 4月   9 17:48 my-els_index_search_slowlog.log
-rw-r--r-- 1 elasticsearch elasticsearch   1201 4月  16 12:42 my-els.log
-rw-r--r-- 1 elasticsearch elasticsearch   2992 4月  16 12:42 my-els_server.json

创建开机自启动服务

systemctl enable elasticsearch.service
chkconfig elasticsearch on

如果报错:

[2022-04-14T17:13:04,076][WARN ][o.e.b.JNANatives         ] [es02] Unable to lock JVM Memory: error=12, reason=无法分配内存
[2022-04-14T17:13:04,078][WARN ][o.e.b.JNANatives         ] [es02] This can result in part of the JVM being swapped out.
[2022-04-14T17:13:04,078][WARN ][o.e.b.JNANatives         ] [es02] Increase RLIMIT_MEMLOCK, soft limit: 65536, hard limit: 65536
[2022-04-14T17:13:04,079][WARN ][o.e.b.JNANatives         ] [es02] These can be adjusted by modifying /etc/security/limits.conf, for example: 
    # allow user 'elasticsearch' mlockall
    elasticsearch soft memlock unlimited
    elasticsearch hard memlock unlimited
[2022-04-14T17:13:04,079][WARN ][o.e.b.JNANatives         ] [es02] If you are logged in interactively, you will have to re-login for the new limits to take effect.

经测试:

/etc/security/limits.conf 文件末尾增加
elasticsearch soft memlock unlimited
elasticsearch hard memlock unlimited 

不能解决问题。
只能修改/usr/lib/systemd/system/elasticsearch.service才行

vim /usr/lib/systemd/system/elasticsearch.service
[service]增加 LimitMEMLOCK=infinity
systemctl daemon-reload
systemctl restart elasticsearch.service
# netstat -antp |grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      25475/java

通过浏览器请求下9200的端口,看下是否成功, 以下为正常:

curl http://127.0.0.1:9200/
{
  "name" : "es01",
  "cluster_name" : "my-els",
  "cluster_uuid" : "GKN9_h8PT5m9vBUi_-W_-Q",
  "version" : {
    "number" : "7.16.2",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "2b937c44140b6559905130a8650c64dbd0879cfb",
    "build_date" : "2021-12-18T19:42:46.604893745Z",
    "build_snapshot" : false,
    "lucene_version" : "8.10.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

如何和elasticsearch交互

curl -i -XGET 'localhost:9200/_count?pretty'
HTTP/1.1 200 OK
X-elastic-product: Elasticsearch
Warning: 299 Elasticsearch-7.16.2-2b937c44140b6559905130a8650c64dbd0879cfb "Elasticsearch built-in security features are not enabled. Without authentication, your cluster could be accessible to anyone. See https://www.elastic.co/guide/en/elasticsearch/reference/7.16/security-minimal-setup.html to enable security."
content-type: application/json; charset=UTF-8
content-length: 115

{
  "count" : 10,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  }
}
3、安装elasticsearch-head插件

通过github下载elasticsearch-head项目
npm安装 下载源码,编译安装,在nodejs环境下运行插件

# yum install git npm bzip2 -y
# git clone git://github.com/mobz/elasticsearch-head.git
正克隆到 'elasticsearch-head'...
fatal: remote error: 
  The unauthenticated git protocol on port 9418 is no longer supported.
Please see https://github.blog/2021-09-01-improving-git-protocol-security-github/ for more information.

然后发现无法正常下载

下载地址:
https://github.com/mobz/elasticsearch-head
https://codeload.github.com/mobz/elasticsearch-head/tar.gz/refs/tags/v5.0.0
通过浏览器下载elasticsearch-head-5.0.0.tar.gz然后上传后解压
tar -zxvf elasticsearch-head-5.0.0.tar.gz
ll 
 ll
总用量 505608
-rw-------. 1 root root      1416 4月   9 16:03 anaconda-ks.cfg
-rwxr-xr-x. 1 root root      1951 4月   9 16:48 centos7start.sh
-rwxr-xr-x. 1 root root       264 4月   9 16:48 date.sh
-rw-r--r--  1 root root 343758474 4月   9 17:31 elasticsearch-7.16.2-x86_64.rpm
drwxrwxr-x  7 root root      4096 4月  14 18:10 elasticsearch-head-5.0.0
-rw-r--r--  1 root root    863489 4月  14 17:41 elasticsearch-head-5.0.0.tar.gz
-rw-r--r--. 1 root root     15608 9月   5 2021 epel-release-latest-7.noarch.rpm
-rw-r--r--  1 root root 159019376 4月   9 17:27 jdk-8u11-linux-x64.tar.gz
-rwxr-xr-x  1 root root       610 4月   9 17:26 jdkinstall.sh
-rw-r--r--  1 root root  14055156 10月 21 2019 node-v12.13.0-linux-x64.tar.xz

wget https://nodejs.org/dist/v12.13.0/node-v12.13.0-linux-x64.tar.xz
tar xf node-v12.13.0-linux-x64.tar.xz
mv node-v12.13.0-linux-x64 /usr/local/node
ll /usr/local/
总用量 0
drwxr-xr-x. 2 root root  18 4月  14 18:01 bin
drwxr-xr-x. 2 root root   6 11月  5 2016 etc
drwxr-xr-x. 2 root root   6 11月  5 2016 games
drwxr-xr-x. 2 root root   6 11月  5 2016 include
drwxr-xr-x  8   10  143 255 6月  17 2014 jdk1.8.0_11
drwxr-xr-x. 3 root root  26 4月  14 18:00 lib
drwxr-xr-x. 2 root root   6 11月  5 2016 lib64
drwxr-xr-x. 2 root root   6 11月  5 2016 libexec
drwxr-xr-x  6 1001 1001 108 10月 21 2019 node
drwxr-xr-x. 2 root root   6 11月  5 2016 sbin
drwxr-xr-x. 5 root root  49 4月   9 15:56 share
drwxr-xr-x. 2 root root   6 11月  5 2016 src

echo 'export PATH=$PATH:/usr/local/node/bin'>>/etc/profile
source /etc/profile
npm -v
8.5.0
node -v
v16.14.1
cd elasticsearch-head-5.0.0/
ll
总用量 96
-rw-rw-r--   1 root root   248 9月  15 2017 Dockerfile
-rw-rw-r--   1 root root   221 9月  15 2017 Dockerfile-alpine
-rw-rw-r--   1 root root   104 9月  15 2017 elasticsearch-head.sublime-project
-rw-rw-r--   1 root root  2171 9月  15 2017 Gruntfile.js
-rw-rw-r--   1 root root  3482 9月  15 2017 grunt_fileSets.js
-rw-rw-r--   1 root root  1088 9月  15 2017 index.html
-rw-rw-r--   1 root root   559 9月  15 2017 LICENCE
drwxr-xr-x 461 root root 32768 4月  14 18:03 node_modules
-rw-------   1 root root   192 4月  14 18:10 nohup.out
-rw-rw-r--   1 root root   886 9月  15 2017 package.json
-rw-rw-r--   1 root root   100 9月  15 2017 plugin-descriptor.properties
drwxrwxr-x   4 root root    53 9月  15 2017 proxy
-rw-rw-r--   1 root root  6944 9月  15 2017 README.textile
drwxrwxr-x   5 root root   140 9月  15 2017 _site
drwxrwxr-x   4 root root    31 9月  15 2017 src
drwxrwxr-x   4 root root    70 9月  15 2017 test
通过阿里巴巴源安装cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org

npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142

added 372 packages in 14s

3 packages are looking for funding
  run `npm fund` for details
npm notice 
npm notice New minor version of npm available! 8.5.0 -> 8.7.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.7.0
npm notice Run npm install -g npm@8.7.0 to update!
npm notice

cnpm install
⠸ [0/10] Installing tiny-lr@^0.2.1
WARN node unsupported "node@v16.14.1" is incompatible with karma@1.3.0, expected node@0.10 || 0.12 || 4 || 5 || 6
⠦ [0/10] Installing karma@1.3.0
WARN node unsupported "node@v16.14.1" is incompatible with grunt-contrib-connect@1.0.2 › http2@^3.3.4, expected node@>=0.12.0 <9.0.0
✔ Installed 10 packages
✔ Linked 383 latest versions
[1/2] scripts.postinstall karma@1.3.0 › core-js@^2.2.0 run "node -e \"try{require('./postinstall')}catch(e){}\"", root: "/root/elasticsearch-head-5.0.0/node_modules/_core-js@2.6.12@core-js"
Thank you for using core-js ( https://github.com/zloirock/core-js ) for polyfilling JavaScript standard library!

The project needs your help! Please consider supporting of core-js on Open Collective or Patreon: 
> https://opencollective.com/core-js 
> https://www.patreon.com/zloirock 

Also, the author of core-js ( https://github.com/zloirock ) is looking for a good job -)

[1/2] scripts.postinstall karma@1.3.0 › core-js@^2.2.0 finished in 276ms
[2/2] scripts.install grunt-contrib-jasmine@1.0.3 › grunt-lib-phantomjs@1.1.0 › phantomjs-prebuilt@^2.1.3 run "node install.js", root: "/root/elasticsearch-head-5.0.0/node_modules/_phantomjs-prebuilt@2.1.16@phantomjs-prebuilt"
PhantomJS not found on PATH
Downloading https://cdn.npmmirror.com/binaries/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
Saving to /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2
Receiving...
  [========================================] 99%
Received 22866K total.
Extracting tar contents (via spawned process)
Removing /root/elasticsearch-head-5.0.0/node_modules/_phantomjs-prebuilt@2.1.16@phantomjs-prebuilt/lib/phantom
Copying extracted folder /tmp/phantomjs/phantomjs-2.1.1-linux-x86_64.tar.bz2-extract-1649930604347/phantomjs-2.1.1-linux-x86_64 -> /root/elasticsearch-head-5.0.0/node_modules/_phantomjs-prebuilt@2.1.16@phantomjs-prebuilt/lib/phantom
Writing location.js file
Done. Phantomjs binary available at /root/elasticsearch-head-5.0.0/node_modules/_phantomjs-prebuilt@2.1.16@phantomjs-prebuilt/lib/phantom/bin/phantomjs
[2/2] scripts.install grunt-contrib-jasmine@1.0.3 › grunt-lib-phantomjs@1.1.0 › phantomjs-prebuilt@^2.1.3 finished in 41s
✔ Run 2 scripts
deprecate grunt-contrib-connect@1.0.2 › http2@^3.3.4 Use the built-in module in node 9.0.0 or newer, instead
deprecate karma@1.3.0 › chokidar@^1.4.1 Chokidar 2 will break on node v14+. Upgrade to chokidar 3 with 15x less dependencies.
anti semver karma@1.3.0 › useragent@2.3.0 › tmp@0.0.x delcares tmp@0.0.x(resolved as 0.0.33) but using ancestor(karma)'s dependency tmp@0.0.28(resolved as 0.0.28)
deprecate karma@1.3.0 › core-js@^2.2.0 core-js@<3.4 is no longer maintained and not recommended for usage due to the number of issues. Because of the V8 engine whims, feature detection in old core-js versions could cause a slowdown up to 100x even if nothing is polyfilled. Please, upgrade your dependencies to the actual version of core-js.
deprecate karma@1.3.0 › socket.io@1.4.7 › socket.io-parser@2.2.6 › json3@3.3.2 Please use the native JSON object instead of JSON 3
deprecate grunt-contrib-jasmine@1.0.3 › grunt-lib-phantomjs@1.1.0 › phantomjs-prebuilt@^2.1.3 this package is now deprecated
deprecate grunt@1.0.1 › coffee-script@~1.10.0 CoffeeScript on NPM has moved to "coffeescript" (no hyphen)
deprecate karma@1.3.0 › socket.io@1.4.7 › socket.io-adapter@0.4.0 › socket.io-parser@2.2.2 › json3@3.2.6 Please use the native JSON object instead of JSON 3
deprecate grunt-contrib-jasmine@1.0.3 › grunt-lib-phantomjs@1.1.0 › phantomjs-prebuilt@2.1.16 › request@^2.81.0 request has been deprecated, see https://github.com/request/request/issues/3142
deprecate karma@1.3.0 › chokidar@1.7.0 › readdirp@2.2.1 › micromatch@3.1.10 › snapdragon@0.8.2 › source-map-resolve@^0.5.0 See https://github.com/lydell/source-map-resolve#deprecated
deprecate grunt-contrib-jasmine@1.0.3 › grunt-lib-phantomjs@1.1.0 › phantomjs-prebuilt@2.1.16 › request@2.88.2 › uuid@^3.3.2 Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
deprecate grunt-contrib-jasmine@1.0.3 › grunt-lib-phantomjs@1.1.0 › phantomjs-prebuilt@2.1.16 › request@2.88.2 › har-validator@~5.1.3 this library is no longer supported
deprecate karma@1.3.0 › chokidar@1.7.0 › readdirp@2.2.1 › micromatch@3.1.10 › snapdragon@0.8.2 › source-map-resolve@0.5.3 › urix@^0.1.0 Please see https://github.com/lydell/urix#deprecated
deprecate karma@1.3.0 › chokidar@1.7.0 › readdirp@2.2.1 › micromatch@3.1.10 › snapdragon@0.8.2 › source-map-resolve@0.5.3 › resolve-url@^0.2.1 https://github.com/lydell/resolve-url#deprecated
deprecate karma@1.3.0 › chokidar@1.7.0 › readdirp@2.2.1 › micromatch@3.1.10 › snapdragon@0.8.2 › source-map-resolve@0.5.3 › source-map-url@^0.4.0 See https://github.com/lydell/source-map-url#deprecated
✔ All packages installed (458 packages installed from npm registry, used 1m(network 23s), speed 358.42KB/s, json 384(1.56MB), tarball 6.39MB, manifests cache hit 0, etag hit 0 / miss 0)

启动es-head插件,必须进入es-head插件目录运行启动命令
cd elasticsearch-head/

nohup npm run start &

使用浏览器访问http://192.168.100.181:9100/即可访问到es-head插件,再使用es-head插件访问es数据库,效果如下


image.png

http://192.168.100.181:9200/

注意
9100端口是插件端口,9200是es端口
通过插件访问http://192.168.100.181:9200点击连接还是失败,是因为es有安全机制只允许服务器本地访问,这时需要设置

设置跨域:
设置跨域同时也适用于安装的es-head插件跟es数据不在同一台服务器上时的情况

#编辑es主配置文件,在文本末尾添加如下两行配置
vim /etc/elasticsearch/elasticsearch.yml
# 是否支持跨域
http.cors.enabled: true
# *表示支持所有域名
http.cors.allow-origin: "*"

systemctl restart elasticsearch
cd elasticsearch-head/
nohup npm run start &

注意 :别把es-head插件后放到 Elasticsearch的plugins目录下,否则会报错!!!

三、安装本地Elasticsearch的IK分词插件和拼音分词插件

1、 安装本地Elasticsearch的IK分词插件

Ik-Analysis: elasticsearch-analysis-ik-7.16.2
下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases
解压下载文件elasticsearch-analysis-ik-7.16.2.zip

ll elasticsearch-analysis-ik-7.16.2
总用量 1428
-rw-r--r-- 1 root root 263965 4月  15 16:59 commons-codec-1.9.jar
-rw-r--r-- 1 root root  61829 4月  15 16:59 commons-logging-1.2.jar
drwxr-xr-x 2 root root    299 4月  15 16:59 config
-rw-r--r-- 1 root root  54595 4月  15 16:59 elasticsearch-analysis-ik-7.16.2.jar
-rw-r--r-- 1 root root 736658 4月  15 16:59 httpclient-4.5.2.jar
-rw-r--r-- 1 root root 326724 4月  15 16:59 httpcore-4.4.4.jar
-rw-r--r-- 1 root root   1807 4月  15 16:59 plugin-descriptor.properties
-rw-r--r-- 1 root root    125 4月  15 16:59 plugin-security.policy

将解压后的elasticsearch-analysis-ik-7.16.2 文件夹上传至elasticsearch安装目录下的plugins下

/usr/share/elasticsearch/plugins/

重启Elasticsearch,可以看到控制台打印日志

loaded plugin [analysis-ik]
2、 安装本地Elasticsearch的拼音分词插件

analysis-pinyin: elasticsearch-analysis-pinyin-7.16.2
下载地址:https://github.com/medcl/elasticsearch-analysis-pinyin/releases
上传下载文件elasticsearch-analysis-pinyin-7.16.2.zip

ll elasticsearch-analysis-pinyin-7.16.2
总用量 7936
-rw-r--r-- 1 root root   27445 4月  15 16:59 elasticsearch-analysis-pinyin-7.16.2.jar
-rw-r--r-- 1 root root 8091448 4月  15 16:59 nlp-lang-1.7.jar
-rw-r--r-- 1 root root    1824 4月  15 16:59 plugin-descriptor.properties

将解压后的elasticsearch-analysis-pinyin-7.16.2 文件夹上传至elasticsearch安装目录下的plugins下

/usr/share/elasticsearch/plugins/

重启Elasticsearch,可以看到控制台打印日志

loaded plugin [analysis-pinyin]

高可用性

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

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

那可能有的小伙伴就会说了,那假如一台主机宕机了,那么不就无法访问这台主机的数据了吗?那假如我要访问的数据正好存在这台主机上,那不就获取不到了吗?难道其他的主机里面也存了一份一模一样的数据?那这岂不是很浪费吗?

为了解答这个问题,这里就引出了 Elasticsearch 的信息存储机制了。首先解答上面的问题,一台主机宕机了,这台主机里面存的数据依然是可以被访问到的,因为在其他的主机上也有备份,但备份的时候也不是整台主机备份,是分片备份的,那这里就又引出了一个概念——分片。

分片,英文叫做 Shard,顾名思义,分片就是对数据切分成了多个部分。我们知道 Elasticsearch 中一个索引(Index)相当于是一个数据库,如存某网站的用户信息,我们就建一个名为 user 的索引。但索引存储的时候并不是整个存一起的,它是被分片存储的,Elasticsearch 默认会把一个索引分成五个分片,当然这个数字是可以自定义的。分片是数据的容器,数据保存在分片内,分片又被分配到集群内的各个节点里。当你的集群规模扩大或者缩小时, Elasticsearch 会自动的在各节点中迁移分片,使得数据仍然均匀分布在集群里,所以相当于一份数据被分成了多份并保存在不同的主机上。

那这还是没解决问题啊,如果一台主机挂掉了,那么这个分片里面的数据不就无法访问了?别的主机都是存储的其他的分片。其实是可以访问的,因为其他主机存储了这个分片的备份,叫做副本,这里就引出了另外一个概念——副本。

副本,英文叫做 Replica,同样顾名思义,副本就是对原分片的复制,和原分片的内容是一样的,Elasticsearch 默认会生成一份副本,所以相当于是五个原分片和五个分片副本,相当于一份数据存了两份,并分了十个分片,当然副本的数量也是可以自定义的。这时我们只需要将某个分片的副本存在另外一台主机上,这样当某台主机宕机了,我们依然还可以从另外一台主机的副本中找到对应的数据。所以从外部来看,数据结果是没有任何区别的。

一般来说,Elasticsearch 会尽量把一个索引的不同分片存储在不同的主机上,分片的副本也尽可能存在不同的主机上,这样可以提高容错率,从而提高高可用性。

但这时假如你只有一台主机,那不就没办法了吗?分片和副本其实是没意义的,一台主机挂掉了,就全挂掉了。

健康状态

针对一个索引,Elasticsearch 中其实有专门的衡量索引健康状况的标志,分为三个等级:

green,绿色。这代表所有的主分片和副本分片都已分配。你的集群是 100% 可用的。

yellow,黄色。所有的主分片已经分片了,但至少还有一个副本是缺失的。不会有数据丢失,所以搜索结果依然是完整的。不过,你的高可用性在某种程度上被弱化。如果更多的分片消失,你就会丢数据了。所以可把 yellow 想象成一个需要及时调查的警告。

red,红色。至少一个主分片以及它的全部副本都在缺失中。这意味着你现在缺少数据:搜索只能返回部分数据,而分配到这个分片上的写入请求会返回一个异常。

如果你只有一台主机的话,其实索引的健康状况也是 yellow,因为一台主机,集群没有其他的主机可以放置副本,所以说,这就是一个不健康的状态,因此集群也是十分有必要的。

存储空间

另外,既然是群集,那么存储空间肯定也是联合起来的,假如一台主机的存储空间是固定的,那么集群它相对于单个主机也有更多的存储空间,可存储的数据量也更大。

所以综上所述,我们需要一个集群!

了解 Elasticsearch 集群

我们再来了解下集群的结构是怎样的。

首先我们应该清楚多台主机构成了一个集群,每台主机称作一个节点(Node)。

如图就是一个三节点的集群:


image.png

在图中,每个 Node 都有三个分片,其中 P 开头的代表 Primary 分片,即主分片,R 开头的代表 Replica 分片,即副本分片。所以图中主分片 1、2,副本分片 0 储存在 1 号节点,副本分片 0、1、2 储存在 2 号节点,主分片 0 和副本分片 1、2 储存在 3 号节点,一共是 3 个主分片和 6 个副本分片。同时我们还注意到 1 号节点还有个 MASTER 的标识,这代表它是一个主节点,它相比其他的节点更加特殊,它有权限控制整个集群,比如资源的分配、节点的修改等等。

这里就引出了一个概念就是节点的类型,我们可以将节点分为这么四个类型:

主节点:

即 Master 节点。主节点的主要职责是和集群操作相关的内容,如创建或删除索引,跟踪哪些节点是群集的一部分,并决定哪些分片分配给相关的节点。稳定的主节点对集群的健康是非常重要的。默认情况下任何一个集群中的节点都有可能被选为主节点。索引数据和搜索查询等操作会占用大量的cpu,内存,io资源,为了确保一个集群的稳定,分离主节点和数据节点是一个比较好的选择。虽然主节点也可以协调节点,路由搜索和从客户端新增数据到数据节点,但最好不要使用这些专用的主节点。一个重要的原则是,尽可能做尽量少的工作。

数据节点:

即 Data 节点。数据节点主要是存储索引数据的节点,主要对文档进行增删改查操作,聚合操作等。数据节点对 CPU、内存、IO 要求较高,在优化的时候需要监控数据节点的状态,当资源不够的时候,需要在集群中添加新的节点。

负载均衡节点:

也称作 Client 节点,也称作客户端节点。当一个节点既不配置为主节点,也不配置为数据节点时,该节点只能处理路由请求,处理搜索,分发索引操作等,从本质上来说该客户节点表现为智能负载平衡器。独立的客户端节点在一个比较大的集群中是非常有用的,他协调主节点和数据节点,客户端节点加入集群可以得到集群的状态,根据集群的状态可以直接路由请求。

预处理节点:

也称作 Ingest 节点,在索引数据之前可以先对数据做预处理操作,所有节点其实默认都是支持 Ingest 操作的,也可以专门将某个节点配置为 Ingest 节点。

以上就是节点几种类型,一个节点其实可以对应不同的类型,如一个节点可以同时成为主节点和数据节点和预处理节点,但如果一个节点既不是主节点也不是数据节点,那么它就是负载均衡节点。具体的类型可以通过具体的配置文件来设置。

参考资料:
Elasticsearch与jdk版本对应关系
https://blog.csdn.net/ibigboy/article/details/123200685
Elasticsearch-head插件的安装
https://blog.csdn.net/weixin_42326851/article/details/123808774
一文学会elasticsearch-head安装
https://www.jianshu.com/p/56eb977a5f08
安装本地Elasticsearch的IK分词插件和拼音分词插件
https://www.jianshu.com/p/1932a7a7eba8
手把手教你搭建一个Elasticsearch集群
https://www.cnblogs.com/tianyiliang/p/10291305.html
Elasticsearch:设置 Elastic 账户安全
https://blog.csdn.net/UbuntuTouch/article/details/100548174

上一篇下一篇

猜你喜欢

热点阅读