Elasticsearch 集群部署
Elasticsearch 安装前准备
安装前环境初始化
CPU 2C
内存4G或更多
操作系统: Ubuntu22.04,Ubuntu20.04,Ubuntu18.04,Rocky8.X,Centos 7.X
操作系统盘50G
主机名设置规则为nodeX
生产环境建议准备单独的数据磁盘
主机名
#各服务器配置自己的主机名
[root@ubuntu1804 ~]# hostnamectl set-hostname es-node1
关闭防火墙和SELinux
关闭防所有服务器的防火墙和 SELinux
#RHEL系列的系统执行下以下配置
[root@es-node1 ~]# systemctl disable firewalld[root@es-node1 ~]# systemctl disable NetworkManager[root@es-node1 ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config[root@es-node1 ~]# reboot
各服务器配置本地域名解析
[root@es-node1 ~]# vim /etc/hosts
10.0.0.101 es-node1
10.0.0.102 es-node2
10.0.0.103 es-node3
优化资源限制配置
修改内核参数
内核参数 vm.max_map_count 用于限制一个进程可以拥有的VMA(虚拟内存区域)的数量 使用默认系统配置,二进制安装时会提示下面错误,包安装会自动修改此配置
#查看默认值
[root@es-node1 ~]#sysctl -a |grep vm.max_map_count
vm.max_map_count = 65530#修改配置
[root@es-node1 ~]#echo "vm.max_map_count = 262144" >> /etc/sysctl.conf#设置系统最大打开的文件描述符数
[root@es-node1 ~]#echo "fs.file-max = 1000000" >> /etc/sysctl.conf
[root@es-node1 ~]#sysctl -p
vm.max_map_count = 262144#Ubuntu22.04默认值已经满足要求
[root@ubuntu2204 ~]#sysctl fs.file-max
fs.file-max = 9223372036854775807
范例: Ubuntu 基于包安装后会自动修改文件
[root@node1 ~]#cat /usr/lib/sysctl.d/elasticsearch.conf
vm.max_map_count=262144
修改资源限制配置(可选)
[root@es-node1 ~]#vi /etc/security/limits.conf* soft core unlimited* hard core unlimited* soft nproc 1000000* hard nproc 1000000* soft nofile 1000000* hard nofile 1000000* soft memlock 32000* hard memlock 32000* soft msgqueue 8192000* hard msgqueue 8192000
安装 Java 环境 (可选)
Elasticsearch 是基于java的应用,所以依赖JDK环境
注意: 安装7.X以后版本官方建议要安装集成JDK的包,所以无需再专门安装 JDK
关于JDK环境说明
1.x 2.x 5.x 6.x都没有集成JDK的安装包,也就是需要自己安装java环境
7.x 版本的安装包分为带JDK和不带JDK两种包,带JDK的包在安装时不需要再安装java,如果不带JDK的包
仍然需要自己去安装java
8.X 版本内置JDK,不再支持自行安装的JDK
如果安装no-jdk的包,才需要安装java环境
官网JAVA版支持说明
https://www.elastic.co/cn/support/matrix#matrix_jvm
因为 Elasticsearch 服务运行需要 Java环境,如果要安装没有JDK的包,需要提前安装JAVA环境,可以使用以下方式安装
如果没有java 环境,安装elasticsearch时会出下面错误提示
[root@node1 ~]#dpkg -i elasticsearch-7.14.0-no-jdk-amd64.deb
方式一:直接使用包管理器yum/apt安装openjdk
[root@es-node1 ~]#yum -y install java-1.8.0-openjdk
[root@es-node1 ~]#apt update;apt -y install openjdk-8-jdk
方式二:本地安装在oracle官网下载rpm安装包:
[root@es-node1 ~]# yum -y install jdk-8u92-linux-x64.rpm
方式三:安装二进制包并自定义环境变量
下载地址:Java Downloads | Oracle
[root@es-node1 ~]# tar xvf jdk-8u121-linux-x64.tar.gz -C /usr/local/
[root@es-node1 ~]# ln -sv /usr/local/jdk1.8.0_121 /usr/local/jdk
[root@es-node1 ~]# ln -sv /usr/local/jdk/bin/java /usr/bin/
[root@es-node1 ~]# vim /etc/profile
export HISTTIMEFORMAT="%F %T `whoami` "
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
[root@es-node1 ~]# source /etc/profile
[root@es-node1 ~]# java -version
java version "1.8.0_121" #确认可以出现当前的java版本号
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
Elasticsearch 安装
包安装 Elasticsearch
下载链接
https://www.elastic.co/cn/downloads/elasticsearch
https://mirrors.tuna.tsinghua.edu.cn/elasticstack/
范例:集群节点 都安装 elasticsearch-8
[root@es-node1 ~]#wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/8.x/apt/pool/main/e/elasticsearch/elasticsearch-8.8.2-amd64.deb
[root@es-node1 ~]#dpkg -i elasticsearch-8.8.2-amd64.deb #内置JAVA
[root@es-node1 ~]#/usr/share/elasticsearch/jdk/bin/java -version
openjdk version "20.0.1" 2023-04-18
OpenJDK Runtime Environment (build 20.0.1+9-29)
OpenJDK 64-Bit Server VM (build 20.0.1+9-29, mixed mode, sharing)[root@es-node2 ~]#wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/8.x/apt/pool/main/e/elasticsearch/elasticsearch-8.8.2-amd64.deb
[root@es-node2 ~]#dpkg -i elasticsearch-8.8.2-amd64.deb #内置JAVA
[root@es-node2 ~]#/usr/share/elasticsearch/jdk/bin/java -version
openjdk version "20.0.1" 2023-04-18
OpenJDK Runtime Environment (build 20.0.1+9-29)
OpenJDK 64-Bit Server VM (build 20.0.1+9-29, mixed mode, sharing)[root@es-node3 ~]#wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/8.x/apt/pool/main/e/elasticsearch/elasticsearch-8.8.2-amd64.deb
[root@es-node3 ~]#dpkg -i elasticsearch-8.8.2-amd64.deb #内置JAVA
[root@es-node3 ~]#/usr/share/elasticsearch/jdk/bin/java -version
openjdk version "20.0.1" 2023-04-18
OpenJDK Runtime Environment (build 20.0.1+9-29)
OpenJDK 64-Bit Server VM (build 20.0.1+9-29, mixed mode, sharing)
范例:8.X 集群配置
#默认配置文件需要以下六行
[root@ubuntu2204 ~]#vim /etc/elasticsearch/elasticsearch.yml
node.name: node-1 #修改此行,每个节点不同
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0 #集群模式必须修改此行,默认是127.0.0.1:9300,否则集群节点无法通过9300端口通信每个节点相同
discovery.seed_hosts: ["10.0.0.201", "10.0.0.202","10.0.0.203"] #修改此行,每个节点相同
cluster.initial_master_nodes: ["10.0.0.201", "10.0.0.202","10.0.0.203"]#修改此行,每个节点相同
xpack.security.enabled: false #修改此行,每个节点相同
xpack.security.enrollment.enabled: true
xpack.security.http.ssl:enabled: truekeystore.path: certs/http.p12
xpack.security.transport.ssl:enabled: trueverification_mode: certificatekeystore.path: certs/transport.p12truststore.path: certs/transport.p12# cluster.initial_master_nodes: ["ubuntu2204.wang.org"] #将此行注释
http.host: 0.0.0.0[root@es-node1 ~]#systemctl enable --now elasticsearch
#打开9200和9300端口
[root@es-node1 ~]#ss -ntlp |grep -E '9200|9300'
LISTEN 0 4096 *:9200 *:* users:(("java",pid=1895,fd=422))
LISTEN 0 4096 *:9300 *:* users:(("java",pid=1895,fd=417))
[root@es-node1 ~]#curl http://127.0.0.1:9200/_cluster/health?pretty=true
{"cluster_name" : "my-application","status" : "green","timed_out" : false,"number_of_nodes" : 3,"number_of_data_nodes" : 3,"active_primary_shards" : 0,"active_shards" : 0,"relocating_shards" : 0,"initializing_shards" : 0,"unassigned_shards" : 0,"delayed_unassigned_shards" : 0,"number_of_pending_tasks" : 0,"number_of_in_flight_fetch" : 0,"task_max_waiting_in_queue_millis" : 0,"active_shards_percent_as_number" : 100.0
}[root@es-node1 ~]#curl 'http://127.0.0.1:9200/_cat/nodes?v'
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
10.0.0.183 32 96 0 0.37 0.12 0.10 cdfhilmrstw - node-3
10.0.0.181 23 87 0 0.02 0.03 0.00 cdfhilmrstw * node-1
10.0.0.182 56 76 0 0.75 1.01 0.62 cdfhilmrstw - node-2