Skip to main content

GitLab 服务器迁移指引

本次迁移着重做数据分离,方便 GitLab 实例迁移至其它服务器或支持多实例同时运行:

  • GitLab 基础数据库分离,使用处于相同可用区的云数据库提高传输效率。
  • Registry 等相关服务,极大占用本地存储空间,本次需接入对象存储服务。
  • 通过 Omnibus 包安装至服务器本地,自定义程度高性能好。不适用 Docker 镜像,相关 Docker 镜像里面实际上也是 Omnibus 包来预先安装好的。因此多包了一层 Docker 来适用 GitLab 至少在我们当前的使用需求下,会使所有事情都变得复杂。

一、选购新的云服务器

根据此前的使用,确认4核8G内存可以满足要求。带宽尽量大,消除服务器端网速慢带来的卡顿感。

腾讯云的价格总体比阿里云价格低(一点点),特别若新人还可以在活动买到300元1年的4C8M服务器。

当然阿里云还是有优势的,功能够全面,配合阿里云的生态,是真正的全家桶。多年来用阿里云形成了习惯,继续使用阿里云的话上手成本较低。

目前的情况总结起来:

  • 低配的生产环境服务器在两者购买都差不了多少钱,首选阿里云。
  • 研发使用的高配服务器,哪个价格低买哪个。如果未来华为云便宜了,就买华为云。

腾讯云遇到的问题:

  • 腾讯云服务器的 Ubuntu 镜像,裸连国外网站(例如 packages.gitlab.com)很容易会 connection timeout,可能要手动改 hosts 来解决。

二、为新服务器安装 GitLab

经实际使用验证,服务器必须有 8G 内存才可以平稳运行 GitLab,在内存宽松的情况下,它需要占用 6.4G 左右的内存。当然若服务器是 4G 内存也可运行,官方指引最低是 4G 内存。

官方教程:https://about.gitlab.com/install/#ubuntu

现在官方的安装连接会被强行重定向到国内的 极狐。因此我们需要翻墙才行。

关于极狐,相关公司买下了 GitLab 在中国大陆的推广和发行权,在里面做了一些本地化的修改,还引入了可耻的收费套餐,个人实在不感冒。我们这里就用纯正的 GitLab 去做适合自己的定制化,当我们把 GitLab 用好了为公司的研发效率带来正面的提升,那么我们的 GitLab 都可以是第二个极狐。

易他知识库教程(仅公司内部成员可访问):https://thoughts.teambition.com/workspaces/5f4e0d8cb9883f0016b6d89e/docs/5f07f55aabb77a0001cd8ab7

注意要将教程里的 gitlab-ee 换成 gitlab-cece 是社区版,免费。

简易安装指引

记录整体操作步骤,仅供参考,以下按顺序执行:

ssh username@ip

当然,第一步是先登进你的 Ubuntu 服务器。

sudo apt-get update

注意若用官方 Ubuntu 镜像的话,需要改国内 mirrors。

curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash

添加 GitLab 官方源。

可能要同时解决的问题:

  • 若卡在 Installing /etc/apt/sources.list.d/gitlab_gitlab-ce.list... 则需要手动创建一下该文件。
  • E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing? 如描述所示,需 apt-get update 一次。

sudo apt-get update

抓取 GitLab 所有可安装的版本信息到本地。

sudo apt-get install gitlab-ce=15.5.5-ce.0

版本号需要自己在 https://packages.gitlab.com/gitlab/gitlab-ce 里面找。尽量找到最新的版本号填进去。

安装成功会有明显提示:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
gitlab-ce
0 upgraded, 1 newly installed, 0 to remove and 131 not upgraded.
Need to get 1,146 MB of archives.
After this operation, 3,061 MB of additional disk space will be used.
Get:1 https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu jammy/main amd64 gitlab-ce amd64 15.5.5-ce.0 [1,146 MB]
Fetched 1,146 MB in 2h 25min 49s (131 kB/s)
Selecting previously unselected package gitlab-ce.
(Reading database ... 88621 files and directories currently installed.)
Preparing to unpack .../gitlab-ce_15.5.5-ce.0_amd64.deb ...
Unpacking gitlab-ce (15.5.5-ce.0) ...
Setting up gitlab-ce (15.5.5-ce.0) ...
It looks like GitLab has not been configured yet; skipping the upgrade script.

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



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


Thank you for installing GitLab!

sudo gitlab-ctl reconfigure

生成 GitLab 配置并启动。

sudo cat /etc/gitlab/initial_root_password

查看管理员 root 的密码。

至此已完成 GitLab 的安装,可浏览器打开你的 external_url

光能跑还不行,接下来是要配置好 GitLab。

三、备份旧服务器的 GitLab

将旧服务器的 GitLab 升级至新服务器 GitLab 对应的版本

