## elastic search集群故障切換測試
### 測試環境說明
- windows server2016 64位
- java version 1.8
- elasticserach 版本:6.6.2 (添加中文分詞插件ik)
### 測試方法說明
- 安裝es,解壓后運行bin目錄中的腳本直接啟動。
- 安裝head插件連接es節點,監控集群狀態和添加以及查詢數據。
- 同時啟動三個es節點,設置不同的端口區分。啟用集群,不做特殊的主從節點區分,使三個節點同時具有主節點、從節點、協調節點的功能。
- 在集群中任意連接一個節點,建立索引,新建數據。
- 依次停止節點1,節點2,接著在剩余的節點中查詢數據,根據是否返回所有的數據來判斷數據是否正常的轉移到了從節點。
- 從head工具上檢查主節點是否進行了重新選舉。
#### 節點狀態意義說明
狀態 | 意義
---|---
green| 所有主分片和從分片都可用
yellow| 所有主分片可用,但存在不可用的從分片
red| 存在不可用的主要分片
#### 測試過程和結果
1. 初始查詢集群健康狀態為green,節點為3個,分別連接3個節點,分別寫入數據。
2. 關閉節點1,從head上看到節點2被選為主節點master,連接到節點2和節點3,查詢數據正常,返回了所有數據。
3. 再次關閉節點2,節點3被選為master,查詢數據正常。
4. 同時集群健康狀態為yellow。
5. 然后單獨在節點3中加入數據,再分別啟動節點1,節點2,集群健康狀態恢復為green,節點1和節點2變成了從節點。
6. 接著分別在節點1和2中查詢節點3中增加的數據,數據正常返回。查詢所有的數據也正常的返回了。
7. 說明數據轉移正常,故障轉移能力正常。
#### 使用的操作
查看集群健康狀況:
```
GET /_cluster/health
{
"cluster_name": "my-test",
"status": "green",
"timed_out": false,
"number_of_nodes": 3,
"number_of_data_nodes": 3,
"active_primary_shards": 5,
"active_shards": 10,
"relocating_shards": 0,
"initializing_shards": 0,
"unassigned_shards": 0,
"delayed_unassigned_shards": 0,
"number_of_pending_tasks": 0,
"number_of_in_flight_fetch": 0,
"task_max_waiting_in_queue_millis": 0,
"active_shards_percent_as_number": 100
}
```
添加數據:
```
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
PUT /megacorp/employee/2
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
PUT /megacorp/employee/3
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
```
查詢數據:
```
查詢單個
GET /megacorp/employee/1
查詢全部
GET /megacorp/employee/_search
```
#### 配置
節點1:
```
cluster.name: my-test
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
transport.tcp.port: 9400
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9400", "127.0.0.1:9401", "127.0.0.1:9402"]
```
節點2:
```
cluster.name: my-test
node.name: node-2
network.host: 0.0.0.0
http.port: 9201
http.cors.enabled: true
http.cors.allow-origin: "*"
transport.tcp.port: 9401
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9400", "127.0.0.1:9401", "127.0.0.1:9402"]
```
節點3:
```
cluster.name: my-test
node.name: node-3
network.host: 0.0.0.0
http.port: 9202
http.cors.enabled: true
http.cors.allow-origin: "*"
transport.tcp.port: 9402
discovery.zen.ping.unicast.hosts: ["127.0.0.1:9400", "127.0.0.1:9401", "127.0.0.1:9402"]
```