Centos7下部署Elasticsearch集群

环境准备:

1.设置主机名

[root@centos7-temp ~]# hostnamectl set-hostname es-node1.zhang.org

[root@centos7-temp ~]# hostnamectl set-hostname es-node2.zhang.org

[root@centos7-temp ~]# hostnamectl set-hostname es-node3.zhang.org

 2.关闭防所有服务器的防火墙和 SELinux

[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 

3.各服务器配置本地域名解析 

[root@es-node1 ~]# vim /etc/hosts
10.0.0.100 es-node1.zhang.org
10.0.0.101 es-node2.zhang.org
10.0.0.102 es-node3.zhang.org
  • 包安装

1.下载rpm包

[root@es-node1 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/yum/elastic-8.x/8.6.2/elasticsearch-8.6.2-x86_64.rpm

[root@es-node2 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/yum/elastic-8.x/8.6.2/elasticsearch-8.6.2-x86_64.rpm

[root@es-node3 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/elasticstack/yum/elastic-8.x/8.6.2/elasticsearch-8.6.2-x86_64.rpm

2.安装

[root@es-node1 ~]# yum install -y elasticsearch-8.6.2-x86_64.rpm

[root@es-node2 ~]# yum install -y elasticsearch-8.6.2-x86_64.rpm

[root@es-node3 ~]# yum install -y elasticsearch-8.6.2-x86_64.rpm

#内置java所以不需要单独部署JDK

[root@es-node1 ~]# /usr/share/elasticsearch/jdk/bin/java -version
openjdk version "19.0.2" 2023-01-17
OpenJDK Runtime Environment (build 19.0.2+7-44)
OpenJDK 64-Bit Server VM (build 19.0.2+7-44, mixed mode, sharing)

3.启动并设置开机自启

systemctl enable --now elasticsearch.service

4.默认8.X后开启xpack安全,端口无法访问

[root@es-node1 ~]# curl 127.0.0.1:9200
curl: (52) Empty reply from server

#解决方法:

[root@es-node1 ~]# vim /etc/elasticsearch/elasticsearch.yml
...省略...
xpack.security.enabled: false
...省略...
[root@es-node1 ~]# systemctl restart elasticsearch.service
[root@es-node1 ~]# curl 127.0.0.1:9200
{
  "name" : "es-node1",
  "cluster_name" : "es-cluster",
  "cluster_uuid" : "o0aJTHFjRu-Ylx9M0FXroQ",
  "version" : {
    "number" : "8.6.1",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "180c9830da956993e59e2cd70eb32b5e383ea42c",
    "build_date" : "2023-01-24T21:35:11.506992272Z",
    "build_snapshot" : false,
    "lucene_version" : "9.4.2",
    "minimum_wire_compatibility_version" : "7.17.0",
    "minimum_index_compatibility_version" : "7.0.0"
  },
  "tagline" : "You Know, for Search"
}

配置文件说明:

[root@es-node1 ~]# grep "^[a-Z]" /etc/elasticsearch/elasticsearch.yml

cluster.name: ELK-Cluster
#ELK集群名称,单节点无需配置,同一个集群内每个节点的此项必须相同,新加集群的节点
此项和其它节点相同即可加入集群,而无需再验证


node.name: es-node1
#当前节点在集群内的节点名称,同一集群中每个节点要确保此名称唯一


path.data: /data/es-data
#ES 数据保存目录,包安装默认路径:/var/lib/elasticsearch/


path.logs: /data/es-logs
#ES 日志保存目录,包安装默认路径:/var/log/elasticsearch/


bootstrap.memory_lock: true
#服务启动的时候立即分配(锁定)足够的内存,防止数据写入swap,提高启动速度,但是true会导致启动失
败,需要优化


network.host: 0.0.0.0
#指定该节点监听IP,如果绑定了错误的IP,可将此修改为指定IP


http.port: 9200
#监听端口
discovery.seed_hosts: ["10.0.0.100","10.0.0.101","10.0.0.102"]
#发现集群的node节点列表,可以添加部分或全部节点IP
#在新增节点到已有集群时,此处需指定至少一个已经在集群中的节点地址


cluster.initial_master_nodes: ["10.0.0.100","10.0.0.101","10.0.0.102"]
#集群初始化时指定希望哪些节点可以被选举为 master,只在初始化时使用,新加节点到已有集群时此项可
不配置


gateway.recover_after_nodes: 2
#一个集群中的 N 个节点启动后,才允许进行数据恢复处理,默认是1,一般设为为所有节点的一半以上,防
止出现脑裂现象
#当集群无法启动时,可以将之修改为1,或者将下面行注释掉,实现快速恢复启动


action.destructive_requires_name: true
#设置是否可以通过正则表达式或者_all匹配索引库进行删除或者关闭索引库,默认true表示必须需要明
确指定索引库名称,不能使用正则表达式和_all,生产环境建议设置为 true,防止误删索引库


node.master: false
#如果不参与主节点选举设为false,默认值为true

node.data: true
#存储数据,默认值为true,此值为false则不存储数据而成为一个路由节点
#如果将原有的true改为false,需要先执行/usr/share/elasticsearch/bin/elasticsearch-node
repurpose 清理数据


discovery.zen.ping.unicast.hosts: ["10.0.0.100","10.0.0.101","10.0.0.102"]
#7.x以后版本下面指令已废弃,在2.x 5.x 6.x 版本中用于配置节点发现列表

5.修改配置文件

[root@es-node1 ~]# grep -Ev '^$|#' /etc/elasticsearch/elasticsearch.yml
cluster.name: es-cluster
node.name: es-node1
path.data: /data/es-data
path.logs: /data/es-logs
bootstrap.memory_lock: true
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["10.0.0.100","10.0.0.101","10.0.0.102"]
cluster.initial_master_nodes: ["10.0.0.100","10.0.0.101","10.0.0.102"]
action.destructive_requires_name: true
xpack.security.enabled: false
xpack.security.enrollment.enabled: true
xpack.security.http.ssl:
  enabled: true
  keystore.path: certs/http.p12
xpack.security.transport.ssl:
  enabled: true
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
http.host: 0.0.0.0

 6.将修改过的配置文件远程复制到另外两个节点

[root@es-node1 ~]# scp /etc/elasticsearch/elasticsearch.yml 10.0.0.101:/etc/elasticsearch/

[root@es-node1 ~]# scp /etc/elasticsearch/elasticsearch.yml 10.0.0.102:/etc/elasticsearch/

7.再次修改配置文件

[root@es-node2 ~]# grep -Ev '^$|#' /etc/elasticsearch/elasticsearch.yml
cluster.name: es-cluster
node.name: es-node2
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["10.0.0.100","10.0.0.101","10.0.0.102"]
cluster.initial_master_nodes: ["10.0.0.100","10.0.0.101","10.0.0.102"]
xpack.security.enabled: false
xpack.security.enrollment.enabled: true
xpack.security.http.ssl:
  enabled: true
  keystore.path: certs/http.p12
xpack.security.transport.ssl:
  enabled: true
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
http.host: 0.0.0.0

[root@es-node3 ~]# grep -Ev '^$|#' /etc/elasticsearch/elasticsearch.yml
cluster.name: es-cluster
node.name: es-node3
path.data: /var/lib/elasticsearch
path.logs: /var/log/elasticsearch
network.host: 0.0.0.0
http.port: 9200
discovery.seed_hosts: ["10.0.0.100","10.0.0.101","10.0.0.102"]
cluster.initial_master_nodes: ["10.0.0.100","10.0.0.101","10.0.0.102"]
xpack.security.enabled: false
xpack.security.enrollment.enabled: true
xpack.security.http.ssl:
  enabled: true
  keystore.path: certs/http.p12
xpack.security.transport.ssl:
  enabled: true
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
http.host: 0.0.0.0

 8.优化资源配置

vim /lib/systemd/system/elasticsearch.service
[Service]
#加下面一行
LimitMEMLOCK=infinity

9. 重载配置并重启服务及检查服务状态

systemctl daemon-reload

systemctl restart elasticsearch.service

systemctl is-active elasticsearch.service

10.测试访问

[root@es-node1 ~]# curl http://es-node1.zhang.org

[root@es-node1 ~]# curl http://es-node2.zhang.org

[root@es-node1 ~]# curl http://es-node3.zhang.org

 11.创建数据和日志目录并分配权限

mkdir -p /data/es-{data,logs}
 chown -R elasticsearch.elasticsearch /data/

你可能感兴趣的:(ELK,elasticsearch,elk)