Build git-sync for side car container in Kubernetes
Problem:
We have some config variables which can be updated some time by developers, so we need use this ENV variable in our container, how to update?
Solution:
As kubernetes we can create a pod with multiple containers. All containers can share volume (data). So we can use git to store data, every time code is pushed to gihub, the data will be pulled to volume by sidecar container.
let try with https://github.com/kubernetes/git-sync
How to use:
we assume there are 3 containers.
First is nginx which will be used for web server, second is hugo which will be used as static and git-sync which will pull source code from github every time new push.
1. using https
This case is simple but our git repo must be public.
Let create a pod with 3 containers
apiVersion: apps/v1
kind: Deployment
metadata:
name: blog
spec:
replicas: 1
selector:
matchLabels:
name: blog
template:
metadata:
labels:
name: blog
spec:
containers:
- name: git-sync
image: k8s.gcr.io/git-sync:v3.0.1
volumeMounts:
- name: markdown
mountPath: /tmp/git
env:
- name: GIT_SYNC_REPO
value: https://github.com/kubernetes/git-sync.git
- name: GIT_SYNC_DEST
value: git-sync
- name: hugo
image: k8s.gcr.io/hugo
volumeMounts:
- name: markdown
mountPath: /src
- name: html
mountPath: /dest
env:
- name: HUGO_SRC
value: /src/git-sync/demo/blog
- name: HUGO_BUILD_DRAFT
value: "true"
- name: HUGO_BASE_URL
value: example.com
- name: nginx
image: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: markdown
emptyDir: {}
- name: html
emptyDir: {}
services file
apiVersion: v1
kind: Service
metadata:
name: blog-service
spec:
type: LoadBalancer
ports:
- port: 80
selector:
name: blog
2. Our repo is private
In this case, we need to add ssh key to GitHub and use public key to pull source from the git. It is quite complicated. I write next …