Elasticsearch集群搭建、Kibana、head、IK实战

一、概述

我们为什么要安装 ES 集群?因为生产没人用单机版本,单机版本那都是用来玩的“玩具”。本篇文章基于 Elasticsearch 7.X +CentOS 7.6 版本进行安装(这里劝诫大家不要再去学习 7.X 之前的版本了,这个版本对之前的版本进行了大改),通过本篇学习你能学到如何在 Linux 下安装 ES 集群,如何整合 IK 分词器,如何整合 head 插件,如何整合 Kibana 客户端等。ESjava 开发的,在安装确保 JDK 环境已经配置好了。今天的集群网络拓扑图如下:

IPPORT
192.168.56.1439200
192.168.56.1449200
192.168.56.1459200
二、集群安装

说明:这里就以 192.168.56.143 安装为例,其它两个节点,只要将 143 上的配置文件修改后,通过 scp 传过去进行简单修改即可。

  • Linux 参数配置

    注意:Linux 内核参数的调整并不仅仅在 ES 的安装过程中需要注意,其它中间件产品的安装也同样需要注意调整,例如:MySQL、Oracle、MQ 等

    修改每个进程最大同时打开文件数、最大线程数:

    [root@centos7 es]# vim /etc/security/limits.conf
    在最后添加如下内容(注意 es 表示你的用户名,给哪一个用户设置的意思):
    es soft nofile 65536  # es 用户最大同时打开soft类型文件数
    es hard nofile 65536 # es 用户最大同时打开hard类型文件数
    es soft nproc 4096  # es 用户最大同时打开soft类型线程数
    es hard nproc 4096 # es 用户最大同时打开hard类型线程数
    

    修改最大虚拟内存大小:

    [root@centos7 es]# vim /etc/sysctl.conf
    # 增加如下:
    vm.max_map_count=262144
    # 修改完之后,执行如下命令生效
    [root@centos7 es]# sysctl -p
    
  • 创建 es 用户并授权

    7.X 版本之后不允许使用 root 用户启动 ES 进程,因此需要创建专门用户启动 ES 进程的的用户

    [root@centos7 es]# groupadd es ## 创建 es 用户组
    [root@centos7 es]#  useradd -m -g es es ## 创建用户名为
    [root@centos7 es]#  passwd es  ## 设置 es 用户的密码
    [root@centos7 es]#   visudo  ## 配置 es 用户的sudo 权限在打开的编辑配置中,添加 es	ALL=(ALL)	NOPASSWD:ALL 这行内容 (表示通过es 用户启动的进程时候,授予root的权限,注意:在安装其他中间件时也要习惯用非root用户作为管理用户)
    
  • 下载安装包并解压

    下载安装包我通过华为镜像下载对应的安装包(官网实在太慢~~),在链接地址搜索 Elasticsearch 然后点击进去找到我们本次安装的版本,复制链接地址,通过 wget 命令下载:

    [root@centos7 es]# wget https://repo.huaweicloud.com/elasticsearch/7.8.0/elasticsearch-7.8.0-linux-x86_64.tar.gz
    [root@centos7 es]# mkdir /usr/local/elasticsearch -pv ## 创建目录
    [root@centos7 es]# tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz -c /usr/local/elasticsearch/  ## 解压
    
  • 修改 ES 配置文件与JVM配置
    [root@centos7 es]# cd /usr/local/elasticsearch/elasticsearch-7.8.0/config/
    [root@centos7 config]# vim elasticsearch.yml ## 编辑并修改为如下内容
    # 增加以下内容
    # 集群名称必须相同
    cluster.name: es-app
    node.name: node-1  ## 另外两个节点分别为 node-2  node-3
    # 当前节点是否可以被选举为master节点,是:true、否:false
    node.master: true
    # 当前节点是否用于存储数据,是:true、否:false
    node.data: true
    # 数据和日志存储的地方,建议与es的安装目录区分,方式es删除后数据的丢失,这两个目录后面需要创建并设置好权限
    path.data: /es/data
    path.logs: /es/logs
    # 需求锁住物理内存,是:true、否:false
    bootstrap.memory_lock: false
    # SecComp检测,是:true、否:false
    bootstrap.system_call_filter: false
    network.host: 0.0.0.0
    # 集群必须要配置此项,yourIp修改为自己的本机服务器的外网ip,否则集群失败
    network.publish_host: 192.168.56.143
    # 主机访问的端口号
    http.port: 9200
    # es7.x 之后新增的配置,写入候选主节点的设备地址,在开启服务后可以被选为主节点
    # es7之后,不需要discover.zen.ping.unicast.hosts这个参数,用discovery.seed_hosts替换,脑列控制由集群自主控制
    discovery.seed_hosts: ["192.168.56.143","192.168.56.144","192.168.56.145"]
    # es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举master
    cluster.initial_master_nodes: ["192.168.56.143","192.168.56.144","192.168.56.145"]
    # 是否支持跨域,是:true,在使用head插件时需要此配置
    http.cors.enabled: true
    # "*" 表示支持所有域名
    http.cors.allow-origin: "*"
    

    修改与 ES 配置文件相同目录下的 jvm.options 配置文件,主要用于控制 JVM 的参数配置

    [root@centos7 config]# vim config/jvm.options
    # 按需修改如下内存大小即可
    -Xms1g
    -Xmx1g
    .....
    # 其它配置项都可以进行调整,因为是 java 开发的,你就当做 java 工程去优化 JVM 参数即可,这又涉及到 JVM 的优化内容了,后面我会出相关文章进行介绍
    
  • 创建必要的目录并授权
    [root@centos7 es]# mkdir /es/{data,logs} -pv  ## 创建数据目录和日志目录
    [root@centos7 es]# chown -R es:es /es  ## 授权
    [root@centos7 es]# cd /usr/local/
    [root@centos7 local]# chown -R es:es /es ./elasticsearch  ## 授权
    注意,权限一定要设置成 es 否则启动会报错
    
  • 集群启动

    注意:到这里我们已经配置好了 192.168.56.143 节点的配置,另外两个节点配置步骤类似,唯一不同的是,你需要将 es 的配置文件通过 scp 传到另外两个节点上,修改 node.name 、network.publish_host 配置项内容为指定节点配置即可,这里就不重复了。

    三台节点分别执行如下脚本进行启动,启动没有先后之分,7.X 版本在节点全部启动后会自动选取主节点

    ## 注意,到这里前面的所有操作都在 root 用户下进行,从这一步开始切到 es 用户下启动进程
    [es@centos7 /]# ./usr/local/elasticsearch/elasticsearch-7.8.0/bin/elasticsearch  ## 前台启动方式,如果测试时想在控制台看日志启动情况,可以使用这种方式,但是生产环境肯定不能用这种方式启动
    [es@centos7 /]# ./usr/local/elasticsearch/elasticsearch-7.8.0/bin/elasticsearch  ## 后台启动方式,生产推荐启动方式
    
  • 集群测试
    ## 通过浏览器访问如下地址查看集群启动状态
    http://192.168.56.145:9200/_cat/health?v
    ## 可以看到如下内容
    epoch      timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
    1618143879 12:24:39  es-app  green           3         3      0   0    0    0        0             0                  -                100.0%
    ## 查看集群信息
    [es@centos7 bin]$ curl 192.168.56.143:9200/_cat/nodes
    192.168.56.144 44 96 0 0.03 0.04 0.05 dilmrt - node-2
    192.168.56.145 51 96 0 0.00 0.01 0.05 dilmrt * node-3 ## 带 * 号的表示主节点
    192.168.56.143 59 96 0 0.01 0.02 0.10 dilmrt - node-1
    
    ##检查分片是否都正常
    [es@centos7 bin]$ curl 192.168.56.143:9200/_cat/shards  ## 如果出现分片的状态不是 STARTED 而是UNASSIGNED说明分片不正常,我就遇到过如果磁盘空间使用量超过 85% 集群的状态不是 green 而是 yellow ,此时分片都是 UNASSIGNED 状态,最终将磁盘空间降下来后分片正常了
    .apm-agent-configuration       0 r STARTED  0    208b 192.168.56.143 node-1
    .apm-agent-configuration       0 p STARTED  0    208b 192.168.56.144 node-2
    ilm-history-2-000001           0 p STARTED            192.168.56.145 node-3
    ilm-history-2-000001           0 r STARTED            192.168.56.143 node-1
    .kibana_1                      0 r STARTED 64 120.4kb 192.168.56.145 node-3
    .kibana_1                      0 p STARTED 64 120.4kb 192.168.56.144 node-2
    .apm-custom-link               0 r STARTED  0    208b 192.168.56.143 node-1
    .apm-custom-link               0 p STARTED  0    208b 192.168.56.144 node-2
    .kibana_task_manager_1         0 p STARTED  5  11.1kb 192.168.56.145 node-3
    .kibana_task_manager_1         0 r STARTED  5  11.1kb 192.168.56.143 node-1
    customer                       0 r STARTED  1   3.5kb 192.168.56.145 node-3
    customer                       0 p STARTED  1   3.5kb 192.168.56.144 node-2
    user                           2 p STARTED  1   3.5kb 192.168.56.145 node-3  ## p 表示 primary shard 主分片,前面的数字表示分片的数量
    user                           2 r STARTED  1   3.5kb 192.168.56.143 node-1   ## r 表示 replica shard 副本分片
    user                           1 r STARTED  0    208b 192.168.56.145 node-3
    user                           1 p STARTED  0    208b 192.168.56.144 node-2
    user                           0 r STARTED  0    208b 192.168.56.143 node-1
    user                           0 p STARTED  0    208b 192.168.56.144 node-2
    .kibana-event-log-7.8.0-000001 0 r STARTED  3  15.5kb 192.168.56.143 node-1
    .kibana-event-log-7.8.0-000001 0 p STARTED  3  15.5kb 192.168.56.144 node-2
    
    
  • 问题总结

    问题1:配置文件的权限问题在调整过程中忘记改成 es 用户权限了

    [es@**** bin]$ ./elasticsearch
    Exception in thread "main" 2021-04-03 17:36:23,881 main ERROR No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
    2021-04-03 17:36:24,113 main ERROR Could not register mbeans java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
            at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
            at java.lang.SecurityManager.checkPermission(SecurityManager.java:585)
            at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.checkMBeanTrustPermission(DefaultMBeanServerInterceptor.java:1848)
            at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:322)
            at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
            at org.apache.logging.log4j.core.jmx.Server.register(Server.java:389)
            at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:167)
            at org.apache.logging.log4j.core.jmx.Server.reregisterMBeansAfterReconfigure(Server.java:140)
            at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:556)
            at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:617)
            at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:634)
            at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:229)
            at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:242)
            at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
            at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
            at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:618)
            at org.elasticsearch.common.logging.ESLoggerFactory.getLogger(ESLoggerFactory.java:54)
            at org.elasticsearch.common.logging.ESLoggerFactory.getLogger(ESLoggerFactory.java:62)
            at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:101)
            at org.elasticsearch.ExceptionsHelper.<clinit>(ExceptionsHelper.java:42)
            at org.elasticsearch.ElasticsearchException.toString(ElasticsearchException.java:663)
            at java.lang.String.valueOf(String.java:2994)
            at java.io.PrintStream.println(PrintStream.java:821)
            at java.lang.Throwable$WrappedPrintStream.println(Throwable.java:748)
            at java.lang.Throwable.printStackTrace(Throwable.java:655)
            at java.lang.Throwable.printStackTrace(Throwable.java:643)
            at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1061)
            at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:1052)
            at java.lang.Thread.dispatchUncaughtException(Thread.java:1959)
    SettingsException[Failed to load settings from /usr/local/elasticsearch-5.6.0/config/elasticsearch.yml]; nested: AccessDeniedException[/usr/local/elasticsearch-5.6.0/config/elasticsearch.yml];
    at org.elasticsearch.node.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:102)
    at org.elasticsearch.cli.EnvironmentAwareCommand.createEnv(EnvironmentAwareCommand.java:72)
    at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:67)
    at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:134)
    at org.elasticsearch.cli.Command.main(Command.java:90)
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:91)
    at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:84)
    Caused by: java.nio.file.AccessDeniedException: /usr/local/elasticsearch-5.6.0/config/elasticsearch.yml
    at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
    at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
    at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
    at java.nio.file.Files.newByteChannel(Files.java:361)
    at java.nio.file.Files.newByteChannel(Files.java:407)
    at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
    at java.nio.file.Files.newInputStream(Files.java:152)
    at org.elasticsearch.common.settings.SettingsBuilder.loadFromPath(Settings.java:1032)atorg.elasticsearch.node.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:100)...6more[elk@dockerbin] Builder.loadFromPath(Settings.java:1032)at org.elasticsearch.node.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:100)... 6 more[elk@docker bin]Builder.loadFromPath(Settings.java:1032)atorg.elasticsearch.node.InternalSettingsPreparer.prepareEnvironment(InternalSettingsPreparer.java:100)...6more[elk@dockerbin]
    

    解决办法:

    看到“Caused by: java.nio.file.AccessDeniedException: /usr/local/elasticsearch-7.8.0/config/elasticsearch.yml”的提示,就去检查目录的权限,果然是 root:root 权限,使用 es 用户去启动,就报错了。
    将目录权限改成 es:es ,并且如果 /es/data 、 /es/logs中有内容要删掉,就好了(chown -R es:es elasticsearch-7.8.0 修改目录所有者为 es )

    问题2:elasticsearch7 出现master not discovered yet, this node has not previously joined a bootstrapped (v7+)

    [2021-03-21T15:52:32,213][INFO ][o.e.c.c.ClusterBootstrapService] [node-2] skipping cluster bootstrapping as local node does not match bootstrap requirements: [node-1]
    [2021-03-2-21T15:52:42,220][WARN ][o.e.c.c.ClusterFormationFailureHelper] [node-2] master not discovered yet, this node has not previously joined a bootstrapped (v7+) cluster, and this node must discover master-eligible nodes [node-1] to bootstrap a cluster: have discovered []; discovery will continue using [192.168.56.143:9300] from hosts providers and [{node-2}{nU2Duwl8Su-qZ7N3rcfwWA}{4OqPHF-FRlyb_F7xhYoAow}{192.168.56.143}{192.168.56.143:9300}{ml.machine_memory=67267657728, xpack.installed=true, ml.max_open_jobs=20}] from last-known cluster state; node term 0, last-accepted version 0 in term 0
    

    解决办法:

    从提示信息上看,是网络问题,检查各个节点的 network.publish_host 配置地址是否通

