什么是ES:
Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎。
官网地址:
https://www.elastic.co/cn/elasticsearch/
单点部署ES:
1.安装es
yum -y localinstall elasticsearch-7.17.5-x86_64.rpm
2.修改配置文件
vim /etc/elasticsearch/elasticsearch.yml
...
# 暴露ES服务的IP地址。
network.host: 0.0.0.0
# 设置ES集群的列表。
discovery.seed_hosts: ["10.0.0.101"]
3.启动es服务
systemctl enable --now elasticsearch
4.ES的端口说明
9200:
ES对外部提供的访问端口,走的是http/https协议。
9300:
ES集群内部的端口,走的是tcp/ip协议。
5.验证
访问http://10.0.0.101:9200/
补充知识: 查看日志
journalctl -u elasticsearch
ES集群搭建:
0.关闭防火墙和禁用SELINUX
systemctl disable --now firewalld && setenforce 0
systemctl stop elasticsearch
1.配置主机解析
cat >> /etc/hosts <
10.0.0.102 elk102.oldboyedu.com
10.0.0.103 elk103.oldboyedu.com
EOF
2.所有节点安装ES服务
yum -y localinstall elasticsearch-7.17.5-x86_64.rpm
3.修改集群的配置文件
vim /etc/elasticsearch/elasticsearch.yml
...
# 指定集群的名称
cluster.name: oldboyedu-linux82
# ES的数据路径
path.data: /var/lib/elasticsearch
# ES的日志路径
path.logs: /var/log/elasticsearch
# 暴露服务的IP地址
network.host: 0.0.0.0
# ES集群的白名单列表
discovery.seed_hosts: ["elk101.oldboyedu.com","elk102.oldboyedu.com","elk103.oldboyedu.com"]
# 启动ES集群时,参与master选举的主机列表。
cluster.initial_master_nodes: ["elk101.oldboyedu.com","elk102.oldboyedu.com","elk103.oldboyedu.com"]
4.清空elk101节点的旧数据,部署集群
rm -rf /var/lib/elasticsearch/*
rm -f /var/log/elasticsearch/*
rm -rf /tmp/*
5.同步配置文件
scp /etc/elasticsearch/elasticsearch.yml 10.0.0.102:/etc/elasticsearch/elasticsearch.yml
scp /etc/elasticsearch/elasticsearch.yml 10.0.0.103:/etc/elasticsearch/elasticsearch.yml
6.启动ES集群
systemctl enable --now elasticsearch
7.验证
curl 10.0.0.101:9200/_cat/nodes
温馨提示:
(1)集群有N台服务器,只要有半数以上节点存活(N/2+1),则此集群可以正常对外提供服务。
(2)如果集群想要容忍N台故障,建议设置集群的服务器数量为"2N +1"台。
小彩蛋:
vim /root/.bashrc
...
alias yy='egrep -v "^#|^$"'
source /root/.bashrc
curl 10.0.0.101:9200/_cat/nodes
URL:
...
URI:
....
----
URL : 协议://主机[:端口]/path/to/file/index.html
curl -X GET http://10.0.0.101:9200/_cat/nodes
curl http://10.0.0.101:9200/_cat/nodes
---
准备工作
(1)修改主机列表
cat >> /etc/hosts <<'EOF'
10.0.0.101 elk101.oldboyedu.com
10.0.0.102 elk102.oldboyedu.com
10.0.0.103 elk103.oldboyedu.com
EOF
(2)elk101节点上生成密钥对
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa -q
(3)elk101配置所有集群节点的免密登录
for ((host_id=101;host_id<=103;host_id++));do ssh-copy-id elk${host_id}.oldboyedu.com ;done
(4)链接测试
ssh 'elk101.oldboyedu.com'
ssh 'elk102.oldboyedu.com'
ssh 'elk103.oldboyedu.com'
(5)所有节点安装rsync数据同步工具
yum -y install rsync
(6)编写同步脚本
cat > /usr/local/sbin/data_rsync.sh <<'EOF'
#!/bin/bash
# Auther: Jason Yin
if [ $# -ne 1 ];then
echo "Usage: $0 /path/to/file(绝对路径)"
exit
fi
# 判断文件是否存在
if [ ! -e $1 ];then
echo "[ $1 ] dir or file not find!"
exit
fi
# 获取父路径
fullpath=`dirname $1`
# 获取子路径
basename=`basename $1`
# 进入到父路径
cd $fullpath
for ((host_id=102;host_id<=103;host_id++))
do
# 使得终端输出变为绿色
tput setaf 2
echo ===== rsyncing elk${host_id}.oldboyedu.com: $basename =====
# 使得终端恢复原来的颜色
tput setaf 7
# 将数据同步到其他两个节点
rsync -apz $basename `whoami`@elk${host_id}.oldboyedu.com:$fullpath
if [ $? -eq 0 ];then
echo "命令执行成功!"
fi
done
EOF
(7)给脚本授权
chmod +x /usr/local/sbin/data_rsync.sh
ES集群的二进制部署:
(1)所有节点创建工作目录
useradd oldboyedu
install -d /oldboyedu/{data,logs,softwares} -o oldboyedu -g oldboyedu
install -d /oldboyedu/logs/es7/ -o oldboyedu -g oldboyedu
install -d /oldboyedu/data/es7/ -o oldboyedu -g oldboyedu
(2)elk101节点解压软件包到指定的目录
tar xf elasticsearch-7.17.5-linux-x86_64.tar.gz -C /oldboyedu/softwares/
chown oldboyedu:oldboyedu -R /oldboyedu/softwares/elasticsearch-7.17.5
(3)elk101节点修改配置文件
vim /oldboyedu/softwares/elasticsearch-7.17.5/config/elasticsearch.yml
...
# 温馨提示,对于各个节点的"node.name"的值不能重复。建议写各个节点的主机名/IP,也可以不设置,默认就是主机名。否则可能导致集群起不来。
cluster.name: oldboyedu-linux82-es7
path.data: /oldboyedu/data/es7
path.logs: /oldboyedu/logs/es7
network.host: 0.0.0.0
discovery.seed_hosts: ["elk101.oldboyedu.com","elk102.oldboyedu.com","elk103.oldboyedu.com"]
cluster.initial_master_nodes: ["elk101.oldboyedu.com","elk102.oldboyedu.com","elk103.oldboyedu.com"]
(4)elk101节点安装jdk环境并配置环境变量
tar xf jdk-8u291-linux-x64.tar.gz -C /oldboyedu/softwares/
vim /etc/profile.d/jdk.sh
#!/bin/bash
export JAVA_HOME=/oldboyedu/softwares/jdk1.8.0_291
export ES_JAVA_HOME=/oldboyedu/softwares/jdk1.8.0_291
export PATH=$PATH:$JAVA_HOME/bin:$ES_JAVA_HOME/bin
source /etc/profile.d/jdk.sh每台都要执行
java -version
(5)配置ES的文件描述符上线,最低要求65535
vi /etc/security/limits.d/es.conf
i* soft nofile 131070
* hard nofile 131070
(6)同步数据到其它节点
data_rsync.sh /etc/profile.d/jdk.sh
data_rsync.sh /etc/security/limits.d/es.conf
data_rsync.sh /oldboyedu/
(7)启动集群
su - oldboyedu -c "/oldboyedu/softwares/elasticsearch-7.17.5/bin/elasticsearch -d" # 手动启动
(8)验证服务
curl 10.0.0.101:9200/_cat/nodes
温馨提示: 若需要重新清理环境,建议先将下面的几个数据目录删除一波数据。
rm -rf /oldboyedu/data/es7/*
rm -f /oldboyedu/logs/es7/*
rm -rf /tmp/*
ES集群多实例:
(1)所有节点创建工作目录
install -d /oldboyedu/logs/es6/ -o oldboyedu -g oldboyedu
install -d /oldboyedu/data/es6/ -o oldboyedu -g oldboyedu
(2)解压软件包
tar xf elasticsearch-6.8.23.tar.gz -C /oldboyedu/softwares/
chown oldboyedu:oldboyedu -R /oldboyedu/softwares/elasticsearch-6.8.23
(3)修改配置文件
vim /oldboyedu/softwares/elasticsearch-6.8.23/config/elasticsearch.yml
...
cluster.name: oldboyedu-linux82-es6
path.data: /oldboyedu/data/es6/
path.logs: /oldboyedu/logs/es6/
network.host: 0.0.0.0
# 指定ES集群的白名单列表
discovery.zen.ping.unicast.hosts: ["elk101.oldboyedu.com","elk102.oldboyedu.com","elk103.oldboyedu.com"]
# 集群参与master选举的服务器数量,官方建议设置为能参与master选举数量的半数以上,以防止脑裂。
discovery.zen.minimum_master_nodes: 2
# ES的外部提供访问的服务端口
http.port: 19200
# ES集群内部通信的端口
transport.tcp.port: 19300
(4)同步软件到其它节点
data_rsync.sh /oldboyedu/softwares/elasticsearch-6.8.23/
(5)启动服务
su - oldboyedu -c "/oldboyedu/softwares/elasticsearch-6.8.23/bin/elasticsearch -d" # 手动启动