Files
k3s_auto_deploy/scripts/create-nginx-argocd-app.sh

107 lines
3.3 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
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
CONFIG_FILE="$PROJECT_DIR/config/cluster-vars.yml"
echo "=== 创建Nginx应用的ArgoCD Application ==="
# 读取配置
NGINX_GIT_REPO=$(yq eval '.nginx_app_git_repo_url' "$CONFIG_FILE")
GIT_USERNAME=$(yq eval '.gitea_user_name' "$CONFIG_FILE")
GIT_PASSWORD=$(yq eval '.gitea_user_password' "$CONFIG_FILE")
# 配置Gitea仓库凭证如果不存在
echo "🔐 配置Gitea仓库凭证..."
kubectl create secret generic gitea-creds \
-n argocd \
--from-literal=username="$GIT_USERNAME" \
--from-literal=password="$GIT_PASSWORD" \
--dry-run=client -o yaml | kubectl apply -f -
# 生成Application配置
cat > /tmp/nginx-argocd-app.yaml <<EOF
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: nginx-app
namespace: argocd
labels:
app: nginx-test
spec:
project: default
source:
repoURL: $NGINX_GIT_REPO
targetRevision: main
path: manifests
destination:
server: https://kubernetes.default.svc
namespace: default
syncPolicy:
automated:
prune: true
selfHeal: true
syncOptions:
- CreateNamespace=true
# 健康检查配置
ignoreDifferences:
- group: apps
kind: Deployment
jsonPointers:
- /spec/replicas
EOF
# 应用配置
echo "📝 创建ArgoCD Application..."
kubectl apply -f /tmp/nginx-argocd-app.yaml
echo ""
echo "✅ ArgoCD Application创建成功"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📊 查看状态:"
echo " kubectl get application nginx-app -n argocd"
echo ""
echo "📝 查看详细信息:"
echo " kubectl describe application nginx-app -n argocd"
echo ""
echo "🌐 访问ArgoCD查看同步状态:"
echo " https://argocd.jpc.net3w.com"
echo ""
echo "⏳ 等待同步完成约3分钟..."
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# 等待并监控同步状态
echo ""
echo "🔍 监控同步状态..."
for i in {1..12}; do
sleep 5
STATUS=$(kubectl get application nginx-app -n argocd -o jsonpath='{.status.sync.status}' 2>/dev/null || echo "Unknown")
HEALTH=$(kubectl get application nginx-app -n argocd -o jsonpath='{.status.health.status}' 2>/dev/null || echo "Unknown")
echo "[$i/12] Sync: $STATUS | Health: $HEALTH"
if [ "$STATUS" = "Synced" ] && [ "$HEALTH" = "Healthy" ]; then
echo ""
echo "✅ 同步完成!应用已成功部署"
break
fi
done
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🎉 部署完成!"
echo ""
echo "📊 验证部署:"
echo " kubectl get pods -l app=nginx-test -n default"
echo " kubectl get svc nginx-test -n default"
echo " kubectl get ingress nginx-test -n default"
echo ""
echo "🌐 访问应用:"
echo " https://ng.jpc.net3w.com"
echo ""
echo "💡 提示:"
echo " - 修改Git仓库中的配置ArgoCD会自动同步"
echo " - 查看ArgoCD UI了解详细的同步状态"
echo " - 首次HTTPS访问需等待证书签发1-2分钟"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"