三、IK 分词器安装
  • 插件安装

ik 分词器插件属于中文分词器插件,可以识别一段话中的词,并且可以指定分词策略甚至是自定义分词(例如,通过配置可以实现:八戒取经路就是牛作为一个词来识别,关于自定义分词规则,也是通过配置文件来实现,具体如何实现,自行查阅资料)

[root@centos7 ik]# su - es
[es@centos7 ~]$ cd /usr/local/elasticsearch/elasticsearch-7.8.0/plugins/
[es@centos7 plugins]$ mkdir ik -pv
[es@centos7 plugins]$ cd ik
[es@centos7 ik]$ wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.8.0/elasticsearch-analysis-ik-7.8.0.zip ##下载版本要对应
[es@centos7 ik]$ unzip elasticsearch-analysis-ik-7.8.0.zip ## 解压
  • 验证

所有节点执行上面的插件安装,注意权限,然后启动所有节点,验证分词器是否生效:

  1. 没有指定分词器时的效果,会将搜索的汉字作为一个词

    [root@centos7 ik]# curl -X GET -H "Content-Type: application/json"  "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中华五千年华夏"}';
    {
      "tokens" : [
        {
          "token" : "中",
          "start_offset" : 0,
          "end_offset" : 1,
          "type" : "<IDEOGRAPHIC>",
          "position" : 0
        },
        {
          "token" : "华",
          "start_offset" : 1,
          "end_offset" : 2,
          "type" : "<IDEOGRAPHIC>",
          "position" : 1
        },
        {
          "token" : "五",
          "start_offset" : 2,
          "end_offset" : 3,
          "type" : "<IDEOGRAPHIC>",
          "position" : 2
        },
        {
          "token" : "千",
          "start_offset" : 3,
          "end_offset" : 4,
          "type" : "<IDEOGRAPHIC>",
          "position" : 3
        },
        {
          "token" : "年",
          "start_offset" : 4,
          "end_offset" : 5,
          "type" : "<IDEOGRAPHIC>",
          "position" : 4
        },
        {
          "token" : "华",
          "start_offset" : 5,
          "end_offset" : 6,
          "type" : "<IDEOGRAPHIC>",
          "position" : 5
        },
        {
          "token" : "夏",
          "start_offset" : 6,
          "end_offset" : 7,
          "type" : "<IDEOGRAPHIC>",
          "position" : 6
        }
      ]
    }
    
  2. ik_max_word 和 ik_smart 分词策略

    • ik_max_word: 将文本按最细粒度的组合来拆分,比如会将“中华五千年华夏”拆分为“五千年、五千、五千年华、华夏、千年华夏”,总之是可能的组合;

      [root@centos7 ik]# curl -X GET -H "Content-Type: application/json"  "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中华五千年华夏","analyzer":"ik_smart"}';
      {
        "tokens" : [
          {
            "token" : "中华",
            "start_offset" : 0,
            "end_offset" : 2,
            "type" : "CN_WORD",
            "position" : 0
          },
          {
            "token" : "五千年",
            "start_offset" : 2,
            "end_offset" : 5,
            "type" : "TYPE_CQUAN",
            "position" : 1
          },
          {
            "token" : "华夏",
            "start_offset" : 5,
            "end_offset" : 7,
            "type" : "CN_WORD",
            "position" : 2
          }
        ]
      }
      
    • ik_smart: 最粗粒度的拆分,比如会将“五千年华夏”拆分为“五千年、华夏”

      [root@centos7 ik]# curl -X GET -H "Content-Type: application/json"  "http://localhost:9200/_analyze?pretty=true" -d'{"text":"中华五千年华夏","analyzer":"ik_max_word"}';
      {
        "tokens" : [
          {
            "token" : "中华",
            "start_offset" : 0,
            "end_offset" : 2,
            "type" : "CN_WORD",
            "position" : 0
          },
          {
            "token" : "五千",
            "start_offset" : 2,
            "end_offset" : 4,
            "type" : "TYPE_CNUM",
            "position" : 1
          },
          {
            "token" : "千年",
            "start_offset" : 3,
            "end_offset" : 5,
            "type" : "CN_WORD",
            "position" : 2
          },
          {
            "token" : "年华",
            "start_offset" : 4,
            "end_offset" : 6,
            "type" : "CN_WORD",
            "position" : 3
          },
          {
            "token" : "年",
            "start_offset" : 4,
            "end_offset" : 5,
            "type" : "COUNT",
            "position" : 4
          },
          {
            "token" : "华夏",
            "start_offset" : 5,
            "end_offset" : 7,
            "type" : "CN_WORD",
            "position" : 5
          }
        ]
      }
      
