ES8—8.5.2集群安装与配置

服务器优化及预备操作

1、更新

#CentOS/Euler/RHEL
yum update -y
yum install unzip wget -y

#ubuntu
apt update
apt upgrade -y
apt install unzip wget -y

2、优化limit.conf

vim /etc/security/limits.conf

*       soft    nofile  102400
*       hard    nofile  102400
*       soft    nproc  102400
*       hard    nproc  102400

3、sysctl.conf优化

vm.max_map_count = 262144

3、设置主机名及hosts

每个节点设置主机名

hostnamectl set-hostname elk1.dokbok.com
hostnamectl set-hostname elk2.dokbok.com
hostnamectl set-hostname elk3.dokbok.com

为每个节点设置主机名解析

vim /etc/hosts

192.168.3.64	elk1.dokbok.com
192.168.3.65	elk2.dokbok.com
192.168.3.66	elk3.dokbok.com

4、创建es用户并下载es

useradd -m -r -s /bin/bash es
su - es
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.5.2-linux-x86_64.tar.gz

5、解压

su - es
tar zxvf elasticsearch-8.5.2-linux-x86_64.tar.gz

安装插件及调整内存

1、安装ik插件

需要自行去github上自行下载ik,要注意下载的ik版本一定要与es的版本一致。


mkdir -p elasticsearch-8.5.2/plugins/ik
unzip elasticsearch-analysis-ik-8.5.2.zip -d elasticsearch-8.5.2/plugins/ik

2、配置es内存

注意:如果不是对Es的内存机制非常熟悉,建议只更改总内存限制。如方法区、栈区等区域尽可能不要更改。es的内存可以根据负载进行自动调配,一般的情况下不需要程序员设置。

vim elasticsearch-8.5.2/config/jvm.options

-Xms5g
-Xmx5g

集群节点间使用tls通讯

cd elasticsearch-8.5.2
mkdir config/certs

1、生成根证书

bin/elasticsearch-certutil ca --out config/certs/elk-cluster-ca.p12 --days 3650

2、生成证书

bin/elasticsearch-certutil cert --ca config/certs/elk-cluster-ca.p12 --out config/certs/elk-cluster-cert.p12 --days 3650

3、添加cert密码到keystore

bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password

ES集群对外使用HTTPS

除了使用tls实现各节点的通讯安全。我们还需要使用https来保证es对外服务的安全性(包括es及Kibana)。

bin/elasticsearch-certutil http
  • Generate a CSR? [y/N]:n 或者直接回车,默认为N
  • Use an existing CA? [y/N]:y 表示使用已经存在的根证书
  • CA Path: /home/es/elasticsearch-8.5.2/config/certs/elk-cluster-ca.p12 输入下面创建的根证书的绝对路径
  • Password for elastic-stack-ca.p12: ******** 输入根证书的密码
  • For how long should your certificate be valid? [5y] 100y 设置证书有效期限
  • Generate a certificate per node? [y/N] n 是否每个节点使用自己的证书,我们选择N(默认),所有节点都使用统一的证书
    • 填写hostnames,列出人们通过http访问集群的主机名。即,你需要对外提供http服务的节点主机名(我这里是所有的主节点对外都提供http服务)
      • elk1.dokbok.com
      • elk2.dokbok.com
      • elk3.dokbok.com
    • Is this correct [Y/n] 默认,直接回车即可
    • 填写ip地址列表,本机的IP(192.168.3.64),如果多条,请每行一个
    • Is this correct [Y/n] 默认,直接回车即可
    • Do you wish to change any of these options? [y/N] 默认为N即可
    • Provide a password for the “http.p12” file: [ for none] ******** 为http.p12提供一个密码,es8.5中建议设置一个,否则可能无法启动
    • Repeat password to confirm: ******** 重新输入一次密码,确认
    • What filename should be used for the output zip file? [/home/es/elasticsearch-8.5.2/elasticsearch-ssl-http.zip] 是否输入到…zip文件,直接回车即可
  • 其他的选项都使用默认即可

经过上述操作后, 会生成了一个名为elasticsearch-ssl-http.zip的压缩文件。将其解压后,包括两个文件夹,一个为elasticsearch及kibana文件夹,将elasticsearch目录下的本主机对应的http.p12文件拷贝到config/certs的目录下:

