Aldebaran

人生最棒的感觉,就是你做到别人说你做不到的事。

0%

维护docker-gitlab

三田寺円

前言

本质是维护docker-gitlab就是维护gitlab哈,命令都差不多。

这里主要是记录备份和还原,以及线上环境中遇到的问题。

修改初始密码:

安装完Gitlab,访问网站的第一件做的事情就是修改密码。默认用户为root。

01

开始使用GitLab,登陆界面。

02

GitLab重新加载配置文件

有时候由于需要调整GitLab,这时重新加载配置是必要的操作:

  • Omnibus

      $ sudo gitlab-ctl reconfigure
      or
      $ sudo docker exec -t <container name> gitlab-ctl reconfigure
    
  • Installations from source

      # 注意:运行该命令服务会中断
      $ sudo service gitlab restart
    

docker GitLab备份与还原

参考链接:backup_restore

备份 docker GitLab

  • 全量备份

      $ sudo docker exec -t <container name> gitlab-rake gitlab:backup:create
    
  • 跳过不备份一些数据

    您可以指定使用环境变量SKIP 跳过不备份一些数据,你可以跳过

      - db (database)
      - uploads (attachments)
      - repositories (Git repositories data)
      - builds (CI job output logs)
      - artifacts (CI job artifacts)
      - lfs (LFS objects)
      - registry (Container Registry images)
    

    例如:

      $ sudo docker exec -t <container name> gitlab-rake gitlab:backup:create SKIP=db,uploads
    

    默认情况下,GitLab创建的备份文件将具有git:git(所有者/组)和0600权限。 这是为了避免其他系统用户读取到GitLab的数据。 如果你需要备份存档具有不同的权限,则可以在配置文件里面修改“archive_permissions”。 例如:

    • In /etc/gitlab/gitlab.rb

        gitlab_rails['backup_archive_permissions'] = 0644 # Makes the backup archives world-readable
      
    • In gitlab.yml

        backup:
        archive_permissions: 0644 # Makes the backup archives world-readable
      
  • 配置定时计划任务以进行每日备份

    用于Omnibus installations(docker-gitlab 就是基于Omnibus安装)

    要计划备份您的存储库和GitLab元数据的cron作业,请使用root用户:

      $ sudo su -
      # crontab -e
    

    在那里,添加以下行以安排备份每天在凌晨2点:

      0 2 * * * docker exec -it <container name> gitlab-rake gitlab:backup:create CRON = 1
    

    你可能还想为备份设置生命周期,以防止磁盘空间不足。为此,将以下行添加到/etc/gitlab/gitlab.rb并重新配置:

      #limit backup lifetime to 7 days - 604800 seconds
      gitlab_rails ['backup_keep_time'] = 604800
    

    请注意,backup_keep_time配置选项仅管理本地文件。 GitLab不会自动删除存储在第三方对象存储(例如AWS S3)中的旧文件,因为用户可能没有列出和删除文件的权限。我们建议您为对象存储配置适当的保留策略。例如,您可以在此处配置S3备份策略,如此处所述。

    注意:此cron作业不备份你的gitlab配置和SSH主机密钥。

  • 备份配置文件

    请注意,上述的备份方法是不会备份配置文件。 一个原因是你的数据库包含两步验证的加密信息。 将加密信息及其密钥存储在不同的位置防止泄密!

    建议完全备份/etc/gitlab目录,或至少将/etc/gitlab/gitlab-secrets.json存放在安全的地方。

      # Example backup command for /etc/gitlab:
      # Create a time-stamped .tar file in the current directory.
      # The .tar file will be readable only to root.
      $ sudo sh -c 'umask 0077; tar -cf $(date "+etc-gitlab-%s.tar") -C / etc/gitlab'
    

    如果你需要使用GitLab应用程序数据备份文件恢复数据,你需要还原gitlab-secrets.json。

    如果gitlab是从源代码安装,那么需要备份config/secrets.yml文件,gitlab.yml文件,任何SSL密钥和证书以及您的SSH主机密钥。

    至少应该备份/etc/gitlab/gitlab.rb和/etc/gitlab/gitlab-secrets.json(Omnibus)或/home/git/gitlab/config/secrets.yml(source)来保存数据库 加密密钥。

    PS: 不建议将配置备份与应用程序数据备份在相同的位置

还原备份docker GitLab

你需要有一个能正常运行的GitLab安装,然后才能执行恢复。

需要注意的是备份文件的GitLab版本要和正常运行的GitLab的版本一致。这主要是因为执行恢复操作(’git’)的系统用户通常不允许创建或删除数据。

所有现有数据将被删除(SQL)或移动到单独的目录(repositories, uploads)。

