2. 单主机 Elasticsearch 双节点或多节点集群环境部署

2022-10-14 补充说明,我在一台2核4G的云服务器上启动两个ES实例,一个Kibana时,会出现没响应的情况,我还是决定搭个单机环境先,暂时不折腾集群了。


我已经买了一年的腾讯云轻量级服务器,并且安装好了ES,也做了一些系统配置,比如 修改vm.max_map_count修改文件描述符数量

同时,也用ES安装目录下的 bin/elasticsearch 脚本尝试了第一次启动 ES,并且用 https://localhost:9200 来访问它。

本文,我打算在我的腾讯云服务器上搭一个双节点的环境,并且用上 kibana 来管理。

网上给出了两种方案:

  1. 把 elasticsearch.tar.gz 解压多次到不同的文件夹,每个文件夹作为一个节点,然后分别修改每个文件夹中的 elasticsearch.yml,再分别启动。比如 Elasticsearch 在本地单机多节点部署集群
  2. 把 elasticsearch.tar.gz 解压一次,然后准备多个YAML配置文件,然后启动时,每个节点用上不同的配置文件。比如 配置Elasticsearch

我倾向于第2种,可以把集群中的各个节点的配置文件放到一个文件夹下,方便查看。

1. 查看可执行文件 elasticsearch

使用命令 more bin/elasticsearch 查看启动脚本,如下图所示:

从启动脚本 elasticsearch 的头部注释可以看出,可以使用

ES_PATH_CONF=/path/to/custom/config ./bin/elasticsearch

这样的命令来指定启动节点时,使用不同的配置文件!

有了思路之后,接下来就开始实践。

2. 准备两个配置文件

配置项 节点1 节点2
节点名称 node1 node2
配置文件目录 /opt/config/es-cluster/node1/ /opt/config/es-cluster/node2/
data目录 /var/lib/es-cluster/node1 /var/lib/es-cluster/node2
log目录 /var/log/es-cluster/node1 /var/log/es-cluster/node2

执行以下命令创建目标文件夹和文件:

[lighthouse@centos ~]$ cd /opt
[lighthouse@centos opt]$ sudo mkdir config
[lighthouse@centos opt]$ cd config
[lighthouse@centos config]$ sudo mkdir es-cluster
[lighthouse@centos config]$ sudo chown elastic:elastic es-cluster/
[lighthouse@centos config]$ ls -al
total 12
drwxr-xr-x   3  root     root     4096  0ct  9  17:59  .
drwxr-xr-x.  6  root     root     4096  0ct  9  17:59  ..
drwxr-xr-x.  2  elastic  elastic  4096  0ct  9  17:59  es-cluster
[lighthouse@centos config]$ su elastic
Password:
[elastic@centos config]$ cd es-cluster
[elastic@centos es-cluster]$ mkdir node1
[elastic@centos es-cluster]$ mkdir node2
[elastic@centos es-cluster]$ touch node1/elasticsearch.yml
[elastic@centos es-cluster]$ touch node2/elasticsearch.yml
[elastic@centos es-cluster]$ exit
[lighthouse@centos es-cluster]$ cd /var/lib
[lighthouse@centos lib]$ sudo mkdir es-cluster
[lighthouse@centos lib]$ cd es-cluster
[lighthouse@centos es-cluster]$ sudo mkdir node1
[lighthouse@centos es-cluster]$ sudo mkdir node2
[lighthouse@centos es-cluster]$ cd ..
[lighthouse@centos lib]$ sudo chown -R elastic:elastic es-cluster/
[lighthouse@centos lib]$ cd /var/log
[lighthouse@centos log]$ sudo mkdir es-cluster
[lighthouse@centos log]$ cd es-cluster
[lighthouse@centos es-cluster]$ sudo mkdir node1
[lighthouse@centos es-cluster]$ sudo mkdir node2
[lighthouse@centos es-cluster]$ cd ..
[lighthouse@centos log]$ sudo chown -R elastic:elastic es-cluster/

修改 node1/elasticsearch.yml 内容如下:

cluster.name: es-cluster
node.name: node1
node.roles: ["master", "data", "ingest"]
network.host: 10.0.4.10
http.port: 9200
transport.port: 9300
path:
  data: /var/lib/es-cluster/node1
  logs: /var/log/es-cluster/node1