mkdir temp
unzip elasticsearch-ssl-http.zip -d ./temp
cp temp/elasticsearch/http.p12 /home/es/elasticsearch-8.5.2/config/certs

将http的密码保存到store中

bin/elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password
bin/elasticsearch-keystore add xpack.security.http.ssl.truststore.secure_password

这一步操作需要输入上面在创建http.p12时设置的密码。

配置集群

1、修改es配置文件

创建数据及日志目录

mkdir -p /home/es/es_data/data && mkdir -p /home/es/es_data/logs

vim elasticsearch-8.5.2/config/elasticsearch.yml

不同的服务器,请注意更改节点的roles、name及ip地址等信息。

node.name: elk1.dokbok.com
node.roles: [master,data]

#bootstrap.memory_lock: true

path:
  data: /home/es/es_data/data
  logs: /home/es/es_data/logs

network.host: 0
# 注意:在openstack的环境中,由于使用虚IP互联。其内部的IP地址并不保证互联,因此需要使用public_host参数指定发布服务的IP地址,否则会导致9300服务无法连接。
network.publish_host: 10.126.28.164
transport.port: 9300

http:
  host: 0.0.0.0
  port: 9200
  cors:
    enabled: false

cluster:
  name: es8_cluster
  initial_master_nodes:
    - elk1.dokbok.com
    - elk2.dokbok.com
    - elk3.dokbok.com

discovery:
  seed_hosts:
    - elk1.dokbok.com
    - elk2.dokbok.com
    - elk3.dokbok.com

xpack.security.enrollment.enabled: true

xpack.security.transport.ssl:
  enabled: true
  verification_mode: certificate
  keystore.path: certs/elk-cluster-cert.p12
  truststore.path: certs/elk-cluster-cert.p12

xpack.security.http.ssl:
  enabled: true
  keystore.path: certs/http.p12
  truststore.path: certs/http.p12

2、创建启动脚本

在创建启动脚本以前,为了测试测试方便,可以直接使用bin/elasticsearch命令启动每一个节点,这样就可以在控制台直接看到相关错误方便调试,调试完成后,再使用脚本或配置服务启动。

vim bin/elasticsearch.sh

#!/bin/sh
CWD=`pwd`

ES_HOME=/home/es/elasticsearch-8.5.2
ES_RUN_CMD=$ES_HOME/bin/elasticsearch

case $1 in
        start)
                nohup $ES_RUN_CMD > /dev/null 2>&1 &
                        ;;
        stop)
                kill -9 `ps -ef | grep "${ES_HOME}"|grep -v "grep"|awk '{print $2}'`
                        ;;
        restart)
                cd "$CMD"
                $0 stop
                $0 start
        ;;
        *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 1
esac
exit 0
chmod 755  bin/elasticsearch.sh

3、配置服务

vi /lib/systemd/system/es.service

[Unit]
Description=es.service
After=network.target

[Service]

LimitNOFILE=65536
LimitNPROC=65536

Type=forking
Environment=JAVA_HOME=/home/es/elasticsearch-8.5.2/jdk/
ExecStart=/home/es/elasticsearch-8.5.2/bin/elasticsearch.sh start
ExecStop=/home/es/elasticsearch-8.5.2/bin/elasticsearch.sh stop
ExecReload=/home/es/elasticsearch-8.5.2/bin/elasticsearch.sh restart
User=es

[Install]
WantedBy=multi-user.target
systemctl enable es
systemctl start es

4、初始化ES的build-in的账号密码

这里为了方便,可以选择自动生成随机密码,并将生成的相关账号及密码记录下来。

bin/elasticsearch-setup-passwords auto
#使用某些环境,可能需要指定请求的url地址,命令需要变更为
bin/elasticsearch-setup-passwords auto -u "https://10.126.28.164:9200"

5、查看集群是否启动成功

浏览器打开:https://92.168.3.71:9200

然后输入账号(elastic)及对应密码即可

配置kibana

1、安装

直接下载kibana的rpm包并使用命令安装即可

#CentOS/Euler/RHEL
wget https://artifacts.elastic.co/downloads/kibana/kibana-8.5.2-x86_64.rpm
rpm -ivh kibana-8.5.2-x86_64.rpm


#Ubuntu
wget https://artifacts.elastic.co/downloads/kibana/kibana-8.5.2-amd64.deb
dpkg -i kibana-8.5.2-amd64.deb 

