Gitsync in kubernetes not deploying properly

7/11/2020

I'm running gitsync container in kubernetes and trying to sync the repository from github. I have already created secret using known_hosts and ssh. However I face following error.

"msg"="failed to sync repo, aborting" "error"="error running command: exit status 128: \"Cloning into '/tmp/git'...\nfatal: Could not read from remote repository.\n\nPlease make sure you have the correct access rights\nand the repository exists.\n\""

Here is my deployment file.

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
-- DevOps
containers
git
github
google-kubernetes-engine
kubernetes

1 Answer

7/11/2020

Seems that you followed the official documentation.

But it turns out that this documentation does not mention at all where to put the public key.

Actually, a git authentication thru SSH requires the following steps :

1. Generate SSH key-pair :

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

This cmd generates 2 files:

  • private key : ./mykey
  • public key : ./mykey.pub

2. Put the public key in your Github Account under Settings > SSH Keys

Copy the content of ./mykey.pub and add it in your github account.

3. Put the Private Key in the k8s secret

The official documentation started from here, and it consider $HOME/.ssh/id_rsa as the private key.

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

the rest should be the same as the official documentation explained.

-- Abdennour TOUMI
Source: StackOverflow