discovery.seed_hosts:
  - 10.0.4.10:9300
  - 10.0.4.10:9301
cluster.initial_master_nodes:
  - node1
  - node2
xpack.security.enabled: false
xpack.security.transport.ssl.enabled: false

修改 node2/elasticsearch.yml 内容如下:

cluster.name: es-cluster
node.name: node2
node.roles: ["master", "data", "ingest"]
network.host: 10.0.4.10
http.port: 9201
transport.port: 9301
path:
  data: /var/lib/es-cluster/node2
  logs: /var/log/es-cluster/node2
discovery.seed_hosts:
  - 10.0.4.10:9300
  - 10.0.4.10:9301
cluster.initial_master_nodes:
  - node1
  - node2
xpack.security.enabled: false
xpack.security.transport.ssl.enabled: false

3. 启动两个es实例

[lighthouse@centos es-cluster]$ su elastic
Password: 
[elastic@centos es-cluster]$ cd /opt/elasticsearch-8.1.0
[elastic@centos elasticsearch-8.1.0]$ ES_PATH_CONF=/opt/config/es-cluster/node1 ES_JAVA_OPTS="-Xms256m -Xmx256m" ./bin/elasticsearch -d
[elastic@centos elasticsearch-8.1.0]$ ES_PATH_CONF=/opt/config/es-cluster/node2 ES_JAVA_OPTS="-Xms256m -Xmx256m" ./bin/elasticsearch -d

首次尝试启动时,遇到异常报错 Exception in thread "main" java.nio.file.NoSuchFileException: /opt/config/es-cluster/node1/jvm.options
于是,执行命令拷贝 jvm.options 文件:

[elastic@centos elasticsearch-8.1.0]$ cp config/jvm.options /opt/config/es-cluster/node1
[elastic@centos elasticsearch-8.1.0]$ cp config/jvm.options /opt/config/es-cluster/node2

类似地,还会出现错误 ERROR: no log4j2.properties found; tried [/opt/config/es-cluster/node2] and its subdirectories
因此,执行命令拷贝 log4j2.properties 文件:

[elastic@centos elasticsearch-8.1.0]$ cp config/log4j2.properties /opt/config/es-cluster/node1
[elastic@centos elasticsearch-8.1.0]$ cp config/log4j2.properties /opt/config/es-cluster/node2

4. Kibana

4.1 下载Kibana

https://www.elastic.co/cn/downloads/past-releases#kibana

选择:8.1.0版本下载,如下图所示:

选择:LINUX_X86_64,如下图所示:

解压并把 kibana 移动到目标文件夹:

[lighthouse@centos Downloads]$ tar -zxvf kibana-8.1.0-linux-x86_64.tar.gz
[lighthouse@centos Downloads]$ sudo mv kibana-8.1.0 /opt/kibana-8.1.0

4.2 修改kibana.yml配置

使用命令 cd /opt/kibana-8.1.0/config 进入配置文件夹,再用命令 vim kibana.yml 修改文件

server.port: 5601
server.host: "10.0.4.10"
elasticsearch.hosts: ["http://10.0.4.10:9200"]
server.publicBaseUrl: "http://10.0.4.10:5601" 

server.publicBaseUrl is missing and should be configured when running in a production environment. Some features may not behave correctly. See the documentation.
报错解决方案

4.3 调节kibana内存设置

kibana默认的内存是1.4g,对我这台2核4G的轻量级服务器来说,还是有点用太多了。所以,进入 kibana安装目录/config 文件夹,然后 vim node.options,需要执行的命令如下:

去掉了 --max-old-space-size=4096 前面的 #,然后输入 :qw 保存退出(vim的使用知识,这里不展开)。

4.4 启动kibana

nohup ./bin/kibana --allow-root & > /dev/null 2>&1

kibana 使用 ps -ef|grep kibana查不到进程的,主要原因大概是因为 kibananode 写的。所以 kibana 运行的时候是运行在 node 里面。

所以,可以使用 ps -ef|grep node 查看到进程。

kibana 启动成功后,可以在浏览器中访问:

选择 Explore on my own,新手暂时不需要添加整合 Add integrations

posted @ 2022-10-10 17:57  极客子羽  阅读(2595)  评论(0编辑  收藏  举报