2、配置

将上面使用命令bin/elasticsearch-certutil http创建生成的kibana文件夹中的证书(elasticsearch-ca.pem)拷贝到/etc/kibana文件夹中。

cp /home/es/elasticsearch-8.5.2/temp/kibana/elasticsearch-ca.pem ./

3、编辑配置文件

vim kibana.yml

server.port: 5601
server.host: "0.0.0.0"
elasticsearch.hosts: ["https://10.126.28.164:9200","https://10.126.28.39:9200","https://10.126.28.194:9200"]
xpack.reporting.roles.enabled: false
monitoring.ui.ccs.enabled: false
elasticsearch.ssl.certificateAuthorities: [ "/etc/kibana/certs/elasticsearch-ca.pem" ]

elasticsearch.username: "kibana_system"
elasticsearch.password: "4BwlsSh1JqsZNVfi5DWd"
  • elasticsearch.username:初始化ES的build-in的账号密码步骤中生成的用户名
  • elasticsearch.password:kibana_system用户对应的密码
  • elasticsearch.ssl.certificateAuthorities:要使用绝对路径,否则kibana服务会启动失败

4、使用nginx代理kibana

    upstream kibana {
        keepalive 32; # keepalive connections
        server 192.168.3.64:5601; # jenkins ip and port
    }

    server {
        listen       443  ssl;
        server_name  kibana.dokbok.com;
        proxy_redirect off;

        ssl_certificate ssl/dokbok_com.pem;
        ssl_certificate_key ssl/dokbok_com.key;
        ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers on;

        location / {
            sendfile off;
            proxy_pass         http://kibana;
            proxy_redirect     default;
            proxy_http_version 1.1;

            proxy_set_header   Upgrade           $http_upgrade;

            proxy_set_header   Host              $host;
            proxy_set_header   X-Real-IP         $remote_addr;
            proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_max_temp_file_size 0;
            #this is the maximum upload size
            client_max_body_size       10m;
            client_body_buffer_size    128k;

            proxy_connect_timeout      90;
            proxy_send_timeout         90;
            proxy_read_timeout         90;
            proxy_buffering            off;
            proxy_request_buffering    off; # Required for HTTP CLI commands
            proxy_set_header Connection ""; # Clear for keepalive
        }
    }

    server {
        listen 80;
        server_name kibana.dokbok.com;
        rewrite ^(.*)$  https://$host$1 permanent;
    }

重置一些账号密码:

bin/elasticsearch-reset-password -u elastic -i
#使用某些环境,可能需要指定请求的url地址,命令需要变更为
bin/elasticsearch-reset-password -u elastic --url  "https://10.126.28.164:9200" -i
  • -u:表示为哪个用户重置密码
  • -i:表示以交互的模式进行,即用户自行设置

配置Metricbeat 

下载并安装

#CentOS/RHEL/Euler
wget https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-8.5.2-x86_64.rpm

#Ubuntu
wget https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-8.5.2-amd64.deb
dpkg -i ./metricbeat-8.5.2-amd64.deb

启用xpack插件,用于配置ssl

metricbeat modules enable elasticsearch-xpack

将p12密钥转换

#导出根证书,此根证书是构建es集群时创建的根证书
openssl pkcs12 -in elk-cluster-ca.p12 -out ca.pem -clcerts -nokeys -passin pass:Gn@2028.

#导出客户端证书
openssl pkcs12 -in http.p12 -out metricbeat.crt.pem -clcerts -nokeys -passin pass:Gn@2028.

#导出客户端密钥
openssl pkcs12 -in http.p12 -out metricbeat.key.pem -nocerts -nodes -passin pass:Gn@2028.

修改配置文件:

vim /etc/metricbeat/metricbeat.yml


output.elasticsearch:
  hosts: ["192.168.2.64:9200","192.168.2.65:9200","192.168.2.66:9200"]
  protocol: "https"

  username: "elastic"
  password: "tdjgamtam"

  ssl.certificate_authorities: ["/etc/metricbeat/certs/ca.pem"]
  ssl.certificate: "/etc/metricbeat/certs/metricbeat.crt.pem"
  ssl.key: "/etc/metricbeat/certs/metricbeat.key.pem"

启动服务

systemctl enable metricbeat
systemctl start metricbeat

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注