版本号一定要完全对应(例如 15.5.5)保证后续的迁移成功。

Tips

认真看完指引

https://docs.gitlab.com/ee/raketasks/backup_gitlab.html

进入 gitlab.example.com/help 查看当前 GitLab 版本

gitlab-ctl show-config 出来的版本号并非与当前的 GitLab 版本号一一对应:

user@SERVER:~# gitlab-ctl show-config
Starting Cinc Client, version 15.17.4

四、在新服务器恢复 GitLab 备份文件

https://docs.gitlab.com/ee/raketasks/restore_gitlab.html

先将备份文件传到新服务器上

备份文件先通过 scp 复制到 /tmp 目录。确保权限充足,旧服务器非 root 用户的话可提前将该文件复制到 tmp 目录;以及在云控制台将对方的 IP 加入 22 端口访问权限(阿里云叫安全组,腾讯云叫防火墙)。

scp [email protected]:/var/opt/gitlab/backups/1671201212_2022_12_16_15.5.5_gitlab_backup.tar /tmp/1671201212_2022_12_16_15.5.5_gitlab_backup.tar

移动到默认的 gitlab 备份目录:

sudo mv /tmp/1671201212_2022_12_16_15.5.5_gitlab_backup.tar /var/opt/gitlab/backups/

设置文件权限:

sudo chown git:git /var/opt/gitlab/backups/1671201212_2022_12_16_15.5.5_gitlab_backup.tar

迁移 gitlab-secrets.json(需提前按前文的方式通过 scp 转移至新服务器):

sudo cp -f /tmp/gitlab-secrets.json /etc/gitlab/

执行恢复备份

sudo gitlab-backup restore BACKUP=1671201212_2022_12_16_15.5.5

注意备份文件名不包含尾部的 _gitlab_backup.tar 否则报错。

使用外部数据库

创建一个数据库

每个 GitLab 大版本都对数据库有相应的要求,必须遵循:https://docs.gitlab.com/ee/install/requirements.html#database

排错 / Troubleshooting

【已解决】Package architecture does not match system

下载了 arm 架构的安装包,与本 amd 架构服务器不匹配。

dpkg: error processing archive /tmp/gitlab-ce_15.6.1-ce.0_arm64.deb (--install):
package architecture (arm64) does not match system (amd64)
Errors were encountered while processing:
/tmp/gitlab-ce_15.6.1-ce.0_arm64.deb

执行 sudo dpkg -i /tmp/gitlab-ce_15.6.1-ce.0_arm64.deb 时报的错。

【已解决】Packages have unmet dependencies

和上个问题一致,误下载了 arm 架构的安装包。

The following packages have unmet dependencies:
gitlab-ce:arm64 : Depends: perl:arm64 but it is not installable
Depends: libatomic1:arm64 but it is not installable

执行 sudo apt-get install /tmp/gitlab-ce_15.6.1-ce.0_arm64.deb 时报的错。 即使 sudo apt-get install -y perl libatomic1 也无法解决问题。

安装过程中可能要同时解决的问题

Connection timed out

packages.gitlab.com:443 connection timed out

最有效的方法是手动下载该 deb 包 cd /tmp && wget --content-disposition https://packages.gitlab.com/gitlab/gitlab-ce/packages/ubuntu/jammy/gitlab-ce_15.6.1-ce.0_arm64.deb/download.deb

然后 sudo apt-get install /tmp/gitlab-ce_15.6.1-ce.0_arm64.deb

直接安装 deb 包可能会出现相关依赖无法自动安装的问题,需要手动解决 sudo apt-get install -y perl libatomic1

Package is missing

Package gitlab-ce is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

在 GitLab CE 社区版的官方源 https://packages.gitlab.com/gitlab/gitlab-ce 找到的版本号,在执行 sudo apt-get install gitlab-ce=15.4.6-ce.0 时报错。

每个版本号的安装包覆盖范围都有差异,例如某些版本号没有 amd64 架构对应的安装包,就会报错。

unable to detect a valid hostname

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

未设 external_url,把它改成服务器 IP 然后执行下一步 sudo gitlab-ctl reconfigure 应用新的 GitLab 配置。若 GitLab 未启动,此时会自动启动。

迁移后出现的问题

【已解决】admin/application_settings/general 在保存时报 500 错误

在管理员的设置界面点击保存按钮,会报 500 错误。

原因:没有迁移配置文件 /etc/gitlab/gitlab-secrets.json 就会报此错误。

解决:复制该文件至新服务器对应目录,并 sudo gitlab-ctl reconfigure

【已解决】推送代码时报 401

无法通过 GitLab 自带的 Web IDE 推送代码。 无法通过本地命令行推送代码。 均报 401 Unauthorized

解决:迁移了 gitlab-secrects.json 以后不仅要 reconfigure 还要 restart

参考