四、head 插件安装

head 插件就相当于 ES 的客户端工具,通过它,可以管理、操作 ES 中的数据。

  • 安装 node 环境

    [root@centos7 ~]# wget https://npm.taobao.org/mirrors/node/v14.16.0/node-v14.16.0-linux-x64.tar.xz ## 下载 nodejs
    [root@centos7 ~]# yum -y install xz ##下载下来的jar包是xz格式的,一般的linux可能不识别,还需要安装xz.
    [root@centos7 ~]# xz -d node-v14.16.0-linux-x64.tar.xz ## 会在当前目录下生成一个 tar.gz 的包
    [root@centos7 ~]# mkdir /usr/local/node-v14 -pv
    [root@centos7 ~]# tar -xvf node-v14.16.0-linux-x64.tar.gz  -C /usr/local/node-v14/
    ## 配置环境变量
    [root@centos7 ~]# vim /etc/profile.d/nodejs.sh
    ## 添加如下内容
    export NODE_HOME=/usr/local/node-v14/node-v14.16.0-linux-x64
    export PATH=$PATH:$NODE_HOME/bin
    ## 重读环境变量
    [root@centos7 ~]#source /etc/profile
    ##测试 node 是否生效
    [root@centos7 ~]# node -v
    [root@centos7 ~]# npm -v
    ##安装cnpm
    [root@centos7 ~]# npm install -g cnpm --registry=https://registry.npm.taobao.org
    ## 安装 grunt 工具,用于打包、压缩、测试、执行等工作
    [root@centos7 ~]# cnpm install -g grunt-cli
    [root@centos7 ~]# grunt -version ## 查看是否安装成功
    
  • 安装 head

    访问地址https://codechina.csdn.net/mirrors/mobz/elasticsearch-head?utm_source=csdn_github_accelerator,下载
    [root@centos7 ~]# wget https://codechina.csdn.net/mirrors/mobz/elasticsearch-head/-/archive/master/elasticsearch-head-master.zip
    [root@centos7 ~]# mkdir /usr/local/elasticsearch-head
    [root@centos7 ~]# tar -xvf elasticsearch-head-master.zip -C /usr/local/elasticsearch-head
    [root@centos7 ~]# cd /usr/local/elasticsearch-head/elasticsearch-head-master
    [root@centos7 ~]# cnpm install ## 安装
    
  • 运行 head

    [root@centos7 ~]# grunt server ## 启动
     或者在head的主目录中运行npm run start也是可以运行head的
     在浏览器上输入:http://192.168.56.143:9100/ 即可访问
    
