一、环境准备
1>准备三台centos7.2(本次实验环境),
192.168.105.55
192.168.105.56
192.168.105.57
并按装JDK1.8
创建用户和组 elastic
groupadd elastic
adduser -g elastic -d /home/elastic elastic
2>必须要的系统配置:
/etc/security/limits.conf
* - nofile 65535
/etc/sysctl.conf
vm.max_map_count = 262144
要执行sysctl -p 持久化配置,不然切换用户后,可能没有生效
二、安装配置
1、上传elasticsearch-6.8.1.tar.gz包只三台服务器/home/elastic目录下,并创建目录/elastic/data 和/elastic/log
2、解压安装包 tar -xzvf elasticsearch-6.8.1.tar.gz,并将文件夹重命名为elasticsearch
3、修改配置文件elasticsearch/config/elasticsearch.yml
三台分别的node-1,node-2,node-3;
network.host分别为三台机器的ip
cluster.name: appEsCls
node.name: node-1
path.data: /home/elastic/elastic/data
path.logs: /home/elastic/elastic/log
network.host: 192.168.105.55
http.port: 9200
discovery.zen.ping.unicast.hosts: ["192.168.105.55", "192.168.105.56","192.168.105.57"]
gateway.recover_after_nodes: 3
然后启动集群
三台机器分别执行elasticsearch/bin/elasticsearch -d (-d表示后台执行,可以不用)
启动成功后查看日志: /home/elastic/log/appEsCls.log
分别有[node-X] started
然后查看集群状态及节点状态(可以分别查看各个节点的情况)
curl http://192.168.105.55:9200/_cat/health?v
curl http:/192.168.105.57:9200/_cat/nodes?v
master下面的*表示该节点为master节点
三、x-pack设置elasticsearch安全访问
1.任意一台服务器上执行命令
./elasticsearch-certgen
#####################################
Please enter the desired output file [certificate-bundle.zip]: cert.zip (压缩包名称)
Enter instance name: appEsCls(实例名)
Enter name for directories and files [p4mES]: elasticsearch(文件夹名)
Enter IP Addresses for instance (comma-separated if more than one) []: 192.168.105.55,192.168.105.56,192.168.105.57(实例ip,多个ip用逗号隔开)
Enter DNS names for instance (comma-separated if more than one) []: node-1,node-2,node-3(节点名,多个节点用逗号隔开)
Would you like to specify another instance? Press 'y' to continue entering instance information: (到达这一步,不需要按y重新设置,按空格键就完成了)
Certificates written to /home/elastic/elasticsearch/bin/cert.zip(这个是生成的文件存放地址,不用填写)
2. 将压缩文件cert.zip分别拷贝纸三台机器的 /home/elastic/elasticsearch/config文件夹下并解压,
生成ca和elasticsearch并修改配置文件elasticsearch.yml
增加如下配置:
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
xpack.ssl.key: elasticsearch/elasticsearch.key
xpack.ssl.certificate: elasticsearch/elasticsearch.crt
xpack.ssl.certificate_authorities: ca/ca.crt
3. 重启三台节点
执行elasticsearch/bin/elasticsearch-setup-passwords interactive
自定义设置elastic、kibana....等所有工具的登录密码 最高级账号elastic 可以登录所有组件
然后再重启三台节点
这时,执行curl命令则需要验证密码了
curl http://192.168.105.57:9200?pretty
curl -u elastic:123456 http://192.168.105.57:9200
四、Java客户端编写
1、普通客户端程序
pom.xml:
org.elasticsearch
elasticsearch
${elasticsearch.version}
org.elasticsearch.client
transport
${elasticsearch.version}
org.elasticsearch.plugin
transport-netty4-client
${elasticsearch.version}
code:
@Configuration
public class ElasticSearchClientConfig {
@Value("${elasticsearch.cluster-nodes}")
private String clusterNodes;
@Value("${elasticsearch.cluster-name}")
private String clusterName;
@Bean
public Client client() {
Settings settings = Settings.builder().put("cluster.name", clusterName)
.put("client.transport.sniff", true).build();
TransportClient client = new PreBuiltTransportClient(settings);
try {
if (clusterNodes != null && !"".equals(clusterNodes)) {
for (String node : clusterNodes.split(",")) {
String[] nodeInfo = node.split(":");
client.addTransportAddress(new TransportAddress(InetAddress.getByName(nodeInfo[0]), Integer.parseInt(nodeInfo[1])));
}
}
} catch (UnknownHostException e) {
}
return client;
}
}
2、带X-PACK授权控制的客户端编写
pom.xml:
org.elasticsearch
elasticsearch
${elasticsearch.version}
org.elasticsearch.client
x-pack-transport
${elasticsearch.version}
org.elasticsearch.plugin
transport-netty4-client
${elasticsearch.version}
注意:version 为6.8.1的x-pack-transport的jar可能无法下载,需要添加repository:
https://artifacts.elastic.co/maven
dev
elasticsearch-releases
https://artifacts.elastic.co/maven
true
false
code:
@Configuration
public class ElasticSearchClientConfig{
@Value("${elasticsearch.cluster-nodes}")
private String clusterNodes;
@Value("${elasticsearch.cluster-name}")
private String clusterName;
@Value("${elasticsearch.user-password}")
private String userPwd;
@Bean
public Client client() {
Settings settings = Settings.builder().put("cluster.name", clusterName)
.put("xpack.security.user", userPwd)
.put("xpack.ssl.key", "E:/elasticsearch/elasticsearch.key")
.put("xpack.ssl.certificate", "E:/elasticsearch/elasticsearch.crt")
.put("xpack.ssl.certificate_authorities", "E:/ca/ca.crt")
.put("xpack.security.transport.ssl.enabled", "true").build();
TransportClient client = new PreBuiltXPackTransportClient(settings);
try {
if (clusterNodes != null && !"".equals(clusterNodes)) {
for (String node : clusterNodes.split(",")) {
String[] nodeInfo = node.split(":");
client.addTransportAddress(new TransportAddress(InetAddress.getByName(nodeInfo[0]), Integer.parseInt(nodeInfo[1])));
}
}
} catch (UnknownHostException e) {
}
return client;
}
}