1、拉取包
wget Elasticsearch 7.8.0 | Elastic
es版本:elasticsearch-7.17.8
2、解压
tar -zxf xxx.gz
3、修改配置文件
vim /opt/es/elasticsearch-7.17.8/config/elasticsearch.yml
# 集群名称
cluster.name: cluster-es
# 当前节点名称,每个节点名称不能重复
node.name: node-1
# 当前节点IP
network.host: 192.168.25.1 (0.0.0.0)
# 是不是有资格为主节点
node.master: true
node.data: true
http.port: 9300
# head插件需要打开这两个配置
http.cors.allow-origin: "*"
http.cors.enabled: true
http.max_content_length: 200mb
# es7.x 之后新增的配置, 初始化一个新的集群时需要此配置来选举master
cluster.initial_master_nodes: ["node-1"]
# es7.x 之后新增配置,节点发现
discovery.seed_hosts: ["192.168.25.1:9300","192.168.25.2:9300","192.168.25.3:9300"]
部署过程种遇到得问题及解决
1、ES启动不能时root 用户,需要新建一个用户
# 新增es用户
useradd es
# 创建密码
passwd es
# 如果错了,可以删除再加
userdel -r es
# 文件夹所有者
chown -R es:es /opt/module/es
2、max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
系统虚拟内存默认最大映射数为65530,无法满足ES系统要求,需要调整为262144以上。
处理办法
设置vm.max_map_count参数
sudo vim /etc/sysctl.conf
vm.max_map_count = 262144
在/etc/security/limits.conf中添加如下内容
[sandwich@centos-elk ~]$ sudo vi /etc/security/limits.conf
#添加以下内容
* soft nofile 65536
* hard nofile 65536
* soft nproc 4096
* hard nproc 4096
3、ElasticsearchException[X-Pack is not supported and Machine Learning is not available for [linux-x86]; you can use the other X-Pack features (unsupported) by s
解决:在config/elasticsearch.yml添加一条配置:
xpack.ml.enabled: false
注意:机器学习使用SSE4.2指令,因此只能在cpu支持SSE4.2的机器上运行。如果在旧的硬件上运行Elasticsearch,那么需要设置xpack.ml.enabled为false
4、[node-1][192.168.1.247:9300][internal:cluster/coordination/join]
解决
删除data 数据目录下的数据(data 指 配置文件中的配置 path.data: /opt/es/data)
network.host: 192.168.1.247 或 0.0.0.0 两个尝试切换
三、集群扩容和缩容
3.1 分片扩容缩容
分片分为主分片和副本分片,只能增加副本分片,主分片发现是通过hash算法,主分片数一旦指定即无法修改,但副本数是可以动态改变的.
副本分片扩容
http://192.168.40.130:9200/user/_settings
{
"number_of_replicas": 2
}
ES在每个ES节点上,不会同时分布两个相同的副本及主分片,以确保某个ES节点宕机,无法进行容灾的转移 。但这里我们一共就有两个ES节点,切分片数为3个,所以只能正常运行3 主分片 + 3副本 6个节点,但我们是3主分片且每个有2个副本,即9个节点,所以有三个副本不可用。
3.2 节点扩容缩容
找一台新的主机,在该主机上搭建好ES所需的环境,可以参考上篇文章的内容,修改ES 的 config/elasticsearch.yml
,写入以下内容:
#集群名称
cluster.name: cluster-es
#节点名称,每个节点的名称不能重复
node.name: node-3
#ip 地址,每个节点的地址不能重复
network.host: 192.168.40.168
#是不是有资格主节点
node.master: true
node.data: true
http.port: 9200
# head 插件需要这打开这两个配置,解决跨域问题
http.cors.allow-origin: "*"
http.cors.enabled: true
http.max_content_length: 200mb
#es7.x 之后新增的配置,初始化一个新的集群时需要此配置来选举 master
cluster.initial_master_nodes: ["node-1"]
#es7.x 之后新增的配置,节点发现
discovery.seed_hosts: ["192.168.40.130:9300","192.168.40.167:9300","192.168.40.168:9300"]
gateway.recover_after_nodes: 2
network.tcp.keep_alive: true
network.tcp.no_delay: true
transport.tcp.compress: true
#集群内同时启动的数据任务个数,默认是 2 个
cluster.routing.allocation.cluster_concurrent_rebalance: 16
#添加或删除节点及负载均衡时并发恢复的线程个数,默认 4 个
cluster.routing.allocation.node_concurrent_recoveries: 16
#初始化数据恢复时,并发恢复线程的个数,默认 4 个
cluster.routing.allocation.node_initial_primaries_recoveries: 16
主要修改的是 cluster.name
必须和原集群保持一致。node.name
要保证唯一,不要和现有集群重复。discovery.seed_hosts
就填写现有集群所有的主机ip 和自己的 ip。启动es
四、ES开启并配置 X-Pack 的认证与鉴权
4.1 在 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
4.2 开启节点证书
bin/elasticsearch-certutil ca 生成证书
bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12 生成密钥
4.3 密钥文件转移
# 先创建目录
mkdir ./config/certificates
# 移动凭证至config的certificates下
mv ./elastic-certificates.p12 ./config/certificates/
# 赋值权限,不然会出问题
chmod 777 ./config/certificates/elastic-certificates.p12
4.4 节点添加密码
bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
4.5 添加配置
##################启用xpack并指定访问节点证书所需的信息##################
#设置为true以开启X-Pack安全功能。
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.security.transport.ssl.verification_mode: certificate
#存放elastic-certificates.p12文件路径
xpack.security.transport.ssl.keystore.path: 【es的安装路径】/config/certificates/elastic-certificates.p12
xpack.security.transport.ssl.truststore.path: 【es的安装路径】/config/certificates/elastic-certificates.p12
#################启用xpack并指定访问节点证书所需的信息###################
4.6 重启ES
4.7 其他节点配置
1)elastic-certificates.p12文件拷贝
把生成的elastic-certificates.p12文件拷贝到对应节点下/config/certificates目录下
(2)文件授权
方式一:使用chown 命令对整个文件夹重新授权 ps:需要使用root账号操作
chown -R es:es /usr/local/huaxing/elasticsearch-7.4.2-8200
方式二:使用chmod 对elastic-certificates.p12文件赋权
chmod 777 /config/certificates/elastic-certificates.p12
(3)配置密钥密码
在之前的步骤中,我们对elastic-certificates.p12 文件配置了密码,故需要配置密码。输入密码:生成密钥步骤设置的密码
bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
(4)添加配置文件(与4.5一致)
4.8 设置各种密码
./bin/elasticsearch-setup-passwords interactive
4.9 配置中的问题
x-pack 密钥配置问题
[2021-11-18T09:14:10,976][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [es02] uncaught exception in thread [main]
org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: failed to load plugin class [org.elasticsearch.xpack.core.XPackPlugin]
at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:163) ~[elasticsearch-7.4.2.jar:7.4.2]
at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.4.2.jar:7.4.2]
at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.4.2.jar:7.4.2]
at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.4.2.jar:7.4.2]
at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.4.2.jar:7.4.2]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.4.2.jar:7.4.2]
at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.4.2.jar:7.4.2]
Caused by: java.io.IOException: keystore password was incorrect
at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:2118) ~[?:?]
at sun.security.util.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:222) ~[?:?]
at java.security.KeyStore.load(KeyStore.java:1472) ~[?:?]
at org.elasticsearch.xpack.core.ssl.TrustConfig.getStore(TrustConfig.java:97) ~[?:?]
at org.elasticsearch.xpack.core.ssl.StoreTrustConfig.createTrustManager(StoreTrustConfig.java:65) ~[?:?]
at org.elasticsearch.xpack.core.ssl.SSLService.createSslContext(SSLService.java:384) ~[?:?]
at java.util.HashMap.computeIfAbsent(HashMap.java:1138) ~[?:?]
... 6 more
Caused by: java.security.UnrecoverableKeyException: failed to decrypt safe contents entry: javax.crypto.BadPaddingException: Given final block not properly padded. Such issues can arise if a bad key is used during decryption.
at sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:2118) ~[?:?]
at sun.security.util.KeyStoreDelegator.engineLoad(KeyStoreDelegator.java:222) ~[?:?]
at java.security.KeyStore.load(KeyStore.java:1472) ~[?:?]
... 6 more
解决办法:
1:可能是elastic-certificates.p12文件归属权不属于es账号所拥有
#执行以下语句,把整个目录的归属权给es账号
chown -R es:es /usr/local/huaxing/elasticsearch-7.4.2-8200
chmod 777 elastic-certificates.p12
2:若是上述问题还没解决,那可能是在生成密钥时设置了密码,需要执行以下命令。弹出提示输入密码就是在生成密钥时设置的密码
./bin/elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
./bin/elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password