user用户下java
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用户,则建立。node
若是没有操做权限,则执行:git
# root下,将文件权限设置为duser用户可操做 chown duser * chgrp duser *
当不使用es远程词典时: github
修改elasticsearch-5.3.0/plugins/ik/config/IKAnalyzer.cfg.xmlbootstrap
<?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>
说明:注释后两项便可vim
添加自定义同义词bash
替换config/synonyms.txt文件服务器
synonyms.txt文件内格式,全部同义词为一行,且用 “, ” 隔开app
root用户下cors
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的文档总数
查询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便可
下载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 # 在当前目录下会生成证书文件
将生成的各个节点的证书,分别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
添加权限
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
生成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
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
过儿,你学废了吗?