132 lines
3.4 KiB
Bash
Executable File
132 lines
3.4 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Consul Cluster Simple Deployment Script
|
|
# 简化版 Consul 集群部署脚本
|
|
|
|
set -e
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
|
|
|
# 颜色定义
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# 日志函数
|
|
log_info() {
|
|
echo -e "${BLUE}[INFO]${NC} $1"
|
|
}
|
|
|
|
log_success() {
|
|
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
|
}
|
|
|
|
log_warning() {
|
|
echo -e "${YELLOW}[WARNING]${NC} $1"
|
|
}
|
|
|
|
log_error() {
|
|
echo -e "${RED}[ERROR]${NC} $1"
|
|
}
|
|
|
|
# 检查依赖
|
|
check_dependencies() {
|
|
log_info "检查依赖项..."
|
|
|
|
if ! command -v ansible-playbook &> /dev/null; then
|
|
log_error "ansible-playbook 未找到,请安装 Ansible"
|
|
exit 1
|
|
fi
|
|
|
|
if ! command -v python3 &> /dev/null; then
|
|
log_error "python3 未找到"
|
|
exit 1
|
|
fi
|
|
|
|
log_success "依赖检查完成"
|
|
}
|
|
|
|
# 检查网络连接
|
|
check_connectivity() {
|
|
log_info "检查目标主机连接性..."
|
|
|
|
local inventory_file="$PROJECT_ROOT/configuration/inventories/production/consul-cluster.ini"
|
|
|
|
if [[ ! -f "$inventory_file" ]]; then
|
|
log_error "清单文件不存在: $inventory_file"
|
|
exit 1
|
|
fi
|
|
|
|
# 测试连接
|
|
if ansible consul_cluster -i "$inventory_file" -m ping --one-line; then
|
|
log_success "所有主机连接正常"
|
|
else
|
|
log_warning "部分主机连接失败,但继续部署..."
|
|
fi
|
|
}
|
|
|
|
# 部署 Consul 集群
|
|
deploy_consul() {
|
|
log_info "开始部署 Consul 集群..."
|
|
|
|
local playbook_file="$PROJECT_ROOT/configuration/playbooks/applications/consul-cluster-simple.yml"
|
|
local inventory_file="$PROJECT_ROOT/configuration/inventories/production/consul-cluster.ini"
|
|
|
|
if [[ ! -f "$playbook_file" ]]; then
|
|
log_error "Playbook 文件不存在: $playbook_file"
|
|
exit 1
|
|
fi
|
|
|
|
# 运行 Ansible playbook
|
|
if ansible-playbook -i "$inventory_file" "$playbook_file" -v; then
|
|
log_success "Consul 集群部署完成"
|
|
else
|
|
log_error "Consul 集群部署失败"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# 验证集群状态
|
|
verify_cluster() {
|
|
log_info "验证 Consul 集群状态..."
|
|
|
|
local inventory_file="$PROJECT_ROOT/configuration/inventories/production/consul-cluster.ini"
|
|
|
|
# 检查服务状态
|
|
log_info "检查 Consul 服务状态..."
|
|
ansible consul_cluster -i "$inventory_file" -m shell -a "systemctl status consul --no-pager" || true
|
|
|
|
# 检查集群成员
|
|
log_info "检查集群成员..."
|
|
ansible consul_cluster -i "$inventory_file" -m shell -a "/usr/local/bin/consul members" -l "$(head -n1 < <(grep -v '^\[' "$inventory_file" | grep -v '^$' | head -n1))" || true
|
|
|
|
# 检查领导者
|
|
log_info "检查集群领导者..."
|
|
ansible consul_cluster -i "$inventory_file" -m shell -a "/usr/local/bin/consul operator raft list-peers" -l "$(head -n1 < <(grep -v '^\[' "$inventory_file" | grep -v '^$' | head -n1))" || true
|
|
}
|
|
|
|
# 主函数
|
|
main() {
|
|
log_info "开始 Consul 集群简化部署..."
|
|
|
|
check_dependencies
|
|
check_connectivity
|
|
deploy_consul
|
|
verify_cluster
|
|
|
|
log_success "Consul 集群部署流程完成!"
|
|
|
|
echo ""
|
|
log_info "后续步骤:"
|
|
echo "1. 检查集群状态: consul members"
|
|
echo "2. 访问 Web UI: http://<node-ip>:8500"
|
|
echo "3. 检查日志: journalctl -u consul -f"
|
|
}
|
|
|
|
# 脚本入口
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
main "$@"
|
|
fi |