五、Kibana 安装

KibanaELK 中的 K , 它是 ES 的客户端,通过该工具可以很方便基于 Rest API 操作 ES 实例,接下来简单介绍安装过程。

[root@centos7 ~]# wget https://artifacts.elastic.co/downloads/kibana/kibana-7.8.0-linux-x86_64.tar.gz ## 下载安装包,注意安装包要和 ES 保持一致
[root@centos7 ~]# mkdir /usr/local/kibana
[root@centos7 ~]# tar -zxvf kibana-7.8.0-linux-x86_64.tar.gz -C /usr/local/kibana
[root@centos7 ~]#  su - es
[es@centos7 ~]# cd /usr/local/
[es@centos7 local]# chown -R es:es ./kibana ## 修改权限
[es@centos7 local]# vim ./kibana/kibana-7.8.0-linux-x86_64/config/kibana.yml ## 修改配置
# 放开注释,将默认配置改成如下:
server.port: 5601
server.host: "0.0.0.0"
#elasticsearch.url: "http://你的服务器外网ip(your domain):9200"
elasticsearch.hosts: ["http://192.168.56.143:9200"]  ## 连接的节点
kibana.index: ".kibana"
i18n.locale: "zh-CN"  ## 修改为中文

打开浏览器,访问 http://192.168.56.143:9200 :

