准备
1、我用3台服务器为例
服务器的IP分别为:192.168.1.115、192.168.1.116、192.168.1.117
2、安装及环境参照 Windows下安装及使用 Elasticsearch
配置
待上面安装步骤在每一台服务器完成后(包括用户已经创建,权限已经授予),如果用xpack用户密码的方式去登录或授权,那么还需要将每一台服务器中Elasticsearch的xpack.security.http.ssl和xpack.security.transport.ssl设置为统一的证书。
首先登录任意一台服务器去生成一个CA证书,在bin目录下cmd
然后创建证书颁发机构,输入 elasticsearch-certutil ca
这里直接点击回车
输入密码的地方继续回车
这时候会发现,安装目录中生成了一个证书颁发机构证书,默认名称为elastic-stack-ca.p12
继续在bin目录下cmd,以elastic-stack-ca.p12颁发机构创建Elasticsearch所需的证书,输入elasticsearch-certutil cert --ca elastic-stack-ca.p12
这里颁发机构的密码没有设置,直接回车即可
这里继续回车
到这里,需要设置一下Elasticsearch新证书的密码,这里需要设置一个密码,以备后面使用,我这里设置的为1234qwer(注意,输入的时候,cmd的保护机制,不会显示出来,输入完成后直接回车即可)
回到安装目录下,可以看到新生成的证书,默认名称为 elastic-certificates.p12
将新生成的证书拷贝到所有服务器config目录的certs目录下(注意:证书只生成一次,所有服务器用同一个证书)
打开config下的elasticsearch.yml,将对应证书的名称替换为新生成证书的名称 elastic-certificates.p12,注意红色标记的都设置为true,每台服务器都需要修改
在bin目录下cmd,依次执行下面命令,设置的密码和上面证书的密码一致,即1234qwer,每台服务器都需要执行
elasticsearch-keystore add xpack.security.transport.ssl.keystore.secure_password
elasticsearch-keystore add xpack.security.transport.ssl.truststore.secure_password
elasticsearch-keystore add xpack.security.http.ssl.keystore.secure_password
elasticsearch-keystore add xpack.security.http.ssl.truststore.secure_password
出现如下确认,输入 “y” 即可
到此为止,证书就全部设置完成了,继续配置elasticsearch.yml
注意,默认情况下,下面标红的默认是打开的,需要先依次将几台服务器的这2个配置注释或删除掉
三台服务器配置的elasticsearch.yml依次如下
192.168.1.117
我将117服务器设置为主节点,配置如下
cluster.name: myelasticsearch
node.name: node1001
node.roles: [master,data]
cluster.initial_master_nodes: [“node1001”]
discovery.seed_hosts: [“192.168.1.115:9302”,“192.168.1.116:9303”]
network.host: 192.168.1.117
http.port: 1001
transport.port: 9301
http.cors.enabled: true
http.cors.allow-origin: “*”
192.168.1.115,配置如下
cluster.name: myelasticsearch
node.name: node1002
node.roles: [master,data]
cluster.initial_master_nodes: [“node1001”]
network.host: 192.168.1.115
http.port: 1002
transport.port: 9302
http.cors.enabled: true
http.cors.allow-origin: “*”
discovery.seed_hosts: [“192.168.1.117:9301”,“192.168.1.116:9303”]
192.168.1.116,配置如下
cluster.name: myelasticsearch
node.name: node1003
node.roles: [master,data]
cluster.initial_master_nodes: [“node1001”]
network.host: 192.168.1.116
http.port: 1003
transport.port: 9303
http.cors.enabled: true
http.cors.allow-origin: “*”
discovery.seed_hosts: [“192.168.1.117:9301”,“192.168.1.115:9302”]
然后先启用主节点服务器192.168.1.117的bin文件夹下的elasticsearch.bat文件,待启动成功后,再依次启动其他服务器的elasticsearch.bat文件
都启动成功后,检查一下节点状态,可发现3个节点启动正常
关闭主节点的elasticsearch.bat文件,再监测一下节点状态,节点数量变为2个,集群依然运行正常
再开启117的elasticsearch.bat文件,节点恢复正常
到此,Windows 下 Elasticsearch 多服务器集群的部署配置完成
下面用代码调试一下,VS创建控制台,并引用Elasticsearch.Net或NEST包,两种都可以,两种的示例如下
以创建索引插入数据为例
Elasticsearch.Net示例如下
/// <summary>
/// 新建并插入或更新索引数据
/// </summary>
public static void CreateIndexUseClusterNet()
{
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
var nodes = new Uri []
{
new Uri("https://192.168.1.117:1001"),
new Uri("https://192.168.1.115:1002"),
new Uri("https://192.168.1.116:1003")
};
var pool = new StaticConnectionPool(nodes);
var settings = new ConnectionSettings(pool).BasicAuthentication("tc", "123456");
var client = new ElasticLowLevelClient(settings);
IEnumerable<PersonDetail> persons = new List<PersonDetail>
{
new PersonDetail()
{
Id = 4,
FirstName = "zhang",
LastName = "san",
},
new PersonDetail()
{
Id = 5,
FirstName = "li",
LastName = "si",
},
new PersonDetail()
{
Id = 6,
FirstName = "wang",
LastName = "wu",
},
new PersonDetail()
{
Id = 7,
FirstName = "zhao",
LastName = "liu",
},
new PersonDetail()
{
Id = 17,
FirstName = "tc",
LastName = "tc",
},
new PersonDetail()
{
Id = 18,
FirstName = "tc",
LastName = "tc",
}
};
foreach (var item in persons)
{
IndexRequestParameters postParam = new IndexRequestParameters();
postParam.Refresh = Refresh.True;
var value = client.Index<StringResponse>("user", item.Id.ToString(), PostData.Serializable(item), postParam);
}
}
运行结果及返回值,测试可以看到已经可以调用成功,我再测试关闭了主节点,再调用依然成功
Nest示例如下
/// <summary>
/// 新建并插入或更新索引数据
/// </summary>
public static void CreateIndexUseClusterNest()
{
ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
var nodes = new Uri[]
{
new Uri("https://192.168.1.117:1001"),
new Uri("https://192.168.1.115:1002"),
new Uri("https://192.168.1.116:1003")
};
var pool = new StaticConnectionPool(nodes);
var settings = new ConnectionSettings(pool).BasicAuthentication("tc", "123456");
var client = new ElasticClient(settings);
IEnumerable<PersonDetail> persons = new List<PersonDetail>
{
new PersonDetail()
{
Id = 4,
FirstName = "zhang",
LastName = "san",
},
new PersonDetail()
{
Id = 5,
FirstName = "li",
LastName = "si",
},
new PersonDetail()
{
Id = 6,
FirstName = "wang",
LastName = "wu",
},
new PersonDetail()
{
Id = 7,
FirstName = "zhao",
LastName = "liu",
},
new PersonDetail()
{
Id = 17,
FirstName = "tc",
LastName = "tc",
},
new PersonDetail()
{
Id = 18,
FirstName = "tc",
LastName = "tc",
}
};
foreach (var item in persons)
{
var value = client.IndexAsync(item, g => g.Index("user")).Result;
}
}
运行结果及返回值,测试可以看到已经可以调用成功,同样测试关闭了主节点,依然可以调用成功