---
title: "CentOS7 安装 elasticsearch 集群"
date: 2019-10-30T01:19:09+08:00
lastmod: 2019-10-30T01:19:09+08:00
keywords: []
tags: ["elasticsearch", "centos"]
categories: ["database"]
---

## 环境

主机名 | IP | 操作系统 | ES 版本
---- | ---- | ---- | ----
es227 | 192.168.1.227 | CentOS7.5 | 6.5.4
es228 | 192.168.1.228 | CentOS7.5 | 6.5.4
es229 | 192.168.1.229 | CentOS7.5 | 6.5.4

- 下载 [elasticsearch-6.5.4.tar.gz](https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.tar.gz)

## 各节点初始配置

- 关闭 selinux、防火墙
- 部署 java 运行环境
- 创建 elastic 用户
    ```
    useradd -m elastic
    ```
- 创建数据目录
    ```
    cd /var/lib
    mkdir elasticsearch
    chown elastic.elastic elasticsearch
    ```
- 创建日志目录
    ```
    cd /var/log
    mkdir -p elasticsearch
    chown elastic.elastic elasticsearch
    ```
- 增加 sysctl.conf 配置，执行 sysctl -p 生效
    ```
    vm.max_map_count = 262144
    ```
- 增加 /etc/security/limits.conf 配置
    ```
    elastic soft nofile 65536
    elastic hard nofile 65536
    elastic soft memlock unlimited
    elastic hard memlock unlimited
    ```

## 部署 ELASTICSEARCH

- 登陆 es227，下载 elasticsearch，解压至 /opt/ 下
- 修改 elasticsearch 目录的权限
    ```
    chown -R elastic.elastic /opt/elasticsearch
    ```
- 修改 jvm 参数文件 /opt/elasticsearch/config/jvm.options
- 修改 /opt/elsaticsearch/config/elasticsearch.yml
    ```
    cluster.name: TEST_ES_CLUSTER
    node.name: es227
    network.host: 192.168.1.227
    path.data: /var/lib/elasticsearch
    path.logs: /var/log/elasticsearch
    bootstrap.memory_lock: true
    discovery.zen.ping.unicast.hosts: ["192.168.1.227:9300", "192.168.1.228:9300", "192.168.1.229:9300"]
    discovery.zen.minimum_master_nodes: 2
    gateway.recover_after_nodes: 2
    ```
- 打包 elasticsearch 目录，复制到 es228 和 es229 上，并修改 elasticsearch.yml
    ```
    # es228
    node.name: es228
    network.host: 192.168.1.228
    # es229
    node.name: es229
    network.host: 192.168.1.229
    ```

## 启动集群(两种启动方式)

- 直接启动二进制
    - 在每个节点上启动 elasticsearch 服务
        ```bash
        su - elastic -c '/opt/elasticsearch/bin/elasticsearch -d'
        ```
- systemd 启动
    - 创建文件 /usr/lib/systemd/system/elasticsearch.service，内容如下
        ```
        [Unit]
        Description=ElasticSearch
        Requires=network.service
        After=network.service
        [Service]
        User=elastic
        Group=elastic
        LimitNOFILE=65536
        LimitMEMLOCK=infinity
        Environment=JAVA_HOME=/opt/jre
        ExecStart=/opt/elasticsearch/bin/elasticsearch
        SuccessExitStatus=143
        [Install]
        WantedBy=multi-user.target
        ```
    - 启动 elasticsearch 服务
        ```bash
        systemctl daemon-reload
        systemctl start elasticsearch
        ```

## 查看集群状态

- 查看集群节点状态
    ```
    # 查看节点状态
    curl http://192.168.1.228:9200/_cat/nodes?pretty
    # 查看集群状态
    curl http://192.168.1.228:9200/_cluster/state?pretty
    ```

## 安装分词插件

- 登陆 es227，下载插件 [elasticsearch-analysis-ik-6.5.4.zip](https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip) 至根目录下
- 复制该文件至 es228 和 es229 的根目录下
- 每个节点上安装
    ```
    su - elastic
    /opt/elasticsearch/bin/elasticsearch-plugin install file:///elasticsearch-analysis-ik-6.5.4.zip
    ```
- 配置远程扩展字典时，出现 java.net.SocketPermission 拒绝连接，此时需配置 jre 策略
    ```
    # vim /opt/jre/lib/security/java.policy，在最后一个 "}" 前追加下面一行
    permission java.net.SocketPermission "*:*","accept,connect,resolve";
    ```

