Linux服务器安装ElasticSearch
一、前言
Elasticsearch 是一个分布式、高扩展、高实时的搜索与数据分析引擎。它能很方便的使大量数据具有搜索、分析和探索的能力。充分利用Elasticsearch的水平伸缩性,能使数据在生产环境变得更有价值。Elasticsearch 的实现原理主要分为以下几个步骤,首先用户将数据提交到Elasticsearch 数据库中,再通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据,当用户搜索数据时候,再根据权重将结果排名,打分,再将返回结果呈现给用户。
二、下载

三、安装
1.1、创建ES文件夹及用户
#创建es文件夹
mkdir es
#解压es文件
tar -zxvf elasticsearch-7.6.2-linux-x86_64.tar.gz
#重命名es文件
mv elasticsearch-7.6.2-linux-x86_64.tar.gz elasticsearch
#创建ES用户组
groupadd es
useradd es -g es -p es
#将elasticsearch文件夹权限增加到ES用户组
chown -Rf es:es elasticsearch
1.2、修改es所用java版本
#打开es配置jdk文件
cd bin
vim elasticsearch-env
#==============================修改前=========================================
# now set the path to java
if [ ! -z "$JAVA_HOME" ]; then
JAVA="$JAVA_HOME/bin/java"
JAVA_TYPE="JAVA_HOME"
#===============================修改后=======================================
# now set the path to java
ES_JAVA_HOME='/usr/local/es/elasticsearch/jdk/'
if [ ! -z "$ES_JAVA_HOME" ]; then
JAVA="$ES_JAVA_HOME/bin/java"
JAVA_TYPE="ES_JAVA_HOME"
#================================保存后退出==================================
1.3、修改配置es内存
#使用root用户进入/etc/security/limits.conf
vi /etc/security/limits.conf
#在文件末尾加入下文
# End of file
es soft nofile 65535
es hard nofile 65535
es soft nproc 4096
es hard nproc 4096
#使用root用户进入/etc/sysctl.conf
vi /etc/sysctl.conf
#在末尾添加下文
vm.max_map_count = 262144
#刷新
sysctl -p //刷新或/sbin/sysctl -p
#配置运行内存
#进入use/local/es/elasticsearch/config/jvm.options
#添加
-Xms8g
-Xmx8g
1.4、配置默认基础参数
目录:elasticsearch-7.6.2/config/elasticsearch.yml
```shell
cluster.name:es-test
node.name = node-1
network.host:0.0.0.0
http.port:9200
cluster.initial_master_nodes:["node-1"]
http.cors.enabled:true
http.cors.allow-origin:"*"
1.5、启动
#启动
./bin/elasticsearch
1.5、ik分词器安装(ingest-attachment-5.4.2同下)
#第一种安装方法:
./elasticsearch-plugin install https://gethub.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.1/elasticsearch-analysis-ik-7.6.1.zip
#导入插件
#./elasticsearch-plugin install ingest-attachment
./elasticsearch-plugin install file:///home/michael/ingest-attachment-7.1.1.zip #推荐使用
#第二种安装方法(本地安装)
#解压ik插件
tar -zxvf elasticsearch-analysis-ik-7.6.2.zip
#进入/usr/share/elasticsearch/plugins/文件夹下新建ik分词器文件夹
mkdir ik
#在ik目录下上传插件并解压ik插件
tar -zxvf elasticsearch-analysis-ik-7.6.2.zip
1.6、常见错误
1.6.1、常见错误
1.[WARN ][o.e.b.BootstrapChecks ] [VM-0-12-centos] max number of threads [1024] for user [es] is too low, increase to at least [4096] //内存太小
2.[WARN ][o.e.b.BootstrapChecks ] [VM-0-12-centos] max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] //内存太小
3.[WARN ][o.e.b.BootstrapChecks ] [VM-0-12-centos] the default discovery settings are unsuitable for production use; at least one of [discovery.seed_hosts, discovery.seed_providers, cluster.initial_master_nodes] must be configured
//默认配置没有配置需要配置
//discovery.seed_hosts集群发现配置每个值格式为:host:port或host
//discovery.seed_providers以文件的方式提供主机列表
//cluster.initial_master_nodes指定可以成为master的所有节点的name或者ip,这些配置将会在第一次选举中进行计算
解决方式:进行1.3和1.4的操作
1.6.2、常见错误
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决方式:在/etc/sysctl.conf文件最后添加一行vm.max_map_count = 262144执行/sbin/sysctl -p 立即生效
1.6.3、常见错误
system call filters failed to install; check the logs and fix your configuration or disable system call filters at your own risk
解决方式:
Centos6不支持SecComp,而ES5.2.0默认bootstrap.system_call_filter为true
禁用:在elasticsearch.yml中配置bootstrap.system_call_filter为false,注意要在Memory下面:
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
discovery.zen.minimum_master_nodes: 1 #最多的主节点个数
生成xpack ca证书
bin/elasticsearch-certutil ca
#---------------------------------------------------------------------------------------------------------
This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.
The 'ca' mode generates a new 'certificate authority'
This will create a new X.509 certificate and private key that can be used
to sign certificate when running in 'cert' mode.
Use the 'ca-dn' option if you wish to configure the 'distinguished name'
of the certificate authority
By default the 'ca' mode produces a single PKCS#12 output file which holds:
* The CA certificate
* The CA's private key
If you elect to generate PEM format certificates (the -pem option), then the output will
be a zip file containing individual files for the CA certificate and private key
Please enter the desired output file [elastic-stack-ca.p12]: #回车默认证书名为elastic-stack-ca.p12
Enter password for elastic-stack-ca.p12 : #是否设置密码 回车为空在下面的配置文件中就不需要添加ca证书的密码
#-------------------------------------------------------------------------------------------------------------
bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
#------------------------------------------------------------------------------------------------------------
This tool assists you in the generation of X.509 certificates and certificate
signing requests for use with SSL/TLS in the Elastic stack.
The 'cert' mode generates X.509 certificate and private keys.
* By default, this generates a single certificate and key for use
on a single instance.
* The '-multiple' option will prompt you to enter details for multiple
instances and will generate a certificate and key for each one
* The '-in' option allows for the certificate generation to be automated by describing
the details of each instance in a YAML file
* An instance is any piece of the Elastic Stack that requires an SSL certificate.
Depending on your configuration, Elasticsearch, Logstash, Kibana, and Beats
may all require a certificate and private key.
* The minimum required value for each instance is a name. This can simply be the
hostname, which will be used as the Common Name of the certificate. A full
distinguished name may also be used.
* A filename value may be required for each instance. This is necessary when the
name would result in an invalid file or directory name. The name provided here
is used as the directory name (within the zip) and the prefix for the key and
certificate files. The filename is required if you are prompted and the name
is not displayed in the prompt.
* IP addresses and DNS names are optional. Multiple values can be specified as a
comma separated string. If no IP addresses or DNS names are provided, you may
disable hostname verification in your SSL configuration.
* All certificates generated by this tool will be signed by a certificate authority (CA).
* The tool can automatically generate a new CA for you, or you can provide your own with the
-ca or -ca-cert command line options.
By default the 'cert' mode produces a single PKCS#12 output file which holds:
* The instance certificate
* The private key for the instance certificate
* The CA certificate
If you specify any of the following options:
* -pem (PEM formatted output)
* -keep-ca-key (retain generated CA key)
* -multiple (generate multiple certificates)
* -in (generate certificates from an input file)
then the output will be be a zip file containing individual certificate/key files
Enter password for CA (elastic-stack-ca.p12) : #输入ca颁发机构的密码 ,如果有密码则输入。如果上面回车设置为空,则 回车
Please enter the desired output file [elastic-certificates.p12]: #回车 默认证书文件为elastic-certificates.p12
Enter password for elastic-certificates.p12 : #给这个证书文件加密(回车,不加密)
Certificates written to /usr/local/elasticsearch-7.6.2/elastic-certificates.p12
This file should be properly secured as it contains the private key for
your instance.
This file is a self contained file and can be copied and used 'as is'
For each Elastic product that you wish to configure, you should copy
this '.p12' file to the relevant configuration directory
and then follow the SSL configuration instructions in the product guide.
For client applications, you may only need to copy the CA certificate and
configure the client to trust this certificate.
#-----------------------------------------------------------------------------------------------------------------------------
#如有集群则配置证书到es集群下的config文件夹内
[root@es_master elasticsearch-7.6.2]# mv elastic-certificates.p12 elastic-stack-ca.p12 config/
[root@es_master config]# scp elastic-certificates.p12 elastic-stack-ca.p12 root@es_node1:/usr/local/elasticsearch-7.6.2/config/
[root@es_master config]# scp elastic-certificates.p12 elastic-stack-ca.p12 root@es_node2:/usr/local/elasticsearch-7.6.2/config/
修改elasticsearch.yml文件
#在结尾添加
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
xpack.security.transport.ssl.keystore.path: elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: elastic-certificates.p12
启动es
./elasticsearch -d
设置用户和密码
./elasticsearch-setup-passwords interactive
#---------------------------------------------------------------------------------------------------
Initiating the setup of passwords for reserved users elastic,apm_system,kibana,logstash_system,beats_system,remote_monitoring_user.
You will be prompted to enter passwords as the process progresses.
Please confirm that you would like to continue [y/N]y
Enter password for [elastic]:
Reenter password for [elastic]:
Enter password for [apm_system]:
Reenter password for [apm_system]:
Enter password for [kibana]:
Reenter password for [kibana]:
Enter password for [logstash_system]:
Reenter password for [logstash_system]:
Enter password for [beats_system]:
Reenter password for [beats_system]:
Enter password for [remote_monitoring_user]:
Reenter password for [remote_monitoring_user]:
Changed password for user [apm_system]
Changed password for user [kibana]
Changed password for user [logstash_system]
Changed password for user [beats_system]
Changed password for user [remote_monitoring_user]
Changed password for user [elastic]
#49161@JMKWZ
#--------------------------------------------------------------------------------------------------------
#如果有集群验证
[root@es_master config]# curl -XGET -u elastic es_master:9200/_cat/health?
Enter host password for user 'elastic':
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1633682925 08:48:45 es_test green 3 3 2 1 0 0 0 0 - 100.0%
##########如果没有用户认证则会报错。
[root@es_master elasticsearch-7.6.2]# curl -XGET es_node1:9200
{"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":"Basic realm=\"security\" charset=\"UTF-8\""}},"status":401}
[root@es_master kibana]# curl -XGET -u elastic "http://127.0.0.1:9200/_cat/nodes
#---------------------------------------------------------------------------------------------------------
忘记用户名和密码解决方案
#创建一个临时的超级用户admin
#====================================================================================================================
[root@cfeea elasticsearch]# ./bin/elasticsearch-users useradd TestSuper -r superuser
Enter new password:
Retype new password:
#------------------------------------------------------------------------------------------------------------------
#用这个用户去修改elastic的密码
#====================================================================================================================
curl -XPUT -u aa:aa@mm http://localhost:9200/_xpack/security/user/elastic/_password -H "Content-Type: application/json" -d '
{
"password": "3123123123"
}'
#--------------------------------------------------------------------------------------------------------------------
四、验证
ps -ef | grep es
curl http://localhost:9200

浙公网安备 33010602011771号