elasticsearch--安装
本地安装
安装包下载
百度网盘 密码:2edb
下载成功后将安装包上传到服务器上
解压到/usr/local的目录
tar -zxvf elasticsearch-7.13.4-linux-x86_64.tar -C /usr/local
重命名文件夹
mv /usr/local/elasticsearch-7.13.4 /usr/local/elasticsearch
添加普通用户elasticSearch,并将/usr/local/elasticsearch授权给elasticSearch的用户
[root@localhost ~]# useradd elasticsearch
[root@localhost ~]# chown -R elasticsearch:elasticsearch /usr/local/elasticsearch
配置修改
/etc/sysctl.conf文件中添加以下的内容
fs.file-max=655360
vm.max_map_count = 262144
fs.file-max主要是配置系统最大打开文件描述符数,建议修改为655360或者更高,vm.max_map_count影响Java线程数量,用于限制一个进程可以拥有的VMA(虚拟内存区域)的大小,系统默认是65530,建议修改成262144或者更高。
/etc/security/limits.conf文件中添加以下的内容
* soft nproc 20480
* hard nproc 20480
* soft nofile 65536
* hard nofile 65536
* soft memlock unlimited
* hard memlock unlimited
进程最大打开文件描述符(nofile)、最大用户进程数(nproc)和最大锁定内存地址空间(memlock)
修改/etc/security/limits.d/20-nproc.conf(centos7.x系统)
方式1:
将
* soft nproc 4096
修改为:
* soft nproc 20480
方式2:
直接删除/etc/security/limits.d/20-nproc.conf文件也行。
elasticsearch的JVM内存资源设置,路径/usr/local/elasticsearch/config/jvm.options
-Xms4g
-Xmx4g
默认JVM内存为2g,可根据服务器内存大小,修改为合适的值。一般设置为服务器物理内存的一半最佳。
让上面配置生效
方式一: 重启机器
方式二: 执行 /sbin/sysctl -p 的命令
配置elasticsearch
文件位置:/usr/local/elasticsearch/config/elasticsearch.yml
cluster.name: elkbigdata #可以随意定义
node.name: server1 #可以随意定义
node.master: true
node.data: true
path.data: /data1/elasticsearch,/data2/elasticsearch
path.logs: /usr/local/elasticsearch/logs
bootstrap.memory_lock: true
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["server1"] #单机的配置
## 下面是集群的配置
discovery.zen.minimum_master_nodes: 1
discovery.zen.ping.unicast.hosts: ["172.16.213.37:9300","172.16.213.78:9300
- cluster.name: elkbigdata
配置elasticsearch集群名称,默认是elasticsearch。这里修改为elkbigdata,elasticsearch会自动发现在同一网段下的集群名为elkbigdata的主机。
- node.name: server1
节点名,任意指定一个即可,这里是server1,我们这个集群环境中有三个节点,分别是server1、server2和server3,记得根据主机的不同,要修改相应的节点名称。
- node.master: true
指定该节点是否有资格被选举成为master,默认是true,elasticsearch集群中默认第一台启动的机器为master角色,如果这台服务器宕机就会重新选举新的master。
- node.data: true
指定该节点是否存储索引数据,默认为true,表示数据存储节点,如果节点配置node.master:false并且node.data: false,则该节点就是client node。这个client node类似于一个“路由器”,负责将集群层面的请求转发到主节点,将数据相关的请求转发到数据节点。
- path.data:/data1/elasticsearch,/data2/elasticsearch
设置索引数据的存储路径,默认是elasticsearch根目录下的data文件夹,这里自定义了两个路径,可以设置多个存储路径,用逗号隔开。
- path.logs: /usr/local/elasticsearch/logs
设置日志文件的存储路径,默认是elasticsearch根目录下的logs文件夹
- bootstrap.memory_lock: true
此配置项一般设置为true用来锁住物理内存。 linux下可以通过“ulimit -l” 命令查看最大锁定内存地址空间(memlock)是不是unlimited
- network.host: 0.0.0.0
此配置项用来设置elasticsearch提供服务的IP地址,默认值为0.0.0.0,此参数是在elasticsearch新版本中增加的,此值设置为服务器的内网IP地址即可。
- http.port: 9200
设置elasticsearch对外提供服务的http端口,默认为9200。其实,还有一个端口配置选项transport.tcp.port,此配置项用来设置节点间交互通信的TCP端口,默认是9300。
- discovery.zen.minimum_master_nodes: 1
配置当前集群中最少的master节点数,默认为1,也就是说,elasticsearch集群中master节点数不能低于此值,如果低于此值,elasticsearch集群将停止运行。在三个以上节点的集群环境中,建议配置大一点的值,推荐2至4个为好。
- discovery.zen.ping.unicast.hosts: ["172.16.213.37:9300","172.16.213.78:9300"]
设置集群中master节点的初始列表,可以通过这些节点来自动发现新加入集群的节点。这里需要注意,master节点初始列表中对应的端口是9300。即为集群交互通信端口。
切换到elasticsearch的用户
su elasticsearch
启动
cd /usr/local/elasticsearch/bin
./elasticsearch -d
docker的安装方式
修改max_map_count
设置max_map_count不能启动es会启动不起来
查看max_map_count的值 默认是65530
cat /proc/sys/vm/max_map_count
重新设置max_map_count的值
sysctl -w vm.max_map_count=262144
创建自定义elasticsearch
增加ik分词器与时区配置
创建Dockerfile
ARG version
FROM docker.elastic.co/elasticsearch/elasticsearch:$version
ARG version
RUN sh -c '/bin/echo -e "y" | /usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v$version/elasticsearch-analysis-ik-$version.zip'
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
Dockerfile中自定义变量version定义了两次,是由于FROM使用version后,自定义的变量version就失效了,故多定义了一次
构建自定义的镜像
已7.13.0为例子
docker build -f Dockerfile -t es:7.13.0 --build-arg version=7.13.0 .
elasticsearch单例yml
version: '3.5'
services:
es01:
image: es:7.13.0
deploy:
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
environment:
- node.name=es01
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx1024m"
- TZ=Asia/Shanghai
- discovery.type=single-node
# 设置访问权限
- xpack.security.enabled=true
- xpack.license.self_generated.type=basic
- ELASTIC_PASSWORD=zhcfelasticsearch
#配置跨域
- http.cors.enabled=true
- http.cors.allow-origin='*'
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /home/mount/es01/data:/usr/share/elasticsearch/data
- /home/mount/es01/log:/usr/share/elasticsearch/log
logging:
driver: "json-file"
options:
max-size: "500m"
max-file: "5"
ports:
- 9200:9200
networks:
elastic:
driver: bridge
elasticsearch集群yml
version: '3.5'
services:
es01:
image: es:7.13.0
container_name: es01
environment:
- node.name=es01
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es02,es03
- cluster.initial_master_nodes=["es01","es02","es03"]
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
- TZ=Asia/Shanghai
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /home/mount/es01/data:/usr/share/elasticsearch/data
- /home/mount/es01/log:/usr/share/elasticsearch/log
logging:
driver: "json-file"
options:
max-size: "500m"
max-file: "5"
ports:
- 9200:9200
networks:
- elastic
es02:
image: es:7.13.0
container_name: es02
environment:
- node.name=es02
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es03
- cluster.initial_master_nodes=["es01","es02","es03"]
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
- TZ=Asia/Shanghai
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /home/mount/es02/data:/usr/share/elasticsearch/data
- /home/mount/es02/log:/usr/share/elasticsearch/log
logging:
driver: "json-file"
options:
max-size: "500m"
max-file: "5"
networks:
- elastic
es03:
image: es:7.13.0
container_name: es03
environment:
- node.name=es03
- cluster.name=es-docker-cluster
- discovery.seed_hosts=es01,es02
- cluster.initial_master_nodes=["es01","es02","es03"]
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms1024m -Xmx1024m"
- TZ=Asia/Shanghai
ulimits:
memlock:
soft: -1
hard: -1
volumes:
- /home/mount/es03/data:/usr/share/elasticsearch/data
- /home/mount/es03/log:/usr/share/elasticsearch/log
logging:
driver: "json-file"
options:
max-size: "500m"
max-file: "5"
networks:
- elastic
networks:
elastic:
driver: bridge
启动成功返回
GET "http://IP:9200/_cat/nodes?v=true&pretty" 会出现以下的东西
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
172.21.0.3 54 56 11 0.29 1.26 1.24 cdfhilmrstw * es01
172.21.0.2 49 56 11 0.29 1.26 1.24 cdfhilmrstw - es02
172.21.0.4 33 56 11 0.29 1.26 1.24 cdfhilmrstw - es03
启动可能会遇到的问题
错误一
ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];
Likely root cause: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes
at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
/usr/share/elasticsearch/data/nodes写入没有权限 解决方式: 将该纹路的映射文件权限修改为777即可
错误二
bootstrap check failure [1] of [1]: initial heap size [536870912] not equal to maximum heap size [1073741824]; this can cause resize pauses and prevents memory locking from locking the entire heap
ERROR: Elasticsearch did not exit normally - check the logs at /usr/share/elasticsearch/logs/es-docker-cluster.log
{"type": "server", "timestamp": "2022-01-24T15:10:39,706+08:00", "level": "INFO", "component": "o.e.n.Node", "cluster.name": "es-docker-cluster", "node.name": "es03", "message": "stopping ..." }
设置jvm堆内存的时候,初始化堆内存和最大堆内存设置不一致导致的 解决方式: 将初始化堆内存和最大堆内存设置一致
错误三
bootstrap check failure [1] of [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
ERROR: Elasticsearch did not exit normally - check the logs at /usr/share/elasticsearch/logs/es-docker-cluster.log
- 在/etc/sysctl.conf文件最后添加一行
vm.max_map_count=262144
- 让配置立即生效
/sbin/sysctl -p
验证是否启动成功
[root@localhost config]# curl http://localhost:9200
{
"name" : "server1",
"cluster_name" : "car_loan",
"cluster_uuid" : "ZPcC1QQBSomHEeV2yHu8xA",
"version" : {
"number" : "7.13.4",
"build_flavor" : "default",
"build_type" : "tar",
"build_hash" : "c5f60e894ca0c61cdbae4f5a686d9f08bcefc942",
"build_date" : "2021-07-14T18:33:36.673943207Z",
"build_snapshot" : false,
"lucene_version" : "8.8.2",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
elasticsearch-head
docker run -d -p 9100:9100 --name docker_es_head5 --restart=always mobz/elasticsearch-head:5
注意:本文归作者所有,未经作者允许,不得转载