remote-task-excutor-cli/scripts/deploy-server.sh

89 lines
2.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 部署 server 到 Kubernetes 的脚本
set -e
# 配置变量
NAMESPACE="${1:-default}"
DEPLOYMENT_NAME="remote-task-executor-server"
# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}部署 Server 到 Kubernetes${NC}"
echo -e "${GREEN}========================================${NC}"
echo -e "命名空间: ${YELLOW}${NAMESPACE}${NC}"
echo -e "部署名称: ${YELLOW}${DEPLOYMENT_NAME}${NC}"
echo ""
# 检查 kubectl 是否可用
if ! command -v kubectl &> /dev/null; then
echo -e "${RED}错误: kubectl 未安装或不在 PATH 中${NC}"
exit 1
fi
# 检查 k8s 目录是否存在
if [ ! -d "k8s" ]; then
echo -e "${RED}错误: k8s 目录不存在${NC}"
exit 1
fi
# 检查命名空间是否存在,不存在则创建
echo -e "${GREEN}[1/4] 检查命名空间...${NC}"
if ! kubectl get namespace ${NAMESPACE} &> /dev/null; then
echo -e "${YELLOW}命名空间不存在,正在创建...${NC}"
kubectl create namespace ${NAMESPACE}
echo -e "${GREEN}命名空间创建成功${NC}"
else
echo -e "${GREEN}命名空间已存在${NC}"
fi
echo ""
# 应用 Secret如果存在注意实际使用时需要先创建真实的 Secret
if [ -f "k8s/secret.yaml" ]; then
echo -e "${GREEN}[2/3] 应用 Secret...${NC}"
kubectl apply -f k8s/secret.yaml -n ${NAMESPACE}
echo -e "${GREEN}Secret 应用成功${NC}"
echo ""
else
echo -e "${YELLOW}[2/3] 跳过 Secret文件不存在如需使用请参考 secret.yaml.example${NC}"
echo ""
fi
# 应用 Deployment 和 Service
echo -e "${GREEN}[3/3] 应用 Deployment 和 Service...${NC}"
kubectl apply -f k8s/deployment.yaml -n ${NAMESPACE}
echo -e "${GREEN}Deployment 和 Service 应用成功${NC}"
echo ""
# 等待部署完成
echo -e "${GREEN}等待部署就绪...${NC}"
kubectl wait --for=condition=available --timeout=300s deployment/${DEPLOYMENT_NAME} -n ${NAMESPACE} || true
# 显示部署状态
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}部署完成!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${YELLOW}部署状态:${NC}"
kubectl get deployment ${DEPLOYMENT_NAME} -n ${NAMESPACE}
echo ""
echo -e "${YELLOW}Pod 状态:${NC}"
kubectl get pods -l app=${DEPLOYMENT_NAME} -n ${NAMESPACE}
echo ""
echo -e "${YELLOW}Service 状态:${NC}"
kubectl get svc ${DEPLOYMENT_NAME} -n ${NAMESPACE}
echo ""
echo -e "${GREEN}查看日志命令:${NC}"
echo -e "kubectl logs -f deployment/${DEPLOYMENT_NAME} -n ${NAMESPACE}"
echo ""
echo -e "${GREEN}进入 Pod 命令:${NC}"
echo -e "kubectl exec -it deployment/${DEPLOYMENT_NAME} -n ${NAMESPACE} -- /bin/sh"