记一次 GitLab 的迁移过程

1. 迁移背景

公司的机房需要迁移,所以代码仓库也要跟着迁。这边先简单介绍下之前 GitLab 的搭建和使用情况。

在迁移之前 GitLab 已经已经运行了一段时间,整个代码仓库的数据量大约在 22G 左右,GitLab 基于 rpm 的方式进行安装,代码数据每天进行全量备份,备份数据保留 7 天,备份的数据没有申请 NAS 备份,而是备份在系统的一个数据分区上面。系统没有加监控,我接手的时候,上面的数据备份分区都已经满了,但是系统运维人员都不知道,导致后面的数据备份都是失败的。

上面就是老 GitLab 大致的情况。迁移的方案:

  • 选择同版本的rpm安装包:如果是新安装 GitLab 的话建议使用 Docker 的安装形式,但是网上有很多地方都说不同版本的 GitLab 对数据有兼容性问题,GitLab 的数据又非常重要,所以为了谨慎起见还是选择使用同版本的 rpm 包进行安装;
  • 备份策略:备份数据选择保留 7 天意义不大,会占用大量的磁盘空间,所以选择保留2天的备份数据,并将数据备份到nas盘上面;
  • 监控策略:磁盘,内存和 CPU 等基本监控指标纳入监控。

2. GitLab 整体架构介绍

在开始安装配置前,先来介绍下 GitLab 的整体架构,熟悉之后对后续的安装、配置和运维都很有帮助。

image-20210806112253680

  • repository:存储代码的库,实际体现就是一个文件夹,可以是硬盘或 NFS 文件系统;
  • Nginx:访问 Git Lab 的 Web 入口;
  • 数据库(PgSQL):包含以下信息:
    • repository 中的数据(元数据,issue,合并请求 merge request 等)
    • 可以登录 Web 的用户(权限)
  • Redis:缓存,负责分发任务;
  • sidekiq:后台任务,主要负责发送电子邮件,任务需要来自 Redis;
  • Unicorn:Gitlab 自身的 Web 服务器,包含了 Gitlab 主进程,负责处理快速/一般任务,与 Redis 一起工作。工作内容包括:
    • 通过检查存储在 Redis 中的用户会话来检查权限
    • 为 Sidekiq 制作任务
    • 从仓库(warehouse)取东西或在那里移动东西
  • gitlab-shell:用于 SSH 交互,而不是 HTTP。gitlab-shell 通过 Redis 与 Sidekiq 进行通信,并直接或通过 TCP 间接访问 Unicorn
  • gitaly:后台服务,专门负责访问磁盘以高效处理 git 操作,并缓存耗时操作。所有的 git 操作都通过 Gitaly 处理
  • gitlab-workhorse:反向代理服务器,可以处理与 Rails 无关的请求(磁盘上的CSS、JS 文件等),处理Git Push/Pull 请求,处理到Rails的连接(修改由Rails发送的响应或发送给 Rails 的请求,管理 Rails 的长期 WebSocket 连接等)。
  • mail_room:处理邮件请求。回复 GitLab 发出的邮件时,GitLab 会调用此服务

3. GitLab 安装

配置选择

迁移之前,你要给你的新的 GitLab 服务器的硬件做合理的配置选择。

关于内存和 CPU 的配置,官网给出的推荐配置:

  • 4核 4GB内存 支持 500 个用户
  • 8核 8GB内存 支持 1000 个用户

根据自己的用户量,做出合理的选择。

关于存储的配置,我这边根据现有的GitLab 上的数据量选择了1T的存储

  • 1个T的存储空间
  • 1个T的NAS盘坐数据备份

安装方式选择

GitLab 官网有详细介绍安装方式。因为我们的机器是在内网区域,不能连接互联网,也为了和之前的安装方式保持一致,所以选择 RPM 包的安装方式。(如果是初次安装的话可以考虑使用 Docker 的安装方式)

GitLab 的 RPM 包下载地址可以使用清华大学的镜像

下载之前你最好看下之前的版本,下载的版本最好是和之前一致,防止数据不兼容。

一般使用下面命令查看版本。

[root@localhost gitlab]# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
12.1.1

