跳转至

0001-ssh

ArgoCD SSH凭证模版配置流程

生成用于argocd部署使用的ssh key,也可使用现有的

ssh-keygen -f ./ssh/id_rsa.argocd

一、安装 argocd cli

安装 argocd-cli 命令行工具

https://argo-cd.readthedocs.io/en/stable/cli_installation/

二、登录 argocd

./argocd login https://argocd.linuxnbg:443 --insecure --username admin --password test111 --grpc-web

三、记录 gitea 的 host key

root@k8s-master01:~/argocd# ssh-keyscan gitea.linuxnbg.com  | ./argocd cert add-ssh --batch --grpc-web
    
    
Enter SSH known hosts entries, one per line. Press CTRL-D when finished.
# gitea.datarc.cn:22 SSH-2.0-OpenSSH_9.9
# gitea.datarc.cn:22 SSH-2.0-OpenSSH_9.9
# gitea.datarc.cn:22 SSH-2.0-OpenSSH_9.9
# gitea.datarc.cn:22 SSH-2.0-OpenSSH_9.9
# gitea.datarc.cn:22 SSH-2.0-OpenSSH_9.9
Successfully created 0 SSH known host entries

四、添加SSH仓库模版

# 仓库的根路径作为模版添加
argocd repocreds add ssh://git@gitea.linuxnbg.com --ssh-private-key-path ./ssh/id_rsa.argocd
# 检查是否添加成功
./argocd repocreds list --grpc-web

五、添加后续应用

apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: helm-git-based-appset
  namespace: argocd

spec:
  generators:
    # 1. 从Git仓库拉取环境列表和Helm源信息
    - git:
        repoURL: "https://gitea.gitops.linuxcdn.com:60000/argocd-test/gitea-argocd-helm-nginx.git"  # 你的Git仓库
        targetRevision: main                                                                         # Git分支
        files:
          - path: "values/*/values.yaml"  # 匹配所有环境的values文件(用于识别环境名)

    # 2. 结合List Generator补充环境元信息(命名空间等)
    - list:
        elements:
          - env: oem
            namespace: nginx-oem
          - env: rc
            namespace: nginx-rc
          - env: chatbi
            namespace: nginx-chatbi

  # 3. 模板:组合Git配置和环境参数生成Application
  template:
    metadata:
      name: helm-app-{{env}}  # 应用名称:helm-app-oem/rc/chatbi
      labels:
        env: {{env}}

    spec:
      project: default

      source:
        # 从Git仓库的helm-source.yaml中读取在线Helm包信息
        repoURL: '{{ index .values "helm-source.yaml" "repoURL" }}'  # 在线Helm仓库地址
        chart: '{{ index .values "helm-source.yaml" "chart" }}'      # Helm Chart名称
        targetRevision: '{{ index .values "helm-source.yaml" "targetRevision" }}'  # Chart版本

        # Helm配置:加载Git仓库中的values文件
        helm:
          valueFiles:
            # 共享配置
            - "git::https://gitea.gitops.linuxcdn.com:60000/argocd-test/gitea-argocd-helm-nginx.git::main::values/shared.yaml"
            # 环境专属配置(动态拼接路径)
            - "git::https://gitea.gitops.linuxcdn.com:60000/argocd-test/gitea-argocd-helm-nginx.git::main::values/{{env}}/values.yaml"

      destination:
        server: "https://kubernetes.default.svc"  # 目标集群
        namespace: {{namespace}}                  # 环境专属命名空间

      syncPolicy:
        automated:
          prune: true
          selfHeal: true
        syncOptions:
          - CreateNamespace=true