image-20210412211527798

具体的 ES 的 CRUD API 咋使用,后面我再计划出一篇文章介绍。

六、安装 cerebro 集群管理工具

cerebroheadKibana 客户端类似,它能更直观的验证和管理集群,cerebro 界面我觉得比较炫酷些。

下载 cerebro

image-20210412215347355 image-20210412215316444

安装过程直接 unzip 解压,然后进入 cerebro/bin 运行 ./cerbro 启动,在浏览器中访问 http://192.168.56.143:9000 最终显示的结果如下:

image-20210412220123982 image-20210412220302829
七、验证 ES 集群故障转移

注意实心星号的是主节点,我们尝试将 192.168.56.145 节点服务关闭,验证,主节点是否进行重新选举,并再次启动 192.168.56.145,看看是否变成候选节点:

image-20210412220605588

发现 192.168.56.143 变成了主节点,然后启动 192.168.56.145,验证其是否变成了候选节点

image-20210412220937330

验证结果,和自己理解的一致。

默认创建索引时,如果不指定分片数量,和分片副本数量,默认只会创建一个分片,没有副本,所以为了保证高可用,在创建索引时候需要指定分片数和副本数:

## 定义创建索引的模板,凡是以 user 名称开头的索引都会根据这个模板规则去创建索引
## 在 Kibana 上执行
POST _template/template_1
{
  "index_patterns": [
    "user*"
  ],
  "settings": {
    "number_of_shards": 3,  ## 三个主分片
    "number_of_replicas": 1 ## 每一个主分片一个副本
  }
}
## 在 Kibana 上执行创建索引,创建后的索引包含 3 个主分片,每一个主分片有一个副本分片
PUT /user