安装的网络区域

一般 GitLab 上都会保存公司的核心代码,所以建议安装在公司的内网区域。

安装 GitLab

# rpm 包方式安装
[root@i-lobhuk71 upload]# rpm -ivh ./gitlab-ce-12.1.1-ce.0.el7.x86_64.rpm --force
warning: ./gitlab-ce-12.1.1-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
error: Failed dependencies:
	policycoreutils-python is needed by gitlab-ce-12.1.1-ce.0.el7.x86_64

# 出现上面的错误,是因为缺少policycoreutils-python,安装依赖
[root@i-lobhuk71 upload]# yum install policycoreutils-python

# 再次执行
[root@i-lobhuk71 upload]# rpm -ivh ./gitlab-ce-12.1.1-ce.0.el7.x86_64.rpm --force
warning: ./gitlab-ce-12.1.1-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:gitlab-ce-12.1.1-ce.0.el7        ################################# [100%]
It looks like GitLab has not been configured yet; skipping the upgrade script.

       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.
  


     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ 
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/
  

Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
Please configure a URL for your GitLab instance by setting `external_url`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:
  sudo gitlab-ctl reconfigure

For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md

# 初始化Gitlab命令(保存配置或重新载入配置):
[root@i-lobhuk71 upload] gitlab-ctl reconfigure

执行完上面的命令后,可以看到下面的目录结构:

  • /opt/gitlab/    # 主目录
  • /etc/gitlab/    # 放置配置文件
  • /var/opt/gitlab/   # 各个组件
  • /var/log/gitlab/    # 放置日志文件
  • /var/opt/gitlab/git-data/repositories    #数据库的地址  
  • /var/opt/gitlab/postgresql/data    #gitlab组和项目的地址
  • /etc/gitlab/gitlab.rb #gitlab配置文件

GitLab 常用命令

# Gitlab服务的启停管理
启动服务: gitlab-ctl start
停止服务: gitlab-ctl stop
重启服务: gitlab-ctl restart
查看状态: gitlab-ctl status

# Gitlab的supervisor方式启动服务
服务启动命令:   systemctl start gitlab-runsvdir.service
服务停止命令:   systemctl stop gitlab-runsvdir.service
服务重启命令:   systemctl restart gitlab-runsvdir.service
服务开机启动命令:   systemctl enable gitlab-runsvdir.service
取消开机启动命令:   systemctl disable gitlab-runsvdir.service
服务查看命令:   systemctl list-unit-files

# 可以查看到gitlab所有插件的日志情况
Gitlab服务日志查看:/usr/bin/gitlab-ctl tail        

配置管理员账号密码

经过上面的配置已经可以通过 IP 地址(安装服务器的IP)访问了,首次访问会让你创建管理员密码(账号默认是root)。

image-20210624131602122

4. 配置 GitLab

配置external_url,修改clone地址

external_url

编辑/etc/gitlab/gitlab.rb,修改其中的external_url配置:

## GitLab URL
##! URL on which GitLab will be reachable.
##! For more details on configuring external_url see:
##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
## 这边配置成本机的IP地址就可以了
external_url 'http://10.2.xx.xx'

clone地址

将克隆地址改成域名(http形式,不是https形式)

编辑/var/opt/gitlab/gitlab-rails/etc/gitlab.yml,将下面的host改成域名。

image-20210806141043280

最后执行 gitlab-ctl restart命令使之配置生效,注意不要执行 gitlab-ctl reconfigure

配置 Ldap

[root@gitlab ~] vim /etc/gitlab/gitlab.rb
......
gitlab_rails['ldap_enabled'] = true
gitlab_rails['ldap_servers'] = YAML.load <<-EOS # remember to close this block with 'EOS' below
main: # 'main' is the GitLab 'provider ID' of this LDAP server
  label: '哈哈集团-Gitlab登录入口'
  host: '192.168.10.141'
  port: 389
  uid: 'userPrincipalName'
  method: 'plain' # "tls" or "ssl" or "plain"
  allow_username_or_email_login: false
  bind_dn: 'cn=王一,ou=技术运维部,dc=kevin,dc=com'
  password: '9oGlYkgDzhp5k6JZ'
  active_directory: true
  base: 'ou=技术运维部,dc=kevin,dc=com'
  user_filter: ''
