关于python:Elasticsearch搜索引擎集群部署

4次阅读

共计 7953 个字符,预计需要花费 20 分钟才能阅读完成。

​一. 部署集群

user 用户下

unzip elasticsearch_bak.zip #解压引擎文件
# 批改默认的配置文件
cd elasticsearch_bak/config
vi elasctsearch.yml
 cluster.name: es-zl  #批改集群名,所有节点配置的该名称必须一样
 node.name: es-46 #批改每个节点的名称,必须不同,以辨别每个节点
 node.master:true #是否可作为主节点
 path.data: /data/es_data #es 数据寄存的地位
 network.host:127.17.30.46 #每个节点的 ip 地址
 http.port: 9200  #节点的端口,必须雷同,默认 9200
 
 bootstrap.memory_lock: true
 bootstrap.system_call_filter: false  #内存锁定,避免 swap 使 es 产生内存交互
 discovery.zen.ping.unicast.hosts:["127.17.30.44","127.17.30.45",,] #集群 master 节点的 ip,不须要所有节点的 ip,其余节点只有能与任意一个 master 节点通信,即可退出到该集群当中
 discovery.zen.minimum_master_nodes:2 #当 2 个有资格成为主节点的从节点认为主节点挂了,则进行从新选主,个别设置为 (n/2)+1,避免 es 呈现脑裂状况
 gateway.recover_after_nodes: 3 #节点数量达到多少之后,进行数据恢复解决
 http.cors.enabled: true
 http.cors.allow-origin: "*"
 
vi yvm.options
 -Xms16g  #默认为 2g,可依据服务器内存,自行批改
 
#通过一下命令查看内存
cat /proc/cpuinfo | grep 'cpu cores' | wc -l #查看 cpu 核数
cat /proc/meminfo | grep MemTotal #只查看内存
free -h  #查看内存的应用
df -h  #查看硬盘存储 

留神 :该操作必须在每台服务器上都要部署,并不能在 root 用户下操作,要在 user 下操作,如果没有 usr 用户,则创立。

如果没有操作权限,则执行:

# root 下,将文件权限设置为 duser 用户可操作
chown duser *
chgrp duser *

当不应用 es 近程词典时

批改 elasticsearch-5.3.0/plugins/ik/config/IKAnalyzer.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
        <comment>IK Analyzer 扩大配置 </comment>
                <!-- 用户能够在这里配置本人的扩大字典 -->
        <entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic</entry>
                 <!-- 用户能够在这里配置本人的扩大进行词字典 -->
        <entry key="ext_stopwords">custom/ext_stopword.dic</entry>
                <!-- 用户能够在这里配置近程扩大字典 -->
       <!-- <entry key="remote_ext_dict">http://10.172.80.229:9301/mydict.dic</entry> -->
                <!-- 用户能够在这里配置近程扩大进行词字典 -->
       <!-- <entry key="remote_ext_stopwords">http://10.172.80.229:9301/stopword.dic</entry> -->
</properties>

阐明 :正文后两项即可

增加自定义同义词

替换 config/synonyms.txt 文件

synonyms.txt 文件内格局,所有同义词为一行,且用“,”隔开

二、批改集群配置

root 用户下

vi /etc/security/limits.conf
 #增加以下语句
 *      soft    nofile    65536
 *      hard    nofile    65536
 *      soft    nproc     65536
 *    hard    nproc  65536
 duser  soft    memlock   unlimited 
    duser  hard    memlock   unlimited 
vi /etc/sysctl.conf
 #批改以下语句
 vm.max_map_count=655360
 
sysctl -p

三、启动集群

user 用户下

cd elasticsearch_bak/bin
./elasticsearch  -d #后盾启动集群
curl 172.17.30.45:9200/_cat/nodes #查看所有节点,* 为主节点
# 启动不了时候,查看 es-log

四、搜寻部署

user 用户下的操作,查看 es-log

