CentOS7.6安装Elasticsearch7.12

329 阅读7分钟

系统环境

CentOS7.6、JDK1.8、Elasticsearch7.12.0、Kibana7.12.0

一、安装Elasticsearch

1.1、下载

下载Elasticsearch安装包,地址:

https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.12.0-linux-x86_64.tar.gz

1.2、解压

将Elasticsearch安装包解压到/usr/local目录下:

tar -zxvf elasticsearch-7.12.0-linux-x86_64.tar.gz -C /usr/local/

1.3、创建用户组和用户

创建用户组elastic
groupadd elastic
# 查看用户组是否创建成功
cat /etc/group

# 创建用户elastic,-m:创建用户目录(在/home目录下创建一个名为es的文件夹),-g:指定该用户所属用户组
useradd -m -g elastic elastic
# 查看用户是否创建成功
cat /etc/passwd

image.png image.png

1.4、授权

给elastic用户组和用户授权

# 授权,更改elasticsearch-7.12.0文件夹所属用户及用户组为elastic:elastic
chown -R elastic:elastic elasticsearch-7.12.0/

#切换用户elastic
su elastic

1.5、改配置

vi elasticsearch-7.12.0/config/jvm.options 
#修改如下配置 
-Xms512m 
-Xmx512m

image.png

还需要对elasticsearch进行相关的配置,编辑config目录下的elasticsearch.yml文件,重点关注以下7个配置项:

注意:要在 Vim 中显示行号,请按 Esc 键进入命令模式并使用下面的命令来显示行号::set number

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#  配置项1: 集群名称
cluster.name: my-application-es1
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
# 配置项2: 节点名称
node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
# 配置项3: 数据目录
path.data: /usr/local/elasticsearch-7.12.0/data
#
# Path to log files:
# 配置项4: log目录
path.logs: /usr/local/elasticsearch-7.12.0/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# By default Elasticsearch is only accessible on localhost. Set a different
# address here to expose this node on the network:
# 配置项5: 部署的centos的ip地址
network.host: 0.0.0.0
#
# By default Elasticsearch listens for HTTP traffic on the first free port it
# finds starting at 9200. Set a specific HTTP port here:
# 配置项6: 对外暴露默认端口
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when this node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.seed_hosts: ["host1", "host2"]
#
# Bootstrap the cluster using an initial set of master-eligible nodes:
# 配置项7: 初始主节点
cluster.initial_master_nodes: ["node-1"]
#
# For more information, consult the discovery and cluster formation module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true

注意:检查elasticsearch-7.12.0目录下,data目录和logs目录是否存在,如果不存在,则手动创建

1.6、启动Elasticsearch

.elasticsearch-7.12.0/bin/elasticsearch

#后台启动命令 
.elasticsearch-7.12.0/bin/elasticsearch -d

1.7、启动报错问题

1.7.1、报错1:

报错信息

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

问题原因

虚拟机的最大虚拟内存面积。最大映射计数[65530]太低,至少增加到[262144]

解决方案

调整虚拟机的最大映射计数为262144,切换root用户下,使用vim /etc/sysctl.conf命令编辑sysctl.conf文件,添加以下内容:

vim /etc/sysctl.conf 
vm.max_map_count=262144

1.7.2、报错2:

报错信息

max file descriptors [4096] for elasticsearch process is too low, increase to at least [65535]

问题原因

当前elasticsearch进程文件描述符4096个句柄,数量太小,至少65535个句柄

解决方案

调整系统文件描述符句柄大小为65535

# 切换到root用户 
su root 
# 打开配置文件 
vim /etc/security/limits.conf 
# 修改配置文件,在文件末尾添加如下4行: 
* hard nofile 65536 
* soft nofile 65536 
* soft nproc 65536 
* hard nproc 65536

注意:修改好后重启服务器 reboot,然后重新启动es(要记得切换到es用户)

1.8、验证

Elasticsearch启动成功后,在浏览器中输入地址:http://ip:9200/,可以看到如下信息:

{ 
    "name": "node-1", 
    "cluster_name": "my-application-es1", 
    "cluster_uuid": "3EYirnGjQjyjVQ0ElsHiXQ", 
    "version": { 
        "number": "7.12.0", 
        "build_flavor": "default", 
        "build_type": "tar", 
        "build_hash": "78722783c38caa25a70982b5b042074cde5d3b3a", 
        "build_date": "2021-03-18T06:17:15.410153305Z", 
        "build_snapshot": false, 
        "lucene_version": "8.8.0", 
        "minimum_wire_compatibility_version": "6.8.0",
        "minimum_index_compatibility_version": "6.0.0-beta1" 
    }, 
    "tagline": "You Know, for Search" 
}

注意:如果是在云服务器上安装Elasticsearch,记得防火墙对外暴露端口,默认端口是:9200 至此,Elasticsearch安装成功,通过9200这个端口就可以访问啦~~~

二、安装Kibana

2.1、下载Kibana(必须和Elasticsearch版本一致):

https://artifacts.elastic.co/downloads/kibana/kibana-7.12.0-linux-x86_64.tar.gz

2.2、解压

解压到/usr/local目录下

tar -zxvf kibana-7.12.0-linux-x86_64.tar.gz -C /usr/local/

2.3、改配置

修改/usr/local/kibana-7.12.0-linux-x86_64/config下的kibana.yml配置文件,主要修改其中几个地方:

