首页 > 解决方案 > Kubernetes 中的 Gitsync 未正确部署

问题描述

我在 kubernetes 中运行 gitsync 容器并尝试从 github 同步存储库。我已经使用 known_hosts 和 ssh 创建了秘密。但是我面临以下错误。

"msg"="failed to sync repo, aborting" "error"="error running command: exit status 128: "Cloning into '/tmp/git'...\nfatal: 无法从远程存储库读取。\n\ n请确保您具有正确的访问权限\n并且存储库存在。\n""

这是我的部署文件。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gitsync-deployment
  labels:
    app: gitsync
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gitsync
  template:
    metadata:
      labels:
        app: gitsync
    spec:
      containers:
      - name: git-sync
        image: k8s.gcr.io/git-sync:v3.1.5
#        command: ["cat"]
#        args: ["/etc/git-secret/ssh"]
        imagePullPolicy: Always
        volumeMounts:
        - name: git-secret
          mountPath: /etc/git-secret
        env:
        - name: GIT_SYNC_REPO
          value: "git@github.com:username/test.git"
        - name: GIT_SYNC_SSH
          value: "true"
        - name: GIT_SYNC_BRANCH
          value: master
        - name: GIT_SYNC_DEST
          value: git
        - name: GIT_SYNC_DEPTH
          value: "1"
      volumes:
      - name: html
        emptyDir: {}
      - name: git-secret
        secret:
          secretName: git-creds
          defaultMode: 256

标签: gitgithubkubernetescontainersgoogle-kubernetes-engine

解决方案


似乎您遵循了官方文档

但事实证明,该文档根本没有提到将公钥放在哪里。

实际上,通过 SSH 进行 git 身份验证需要以下步骤:

1. 生成 SSH 密钥对:

ssh-keygen -t rsa -N "" -f mykey

此 cmd 生成 2 个文件:

  • 私钥:./mykey
  • 公钥:./mykey.pub

2. 将公钥放入您的 Github 帐户下的 Settings > SSH Keys

复制内容./mykey.pub并将其添加到您的 github 帐户中。

3.将私钥放入k8s secret中

官方文档从这里开始,它被认为$HOME/.ssh/id_rsa是私钥。

kubectl create secret generic git-creds \
    --from-file=ssh=./mykey \ 
  ....

其余的应该和官方文档解释的一样。


推荐阅读