然后关闭主节点,发现另外两个节点还有完整的3个分片可以用,关闭从节点,发现也一样(注意在做验证时,需要多刷新几次,默认30秒分片才会切换完成,虚线的表示副本分片,实线的表示主分片)

image-20210412231449712

八、开机启动脚本及配置

ES 开机启动配置

在**/etc/init.d目录下新建文件elasticsearch**,添加如下内容:

#!/bin/sh
#chkconfig:
#description: elasticsearch
 
export JAVA_HOME=/usr/local/java/jdk1.8.0_112
export JAVA_BIN=/usr/local/java/jdk1.8.0_112/bin
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME JAVA_BIN PATH CLASSPATH
 
case "$1" in
start)
#下面的“<<!”是切换用户后,待执行的命令,执行完后使用“!”来进行结束
    su es<<!
    cd 你的elasticsearch主目录
    ./bin/elasticsearch -d
!
#上面的“!”是以上面的对应起来,并且顶格放置,这是语法
    echo "elasticsearch startup" #将该行替换成你自己的服务启动命令
    ;;  
stop)
    es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
    kill -9 $es_pid
    echo "elasticsearch stopped"
    ;;  
restart)
    es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
    kill -9 $es_pid
    echo "elasticsearch stopped"
    su es<<!
    cd 你的elasticsearch主目录
    ./bin/elasticsearch -d
