环境
[10:18:27 root@elk-01 ~ $]cat /etc/centos-release
CentOS Linux release 7.6.1810 (Core)
elasticsearch-7.1.1-x86_64.rpm
要点
搜索性能取决于最慢的节点的响应时间
环境规划
节点1:Elasticsearch + kafka 192.168.50.14 负责存储数据、可参与 Master 讯据
节点2:Elasticsearch + kafka 192.168.50.15 负责存储数据、可参与 Master 讯据
节点3:Elasticsearch + kafka 192.168.50.16 负责存储数据、可参与 Master 讯据
节点4:Elasticsearch协调节点 + Kibana 192.168.50.17 充当 ES 集群智能负载均衡器 + Kibana前端展示用页面使用
第一步:系统优化设置
1.告诉操作系统可以无限制分配内存给一个进程
echo '* soft memlock unlimited' >>/etc/security/limits.conf
echo '* hard memlock unlimited' >>/etc/security/limits.conf
cat /etc/security/limits.conf
2.禁用SWAP
## 完全禁用SWAP
## swapoff -a
## 尽量避免使用SWAP
echo 'vm.swappiness = 1' >> /etc/sysctl.conf
3. 修改最大打开文件描述符
echo '* soft nofile 65535' >> /etc/security/limits.conf
echo '* hard nofile 65535' >> /etc/security/limits.conf
cat /etc/security/limits.conf
ulimit -n 65535
4.重启生效
init 6
第二步:安装和配置Elasticsearch
下载地址:https://www.elastic.co/cn/downloads/elasticsearch
帮助文档:https://www.elastic.co/cn/webinars/getting-started-elasticsearch?elektra=startpage
1.下载并安装
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.1.1-x86_64.rpm
rpm -ivh elasticsearch-7.1.1-x86_64.rpm
2.修改配置文件以支持集群
1.注意修改network.host为各节点IP地址
2.注意修改discovery.zen.ping.unicast.hosts列表
cp /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak
cat <<EOF >>/etc/elasticsearch/elasticsearch.yml
## 集群名称
cluster.name: ELK-Cluster
## 节点名称(每个节点名称不能相同)
node.name: elk-01
## 允许 JVM 锁住内存,禁止操作系统交换出去
# bootstrap.memory_lock: true
## 是否有资格成为主节点
## 通过 node.master 可以配置该节点是否有资格成为主节点,如果配置为 true,则主机有资格成为主节点
## 注意这里是有资格成为主节点,不是一定会成为主节点
node.master: true
## 是否是数据节点
## 当 node.master 和 node.data 均为 false,则该主机会作为负载均衡节点
node.data: true
## 设置访问的地址和端口
network.host: 192.168.50.14
http.port: 9200
## 集群地址设置
## 配置之后集群的主机之间可以自动发现
# discovery.zen.ping.unicast.hosts: ["192.168.50.14", "192.168.50.15", "192.168.50.16"]
discovery.seed_hosts: ["192.168.50.14", "192.168.50.15", "192.168.50.16"]
cluster.initial_master_nodes: ["192.168.50.14", "192.168.50.15", "192.168.50.16"]
## 配置大多数节点(通常为主节点的节点总数/ 2 + 1)来防止“裂脑”:
discovery.zen.minimum_master_nodes: 2
## 在完全集群重启后阻止初始恢复,直到启动N个节点
gateway.recover_after_nodes: 2
EOF
vim /etc/elasticsearch/elasticsearch.yml
3.为elasticsearch设置jdk路径,否则启动时候会报错
请根据自己实际路径进行修改
cp /etc/sysconfig/elasticsearch /etc/sysconfig/elasticsearch.bak
cat <<EOF >>/etc/sysconfig/elasticsearch
JAVA_HOME=/home/jdk1.8.0_202
EOF
4.设置堆内存(根据物理内存情况设置)
http://openskill.cn/article/304
https://www.elastic.co/guide/en/elasticsearch/guide/current/heap-sizing.html
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_limiting_memory_usage.html
https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html
## 编辑以下值
## 内存设置为物理内存的50%,并且不要超过32G
## 确保这两个值相等,防止程序在运行时改变堆内存大小, 这是一个很耗系统资源的过程
cp /etc/elasticsearch/jvm.options /etc/elasticsearch/jvm.options.bak
vim /etc/elasticsearch/jvm.options
-Xms2g
-Xmx2g
5.启动服务
systemctl daemon-reload
systemctl enable elasticsearch.service
systemctl stop elasticsearch.service
systemctl start elasticsearch.service
systemctl status elasticsearch.service
6.验证
## 查看单节点状态
curl -X GET "192.168.50.14:9200/"
curl -X GET "192.168.50.15:9200/"
curl -X GET "192.168.50.16:9200/"
## 查看集群内节点分布
curl -XGET 'http://192.168.50.14:9200/_cat/nodes?pretty'
curl -XGET 'http://192.168.50.15:9200/_cat/nodes?pretty'
curl -XGET 'http://192.168.50.16:9200/_cat/nodes?pretty'
## 检测集群健康状态
curl 192.168.50.14:9200/_cat/health?v
curl 192.168.50.15:9200/_cat/health?v
curl 192.168.50.16:9200/_cat/health?v
## 查看所有支持的 _cat 命令
curl -XGET 'http://192.168.50.14:9200/_cat?pretty'
curl -XGET 'http://192.168.50.15:9200/_cat?pretty'
curl -XGET 'http://192.168.50.16:9200/_cat?pretty'
_cat代表查看信息
nodes为查看节点信息,默认会显示为一行,所以就用刀了?preety让信息更有好的显示
?preety让输出信息更友好的显示
第三步:安装中文分词器 elasticsearch-analysis-ik
https://github.com/medcl/elasticsearch-analysis-ik
1.创建目录
mkdir /usr/share/elasticsearch/plugins/ik
2.下载并解压
cd /usr/share/elasticsearch/plugins/ik
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.1.1/elasticsearch-analysis-ik-7.1.1.zip
unzip elasticsearch-analysis-ik-7.1.1.zip
3.重启 Elasticsearch
systemctl stop elasticsearch.service
systemctl start elasticsearch.service
systemctl status elasticsearch.service
4.验证
## 1.登录 Kibana
## 2.Dev Tools - Console
GET _analyze?pretty
{
"analyzer": "ik_smart",
"text":"安徽省长江流域"
}
GET _analyze?pretty
{
"analyzer": "ik_max_word",
"text":"安徽省长江流域"
}
第四步:常用命令
## 查询所有索引的分片和副本信息
GET _settings?pretty