EOS
 
# 接着执行下面命令,使上面配置生效:
# 这里最好使用该命令,表示重载配置。不要使用"gitlab-ctl restart"重启服务,否则可能出现500报错!
[root@gitlab ~]# gitlab-ctl reconfigure        
 
# 然后执行下面命令,检查LDAP信息是否成功同步过来
[root@gitlab ~]# gitlab-rake gitlab:ldap:check
Checking LDAP ...
 
Server: ldapmain
LDAP authentication... Success
LDAP users with access to your GitLab server (only showing the first 100 results)
  DN: CN=李某某,OU=技术运维部,DC=kevin,DC=com  userPrincipalName: limoumou@kevin.com
  DN: CN=李二,OU=技术运维部,DC=kevin,DC=com   userPrincipalName: lier@kevin.com
  DN: CN=lier1,OU=技术运维部,DC=kevin,DC=com   userPrincipalName: lier1@kevin.com
  DN: CN=test,OU=技术运维部,DC=kevin,DC=com   userPrincipalName: test@kevin.com
  DN: CN=王一,OU=技术运维部,DC=kevin,DC=com  userPrincipalName: wangyi@kevin.com
  DN: CN=张三,OU=技术运维部,DC=kevin,DC=com   userPrincipalName: zhangsan@kevin.com
  DN: CN=张三,OU=网络,OU=技术运维部,DC=kevin,DC=com   userPrincipalName: zhangsan02@kevin.com
  DN: CN=赵四,OU=网络,OU=技术运维部,DC=kevin,DC=com   userPrincipalName: zhaosi@kevin.com
 
Checking LDAP ... Finished

设置定时备份

设置 GitLab 备份目录

可以通过/etc/gitlab/gitlab.rb配置文件来修改默认存放备份文件的目录

# /var/opt/gitlab/backups 修改为你想存放备份的目录即可, 修改完成之后使用gitlab-ctl reconfigure命令重载配置文件即可.
gitlab_rails['backup_path']="/var/opt/gitlab/backups"

设置定时任务(crontab -e)

0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create

重启crontab

systemctl restart crond

设置备份的天数

编辑/etc/gitlab/gitlab.rb文件。

###! The duration in seconds to keep backups before they are allowed to be deleted
### keep 2 days
gitlab_rails['backup_keep_time'] = 172800

恢复备份的数据

# 停止相关数据连接服务
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
 
# 从1628100892_2021_08_05_12.1.1_gitlab_backup.tar 备份中恢复
gitlab-rake gitlab:backup:restore BACKUP=1628100892_2021_08_05_12.1.1
 
# 启动Gitlab
gitlab-ctl start

申请 NAS 存储备份数据

需要注意的是 NAS 盘是有写入权限的,GitLab 生成的文件默认的用户和用户组都是git:git,所以在申请nas盘的时候需要将写入权限配置给git:git。

5. 问题记录

备份过程中连接数据库失败

问题描述

PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/opt/gitlab/postgresql/.s.PGSQL.5432"?
/opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/db.rake:49:in block (3 levels) in <top (required)>' /opt/gitlab/embedded/bin/bundle:23:in load'
/opt/gitlab/embedded/bin/bundle:23:in `

'
Tasks: TOP => gitlab:db:configure
(See full trace by running task with --trace)
————————————————
版权声明:本文为CSDN博主「ArvinWoo」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_37595946/article/details/86534961

解决方法:
1、按住CTRL+C强制结束
2、先停止 gitlab ,命令: sudo gitlab-ctl stop
3、执行命令: sudo chmod 755 /var/opt/gitlab/postgresql
4、执行命令:sudo systemctl restart gitlab-runsvdir
5、再次配置:sudo gitlab-ctl reconfigure
6、启动即可:sudo gitlab-ctl restart

6. 参考

人生的主旋律其实是苦难,快乐才是稀缺资源。在困难中寻找快乐,才显得珍贵~
原文地址:https://www.cnblogs.com/54chensongxia/p/14964465.html