ElasticSearch Linux单点和集群配置

ES学习

第一篇:ElasticSearch 邂逅ES
第二篇:ElasticSearch入门
第三篇:ElasticSearch JavaAPI操作
第四篇:Windows单点和集群配置
第五篇:Linux单点和集群配置
第六篇:ElasticSearch核心概念
第七篇:ElasticSearch系统架构
第八篇:ElasticSearch分布式集群
第九篇:ElasticSearch路由计算

Part4:ElasticSearch环境

4.1 相关概念

4.1.1 单机&集群
  • 单台ElasticSearch服务器提供服务,往往都有最大的负载能力,超过这个阈值,服务器的性能就会大大降低甚至不可用,所以生产环境中,一般都是运行在指定服务器集群中。
  • 除了负载能力,单点服务器也存在其他问题
    • 单台机器存储容量有限
    • 单服务器容易出现单点故障,无法实现高可用
    • 单服务的并发处理能力有限
  • 配置服务器集群时,集群中节点数量没有限制,大于等于2个节点就可以看做是集群了。一般出于高性能及高可用方面来考虑集群中节点数量都是3个以上
4.1.2 集群Cluster
  • 一个集群就是由一个或多个服务器节点组织在一起,共同持有整个数据,并一起提供索引和搜索功能。一个ElasticSearch集群有一个唯一的名字标识,这个名字默认是“elasticsearch”。这个名字是很重要的,因为一个节点只能通过指定某个集群的名字来加入这个集群。
4.1.3 节点Node
  • 集群中包含很多服务器,一个节点就是其中的一个服务器。作为集群的一部分,它存储数据,参与集群的索引和搜索功能。
  • 一个节点也是由一个名字来标识的,默认情况下,这个名字是一个随机的漫威漫画角色的名字,这个名字会在启动的时候赋予节点。这个名字对于管理工作来说挺重要的,因为在这个管理过程中,你会去确定网络中哪些服务器对应于ElasticSearch集群中的哪些节点。
  • 一个节点可以通过配置集群名称的方式来加入一个指定的集群默认情况下,如果你得网络中没有运行任何ElasticSearch节点,这时启动任意节点都会被安排加入到一个叫做“elasticsearch”的集群中,这意味着,如果你在你的网络中启动了若干个节点,并假定他们能够互相发现彼此,他们将自动的形成并加入到一个叫做“elasticsearch”的集群中。
  • 在一个集群中,只要你想,可以拥有任意多个节点。

4.2 Linux单机

  • 使用VMWare15 Pro虚拟机
    • 链接:https://pan.baidu.com/s/1FYuUxFIYo7ARUUYWMyv1AA 提取码:gqru
  • 使用CentOS7镜像
    • 链接:https://pan.baidu.com/s/16mKfHgby5jlPk9GMxnCnhw 提取码:egaf
4.2.1 软件下载
  • ES地址:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-8-0
    • image-20210525181451330
4.2.2 软件安装
  • 解压软件

    • 将下载的包上传到Linux,然后解压

      tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz
      
    • 改名

      mv elasticsearch-7.8.0 es
      
    • image-20210526095509775

  • 创建用户

    • 因为安全问题,ElasticSearch不允许Linux中的root用户直接运行,所以需要创建新用户,在root用户中创建新用户

      useradd es # 新增es用户(名称可以自定义)
      passwd es # 为es设置密码
      
      # 如果设置错了,可以删除再进行
      userdel -r es # 删除
      chown -R es:es /opt/module/software/es # 设置es用户对该文件夹的操作权限
      
    • 在这里插入图片描述

  • 修改配置文件

    • ./config/elasticsearch.yml(可直接在末尾添加)

      cluster.name: test-elasticsearch-cluster # 配置集群名称(有默认值)
      node.name: node01 # 配置当前节点名称
      network.host: 0.0.0.0 # 配置IP地址
      http.port: 9200	# 配置端口号
      cluster.initial_master_nodes: ["node01"] # 指定集群中存在的节点名称集合
      
    • 修改/etc/security/limits.conf

      # 设置每个进程可以打开的文件数的限制
      es		 soft    nofile          65536
      es       hard	 nofile          65536
      
    • 修改/etc/security/limits.d/20-nproc.conf

      # 设置每个进程可以打开的文件数的限制
      es	 soft    nofile          65536
      es   hard	 nofile          65536
      # 操作系统级别对每个用户创建的进程数的限制
      *	 hard	 nproc		 4096
      # * 代表Linux所有用户名称
      
    • 修改/etc/sysctl.conf

      # 一个进程可以拥有的VMA(虚拟几寸区域)的数量,默认值为65536
      vm.max_map_count=655360
      
    • 重新加载(直接命令行执行即可)

      sysctl -p
      
