KubeSphere DevOps 如何获取 ArgoCD 初始密码并用 GitOps Application 部署应用
【免费下载链接】kubesphereThe container platform tailored for Kubernetes multi-cloud, datacenter, and edge management ⎈ 🖥 ☁️项目地址: https://gitcode.com/GitHub_Trending/ku/kubesphere
KubeSphere DevOps 扩展自带 ArgoCD v2.11.7 子图表,以声明式 GitOps 方式做持续部署。部署应用前有两件必做的事:先从集群里取出 ArgoCD 的初始 admin 密码登录 UI 验证环境,再创建 GitOps Application 把 Git 仓库中的清单同步到目标命名空间。本文按这条路径走一遍:先确认 ArgoCD 组件已就绪,再获取初始密码、打通 UI 访问,最后通过 KubeSphere GitOps Application 完成一次应用部署并核对同步与运行状态。
前提条件:确认 DevOps 扩展与 ArgoCD 已安装
ArgoCD 作为 DevOps 扩展的 bundled 子图表安装,默认部署在argocd命名空间(可通过argocd.namespace配置)。扩展本身通过 InstallPlan 安装,文档给出的示例使用扩展版本1.2.4:
apiVersion: kubesphere.io/v1alpha1 kind: InstallPlan metadata: name: devops namespace: kubesphere-system spec: extension: name: devops version: 1.2.4 enabled: true upgradeStrategy: Manual # Required for production config: | agent: argocd: enabled: true # Enable ArgoCD namespace: "argocd" # ArgoCD namespaceconfig段可省略以使用扩展默认值;upgradeStrategy: Manual在生产环境必须保留。安装后按下面命令验证组件是否就绪:
# Verify ArgoCD namespace exists kubectl get ns argocd # Check all ArgoCD pods kubectl get pods -n argocd # Check ArgoCD services kubectl get svc -n argocdArgoCD 的主要组件及 Pod 命名规则如下,排障时可按此定位:
| 组件 | Pod 名称模式 | 作用 |
|---|---|---|
| Application Controller | devops-agent-argocd-application-controller-* | 调和 Application 状态 |
| ApplicationSet Controller | devops-agent-argocd-applicationset-controller-* | 管理 ApplicationSet CRD |
| Dex Server | devops-agent-argocd-dex-server-* | SSO 认证代理 |
| Notifications Controller | devops-agent-argocd-notifications-controller-* | 事件通知 |
| Redis | devops-agent-argocd-redis-* | 缓存与状态存储 |
| Repo Server | devops-agent-argocd-repo-server-* | Git 仓库操作 |
| ArgoCD Server | devops-agent-argocd-server-* | API 与 UI |
版本信息(文档记录):ArgoCD v2.11.7,ArgoCD Helm Chart 7.3.11,Redis 7.2.4,Dex v2.38.0。
获取 ArgoCD 初始管理员密码
初始密码保存在argocd命名空间的argocd-initial-admin-secretSecret 中,字段password经过 base64 编码:
# Get initial admin password kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath='{.data.password}' | base64 -d拿到密码后,可通过端口转发登录 ArgoCD UI 核对环境:
# Get ArgoCD server service kubectl get svc devops-agent-argocd-server -n argocd # Port-forward for local access kubectl port-forward svc/devops-agent-argocd-server -n argocd 8080:443 # Access via: https://localhost:8080 # Username: admin # Password: (上一步命令的输出)浏览器访问https://localhost:8080,用户名admin,密码使用上面命令的输出。这个密码同样适用于 argocd CLI 登录,例如在添加 Git 仓库时:
# Login to argocd CLI argocd login localhost:8080 --username admin --password $(kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath='{.data.password}' | base64 -d)准备租户身份与 API Token
GitOps Application 路径面向租户:租户不需要(也没有)访问argocd命名空间的权限,所有操作通过 KubeSphere API 完成。开始前准备以下环境变量,API_TOKEN是租户的 KubeSphere OAuth Token,DEVOPS_PROJECT是目标 DevOps 项目(即命名空间),KUBESPHERE_API是 KubeSphere API 地址:
export API_TOKEN="<tenant-kubesphere-token>" export KUBESPHERE_API="https://kubesphere-api.example.com" export DEVOPS_PROJECT="demo-project"如果没有现成 Token,可通过 OAuth 端点换取(文档给出的方式,client_id与client_secret均固定为kubesphere,Token 有效期 7200 秒):
# Get token export API_TOKEN=$(curl -s -X POST "${KUBESPHERE_API}/oauth/token" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "grant_type=password&username=${USERNAME}&password=${PASSWORD}&client_id=kubesphere&client_secret=kubesphere" \ | jq -r '.access_token')其中USERNAME、PASSWORD替换为租户自己的账号密码。
创建 GitOps Application 部署应用
向 KubeSphere API 提交一个gitops.kubesphere.io/v1alpha1的 Application 资源,spec 内嵌 ArgoCD 应用的argoApp.spec:
# Create GitOps Application via API curl -s -X POST "${KUBESPHERE_API}/kapis/gitops.kubesphere.io/v1alpha1/namespaces/${DEVOPS_PROJECT}/applications" \ -H "Authorization: Bearer ${API_TOKEN}" \ -H "Content-Type: application/json" \ -d '{ "apiVersion": "gitops.kubesphere.io/v1alpha1", "kind": "Application", "metadata": { "name": "guestbook", "namespace": "'${DEVOPS_PROJECT}'", "labels": { "gitops.kubesphere.io/argocd-location": "argocd" } }, "spec": { "argoApp": { "spec": { "project": "default", "source": { "repoURL": "https://github.com/stoneshi-yunify/argocd-example-apps", "targetRevision": "HEAD", "path": "guestbook" }, "destination": { "server": "https://kubernetes.default.svc", "namespace": "'${DEVOPS_PROJECT}'" }, "syncPolicy": { "automated": { "prune": true, "selfHeal": true }, "syncOptions": [ "CreateNamespace=true" ] } } } } }'示例仓库与路径(argocd-example-apps的guestbook)来自文档示例,实际部署时把repoURL、targetRevision、path换成自己的仓库、分支/标签和清单目录。syncPolicy中prune: true表示删除 Git 中已移除的资源,selfHeal: true表示手动删除的资源会被自动重建,文档建议生产 GitOps 工作流同时启用两者。
创建后发生的事:租户在自身命名空间创建 Application → KubeSphere 控制器在argocd命名空间自动生成对应的 ArgoCD Application → ArgoCD 把应用同步进租户命名空间。该路径依赖 KubeSphere GitOps 控制器处于运行状态。
两个关键约束必须满足:
必须带标签
gitops.kubesphere.io/argocd-location: argocd。缺少该标签时控制器会静默忽略 Application,不会创建 ArgoCD Application,状态停留在 Unknown。控制器日志会出现:Warning Invalid application/private-guestbook Cannot find the namespace of the Argo CD instance from key: gitops.kubesphere.io/argocd-location不要手动创建同名 ArgoCD Application。控制器会自动创建对应的 ArgoCD Application,若手动再建一个同名或指向相同资源的 ArgoCD Application,会产生资源冲突(SharedResourceWarning、OutOfSync 状态),例如:
Deployment/guestbook-ui is part of applications argocd/private-guestbook and stone-devops-private-guestbook
另外,当spec.argoApp.spec.destination.server为https://kubernetes.default.svc且destination.name为空或in-cluster时,应用部署到 API 路径指定的集群:不带/clusters/{cluster}前缀访问 host 集群,带前缀(如/clusters/member-1/...)则访问对应成员集群。
验证部署结果
租户可能没有权限直接查询目标命名空间的 Pod,文档明确建议通过 Application 状态而不是直连资源来验证部署。
方法一:检查状态标签(租户可访问):
curl -s "${KUBESPHERE_API}/kapis/gitops.kubesphere.io/v1alpha1/namespaces/${DEVOPS_PROJECT}/applications/guestbook" \ -H "Authorization: Bearer ${API_TOKEN}" | jq -r '{ health: .metadata.labels["gitops.kubesphere.io/health-status"], sync: .metadata.labels["gitops.kubesphere.io/sync-status"] }'同步且健康时的预期输出(文档示例):
{ "health": "Healthy", "sync": "Synced" }标签取值范围:gitops.kubesphere.io/health-status为 Healthy / Progressing / Degraded / Missing / Unknown;gitops.kubesphere.io/sync-status为 Synced / OutOfSync。
方法二:读取详细状态。.status.argoApp是 JSON 字符串,二次解析后可得到 revision、逐资源状态和镜像列表:
curl -s "${KUBESPHERE_API}/kapis/gitops.kubesphere.io/v1alpha1/namespaces/${DEVOPS_PROJECT}/applications/guestbook" \ -H "Authorization: Bearer ${API_TOKEN}" | jq -r '.status.argoApp' | jq -r '{ syncStatus: .sync.status, healthStatus: .health.status, revision: .sync.revision, resources: [.resources[] | {kind: .kind, name: .name, status: .status, health: .health.status}] }'文档示例输出(其中的 revision、资源列表和镜像名为示例值,实际以仓库内容为准):
{ "syncStatus": "Synced", "healthStatus": "Healthy", "revision": "f946a1c393d50a460cc44944a476971fe13961f4", "resources": [ {"kind": "Service", "name": "guestbook-ui", "status": "Synced", "health": "Healthy"}, {"kind": "Deployment", "name": "guestbook-ui", "status": "Synced", "health": "Healthy"} ], "images": ["gcr.io/google-samples/gb-frontend:v5"] }方法三:查看同步操作状态,用于确认最近一次 sync 是否成功:
curl -s "${KUBESPHERE_API}/kapis/gitops.kubesphere.io/v1alpha1/namespaces/${DEVOPS_PROJECT}/applications/guestbook" \ -H "Authorization: Bearer ${API_TOKEN}" | jq -r '.status.argoApp' | jq -r '.operationState | { phase: .phase, message: .message, startedAt: .startedAt, finishedAt: .finishedAt }'同步成功时的文档示例输出:
{ "phase": "Succeeded", "message": "successfully synced (all tasks run)", "startedAt": "2026-03-27T09:09:12Z", "finishedAt": "2026-03-27T09:09:15Z" }管理员侧则可以直接查 ArgoCD Application:
# Check ArgoCD Application (admin only) kubectl get application -n argocd | grep guestbook # Detailed status kubectl get applications.argoproj.io guestbook -n argocd -o custom-columns=\ SYNC:.status.sync.status,HEALTH:.status.health.status,REVISION:.status.sync.revision # Check deployed resources kubectl get all -n ${DEVOPS_PROJECT}可选分支:管理员直接创建 ArgoCD Application
管理员(有权访问argocd命名空间)可以不走 KubeSphere API,直接创建argoproj.io/v1alpha1的 Application:
apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: guestbook namespace: argocd spec: project: default source: repoURL: https://github.com/stoneshi-yunify/argocd-example-apps targetRevision: HEAD path: guestbook destination: server: https://kubernetes.default.svc namespace: argo-guestbook syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=true保存为guestbook-app.yaml后应用并检查状态:
kubectl apply -f guestbook-app.yaml # Quick status kubectl get applications.argoproj.io guestbook -n argocd # Detailed status kubectl get applications.argoproj.io guestbook -n argocd -o custom-columns=\ SYNC:.status.sync.status,HEALTH:.status.health.status,REVISION:.status.sync.revision # Check deployed resources kubectl get all -n argo-guestbook文档示例的期望输出(revision 为示例值):
SYNC HEALTH REVISION Synced Healthy 335cffbb730e59b165c308b98c3fa4037822bf2b私有仓库需要先在 ArgoCD 中登记凭据,两种方式:CLI(配合前文获取的初始密码登录)
# Add repository argocd repo add https://github.com/example/repo.git \ --username <user> \ --password <token>或在argocd命名空间创建 repository 类型的 Secret(<username>、<personal-access-token>替换为实际值):
apiVersion: v1 kind: Secret metadata: name: repo-github-example namespace: argocd labels: argocd.argoproj.io/secret-type: repository stringData: type: git url: https://github.com/example/repo.git username: <username> password: <personal-access-token>注意两条路径不要对同一批资源混用:用 KubeSphere GitOps Application 创建过的应用,就不要再手动建同名 ArgoCD Application,否则出现前文所说的资源冲突。
手动同步、强制刷新与清理
触发手动同步(租户可通过 KubeSphere API):
# Trigger sync curl -s -X POST "${KUBESPHERE_API}/kapis/gitops.kubesphere.io/v1alpha1/namespaces/${DEVOPS_PROJECT}/applications/guestbook/sync" \ -H "Authorization: Bearer ${API_TOKEN}" \ -H "Content-Type: application/json" \ -d '{"prune":true}'成功时响应可能为空;若自动同步刚完成不久,会返回 HTTP 400 和another operation is already in progress。
强制重新同步(例如手动删除资源后),给 Application 打 refresh 注解:
kubectl patch applications.argoproj.io guestbook -n argocd --type merge \ -p '{"metadata":{"annotations":{"argocd.argoproj.io/refresh":"hard"}}}'加完注解后文档建议等 5–10 秒再查状态。若selfHeal: true已开启,ArgoCD 本身会自动重建被删除的资源,一般无需手动触发。
删除应用:
# Delete Application (resources remain by default) kubectl delete applications.argoproj.io guestbook -n argocd # Clean up remaining resources kubectl delete all --all -n argo-guestbook注意该命令会删除整个命名空间内所有工作负载,执行前确认命名空间里没有其他业务资源。若syncPolicy.automated.prune: true,删除 Application 时 ArgoCD 会一并清理其管理的资源,无需再执行第二步。
常见问题与限制
文档给出的对照表(与 GitOps 部署直接相关的部分):
| 现象 | 原因 | 处理 |
|---|---|---|
| 状态卡在 Unknown(KubeSphere GitOps) | 缺少必需标签 | 添加标签gitops.kubesphere.io/argocd-location: argocd |
| SharedResourceWarning / OutOfSync | 存在重复的 ArgoCD Application | 删除手动创建的 ArgoCD Application,只保留 KubeSphere GitOps Application |
| Sync failed | 清单无效 | 检查status.operationState中的错误 |
| Permission denied | RBAC 问题 | 确认 ArgoCD 在目标命名空间有权限 |
| Repo not found | 凭据问题 | 检查 repository secret 与 URL |
| OutOfSync | 检测到漂移 | 开启自动同步或手动 sync |
| 删除资源后未重建 | 未开启自动同步 | 添加selfHeal: true或手动触发 sync |
管理员侧排查命令:
# Check application conditions kubectl get application guestbook -n argocd -o jsonpath='{.status.conditions}' # View application events kubectl describe application guestbook -n argocd # Application controller logs kubectl logs -n argocd -l app.kubernetes.io/name=argocd-application-controller租户侧的限制(文档明确列出):可以创建 GitOps Application 并在自己命名空间查看部署的资源,但不能修改 ArgoCD 配置、不能访问argocd命名空间、看不到 ArgoCD UI,也不能把应用命名空间加入 ArgoCD 的application.namespaces(该操作需要管理员)。
进一步的操作细节可参考仓库内的 ArgoCD 配置说明、DevOps 扩展总览 与 租户操作指南。
【免费下载链接】kubesphereThe container platform tailored for Kubernetes multi-cloud, datacenter, and edge management ⎈ 🖥 ☁️项目地址: https://gitcode.com/GitHub_Trending/ku/kubesphere
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考