!
    echo "es startup"
    ;;  
*)
    echo "start|stop|restart"
    ;;  
esac
 
exit $?
chmod +x elasticsearch 添加执行权限记得是给 es
sudo chkconfig --add /etc/init.d/elasticsearch ## 加入开启启动项
以后使用service elasticsearch start/stop/restart来管理服务

elasticsearch-head 开机启动配置

在**/etc/init.d目录下新建文件elasticsearch_head**,添加如下内容:

#!/bin/sh
#chkconfig: 2345 80 05
#description: es_head_run
 
export NODE_PATH=/home/soft/node-v8.11.1-linux-x64
export PATH=$PATH:$NODE_PATH/bin
#如果centOs使用yum命令安装了nodejs就不用输入上面两行export语句了,因为yum安装自动配置好了
 
cd /home/soft/elasticsearch-head-master
nohup npm run start >/home/soft/elasticsearch-head-master/nohup.out 2>&1 &
chmod +x elasticsearch_head 添加执行权限记得是给 es
sudo chkconfig --add /etc/init.d/elasticsearch_head ## 加入开启启动项
以后使用service elasticsearch_head start/stop/restart来管理服务
九、参考文献

参考文献1

十、下篇预告

《基于 Kibana 客户端操作 ES -增删改查API篇》

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值