tikv regin分裂

 

 

均衡

 查询组件

 

 

 

---

### **通过 `pd-ctl` 和 `curl` 验证 PD 功能的完整指南**

以下使用 **`pd-ctl`** 和 **`curl`** 命令,结合具体场景,说明如何验证 PD 的核心功能:

---

#### **1. 查看元数据与集群状态**
##### **(1) 使用 `pd-ctl`**
- **查看所有 Region 分布**:
  ```bash
  pd-ctl -u http://<pd-ip>:<pd-port> region --jq=".regions[] | {id, start_key, end_key, peers}"
  ```
  - **输出示例**:
    ```json
    {
      "id": 1001,
      "start_key": "7480000000000000FF",
      "end_key": "7480000000000001FF",
      "peers": [{"store_id": 1}, {"store_id": 2}, {"store_id": 3}]
    }
    ```

- **查看指定 TiKV 节点状态**:
  ```bash
  pd-ctl -u http://<pd-ip>:<pd-port> store <store_id>
  ```
  - **关键字段**:`capacity`(磁盘容量)、`available`(可用空间)、`region_count`(Region 数量)。

##### **(2) 使用 `curl`**
- **获取集群健康状态**:
  ```bash
  curl http://<pd-ip>:<pd-port>/pd/api/v1/health
  ```
  - **输出示例**:
    ```json
    [{"member_id": 1, "health": true}, {"member_id": 2, "health": true}]
    ```

- **查看所有 TiKV 节点信息**:
  ```bash
  curl http://<pd-ip>:<pd-port>/pd/api/v1/stores
  ```

---

#### **2. 验证副本管理(3 副本策略)**
##### **(1) 使用 `pd-ctl`**
- **检查某个 Region 的副本分布**:
  ```bash
  pd-ctl -u http://<pd-ip>:<pd-port> region <region_id>
  ```
  - **验证副本数是否为 3**:查看输出中的 `peers` 数组长度。

##### **(2) 使用 `curl`**
- **强制触发副本补足**(例如某节点故障后):
  ```bash
  # 下线节点(替换 store_id)
  curl -X DELETE http://<pd-ip>:<pd-port>/pd/api/v1/store/<store_id>
  
  # 检查新副本是否创建(观察 peers 变化)
  curl http://<pd-ip>:<pd-port>/pd/api/v1/region/<region_id>
  ```

---

#### **3. 验证负载均衡**
##### **(1) 使用 `pd-ctl`**
- **查看当前调度操作**:
  ```bash
  pd-ctl -u http://<pd-ip>:<pd-port> operator show
  ```
  - **输出示例**:
    ```json
    [
      {"name":"balance-region", "from-store":1, "to-store":2, "region_id":1001}
    ]
    ```

##### **(2) 使用 `curl`**
- **手动触发 Region 迁移**(测试负载均衡):
  ```bash
  # 生成迁移操作(替换 region_id 和目标 store_id)
  curl -X POST http://<pd-ip>:<pd-port>/pd/api/v1/operators -d '{
    "name": "transfer-region",
    "region_id": 1001,
    "to_store_id": 3
  }'
  
  # 验证迁移是否完成
  curl http://<pd-ip>:<pd-port>/pd/api/v1/region/1001 | jq .peers
  ```

---

#### **4. 验证高可用性(PD 自身集群)**
##### **(1) 使用 `curl`**
- **查看 PD 集群成员状态**:
  ```bash
  curl http://<pd-ip>:<pd-port>/pd/api/v1/members
  ```
  - **输出示例**:
    ```json
    {
      "members": [
        {"name": "pd-1", "client_urls": ["http://10.0.0.1:2379"], "health": true},
        {"name": "pd-2", "client_urls": ["http://10.0.0.2:2379"], "health": true}
      ]
    }
    ```

##### **(2) 模拟 PD 节点宕机**
1. **停止一个 PD 节点**:
   ```bash
   systemctl stop pd-server
   ```
2. **检查剩余节点是否正常响应**:
   ```bash
   curl http://<healthy-pd-ip>:<pd-port>/pd/api/v1/health
   ```

---

#### **5. 修改与验证调度策略**
##### **(1) 使用 `pd-ctl`**
- **修改副本数为 5**:
  ```bash
  pd-ctl -u http://<pd-ip>:<pd-port> config set max-replicas 5
  ```
- **验证新 Region 的副本数**:
  ```bash
  pd-ctl -u http://<pd-ip>:<pd-port> region <new_region_id>
  ```

##### **(2) 使用 `curl`**
- **设置副本标签约束(如跨机房部署)**:
  ```bash
  curl -X POST http://<pd-ip>:<pd-port>/pd/api/v1/config/replicate -d '{
    "location-labels": ["zone", "host"]
  }'
  ```
  - **验证标签是否生效**:
    ```bash
    curl http://<pd-ip>:<pd-port>/pd/api/v1/config/replicate
    ```

---

### **总结:`pd-ctl` 与 `curl` 的核心用途**
| **功能**         | **`pd-ctl` 命令示例**               | **`curl` API 示例**                     |
|------------------|------------------------------------|----------------------------------------|
| 元数据查询        | `pd-ctl region`                    | `curl /pd/api/v1/region/<id>`          |
| 副本管理          | `pd-ctl store <store_id>`          | `curl -X DELETE /pd/api/v1/store/<id>` |
| 负载均衡          | `pd-ctl operator show`             | `curl -X POST /pd/api/v1/operators`    |
| 高可用性          | `pd-ctl member`                    | `curl /pd/api/v1/members`              |
| 调度策略配置      | `pd-ctl config set max-replicas 5` | `curl -X POST /pd/api/v1/config`       |

通过 `pd-ctl` 和 `curl`,可以灵活地查询、修改 PD 的配置与状态,适用于自动化脚本或深度调试场景。

 

posted on 2025-04-28 13:43  吃草的青蛙  阅读(47)  评论(0)    收藏  举报

导航