1、环境初始化

最小化安装 Centos 7.x/Ubuntu x86_64 操作系统的虚拟机,vcpu 2,内存 4G 或更多,操作系统盘 50G,为保证效果特额外添加一块单独的数据磁盘大小为 50G 并格式化挂载到/data

1.1 设置主机名和磁盘挂载

节点信息

IP主机名角色系统版本
10.10.100.110es-node1node1Ubuntu 18.04.3
10.10.100.111es-node2node2Ubuntu 18.04.3
10.10.100.112es-node3node3Ubuntu 18.04.3
#修改三个节点的主机名
root@ubuntu-node1:~# hostnamectl set-hostname es-node1
root@ubuntu-node1:~# hostnamectl set-hostname es-node2
root@ubuntu-node1:~# hostnamectl set-hostname es-node3

#分区
root@es-node1:~# lsblk -l
NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda    8:0    0  100G  0 disk 
sda1   8:1    0  100G  0 part /
sdb    8:16   0   50G  0 disk 
sr0   11:0    1 1024M  0 rom 
root@es-node1:~# fdisk /dev/sdb
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-104857599, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599): 

Created a new partition 1 of type 'Linux' and of size 50 GiB.

Command (m for help): w

root@es-node1:~# mkdir /esdata
root@es-node1:~# mkfs.xfs /dev/sdb1 
meta-data=/dev/sdb1              isize=512    agcount=4, agsize=3276736 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=1, sparse=0, rmapbt=0, reflink=0
data     =                       bsize=4096   blocks=13106944, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal log           bsize=4096   blocks=6399, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
root@es-node1:~# mount /dev/sdb1 /esdata
root@es-node1:~# vim /etc/fstab
/dev/sdb1                                /esdata           xfs defaults  0 0 

2、安装elasticsearch集群

https://github.com/elastic #github 地址

2.1 jdk安装

elasticsearch 服务运行需要 java 环境

root@es-node1:~# tar xf jdk-8u301-linux-x64.tar.gz -C /usr/local/
root@es-node1:~# ln -sv /usr/local/jdk1.8.0_301 /usr/local/jdk
root@es-node1:~# ln -sv /usr/local/jdk/bin/java /usr/bin/
root@es-node1:~# vim /etc/profile
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-node2:~# source /etc/profile
root@es-node1:~# java -version
java version "1.8.0_301"
Java(TM) SE Runtime Environment (build 1.8.0_301-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.301-b09, mixed mode)

2.2 下载 elasticsearch 并安装

下载地址:https://www.elastic.co/downloads/elasticsearch

#下载安装包
root@es-node1:~# wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.9.3-amd64.deb
#安装
root@es-node1:~# dpkg -i elasticsearch-7.9.3-amd64.deb

#配置文件
root@es-node1:~# grep "^[^#]" /etc/elasticsearch/elasticsearch.yml 
#ELK 的集群名称,名称相同即属于是同一个集群
cluster.name: es-cluster
#当前节点在集群内的节点名称
node.name: node-1
#ES 数据保存目录 
path.data: /esdata/elasticsearch
#ES 日志保存目
path.logs: /esdata/log/elasticsearch
#服务启动的时候锁定足够的内存,防止数据写入swap 
bootstrap.memory_lock: true
#监听 IP 
network.host: 0.0.0.0
#监听端口
http.port: 9200
#集群中 node 节点发现列表 
discovery.seed_hosts: ["10.10.100.110", "10.10.100.111","10.10.100.112"]
#集群初始化那些节点可以被选举为 master 
cluster.initial_master_nodes: ["10.10.100.110", "10.10.100.111","10.10.100.112"]
#一个集群中的 N 个节点启动后,才允许进行数据恢复处理,默认是 1
gateway.recover_after_nodes: 2
# 设置是否可以通过正则或者_all 删除或者关闭索引库,默认 true 表示必须需要显式指定索引库名称,生产环境建议设置为 true,删除索引库的时候必须指定,否则可能会误删索引库中的索引库。
action.destructive_requires_name: true

#拷贝配置文件到其他节点,并修改节点名称 node.name:
root@es-node1:~# scp /etc/elasticsearch/elasticsearch.yml 10.10.100.111:/etc/elasticsearch/
root@es-node1:~# scp /etc/elasticsearch/elasticsearch.yml 10.10.100.112:/etc/elasticsearch/

2.3 修改内存限制

root@es-node1:~# vim /usr/lib/systemd/system/elasticsearch.service
 LimitMEMLOCK=infinity #无限制使用内存
root@es-node1:~#  vim /etc/elasticsearch/jvm.options
-Xms2g 
-Xmx2g #最小和最大内存限制
#官方配置文档最大建议 30G 以内
https://www.elastic.co/guide/en/elasticsearch/reference/current/heap-size.html

2.4 目录权限修改

root@es-node1:~#  chown elasticsearch.elasticsearch /esdata/ -R

2.5 启动elasticsearch 并验证

#所有节点启动
root@es-node1:~# systemctl start elasticsearch
root@es-node1:~# systemctl enable elasticsearch
root@es-node2:~# systemctl start elasticsearch
root@es-node2:~# systemctl enable elasticsearch
root@es-node3:~# systemctl start elasticsearch
root@es-node3:~# systemctl enable elasticsearch

查看监听端口
在这里插入图片描述
浏览器访问查看
在这里插入图片描述

2.6 安装 elasticsearch 插件 head

2.6.1 amp安装head

root@es-node1:~# apt install -y npm
root@es-node1:~# cd /usr/local/src/
root@es-node1:/usr/local/src# git clone https://github.com/mobz/elasticsearch-head.git
root@es-node1:/usr/local/src# ls
elasticsearch-head
root@es-node1:/usr/local/src# cd elasticsearch-head/
#配置阿里npm镜像
root@es-node1:/usr/local/src/elasticsearch-head# npm --registry https://registry.npm.taobao.org install express
root@es-node1:/usr/local/src/elasticsearch-head# npm config set registry https://registry.npm.taobao.org
root@es-node1:/usr/local/src/elasticsearch-head# npm install grunt -save
#后台运行
root@es-node1:/usr/local/src/elasticsearch-head# npm run start &

2.6.2 通过docker安装head

# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# Step 4: 更新并安装Docker-CE
sudo apt-get -y update
sudo apt-get -y install docker-ce

#启动docker
root@es-node2:~# systemctl start docker
root@es-node2:~# systemctl enable docker

#通过docker启动head
root@es-node2:~# docker run -d -p 9100:9100 mobz/elasticsearch-head:5

2.7 修改 elasticsearch 服务配置文件

开启跨域访问支持,然后重启 elasticsearch 服务

root@es-node1:~# vim /etc/elasticsearch/elasticsearch.yml
#开启支持跨域访问 
http.cors.enabled: true
#指定允许访问范围 
http.cors.allow-origin: "*"

#重启
root@es-node1:~# systemctl restart elasticsearch

2.8 浏览器访问测试

head访问地址 ip:9100
在这里插入图片描述
在这里插入图片描述

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