4.2.3 启动软件
  • 使用ES用户启动

    • image-20210527131422430
  • 启动时,会动态生成文件,如果文件所属用户不匹配,就会发生错误,需要重新进行修改用户和用户组

    • ES不允许使用root用户进行启动
  • 使用su命令,切换到之前创建的ES用户,然后重新启动

    • image-20210527131622528
  • 发现又出了问题:AccesDeniedException异常

    • image-20210527131842549
    • 这是因为ES在启动过程中会动态生成一些文件,但是生成的文件对于es用户来说,没有权限来操作,所以我们需要重新执行一下授权命令
  • 执行授权命令(需要切换到root用户来执行)

    chown -R es:es /opt/module/software/es
    
    • image-20210527132333252
  • 再次启动(切换到es用户)

    • 启动成功
    • image-20210527133152789
    • 这里发现,es的启动后会占用当前窗口,我们可以选择后台启动
      • bin/elasticsearch -d
4.2.4 软件测试
  • 使用浏览器访问:http://你的LinuxIP地址:9200/

    • image-20210527132823791
    • 发现浏览器一直未响应,因为Linux的防火墙拦截了
  • 简单方式

    • 可以直接关闭防火墙或者开放对应端口,这里选择关闭

      # 暂时关闭防火墙,下次开机自动恢复
      systemctl stop firewalld
      # 永久关闭(重启不复原)
      systemctl disable firewalld.service
      # 永久开启
      systemctl enable firewalld.service
      
    • image-20210527133237707

    • 执行关闭防火墙时,使用的不是root用户,需要输入几次密码才行

  • 再次浏览器测试

    • image-20210527134036535

4.3 Linux集群

  • 准备环境:三台Linux,然后写一下xsync脚本(作用就是用来在不同Linux上分发文件),也可以不写,直接拷贝三份也可以
4.3.1 软件下载
  • ES地址:https://www.elastic.co/cn/downloa,ds/past-releases/elasticsearch-7-8-0
    • image-20210525181451330
4.3.2 软件安装
4.3.2.1 解压软件
  • 解压后改名,然后分发到其他机器(02和03)

    # 解压缩
    tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz
    # 改名
    mv elasticsearch-7.8.0 es-cluster
    # 分发文件(输入确认信息yes和密码即可)
    xsync es-cluster
    
4.3.2.2 创建用户
  • 由于安全问题,ES不允许root用户直接运行,在其他两个节点分别创建新用户,这里改名为es

    useradd es # 新增es用户
    passwd es # 设置密码
    userdel -r es # 如果设置错误可以删除
    chown -R es:es /opt/module/software/es-cluster # 设置es用户对文件的访问权限,根据自己路径动态修改
    