如果你的GitLab用户有使用了两步验证(2FA),那么你还必须确保还原/etc/gitlab/gitlab.rb 或 /etc/gitlab/gitlab-secrets.json(Omnibus)/home/git/gitlab/config/secrets.yml(从源代码安装)

注意: 你需要在更改gitlab-secrets.json后运行gitlab-ctl reconfigure,Omnibus installations(源码安装还原详见官方文档)。

  • 还原过程假定:

    • 您已安装与创建的备份文件完全相同的GitLab Omnibus版本

    • 你已经至少一次运行sudo gitlab-ctl重新配置(可选)

    • GitLab正在运行。如果没有,使用sudo gitlab-ctl start启动它。

    • 首先确保你的备份tar文件在gitlab.rb配置gitlab_rails[‘backup_path’]中描述的备份目录中。默认值为/var/opt/gitlab/backups。

    • 停止连接到数据库的进程,让其余的GitLab程序运行:

        $ sudo docker exec -it <container name> gitlab-ctl stop unicorn
        ok: down: unicorn: 0s, normally up
        $ sudo docker exec -it <container name> gitlab-ctl stop sidekiq
        ok: down: sidekiq: 1s, normally up
      
    • 验证

        $ sudo docker exec -t <container name> gitlab-ctl status
      
  • 接下来,恢复备份,指定要恢复的备份的时间戳,例如:

    此命令将覆盖您的GitLab数据库的内容!

      $ sudo docker exec -it <container name> gitlab-rake gitlab:backup:restore BACKUP=1393513186_2014_02_27
    
      Unpacking backup ... done
      Before restoring the database we recommend removing all existing
      tables to avoid future upgrade problems. Be aware that if you have
      custom tables in the GitLab database these tables and all data will be
      removed.
    
      Do you want to continue (yes/no)? yes
      ......
    
  • 还原之前备份的gitlab配置文件。

  • 重新启动并检查GitLab:

      $ sudo docker exec -it <container name> gitlab-ctl start
      $ sudo docker exec -it <container name> gitlab-rake gitlab:check SANITIZE=true
    
      Checking GitLab Shell ...
      GitLab Shell version >= 4.1.1 ? ... OK (4.1.1)
      Repo base directory exists?
      default... yes
      Repo storage directories are symlinks?
      default... no
      Repo paths owned by git:git?
      default... yes
      Repo paths access is drwxrws---?
      default... yes
      hooks directories in repos are links: ... can't check, you have no projects
      Running /opt/gitlab/embedded/service/gitlab-shell/bin/check
      ......
    

    PS: 如果您的备份tar文件和安装的GitLab版本之间存在GitLab版本不匹配,则restore命令将中止并显示错误。 安装正确的GitLab版本,然后重试

更新升级docker-gitlab

参考文档01

参考文档02

更新gitlab/gitlab-ce

这里唯一要强调的就是请做好备份工作。

  1. 停止正在运行的容器。

     $ sudo docker stop gitlab
    
  2. 删除已经存在的容器。

     $ sudo docker rm gitlab
    
  3. Pull 新的镜像

     $ sudo docker pull gitlab/gitlab-ce:latest
    
  4. 再次使用以前指定的选项创建容器:

     $ sudo docker run --detach \
         --hostname gitlab.silentlive.com \
         --publish 443:443 --publish 80:80 --publish 10022:22 \
         --name gitlab \
         --restart always \
         --volume /data/gitlab/config:/etc/gitlab \
         --volume /data/gitlab/logs:/var/log/gitlab \
         --volume /data/gitlab/data:/var/opt/gitlab \
         gitlab/gitlab-ce:latest
     在第一次运行时,GitLab将重新配置和更新自身。
    

更新使用docker-compose安装的gitlab/gitlab-ce

  1. 修改docker-compose.yml里面的gitlab/gitlab-ce版本

     $ sudo docker-compose pull
     Pulling web (gitlab/gitlab-ce:8.17.4-ce.0)...
     8.17.4-ce.0: Pulling from gitlab/gitlab-ce
     d54efb8db41d: Already exists
     f8b845f45a87: Already exists
     e8db7bf7c39f: Already exists
     ...
     96c4a451cbf5: Pull complete
     15fdd6b9c993: Pull complete
     Digest: sha256:8cee4683b19dc53350e87b68e90e1bc7f8bfd73dcc7a24b751cd6d104097139c
     Status: Downloaded newer image for gitlab/gitlab-ce:8.17.4-ce.
    

    这个命令将会pull对应得版本镜像。

  2. 创建新的容器。

     $ sudo docker-compose up -d
    
  3. 检查更新是否正常

     $ sudo docker exec -it <container name> gitlab-rake gitlab:check SANITIZE=true
    
  4. 问题解决(可选)

    如果出去权限异常,那么可以尝试的使用

     $ docker exec -it gitlab update-permissions
     $ docker restart gitlab