创建用户
1 | groupadd es && useradd es -g es -M -s /bin/bash |
系统参数修改
- vi /etc/sysctl.conf添加如下配置
1 | vm.max_map_count=262144 |
使得重启之后也生效,即永久生效
执行如下命令使得立即生效
1 | sysctl –p |
修改/etc/security/limits.conf添加以下配置
1
2es hard nofile 65535
es soft nofile 65535/etc/systemd/user.conf和/etc/systemd/system.conf分别添加以下配置
1
DefaultLimitNOFILE=65535
systemd启动elasticsearch,需要设置这两个文件,否则启动会报如下错误
[1] bootstrap checks failed
[1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
解压
1 | tar xzvf elasticsearch-7.6.1-linux-x86_64.tar.gz -C /opt/ |
配置ES
1主2从的配置
- node-1
1 | cluster.name: my-es |
- node-2
1 | cluster.name: my-es |
- node-3
1 | cluster.name: my-es |
生产环境建议network.host: 指定IP
检查集群状态
1 | curl -XGET http://192.168.56.131:9200/_cluster/health?pretty |
开机启动ElasticSearch
方案一 在/etc/init.d目录下
创建名为elasticsearch的文件,内容如下
1 | !/bin/sh |
1 | chmod a+x elasticsearch.sh |
方案二 在/lib/systemd/system目录下
创建名为elasticsearch.service的文件,内容如下1
2
3
4
5
6
7
8
9
10
11
12
13[Unit]
Description=elasticsearch
After=network.target
[Service]
Type=simple
ExecStart=/bin/bash /opt/elasticsearch-7.6.1/bin/elasticsearch
Restart=always
User=es
Group=es
WorkingDirectory=/opt/elasticsearch-7.6.1
[Install]
WantedBy=mutil-user.target
重载配置文件1
sudo systemctl daemon-reload
启用服务1
sudo systemctl enable elasticsearch
启动服务1
sudo systemctl start elasticsearch
如果启动失败,想看下日志
1 | 查看状态 |
通过systemctl 启动服务报如下错误1
2
3
4
5
6
7Mar 15 10:08:51 ubuntu bash[1308]: ERROR: [1] bootstrap checks failed
Mar 15 10:08:51 ubuntu bash[1308]: [1]: max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]
Mar 15 10:08:51 ubuntu bash[1308]: ERROR: Elasticsearch did not exit normally - check the logs at /opt/elasticsearch-7.6.1/logs/my-es.log
Mar 15 10:08:51 ubuntu bash[1308]: [2020-03-15T02:08:51,352][INFO ][o.e.n.Node ] [node-1] stopping ...
Mar 15 10:08:51 ubuntu bash[1308]: [2020-03-15T02:08:51,462][INFO ][o.e.n.Node ] [node-1] stopped
Mar 15 10:08:51 ubuntu bash[1308]: [2020-03-15T02:08:51,463][INFO ][o.e.n.Node ] [node-1] closing ...
Mar 15 10:08:51 ubuntu bash[1308]: [2020-03-15T02:08:51,535][INFO ][o.e.n.Node ] [node-1] closed
划重点:通过systemctrl自建elasticsearch系统服务的形式来启动的es,则需要修改/etc/systemd/user.conf,/etc/systemd/system.conf这两个文件,添加以下配置1
DefaultLimitNOFILE=65536
常见错误
1 | org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root |
这个是最常见的了,不能用root用户来启动elasticsearch
1 | Exception in thread "main" org.elasticsearch.bootstrap.BootstrapException: java.nio.file.AccessDeniedException: /opt/elasticsearch-7.6.1/config/elasticsearch.keystore |
处理方法 chown -R es:es $ES_HOME/conf
1 | 2020-03-07 14:50:20,774 main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.RollingFileAppender |
处理方法 chown -R es:es $ES_HOME/logs
1 | [2020-03-07T14:11:03,119][WARN ][o.e.c.c.Coordinator ] [node-1] failed to validate incoming join request from node [{node-2}{OTFjHR3wRuyQBMbb9PdGoQ}{GH_dXyYZQIuyjnnBTsELRw}{192.168.56.132}{192.168.56.132:9300}{dil}{ml.machine_memory=3665874944, ml.max_open_jobs=20, xpack.installed=true}] |
如果集群所有节点cluster.name设置都一样的,但还是报以上错误,可以将集群内的节点的data目录删掉后重启
附加
目前es没有桌面客户端,不过elasticsearch-head算是比较方便的,用于快速查询下数据。
这里我就给出elastic-head,systemd服务的脚本
1 | [Unit] |
请事先安装好node环境,并在/opt/elasticsearch-head目录,执行好npm i -V安装好相关module
执行如下命令,下次就能开机启动了1
2 systemctl daemon-reload
systemctl enable elasticsearch-head
参考
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-network.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/targz.html
https://www.elastic.co/guide/en/elasticsearch/reference/7.6/docker.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/settings.html
https://www.linuxtechi.com/set-ulimit-file-descriptors-limit-linux-servers/
https://askubuntu.com/questions/1102512/set-ulimit-for-non-root-user
https://superuser.com/questions/1200539/cannot-increase-open-file-limit-past-4096-ubuntu/1200818#1200818
http://www.ruanyifeng.com/blog/2018/03/systemd-timer.html