使用juicefs 解决基于s3做为git 存储的问题

开发一个自定义的git server 实际上是比较有意思的,而且很多时候也能解决我们好多问题

一些场景

  • 基于git 的配置管理
  • 基于git 的数据同步
  • 基于git 的数据备份

存储的问题

对于git的数据存储,我们有几种方法,本地,共享存储(nas,nfs)
本地的问题很明显,不能共享,使用有点费事,共享存储的好处比较好,实现一个简单的ha,但是问题也很明显共享存储管理比较费事(nfs)
基于s3 是一个不错的选择,但是s3 也有一个问题,就是lock 的问题(原生git 是需要使用文件锁的)

解决方法

我们可以基于s3进行存储,使用juicefs,juicefs 实现了完备的posix 协议,可以很好的解决此问题

参考代码

  • 环境
 
version: "3"
services:
    s3:
      image: minio/minio
      environment:
      - "MINIO_ACCESS_KEY=minio"
      - "MINIO_SECRET_KEY=minio123"
      command: server /data --console-address ":9001"
      ports:
      - "9000:9000"
      - "9001:9001"
  • juicefs 格式化以及挂载

    元数据使用本地,没有使用共享的

juicefs format --storage minio \
    --bucket http://127.0.0.1:9000/jfs2 \
    --access-key minio \
    --secret-key minio123 \
    sqlite3://myjfs.db myjfs
 
juicefs mount sqlite3://myjfs.db mydemoapp
  • git 代码
    基于node-git-server模块
 
const  { Git } = require('node-git-server');
const { join } = require('path');
const port =
  !process.env.PORT || isNaN(process.env.PORT)
    ? 7005
    : parseInt(process.env.PORT);
 
const repos = new Git(join(__dirname, './mydemoapp'), {
  autoCreate: true,
});
 
 
repos.on('push', (push) => {
  console.log(`push ${push.repo}/${push.commit} ( ${push.branch} )`);
  push.accept();
});
 
repos.on('fetch', (fetch) => {
  console.log(`fetch ${fetch.commit}`);
  fetch.accept();
});
 
repos.listen(port,{type:'http'},()=>{
  console.log(`node-git-server running at http://localhost:${port}`);
})

使用

  • 启动服务
docker-compose up -d 
node app.js
  • 创建git 项目
git init 
touch demo.txt
git add .
git commit -m "demo"
git remote add http://localhost:7005/demo.git
git push -u origin master
  • 效果

 

 

 

 

说明

基于juicefs + s3 可以很方便的解决我们基于s3 存储git 应用的问题,而且管理也比较方便,当然也可以直接使用csi 和容器集成起来

参考资料

https://gabrielcsapo.github.io/node-git-server/docs/intro
https://juicefs.com/docs/zh/community/quick_start_guide
https://github.com/gabrielcsapo/node-git-server
https://github.com/rongfengliang/juicefs-gitserver-storage

posted on 2022-02-02 13:53  荣锋亮  阅读(202)  评论(0编辑  收藏  举报

导航