Elasticsearch 5安装
环境准备
- 准备JDK环境 需要1.8.0_73 or later
- 创建启动用户不能使用root用户启动Elastic
创建用户
useradd elastic 创建用户
passwd Tivoli@0722 设置密码
Elasticsearch安装及配置
自定义JDK
如已经配置环境变量的JDK为1.8则不需要指定,否则需要指定JDK
在 bin/elasticshearch
文件首添加
export JAVA_HOME=/home/elastic/jdk1.8.0_121
如需安装插件则在bin/elasticshearch-plugin
文件首添加
export JAVA_HOME=/home/elastic/jdk1.8.0_121
安装运行
Elasticsearch默认是运行在64位server JVM上。如果要运行在32位client JVM上必须从jvm.options移除 –server选项并且必须更改 –Xss1m 为 –Xss320k
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.3.0.tar.gz
sha1sum elasticsearch-5.3.0.tar.gz
tar -xzf elasticsearch-5.3.0.tar.gz
cd elasticsearch-5.3.0/
./bin/elasticsearch
如果需要作为服务启动请参考官网
配置
配置文件说明:
elasticsearch.yml:Elasticsearch 配置文件
log4j2.properties:Elasticsearch 日志配置文件
jvm.options:JVM配置文件
默认配置文件路径在:config/
也可以在启动的时候指定配置文件夹:
./bin/elasticsearch -Epath.conf=/path/to/my/config/
注意:elasticsearch.yml文件的格式必须遵循YAML文件格式
数据存储及日志存储路径
默认数据存储及日志路径是:data/和logs/文件夹
path:
data: /var/lib/elasticsearch
logs: /var/log/elasticsearch
等价于:
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
集群名称
一个节点能够加入一个集群是根据每个节点的cluster.name来确定该节点是否能够加入集群。
因此需要设置需要加入集群的每个节点相同的名字。其默认值是:elasticsearch
示例:
cluster.name: logging-prod
节点名称
Elasticsearch默认会自动生成节点名称,也可以自定义名称。
示例:
node.name: prod-data-2
内存锁定
一般设置为true
bootstrap.memory_lock: true
IP绑定
默认Elasticsearch绑定127.0.0.1和[::1]
为了与其它节点进行服务沟通需要配置该选项
network.host: 192.168.1.10
配置多个
network.host: [192.168.1.10,127.0.0.1]
自动创建索引
禁用自动创建索引
action.auto_create_index: false
允许自动创建指定索引
action.auto_create_index: +.security,+.monitoring*,+.watches,+.triggered_watches,+.watcher-history*,+.kibana,+db4a_*
示例是在使用X-pack插件和Kibana时的配置
备注:+表示允许 –表示不允许
其它参数
discovery.zen.ping.unicast.hosts
discovery.zen.minimum_master_nodes
日志配置
Elasticsearch使用Log4j2来记录日志。其配置日志文件是log4j2.properties
客户端连接
无X-pack插件
Settings settings = Settings.builder()
.put("client.transport.sniff", true) // 启用嗅探
.put("cluster.name", "test") // 设置集群名称
.build();
TransportClient client = null;
try {
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.101.50.174"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
有X-pack插件
Settings settings = Settings.builder()
.put("client.transport.sniff", true) // 启用嗅探
.put("cluster.name", "test") // 设置集群名称
.put("xpack.security.user", "elastic:tivoli@0506")
.put("xpack.security.transport.ssl.enabled", false)
.build();
TransportClient client = null;
try {
client = new PreBuiltXPackTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.101.50.174"), 9300));
} catch (UnknownHostException e) {
e.printStackTrace();
}
动态Mapping模版
示例:
curl -XPUT -u elastic localhost:9200/_template/db4a -d '{
"template" : "db4a_*",
"settings": {
"index.number_of_shards" : 5,
"number_of_replicas" : 0
},
"mappings": {
"log_event": {
"properties": {
"id": {
"type": "text"
},
"event_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd HH:mm:ss.S||yyyy-MM-dd HH:mm:ss.SS||yyyy-MM-dd HH:mm:ss.SSS||yyyy-MM-dd HH:mm:ss.SSSS||yyyy-MM-dd HH:mm:ss.SSSSS||yyyy-MM-dd HH:mm:ss.SSSSSS||yyyy-MM-dd"
}
},
"dynamic_templates": [{
"integers": {
"match_mapping_type": "long",
"mapping": {
"type": "integer"
}
}
},{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "text"
}
}
}
]
}
}
}'
X-pack插件安装
X-Pack是一个Elastic Stack的扩展,将安全,警报,监视,报告和图形功能包含在一个易于安装的软件包中
bin/elasticsearch-plugin install file:///home/elastic/x-pack-5.3.0.zip
安装后所有通过URL进行的访问都需要输入用户名和密码
安装后默认的用户名和密码:elastic/changeme
修改密码:
curl -XPUT -u elastic 'localhost:9200/_xpack/security/user/elastic/_password' -H "Content-Type: application/json" -d '{
"password" : "tivoli@0506"
}'
如果配置禁用自动创建索引,需要在elasticsearch.yml
配置action.auto_create_index
允许创建以下索引:
action.auto_create_index: .security,.monitoring*,.watches,.triggered_watches,.watcher-history*
问题及解决方案
max file descriptors:
描述:
max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
解决方案:
- 方法一:
vi /etc/security/limits.conf
添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
如果使用非root用户登录在切换root进行修改需要确保SSH服务器没有配置为使用PAM (UsePAM yes) 并重启SSH服务/etc/init.d/sshd restart
- 方法二:在当前用户的.bashrc里加入
ulimit -n 65536
配置完成后需要重新登录
查看配置是否成功:ulimit –n
参考配置:
https://www.elastic.co/guide/en/elasticsearch/reference/current/file-descriptors.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/setting-system-settings.html#limits.conf
max virtual memory areas vm.max_map_count
描述:
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决方案:
vi /etc/sysctl.conf
添加下面配置:
vm.max_map_count=655360
并执行命令:
sysctl -p
max number of threads
描述:
max number of threads [1024] for user [lish] likely too low, increase to at least [2048]
解决问题:
vi /etc/security/limits.d/90-nproc.conf
修改如下内容:
* soft nproc 1024
#修改为
* soft nproc 2048
unable to install syscall filter
java.lang.UnsupportedOperationException: seccomp unavailable: requires kernel 3.5+ with CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in
-
方法一:Use the image on a Linux kernel/distribution that has CONFIG_SECCOMP* compiled in the kernel.
-
方法二:Accept the risk of not doing this by setting bootstrap.system_call_filter to false in elasticsearch.yml
参考地址 -
方法三:升级内核到3.5+并使用CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER compiled in