web-dev-qa-db-ja.com

秘密のSSHキーを使用してプライベートgitリポジトリをkubernetesポッドにクローンする方法は?

認証にSSHキーを使用して、プライベートgitリポジトリ(gitLab)をkubernetesポッドにクローンしようとしています。キーを秘密に保存しました。これは、目的のタスクを実行するジョブのyamlファイルです。

同じ質問がありますが、正確な解決策はありません:

Kubernetesポッドで安全なgitリポジトリを複製

実行後の初期化コンテナのログ:

fetch http://dl-cdn.alpinelinux.org/Alpine/v3.7/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/Alpine/v3.7/community/x86_64/APKINDEX.tar.gz
v3.7.1-66-gfc22ab4fd3 [http://dl-cdn.alpinelinux.org/Alpine/v3.7/main]
v3.7.1-55-g7d5f104fa7 [http://dl-cdn.alpinelinux.org/Alpine/v3.7/community]
OK: 9064 distinct packages available
OK: 23 MiB in 23 packages
Cloning into '/tmp'...
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

パブリックリポジトリに最適なyamlファイル:

apiVersion: batch/v1
kind: Job
metadata:
  name: nest-build-kaniko
  labels:
    app: nest-kaniko-example
spec:
  template:
    spec:
      containers:
        -
          image: 'gcr.io/kaniko-project/executor:latest'
          name: kaniko
          args: ["--dockerfile=/workspace/Dockerfile",
                "--context=/workspace/",
                "--destination=aws.dest.cred"]
          volumeMounts:
            -
              mountPath: /workspace
              name: source
            -
              name: aws-secret
              mountPath: /root/.aws/
            -
              name: docker-config
              mountPath: /kaniko/.docker/
      initContainers:
        -
          name: download
          image: Alpine:3.7
          command: ["/bin/sh","-c"]
          args: ['apk add --no-cache git && git clone https://github.com/username/repo.git /tmp/']
          volumeMounts:
            -
              mountPath: /tmp
              name: source
      restartPolicy: Never
      volumes:
        -
          emptyDir: {}
          name: source
        -
          name: aws-secret
          secret:
            secretName: aws-secret
        -
          name: docker-config
          configMap:
            name: docker-config

プライベートリポジトリのクローンを作成するためにgit-syncを使用した後のyamlファイル:

apiVersion: batch/v1
kind: Job
metadata:
  name: nest-build-kaniko
  labels:
    app: nest-kaniko-example
spec:
  template:
    spec:
      containers:
        -
          image: 'gcr.io/kaniko-project/executor:latest'
          name: kaniko
          args: ["--dockerfile=/workspace/Dockerfile",
                "--context=/workspace/",
                "--destination=aws.dest.cred"]
          volumeMounts:
            -
              mountPath: /workspace
              name: source
            -
              name: aws-secret
              mountPath: /root/.aws/
            -
              name: docker-config
              mountPath: /kaniko/.docker/
      initContainers:
        -
          name: git-sync
          image: gcr.io/google_containers/git-sync-AMD64:v2.0.4
          volumeMounts:
            -
              mountPath: /git/tmp
              name: source
            -
              name: git-secret
              mountPath: "/etc/git-secret"
          env:
            - name: GIT_SYNC_REPO
              value: "[email protected]:username/repo.git"
            - name: GIT_SYNC_SSH
              value: "true"
            - name: GIT_SYNC_DEST
              value: "/tmp"
            - name: GIT_SYNC_ONE_TIME
              value: "true"
          securityContext:
            runAsUser: 0
      restartPolicy: Never
      volumes:
        -
          emptyDir: {}
          name: source
        -
          name: aws-secret
          secret:
            secretName: aws-secret
        -
          name: git-secret
          secret:
            secretName: git-creds
            defaultMode: 256
        -
          name: docker-config
          configMap:
            name: docker-config
6
Rohan Mehto

git-sync を使用できます

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: git-sync-test
spec:
  selector:
    matchLabels:
      app: git-sync-test
  serviceName: "git-sync-test"
  replicas: 1
  template:
    metadata:
      labels:
        app: git-sync-test
    spec:
      containers:
      - name: git-sync-test
        image: <your-main-image>
        volumeMounts:
        - name: service
          mountPath: /var/magic
      initContainers:
      - name: git-sync
        image: k8s.gcr.io/git-sync-AMD64:v2.0.6
        imagePullPolicy: Always
        volumeMounts:
        - name: service
          mountPath: /magic
        - name: git-secret
          mountPath: /etc/git-secret
        env:
        - name: GIT_SYNC_REPO
          value: <repo-path-you-want-to-clone>
        - name: GIT_SYNC_BRANCH
          value: <repo-branch>
        - name: GIT_SYNC_ROOT
          value: /magic
        - name: GIT_SYNC_DEST
          value: <path-where-you-want-to-clone>
        - name: GIT_SYNC_PERMISSIONS
          value: "0777"
        - name: GIT_SYNC_ONE_TIME
          value: "true"
        - name: GIT_SYNC_SSH
          value: "true"
        securityContext:
          runAsUser: 0
      volumes:
      - name: service
        emptyDir: {}
      - name: git-secret
        secret:
          defaultMode: 256
          secretName: git-creds # your-ssh-key

詳細については、 this リンクを確認してください。

6
Abu Hanifa
  initContainers:
    -
      name: git-sync
      image: gcr.io/google_containers/git-sync-AMD64:v2.0.4
      volumeMounts:
        -
          mountPath: /workspace
          name: source
        -
          name: git-secret
          mountPath: "/etc/git-secret"
      env:
        - name: GIT_SYNC_REPO
          value: "[email protected]:username/repo.git"
        - name: GIT_SYNC_SSH
          value: "true"
        - name: GIT_SYNC_ROOT
          value: /workspace
        - name: GIT_SYNC_DEST
          value: "tmp"
        - name: GIT_SYNC_ONE_TIME
          value: "true"

注:GIT_SYNC_ROOT envを/ workspaceに設定します

EmptyDir sourceボリュームの/workspace/tmpディレクトリに複製されます。

3
Abu Hanifa