Linux安装单机和集群Elasticsearch

Linux单机版本

一、安装JDK

网上教程很多,这里就不做过多介绍。

二、安装Elasticsearch

1. 创建elasticsearch目录

cd /usr/local/
mkdir tool
cd tool
mkdir elasticsearch
cd elasticsearch

2. 下载Elasticsearch

  • 在刚刚创建好的文件夹内下载Elasticsearch(以下简称es)

    curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.2.tar.gz
    
  • 解压es包

    tar -xvf elasticsearch-5.4.2.tar.gz
    
  • 进入es/bin包

    cd elasticsearch-5.4.2/bin
    
  • 启动es

    ./elasticsearch
    

三、解决遇到的错误

1. 第一个错误(内存不够)

  • 直接启动,遇到如图问题,如下:
    在这里插入图片描述

  • 这个问题是由于内存分配不够造成的,修改适合本机的内存,修改文件config/jvm.options

    vi ../config/jvm.options
    
  • 修改如下位置(由于我的服务器内存较小,修改为512m,具体可以根据情况修改)
    在这里插入图片描述

  • 修改后在次启动:

    ./elasticsearch
    

2. 第二个错误(root用户下启动错误)

  • 出现如下错误:

    org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
    

    如图:
    在这里插入图片描述

  • 这个问题很明显,不允许使用root用户启动,那么我们新建一个es用户,并赋予权限:

  • 添加es用户

    useradd es
    
  • 添加密码

    passwd es
    
  • 将文件夹elasticsearch-5.4.2赋予es权限

    chown -R es:es /usr/local/tool/elasticsearch/elasticsearch-5.4.2
    
  • 切换es用户

    su es
    
  • 再次启动

    ./elasticsearch
    
  • 这次启动成功了,我们在使用一个窗口登录root用户,输入命令:

    curl -X GET http://localhost:9200
    
  • 如图所示,即成功:
    在这里插入图片描述

3. 第三个错误(浏览器访问拒绝连接)

  • 在浏览器访问http://118.24.242.170:9200/拒绝访问(118.24.242.170为服务器ip)

  • 使用root用户,打开elasticsearch.yml文件,如下:

    vi /usr/local/tool/elasticsearch/elasticsearch-5.4.2/config/elasticsearch.yml
    
  • 文件内增加如下代码

    network.host: 0.0.0.0
    
  • 要是添加完启动,发现出现了如下错误,得到错误信息如图
    在这里插入图片描述

  • 使用root用户打开如下文件:

    vi elasticsearch.yml
    
  • 在Discovery栏下加上cluster.initial_master_nodes: ["node-1"]
    在这里插入图片描述

  • 使用es用户启动,发现又出现了错误如下,得到错误信息如图
    在这里插入图片描述

  • 使用root用户打开如下文件:

    vim /etc/sysctl.conf
    
  • 添加如下配置:

    vm.max_map_count = 655360
    
  • 使配置生效

    /sbin/sysctl -p
    
  • 然后使用es用户启动Elasticsearch,这次可以成功启动了,如果需要后台启动的话,在启动命令后加&,如下所示:

    ./elasticsearch &
    

Linux集群版本

搭建集群节点前,首先了解下节点的三个角色:

  • 主结点:master节点主要用于集群的管理及索引 比如新增结点、分片分配、索引的新增和删除等。
  • 数据结点:data 节点上保存了数据分片,它负责索引和搜索操作。
  • 客户端结点:client 节点仅作为请求客户端存在,client的作用也作为负载均衡器,client 节点不存据,只是将请求均衡转发到其它结点。

通过下边两项参数来配置结点的功能:

  • node.master: #是否允许为主结点
  • node.data: #允许存储数据作为数据结点
  • node.ingest: #是否允许成为协调节点

这两种选项具有4总组合方式:

  • master=true,data=true:即是主结点又是数据结点
  • master=false,data=true:仅是数据结点
  • master=true,data=false:仅是主结点,不存储数据
  • master=false,data=false:即不是主结点也不是数据结点,此时可设置ingest为true表示它是一个客户

1. 创建elasticsearch目录

cd /usr/local/
mkdir tool
cd tool
mkdir elasticsearch
cd elasticsearch

2. 创建用户(每一台机器都需要配置)

  • 因为安全问题,Elasticsearch 不允许 root 用户直接运行,所以要在每个节点中创建新用
    户,在 root 用户中创建新用户

    useradd es #新增 es 用户
    passwd es #为 es 用户设置密码
    
    userdel -r es #如果错了,可以删除再加
    
    chown -R es:es /usr/local/tool/elasticsearch/ #文件夹所有者
    
  • 修改/etc/security/limits.conf

    # 在文件末尾中增加下面内容
    es soft nofile 65536
    es hard nofile 65536
    # es 是Linux下的用户名 
    # * 代表Linux下的所有用户
    
  • 修改/etc/sysctl.conf

    # 在文件中增加下面内容
    vm.max_map_count=655360
    
  • 配置生效

    sysctl -p
    

