64 lines
1.9 KiB
Bash
Executable File
64 lines
1.9 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
echo "🚀 Consul 集群演示脚本"
|
|
|
|
# 检查 Consul 集群状态
|
|
check_cluster() {
|
|
echo "📊 检查集群状态..."
|
|
|
|
for node in consul1 consul2 consul3; do
|
|
echo "检查节点: $node"
|
|
curl -s http://$node:8500/v1/status/leader 2>/dev/null && echo " - Leader: $(curl -s http://$node:8500/v1/status/leader 2>/dev/null)" || echo " - 节点不可达"
|
|
curl -s http://$node:8500/v1/status/peers 2>/dev/null && echo " - 集群节点: $(curl -s http://$node:8500/v1/status/peers 2>/dev/null)" || echo " - 无法获取集群信息"
|
|
echo ""
|
|
done
|
|
}
|
|
|
|
# 测试配置读写
|
|
test_config() {
|
|
echo "🔧 测试配置读写..."
|
|
|
|
# 写入配置到不同节点
|
|
echo "写入配置到 consul1..."
|
|
curl -X PUT http://consul1:8500/v1/kv/test/config "value-from-consul1" 2>/dev/null
|
|
|
|
echo "从 consul2 读取配置..."
|
|
value=$(curl -s http://consul2:8500/v1/kv/test/config?raw 2>/dev/null)
|
|
echo "读取到的值: $value"
|
|
|
|
echo "从 consul3 读取配置..."
|
|
value=$(curl -s http://consul3:8500/v1/kv/test/config?raw 2>/dev/null)
|
|
echo "读取到的值: $value"
|
|
}
|
|
|
|
# 模拟故障转移
|
|
simulate_failure() {
|
|
echo "💥 模拟 Leader 故障..."
|
|
|
|
# 获取当前 Leader
|
|
leader=$(curl -s http://consul1:8500/v1/status/leader 2>/dev/null | tr -d '"')
|
|
echo "当前 Leader: $leader"
|
|
|
|
# 这里只是演示,实际环境中你可以停止 Leader 节点
|
|
echo "在实际环境中,你可以:"
|
|
echo "docker stop consul-leader-container"
|
|
echo "然后观察其他节点自动选举新 Leader"
|
|
}
|
|
|
|
case "$1" in
|
|
"status")
|
|
check_cluster
|
|
;;
|
|
"test")
|
|
test_config
|
|
;;
|
|
"failure")
|
|
simulate_failure
|
|
;;
|
|
*)
|
|
echo "用法: $0 {status|test|failure}"
|
|
echo " status - 检查集群状态"
|
|
echo " test - 测试配置同步"
|
|
echo " failure - 模拟故障转移"
|
|
;;
|
|
esac |