vi index_search_mapping.sh
 #批改的 ip 和端口与集群一样
 curl -XPUT "http://10.25.114.90:9200/index" 
 {settings": {"number_of_shards": 6, #主分片数量,个别为 服务器总核数"number_of_replicas": 2, #正本分片的数量,避免数据失落}

留神 :分词器 anslyzer 能够依据理论状况进行批改、删除和增加

增加删除字段 当须要增加字段时,只有在以下地位增加即可

"mappings": {
    "index": {
      "properties": {
   "id": {"type": "keyword"},
       "": {#须要增加的字段名"type":"" #增加的字段的约束条件}
       }
      }
     }

留神 :批改某个文件的字段之前,须要先删除该文件的 core

重新启动搜寻时,也须要进行以下步骤

curl -XDELETE "http://172.19.30.45:9200/index"
#批改完之后,从新运行该文件
sh index_search_mapping.sh

五、查问启动后果

curl http://ip:9200/index?pretty# 查问字段
curl http://ip:9200/_cat/nodes #查问节点
curl http://ip:9200/_cat/health #查问集群衰弱,green
curl http://ip:9200/_cat/shards #查问集群分片状态 

六、数据量查问

curl http://ip:9200/one_index/_stats #查问某一个 index 下的数据量
curl http://ip:9200/one_index,two_index/_stats #查问多个 index 的数据量,应用, 隔开
curl http://ip:9200/_all/_stats  #查问所有 index 下的数据量
# 返回 search、fetch、get、index、bulk、segment、counts
curl http://ip:9200/_cat/count/index #只查问某个 index 下的文档数,如果不加 index,则查问所有的 index 的文档总数 

七、search-guard 为 es 集群配置 auth

1、装置 search-guard 插件和 ssl 插件

查问 es 的版本

curl ip:9200
# 后果中的 number 数据即代表版本号 

装置 search-guard https://repo1.maven.org/maven…

找到对应的版本,下载即可

或者应用命令行下载,es 目录下

bin/elasticsearch-plugin install -b com.floragunn:search-guard-5:5.3.0-12

装置 search-guard-ssl https://repo1.maven.org/maven…

命令行

bin/elasticsearch-plugin install -b com.floragunn:search-guard-ssl:5.3.0-22

留神 :当 es 版本较高时,可能并不需要这两个都存在,取 search-guard 即可

2、生成 es 证书文件

下载 search-guard 源码工具,生成证书的工具

git clone https://github.com/floragunncom/search-guard-ssl.git

批改 ca 配置

cd search-guard-ssl/example-pki-scripts/etc/
vi root-ca.conf  # 根证书配置
 0.domainComponent       = "www.test.com”# 域名
 1.domainComponent       = "www.test.com"    # 域名
 organizationName        = "Test"            # 组织名称
 organizationalUnitName  = "Test Root CA"    # 组织单位名称
 commonName              = "Test Root CA"    # 通用名称
 
vi signing-ca.conf # 签名证书配置
 0.domainComponent       = "www.test.com”# 域名
 1.domainComponent       = "www.test.com"    # 域名
 organizationName        = "Test"            # 组织名称
 organizationalUnitName  = "Test Signing CA" # 组织单位名称
 commonName              = "Test Signing CA" # 通用名称 

留神

以上信息填写,必须保障和生成证书时信息统一

批改 sh 文件 返回 example-pki-scripts 目录下,批改 example.sh 文件:

#!/bin/bash
set -e
./clean.sh
./gen_root_ca.sh 12345678 12345678
./gen_node_cert.sh 0 12345678 12345678&& ./gen_node_cert.sh 1 12345678 12345678 &&  ./gen_node_cert.sh 2 12345678 12345678
./gen_client_node_cert.sh test 12345678 12345678
./gen_client_node_cert.sh test 12345678 12345678

参数阐明

./gen_root_ca.sh 12345678 12345678

第一个参数为 CA_PASS,即 CA 明码(根证书明码)

第二个参数为 TS_PASS,即 TS 明码(truststore,信赖证书明码)

./gen_node_cert.sh 0 12345678 12345678

第一个参数为 node 编号,生成证书后的文件名为 node-0*

第二个参数为 KS_PASS(keystore 文件明码)

第三个参数为 CA_PASS

./gen_client_node_cert.sh test 12345678

第一个参数为客户端节点名称,生成证书后的文件名为 test*

第二个参数为 KS_PASS

第三个参数为 CA_PASS

生成证书,运行 example.sh

sh example.sh
# 在当前目录下会生成证书文件 

3、配置 es,启动 search-guard

将生成的各个节点的证书,别离 copy 到各个节点上

# 将 node-0-keystore.jks 和 truststore.jks 复制到 es 的 config 目录下
cp node-0-keystore.jks ~/elasticsearch/config/
cp truststore.jks ~/elasticsearch/config/
# 将 test-keystore.jks 和 truststore.jks 复制到 es 的 plugins/search-guard-2/sgconfig 目录下
cp test-keystore.jks ~/elasticsearch/plugins/search-guard-2/sgconfig/
cp truststore.jks ~/elasticsearch/plugins/search-guard-2/sgconfig/

留神

放弃证书的一致性,因而只须要在一台机器上生成 CA 证书即可

批改 es 的配置文件 config/elaticsearch.yml

在之前批改的根底上,减少如下配置

 配置 ssl
searchguard.ssl.transport.enabled: true
searchguard.ssl.transport.keystore_filepath: node-0-keystore.jks
searchguard.ssl.transport.keystore_password: 12345678
searchguard.ssl.transport.truststore_filepath: truststore.jks
searchguard.ssl.transport.truststore_password: 12345678
searchguard.ssl.transport.enforce_hostname_verification: false
searchguard.ssl.transport.resolve_hostname: false
# 配置 https
# http 配置,这里我只是为了测试不便,配置完,应该设置为 true
searchguard.ssl.http.enabled: false
searchguard.ssl.http.keystore_filepath: node-0-keystore.jks
searchguard.ssl.http.keystore_password: 12345678
searchguard.ssl.http.truststore_filepath: truststore.jks
searchguard.ssl.http.truststore_password: 12345678
# CN 为生成 CA 证书时填写的信息,即./gen_client_node_cert.sh test 12345678 命令的第一个参数
searchguard.authcz.admin_dn:
- CN=test, OU=client, O=client, L=Test, C=DE

启动 es

4、将 search-guard 的配置写入 es 中

增加权限

chmod -R 777 plugins/search-guard-5/tools/sgadmin.sh

配置格局

./plugins/search-guard-5/tools/sgadmin.sh -cn clustername -h hostname -cd plugins/search-guard-5/sgconfig -ks plugins/search-guard-5/sgconfig/admin-keystore.jks -kspass password -ts plugins/search-guard-5/sgconfig/truststore.jks -tspass password -nhnv

留神

clustername 为集群的名称

hostname 为 elasticsearch.yml 文件中 network.host 的值

password 生成证书设置的明码

以上配置生成的命令为

./plugins/search-guard-5/tools/sgadmin.sh -cn es-nlp -h 192.168.124.18 -cd plugins/search-guard-5/sgconfig -ks plugins/search-guard-5/sgconfig/test-keystore.jks -kspass 12345678 -ts plugins/search-guard-5/sgconfig/truststore.jks -tspass 12345678 -nhnv

5、增加 es 用户及明码

生成 md5 加密明码

chmod -R 777 plugins/search-guard-2/tools/hash.sh
cd plugins/search-guard-2/tools/
plugins/search-guard-2/tools/hash.sh -p 123456
# 保留生成的 hash 明码 

增加用户及明码

vim plugins/search-guard-5/sgconfig/sg_internal_users.yml
sunww:
  hash: 加密后的字符串
  # password: 123456
  
# 最初一行时 password 提醒,避免遗记 

增加用户的权限

vim plugins/search-guard-5/sgconfig/sg_roles_mapping.yml
ag_all_access:
  users:
    - admin
    - sunww
# 增加所有权限给 sunww 用户 

从新将配置写入 es 中失效

./plugins/search-guard-5/tools/sgadmin.sh -cn es-nlp -h 192.168.124.18 -cd plugins/search-guard-5/sgconfig -ks plugins/search-guard-5/sgconfig/test-keystore.jks -kspass 12345678 -ts plugins/search-guard-5/sgconfig/truststore.jks -tspass 12345678 -nhnv

6、测试

curl http://sunww:123456@127.0.0.1:9200
curl -u sunww:123456 http://127.0.0.1:9200
curl --user sunww:123456 http://127.0.0.1:9200

过儿,你学废了吗?

正文完
 0