Elasticsearch7 安装与使用
Elasticsearch 单机安装
下载 Elasticsearch
Elasticsearch 版本 7.17.4
下载地址:https://www.elastic.co/cn/downloads/past-releases/elasticsearch-7-17-4
网速不好可以先下载在上传至服务器
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.4-linux-x86_64.tar.gz
tar -zvxf elasticsearch-7.17.4-linux-x86_64.tar.gz
配置 Elasticsearch
elasticsearch.yml
vim config/elasticsearch.yml
+ network.host: 0.0.0.0
- discovery.seed_hosts: 集群主机列表
- discovery.seed_providers: 基于配置文件配置集群主机列表
- cluster.initial_master_nodes: 启动时初始化的参与选主的node,生产环境必填
# node.name: node-1
+ node.name: node-1
- #discovery.seed_hosts: ["host1", "host2"]
discovery.seed_hosts: ["node-1"]
- #cluster.initial_master_nodes: ["node-1", "node-2"]
+ cluster.initial_master_nodes: ["node-1"]
vm.max_map_count
查看当前最大描述符大小系统环境变量
sysctl -a | grep vm.max_map_count
修改最大描述符大小
vim /etc/sysctl.conf
+ vm.max_map_count=262144
修改后需要重启才能生效
不想重启可以设置临时生效
sysctl -w vm.max_map_count=262144
JVM 分配大小
根据自己的内存自行调整,内存不够则会启动失败
vim config/jvm.options
- ## -Xms4g
- ## -Xmx4g
+ -Xms512m
+ -Xmx512m
最大描述符和最大线程数
修改后需要重新登录生效
vim /etc/security/limits.conf
# 添加以下内容
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
$ ulimit -Hn
65536
启动 Elasticsearch
** 不能以 root 用户启动 **
创建用户
useradd lzscxb
passwd lzscxb
增加管理员权限
vim /etc/sudoers
+ lzscxb ALL=(ALL) ALL
修改 Elasticsearch 权限
chown -R lzscxb:lzscxb elasticsearch-7.17.4
# 切换用户
su lzscxb
启动命令
bin/elasticsearch
# 后台启动
bin/elasticsearch -d
启动成功后 http://[ip]:9200 访问
访问失败请关闭防火墙
systemctl stop firewalld
分词器
elasticsearch-analysis-ik 分词器
https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.17.4
下载安装插件
cd /opt/Software/es/elasticsearch-7.17.4/plugins
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.4/elasticsearch-analysis-ik-7.17.4.zip
unzip -d ik elasticsearch-analysis-ik-7.17.4.zip
rm elasticsearch-analysis-ik-7.17.4.zip
elasticsearch-analysis-pinyin 拼音分词器
https://github.com/medcl/elasticsearch-analysis-pinyin/releases/tag/v7.17.4
下载安装插件
cd /opt/Software/es/elasticsearch-7.17.4/plugins
wget https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v7.17.4/elasticsearch-analysis-pinyin-7.17.4.zip
unzip -d pinyin elasticsearch-analysis-pinyin-7.17.4.zip
rm elasticsearch-analysis-pinyin-7.17.4.zip
启动 es7,查看启动日志文件
Kibana
安装 Libana
kibana 版本 7.17.4
下载地址:https://www.elastic.co/cn/downloads/past-releases/kibana-7-17-4
wget https://artifacts.elastic.co/downloads/kibana/kibana-7.17.4-linux-x86_64.tar.gz
tar -zvxf kibana-7.17.4-linux-x86_64.tar.gz
mv kibana-7.17.4-linux-x86_64.tar.gz kibana-7.17.4
配置 Kibana
vim config/kibana.yml
server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["http://localhost:9200"]
启动 Kibana
nohup bin/kibana > logs/kibana-start.log 2>&1 &
节点监控
测试分词器
Management -> Dev Tools
普通分词
POST /_analyze
{
"analyzer": "simple",
"text": "我是一个程序员,嘿嘿嘿!"
}
ik 分词
POST /_analyze
{
"analyzer": "ik_smart",
"text": "我是一个程序员,嘿嘿嘿!"
}
pinyin 分词
- 使用自定义拼音分析器创建索引
PUT /medcl/
{
"settings" : {
"analysis" : {
"analyzer" : {
"pinyin_analyzer" : {
"tokenizer" : "my_pinyin"
}
},
"tokenizer" : {
"my_pinyin" : {
"type" : "pinyin",
"keep_separate_first_letter" : false,
"keep_full_pinyin" : true,
"keep_original" : true,
"limit_first_letter_length" : 16,
"lowercase" : true,
"remove_duplicated_term" : true
}
}
}
}
}
- 查询
GET /medcl/_analyze
{
"text": ["刘德华"],
"analyzer": "pinyin_analyzer"
}
Elasticsearch 集群
克隆三台虚拟机,并配置ip,删除 ./data 数据目录
修改elasticsearch.yml
cluster.name: kkb-es
node.name: node-0
node.master: true
network.host: 0.0.0.0
http.port: 9200
transport.tcp.port: 9300 # tcp 端口
discovery.zen.ping.unicast.hosts: ["192.168.147.66:9300","192.168.147.67:9300","192.168.147.68:9300"]
discovery.zen.minimum_master_nodes: 2
http.cors.enabled: true
http.cors.allow-origin: "*"