# Kibana is served by a back end server. This setting specifies the port to use.
# 配置项1:kibana对外暴露端口
server.port: 5601

# Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
# The default is 'localhost', which usually means remote machines will not be able to connect.
# To allow connections from remote users, set this parameter to a non-loopback address.
# 配置项2:可访问服务器ip
server.host: "0.0.0.0"

# Enables you to specify a path to mount Kibana at if you are running behind a proxy.
# Use the `server.rewriteBasePath` setting to tell Kibana if it should remove the basePath
# from requests it receives, and to prevent a deprecation warning at startup.
# This setting cannot end in a slash.
#server.basePath: ""

# Specifies whether Kibana should rewrite requests that are prefixed with
# `server.basePath` or require that they are rewritten by your reverse proxy.
# This setting was effectively always `false` before Kibana 6.3 and will
# default to `true` starting in Kibana 7.0.
#server.rewriteBasePath: false

# Specifies the public URL at which Kibana is available for end users. If
# `server.basePath` is configured this URL should end with the same basePath.
#server.publicBaseUrl: ""

# The maximum payload size in bytes for incoming server requests.
#server.maxPayloadBytes: 1048576

# The Kibana server's name.  This is used for display purposes.
#server.name: "your-hostname"

# The URLs of the Elasticsearch instances to use for all your queries.
# 配置项3:es的服务器ip
elasticsearch.hosts: ["http://42.192.205.148:9200"]

# Kibana uses an index in Elasticsearch to store saved searches, visualizations and
# dashboards. Kibana creates a new index if the index doesn't already exist.
# 配置项4:kibana的索引
kibana.index: ".kibana"

# The default application to load.
#kibana.defaultAppId: "home"

# If your Elasticsearch is protected with basic authentication, these settings provide
# the username and password that the Kibana server uses to perform maintenance on the Kibana
# index at startup. Your Kibana users still need to authenticate with Elasticsearch, which
# is proxied through the Kibana server.
# elasticsearch.username: "kibana_system"
# elasticsearch.password: "pass"

# Enables SSL and paths to the PEM-format SSL certificate and SSL key files, respectively.
# These settings enable SSL for outgoing requests from the Kibana server to the browser.
#server.ssl.enabled: false
#server.ssl.certificate: /path/to/your/server.crt
#server.ssl.key: /path/to/your/server.key

# Optional settings that provide the paths to the PEM-format SSL certificate and key files.
# These files are used to verify the identity of Kibana to Elasticsearch and are required when
# xpack.security.http.ssl.client_authentication in Elasticsearch is set to required.
#elasticsearch.ssl.certificate: /path/to/your/client.crt
#elasticsearch.ssl.key: /path/to/your/client.key

# Optional setting that enables you to specify a path to the PEM file for the certificate
# authority for your Elasticsearch instance.
#elasticsearch.ssl.certificateAuthorities: [ "/path/to/your/CA.pem" ]

# To disregard the validity of SSL certificates, change this setting's value to 'none'.
#elasticsearch.ssl.verificationMode: full

# Time in milliseconds to wait for Elasticsearch to respond to pings. Defaults to the value of
# the elasticsearch.requestTimeout setting.
#elasticsearch.pingTimeout: 1500

# Time in milliseconds to wait for responses from the back end or Elasticsearch. This value
# must be a positive integer.
#elasticsearch.requestTimeout: 30000

# List of Kibana client-side headers to send to Elasticsearch. To send *no* client-side
# headers, set this value to [] (an empty list).
#elasticsearch.requestHeadersWhitelist: [ authorization ]
# Header names and values that are sent to Elasticsearch. Any custom headers cannot be overwritten
# by client-side headers, regardless of the elasticsearch.requestHeadersWhitelist configuration.
#elasticsearch.customHeaders: {}

# Time in milliseconds for Elasticsearch to wait for responses from shards. Set to 0 to disable.
#elasticsearch.shardTimeout: 30000

# Logs queries sent to Elasticsearch. Requires logging.verbose set to true.
#elasticsearch.logQueries: false

# Specifies the path where Kibana creates the process ID file.
#pid.file: /run/kibana/kibana.pid

# Enables you to specify a file where Kibana stores log output.
#logging.dest: stdout

# Set the value of this setting to true to suppress all logging output.
#logging.silent: false

# Set the value of this setting to true to suppress all logging output other than error messages.
#logging.quiet: false

# Set the value of this setting to true to log all events, including system usage information
# and all requests.
#logging.verbose: false

# Set the interval in milliseconds to sample system and process performance
# metrics. Minimum is 100ms. Defaults to 5000.
#ops.interval: 5000

# Specifies locale to be used for all localizable strings, dates and number formats.
# Supported languages are the following: English - en , by default , Chinese - zh-CN .
#i18n.locale: "en"

2.4、授权

对elastic进行kibana目录授权:

chown -R elastic:elastic kibana-7.12.0-linux-x86_64

2.5、启动

切换到elastic用户,启动kibana:

# 切换到elastic用户
su elastic 
# 进入到kibana的bin目录 
cd /usr/local/kibana-7.12.0-linux-x86_64/bin 
# 启动kibana 
nohup ./kibana >/dev/null 2>&1 &

注意:如果是在云服务器上,需要开通防火墙,暴露kibana对外端口,默认是:5601

2.6、验证

Kibana启动成功后,在浏览器中输入地址:http://ip:5601/,可以看到如下图:

image.png

至此,Kibana安装完成,通过5601端口就能访问啦~~~