Files
k8s-docs/k8s/xiongxiao.me/deploy-apps.sh
2025-11-26 15:44:15 +08:00

92 lines
2.9 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
# Nginx 配置迁移到 K3s + Traefik 部署脚本
# tags: kubernetes, k3s, traefik, deployment, automation
# description: 自动部署外部服务和 IngressRoute 配置的脚本
# title: 自动部署脚本
# createdAt: 2025-11-26
set -e
echo "======================================"
echo "Nginx 配置迁移到 K3s + Traefik"
echo "======================================"
echo ""
# 颜色定义
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# 检查 kubectl
if ! command -v kubectl &> /dev/null; then
echo -e "${RED}错误: kubectl 未安装${NC}"
exit 1
fi
# 检查 Traefik
echo -e "${YELLOW}步骤 1/4: 检查 Traefik 部署状态...${NC}"
if ! kubectl get namespace traefik &> /dev/null; then
echo -e "${YELLOW}Traefik namespace 不存在,正在创建...${NC}"
kubectl create namespace traefik
fi
if ! kubectl get deployment traefik -n traefik &> /dev/null; then
echo -e "${YELLOW}Traefik 未部署,正在部署...${NC}"
kubectl apply -f traefik/traefik-complete.yaml
echo -e "${GREEN}等待 Traefik Pod 就绪...${NC}"
kubectl wait --for=condition=ready pod -l app=traefik -n traefik --timeout=120s
else
echo -e "${GREEN}✓ Traefik 已部署${NC}"
fi
# 部署外部服务
echo ""
echo -e "${YELLOW}步骤 2/4: 部署外部服务配置...${NC}"
kubectl apply -f services/external-services.yaml
echo -e "${GREEN}✓ 外部服务配置已应用${NC}"
# 验证服务
echo ""
echo -e "${YELLOW}步骤 3/4: 验证服务创建...${NC}"
sleep 2
SERVICE_COUNT=$(kubectl get svc -n default | grep -c "external" || true)
ENDPOINT_COUNT=$(kubectl get endpoints -n default | grep -c "external" || true)
echo -e "${GREEN}✓ 创建了 ${SERVICE_COUNT} 个服务${NC}"
echo -e "${GREEN}✓ 创建了 ${ENDPOINT_COUNT} 个 Endpoints${NC}"
# 部署 IngressRoute
echo ""
echo -e "${YELLOW}步骤 4/4: 部署 IngressRoute 配置...${NC}"
kubectl apply -f ingress/apps-ingressroute.yaml
sleep 2
ROUTE_COUNT=$(kubectl get ingressroute -n default 2>/dev/null | grep -c "https" || true)
echo -e "${GREEN}✓ 创建了 ${ROUTE_COUNT} 个 IngressRoute${NC}"
# 显示结果
echo ""
echo -e "${GREEN}======================================"
echo "部署完成!"
echo "======================================${NC}"
echo ""
echo "已部署的服务:"
kubectl get svc -n default | grep external || echo "无"
echo ""
echo "已部署的 IngressRoute"
kubectl get ingressroute -n default || echo "无"
echo ""
echo -e "${YELLOW}提示:${NC}"
echo "1. 查看 Traefik Dashboard:"
echo " kubectl port-forward svc/traefik 8080:8080 -n traefik"
echo " 然后访问 http://localhost:8080/dashboard/"
echo ""
echo "2. 查看 Traefik 日志:"
echo " kubectl logs -n traefik -l app=traefik -f"
echo ""
echo "3. 测试服务访问:"
echo " curl -k https://blinko.xiongxiao.me"
echo ""
echo "4. 确保 DNS 已配置正确,将域名指向 Traefik 的 NodePort (30443)"
echo ""