ElasticSearch

Author Avatar
沐成尘 12月 09, 2022
  • 在其它设备中阅读本文章

ElasticSearch 集群安装

安装

下载

创建用户组和用户

  • 用户名根据实际情况定,这里就用 elastic 了
# 创建用户组
groupadd elastic
# 创建用户
useradd -m -d /home/elastic -g elastic elastic
# 修改密码
passwd elastic

创建目录

# 创建ElasticSearch数据目录和日志目录
mkdir -p /opt/es/data
mkdir -p /opt/es/logs
  • 解压至当前目录
tar -zxvf elasticsearch-7.13.2-linux-x86_64.tar.gz -C /opt/es/
  • 赋予所有人都可以修改/读取的权限
chmod -R 777 /opt/es/*

修改配置

cd /opt/es/elasticsearch-7.13.2/config/
vim elasticsearch.yml
  • 修改ip等信息
# 集群配置
cluster.name: ES #修改集群名称,自定义即可
node.name: node01 #修改当前ES节点名称
node.master: true #作为master节点
node.data: true #作为node节点
path.data: /opt/es/data #修改数据保存目录
path.logs: /opt/es/logs #修改日志保存目录
network.host: 192.xxx.xxx.xxx #修改ES网络IP
discovery.seed_hosts: ["ip", "ip", "ip"] #集群通信
cluster.initial_master_nodes: ["node01", "node02","node03"] #添加集群节点名称

# 密码配置
xpack.security.enabled: true
xpack.license.self_generated.type: basic
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 #证书位置

配置证书

cd /opt/es/elasticsearch-7.13.2
  • 在 /opt/es/elasticsearch-7.13.2目录下生成 elastic-stack-ca.p12 文件
./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]:   #直接回车
Enter password for elastic-stack-ca.p12 :  #直接回车
  • 在 /opt/es/elasticsearch-7.13.2目录下生成elastic-certificates.p12文件
./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)
      unless the --self-signed command line option is specified.
      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) :  #直接回车
Please enter the desired output file [elastic-certificates.p12]:  #直接回车
Enter password for elastic-certificates.p12 :  #直接回车

Certificates written to /opt/es/elasticsearch-7.13.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.
  • 移动 elastic-stack-ca.p12 elastic-certificates.p12 到config目录下
mv elastic-stack-ca.p12 elastic-certificates.p12 config/
  • 如果之前节点证书设置了密码,将密码添加到 keystore
./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password

拷贝安装目录

  • 分发ElasticSearch目录到集群所有服务器,同时修改每个文件的node.name和network.host

修改相关配置文件

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

原因:elasticsearch用户拥有的内存权限太小,至少需要262144

vim /etc/security/limits.conf


# 在最后添加

* soft nofile 65536
* hard nofile 131072
* soft nproc 4096
* hard nproc 4096
  • 切换到root用户,在/etc/sysctl.conf文件最后添加一行
echo "vm.max_map_count=655360" >> /etc/sysctl.conf
  • 添加完毕之后,执行命令,使配置生效:
sysctl -p

启动

  • 切换用户
su elastic
  • 启动
./bin/elasticsearch -d

设置密码

  • 这一步需要将集群中的所有机器启动。
  • 访问ip 验证集群的通讯是否正常。
http://ip:9200/_cluster/stats?pretty
  • 集群构建成功后:
# 手动设置密码
./bin/elasticsearch-setup-passwords interactive

Initiating the setup of passwords for reserved users elastic,apm_system,kibana,kibana_system,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_system]: 
Reenter password for [kibana_system]: 
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_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]