kubernetesdeploymentsidecargit-sync

How to use git-sync image as a sidecar in kubernetes that git pulls periodically


I am trying to use git-sync image as a side car in kubernetes that runs git-pull periodically and mounts cloned data to shared volume.

Everything is working fine when I configure it for sync one time. I want to run it periodically like every 10 mins. Somehow when I configure it to run periodically pod initializing is failing.

I read documentation but couldn't find proper answer. Would be nice if you help me to figure out what I am missing in my configuration.

Here is my configuration that failing.

Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx-helloworld
          image: nginx
          ports:
            - containerPort: 80
          volumeMounts:
            - mountPath: "/usr/share/nginx/html"
              name: www-data
      initContainers:
        - name: git-sync
          image: k8s.gcr.io/git-sync:v3.1.3
          volumeMounts:
            - name: www-data
              mountPath: /data
          env:
            - name: GIT_SYNC_REPO
              value: "https://github.com/musaalp/design-patterns.git" ##repo-path-you-want-to-clone
            - name: GIT_SYNC_BRANCH
              value: "master" ##repo-branch
            - name: GIT_SYNC_ROOT
              value: /data
            - name: GIT_SYNC_DEST
              value: "hello" ##path-where-you-want-to-clone
            - name: GIT_SYNC_PERIOD
              value: "10"
            - name: GIT_SYNC_ONE_TIME
              value: "false"
          securityContext:
            runAsUser: 0
      volumes:
        - name: www-data
          emptyDir: {}

Pod

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx-helloworld
  name: nginx-helloworld
spec:
  containers:
  - image: nginx
    name: nginx-helloworld
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

Solution

  • you are using the git-sync as an initContainers, which run only during init (once in lifecycle)

    A Pod can have multiple containers running apps within it, but it can also have one or more init containers, which are run before the app containers are started.

    Init containers are exactly like regular containers, except:

    • Init containers always run to completion.
    • Each init container must complete successfully before the next one starts. init-containers

    So use this as a regular container

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      labels:
        app: nginx
    spec:
      selector:
        matchLabels:
          app: nginx
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
            - name: git-sync
              image: k8s.gcr.io/git-sync:v3.1.3
              volumeMounts:
                - name: www-data
                  mountPath: /data
              env:
                - name: GIT_SYNC_REPO
                  value: "https://github.com/musaalp/design-patterns.git" ##repo-path-you-want-to-clone
                - name: GIT_SYNC_BRANCH
                  value: "master" ##repo-branch
                - name: GIT_SYNC_ROOT
                  value: /data
                - name: GIT_SYNC_DEST
                  value: "hello" ##path-where-you-want-to-clone
                - name: GIT_SYNC_PERIOD
                  value: "20"
                - name: GIT_SYNC_ONE_TIME
                  value: "false"
              securityContext:
                runAsUser: 0
            - name: nginx-helloworld
              image: nginx
              ports:
                - containerPort: 80
              volumeMounts:
                - mountPath: "/usr/share/nginx/html"
                  name: www-data
          volumes:
            - name: www-data
              emptyDir: {}