3. 配置Elasticsearch集群

  • 在刚刚创建好的文件夹内下载Elasticsearch(以下简称es)

    curl -L -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.2.tar.gz
    
  • 解压es包

    tar -xvf elasticsearch-5.4.2.tar.gz  -C es_cluster_9200
    
  • 分发文件(我这里就有一台机器来模拟)

    cp -r es_cluster_9200 es_cluster_9300
    cp -r es_cluster_9200 es_cluster_9400
    
  • 修改 es_cluster_9200 /usr/local/tool/elasticsearch/es_cluster_9200/config/elasticsearch.yml文件

    # 加入如下配置
    # #集群名称
     cluster.name: cluster-es
    # #节点名称,每个节点的名称不能重复
     node.name: node-1
    # #ip 地址,每个节点的地址不能重复
     network.host: 0.0.0.0
    # #是不是有资格主节点
     node.master: true
     node.data: true
     #要是用多台机器端口可以相同
     http.port: 9200
    #tcp 监听端口 #要是用多台机器监听端口也可以相同
     transport.tcp.port: 9201
    # # head 插件需要这打开这两个配置
     http.cors.allow-origin: "*"
     http.cors.enabled: true
     http.max_content_length: 200mb
    # #es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举 master
     cluster.initial_master_nodes: ["node-1","node-2","node-3"]
    # #es7.x 之后新增的配置,节点发现
     discovery.seed_hosts: ["localhost:9201","localhost:9301","localhost:9401"]
     #要是用多台机器的可以配这个(tcp 监听端口用一样的)
     #discovery.seed_hosts: ["localhost:9201","localhost:9201","localhost:9201"]
     gateway.recover_after_nodes: 2
     network.tcp.keep_alive: true
     network.tcp.no_delay: true
     transport.tcp.compress: true
    # #集群内同时启动的数据任务个数,默认是 2 个
     cluster.routing.allocation.cluster_concurrent_rebalance: 16
    # #添加或删除节点及负载均衡时并发恢复的线程个数,默认 4 个
     cluster.routing.allocation.node_concurrent_recoveries: 16
    # #初始化数据恢复时,并发恢复线程的个数,默认 4 个
     cluster.routing.allocation.node_initial_primaries_recoveries: 16
    #
    
  • 修改 es_cluster_9300 /usr/local/tool/elasticsearch/es_cluster_9300/config/elasticsearch.yml文件

    # 加入如下配置
    # #集群名称
     cluster.name: cluster-es
    # #节点名称,每个节点的名称不能重复
     node.name: node-2
    # #ip 地址,每个节点的地址不能重复
     network.host: 0.0.0.0
    # #是不是有资格主节点
     node.master: true
     node.data: true
     #要是用多台机器端口可以相同
     http.port: 9300
    #tcp 监听端口 #要是用多台机器监听端口也可以相同
     transport.tcp.port: 9301
    # # head 插件需要这打开这两个配置
     http.cors.allow-origin: "*"
     http.cors.enabled: true
     http.max_content_length: 200mb
    # #es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举 master
     cluster.initial_master_nodes: ["node-1","node-2","node-3"]
    # #es7.x 之后新增的配置,节点发现
     discovery.seed_hosts: ["localhost:9201","localhost:9301","localhost:9401"]
     #要是用多台机器的可以配这个(tcp 监听端口用一样的)
     #discovery.seed_hosts: ["localhost:9201","localhost:9201","localhost:9201"]
     gateway.recover_after_nodes: 2
     network.tcp.keep_alive: true
     network.tcp.no_delay: true
     transport.tcp.compress: true
    # #集群内同时启动的数据任务个数,默认是 2 个
     cluster.routing.allocation.cluster_concurrent_rebalance: 16
    # #添加或删除节点及负载均衡时并发恢复的线程个数,默认 4 个
     cluster.routing.allocation.node_concurrent_recoveries: 16
    # #初始化数据恢复时,并发恢复线程的个数,默认 4 个
     cluster.routing.allocation.node_initial_primaries_recoveries: 16
    #
    
  • 修改 es_cluster_9400 /usr/local/tool/elasticsearch/es_cluster_9400/config/elasticsearch.yml文件

    # 加入如下配置
    # #集群名称
     cluster.name: cluster-es
    # #节点名称,每个节点的名称不能重复
     node.name: node-3
    # #ip 地址,每个节点的地址不能重复
     network.host: 0.0.0.0
    # #是不是有资格主节点
     node.master: true
     node.data: true
     #要是用多台机器端口可以相同
     http.port: 9400
    #tcp 监听端口 #要是用多台机器监听端口也可以相同
     transport.tcp.port: 9401
    # # head 插件需要这打开这两个配置
     http.cors.allow-origin: "*"
     http.cors.enabled: true
     http.max_content_length: 200mb
    # #es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举 master
     cluster.initial_master_nodes: ["node-1","node-2","node-3"]
    # #es7.x 之后新增的配置,节点发现
     discovery.seed_hosts: ["localhost:9201","localhost:9301","localhost:9401"]
     #要是用多台机器的可以配这个(tcp 监听端口用一样的)
     #discovery.seed_hosts: ["localhost:9201","localhost:9201","localhost:9201"]
     gateway.recover_after_nodes: 2
     network.tcp.keep_alive: true
     network.tcp.no_delay: true
     transport.tcp.compress: true
    # #集群内同时启动的数据任务个数,默认是 2 个
     cluster.routing.allocation.cluster_concurrent_rebalance: 16
    # #添加或删除节点及负载均衡时并发恢复的线程个数,默认 4 个
     cluster.routing.allocation.node_concurrent_recoveries: 16
    # #初始化数据恢复时,并发恢复线程的个数,默认 4 个
     cluster.routing.allocation.node_initial_primaries_recoveries: 16
    #
    
  • 启动每一个结点

    #加 & 后台启动
    su es
    ./es_cluster_9200/bin/elasticsearch &
    ./es_cluster_9300/bin/elasticsearch &
    ./es_cluster_9400/bin/elasticsearch &
    
  • 测试GET linux1:9200/_cluster/health 或者linux1:9200/_cat/nodes

  • 在这里插入图片描述

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值