ES入门篇2--Linux搭建集群

600 阅读3分钟

1. 使用虚拟机搭建6台集群

1.1 准备一台虚拟机

我安装的是centos 6.5 minimal(用习惯了),使用root登录centos

修改虚拟机ip为静态ip:192.168.0.115

下载jdk8并配置环境变量

1.2 下载ES并上传到服务器

  1. 官方下载地址 www.elastic.co/downloads/e…

官方下载比较慢,建议去国内的镜像仓库下载 mirrors.huaweicloud.com/elasticsear… 下载7.7.0版本的ES(2020/5/20时最新版本)

  1. 上传文件到服务器

用scp命令或者WinSCP这些工具把ES安装包上传到/usr/local目录下

1.3 ES排坑

1.启动ES

cd /usr/local/elasticsearch-7.7.0/bin
./elasticsearch

提示未来的ES版本将会需要java11,java8不符合要求,先这样用着吧,后续有什么坑再回退旧版ES。

报错:can not run elasticsearch as root

future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/jdk1.8.0_241/jre] does not meet this requirement
future versions of Elasticsearch will require Java 11; your Java version from [/usr/local/jdk1.8.0_241/jre] does not meet this requirement
[2020-05-24T19:47:54,001][ERROR][o.e.b.ElasticsearchUncaughtExceptionHandler] [ES01] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:174) ~[elasticsearch-7.7.0.jar:7.7.0]
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161) ~[elasticsearch-7.7.0.jar:7.7.0]
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.7.0.jar:7.7.0]
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127) ~[elasticsearch-cli-7.7.0.jar:7.7.0]
        at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.7.0.jar:7.7.0]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126) ~[elasticsearch-7.7.0.jar:7.7.0]
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.7.0.jar:7.7.0]
Caused by: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:111) ~[elasticsearch-7.7.0.jar:7.7.0]
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:178) ~[elasticsearch-7.7.0.jar:7.7.0]
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393) ~[elasticsearch-7.7.0.jar:7.7.0]
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170) ~[elasticsearch-7.7.0.jar:7.7.0]
        ... 6 more
uncaught exception in thread [main]
java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:111)
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:178)
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393)
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
        at org.elasticsearch.cli.Command.main(Command.java:90)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
For complete error details, refer to the log at /usr/local/elasticsearch-7.7.0/logs/elasticsearch.log

为了安全不允许使用root用户启动ES

新建一个用户启动ES

useradd huazhi
# 输入两次密码即可修改密码
passwd huazhi
# 把ES文件夹的所有权哥给huazhi:huazhi
chown -R huazhi:huazhi elasticsearch-7.7.0/
# 给huazhi授权ES文件夹rwx权限
chmod -R u+rwx elasticsearch-7.7.0/

2. 使用新建的用户登录,启动ES

su huazhi
cd /usr/local/elasticsearch-7.7.0/bin
./elasticsearch

启动报错:

ERROR: [5] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
[2]: max number of threads [1024] for user [huazhi] is too low, increase to at least [4096]
[3]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
[4]: system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
[5]: the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured

原因:默认进程最大同时打开文件数太小

解决办法:

# 使用root账户登录
vi /etc/security/limits.conf
# 添加配置
* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
# 保存配置
:wq!
vi /etc/sysctl.conf 
# 文件添加
vm.max_map_count=262144

重启服务器生效

shutdown -r 0

修改ES配置文件

# 修改配置es文件
vi /usr/local/elasticsearch-7.7.0/config/application.yml
# 加上
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
# 修改
cluster.name: myes
node.name: es01
# 加上
cluster.initial_master_nodes: ["es01"]

4. 访问不了ES

访问:192.168.0.115:9200,报404。需要修改application.yml,配置network.host和http.port

重启ES

5. 再次访问192.168.0.115:9200

{
  "name" : "es01",
  "cluster_name" : "myes",
  "cluster_uuid" : "ikCpamWESNOivYyfs21fZQ",
  "version" : {
    "number" : "7.7.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "81a1e9eda8e6183f5237786246f6dced26a10eaf",
    "build_date" : "2020-05-12T02:01:37.602180Z",
    "build_snapshot" : false,
    "lucene_version" : "8.5.1",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

单机版ES安装完成

1.4 复制5台虚拟机

修改静态ip为: 分别192.168.0.116、192.168.0.117、192.168.0.118、192.168.0.119、192.168.0.120

修改es config目录下application.yml

cluster.name: myes
# 116修改成es02、117修改成es03,以此类推
node.name: es01
#修改为对应ip
network.host
#此属性的值为true,并不意味着这个节点就是主节点
node.master: true
#是否存储数据
node.data: true
discovery.zen.ping.unicast.hosts: ["192.168.0.116","192.168.0.117","192.168.0.118","192.168.0.119","192.168.0.120"]

分别启动ES即可