4.3.2.3 修改配置文件
  • 修改一个机器的文件后,进行分发即可,然后在其他节点上稍作修改就行

  • 修改es-cluster/config/elasticsearch.yml,然后分发文件

    # ======================== Elasticsearch Configuration =========================
    #
    # NOTE: Elasticsearch comes with reasonable defaults for most settings.
    #       Before you set out to tweak and tune the configuration, make sure you
    #       understand what are you trying to accomplish and the consequences.
    #
    # The primary way of configuring a node is via this file. This template lists
    # the most important settings you may want to configure for a production cluster.
    #
    # Please consult the documentation for further information on configuration options:
    # https://www.elastic.co/guide/en/elasticsearch/reference/index.html
    #
    # ---------------------------------- Cluster -----------------------------------
    #
    # Use a descriptive name for your cluster:
    #
    #cluster.name: my-application
    # 配置集群名称
    cluster.name: test-cluster
    #
    # ------------------------------------ Node ------------------------------------
    #
    # Use a descriptive name for the node:
    #
    #node.name: node-1
    # 配置节点信息,每个节点名称不能重复
    node.name: node01
    # 是否有资格作为主节点和数据节点
    node.master: true
    node.data: true
    #
    # Add custom attributes to the node:
    #
    #node.attr.rack: r1
    #
    # ----------------------------------- Paths ------------------------------------
    #
    # Path to directory where to store the data (separate multiple locations by comma):
    #
    #path.data: /path/to/data
    #
    # Path to log files:
    #
    #path.logs: /path/to/logs
    #
    # ----------------------------------- Memory -----------------------------------
    #
    # Lock the memory on startup:
    #
    #bootstrap.memory_lock: true
    #
    # Make sure that the heap size is set to about half the memory available
    # on the system and that the owner of the process is allowed to use this
    # limit.
    #
    # Elasticsearch performs poorly when the system is swapping the memory.
    #
    # ---------------------------------- Network -----------------------------------
    #
    # Set the bind address to a specific IP (IPv4 or IPv6):
    #
    #network.host: 192.168.0.1
    # ip地址,每个节点不相同
    network.host: 192.168.111.10
    #
    # Set a custom port for HTTP:
    #
    #http.port: 9200
    # 设置端口号
    http.port: 9200
    #
    # For more information, consult the network module documentation.
    #
    # --------------------------------- Discovery ----------------------------------
    #
    # Pass an initial list of hosts to perform discovery when this node is started:
    # The default list of hosts is ["127.0.0.1", "[::1]"]
    # es7.x之后新增的配置,节点发现
    discovery.seed_hosts: ["192.168.111.10:9300", "192.168.111.11:9300","192.168.111.12:9300"]
    #
    # Bootstrap the cluster using an initial set of master-eligible nodes:
    # es7.x之后新增配置,初始化一个新的集群时需要此配置来选举master
    cluster.initial_master_nodes: ["node01"]
    #
    # For more information, consult the discovery and cluster formation module documentation.
    #
    # ---------------------------------- Gateway -----------------------------------
    #
    # Block initial recovery after a full cluster restart until N nodes are started:
    #
    gateway.recover_after_nodes: 2
    network.tcp.keep_alive: true
    network.tcp.no_delay: true
    transport.tcp.compress: true
    #
    # For more information, consult the gateway module documentation.
    #
    # ---------------------------------- Various -----------------------------------
    #
    # Require explicit names when deleting indices:
    #
    #action.destructive_requires_name: true
    
    
    # head插件需要打开这两个设置
    http.cors.allow-origin: "*"
    http.cors.enabled: true
    http.max_content_length: 200mb
    
    # 集群内同时启动的数据任务个数,默认是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
    
    
    • 然后执行xsync分发到其他机器,然后修改节点名称和ip地址即可
  • 修改/etc/security/limits.conf,然后分发文件

    es               soft    nofile          65536
    es               hard    nofile          65536
    
  • 修改/etc/security/limits.d/20-nproc.conf,然后分发文件

    # 每个进程可以打开的文件数的限制
    es       soft    nofile          65536
    es       hard    nofile          65536
    # 操作系统级别对每个用户创建的进程数的限制
    *        hard    nproc           4096
    # * 代表Linux的所有用户名称
    
    
  • 修改/etc/sysctl.conf,然后分发文件

    # 一个进程可以拥有的VMA(虚拟内存区域)的数量,默认值为65536
    vm.max_map_count=655360
    
  • 在每个机器上重新加载,需要切换root用户

    sysctl -p
    
4.3.3 启动软件
  • 分别在三个机器上启动ES
  • 遇到问题
    • image-20210527164754272
    • elasticsearch.yml有问题,配置K:V 值的时候冒号后面要有一个空格
  • 启动成功
    • image-20210527180707083
    • image-20210527180724376
    • image-20210527180733176
4.3.4 测试集群
  • 浏览器访问:http://192.168.111.10:9200/_cat/nodes
    • ip根据自己Linux决定
      image-20210527180852956
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值