CentOS 7 EOL 了!手把手教你把 GitLab 搬家到 Ubuntu 24 並升級到最新版

前言

2024 年 6 月 30 日,CentOS 7 正式走入歷史,結束了它長達 10 年的生命週期。對於許多還在 CentOS 7 上運行 GitLab 的團隊來說,這是一個不得不面對的問題——不僅作業系統停止安全更新,GitLab 官方也逐漸停止對舊版本的支援。

我們公司的 GitLab 伺服器就是這種情況:CentOS 7 + GitLab CE 16.11.10,累積了 5 年的專案資料,4 個 Runner 分散在不同機器上。趁這個機會,決定一次把作業系統升級到 Ubuntu 24.04,同時把 GitLab 更新到最新的 18.8.2 版本。

這篇文章完整記錄了整個遷移過程,包含遇到的坑和解決方案,希望能幫助到有同樣需求的朋友。


遷移前的環境

項目舊環境新環境
作業系統CentOS 7 (EOL)Ubuntu 24.04 LTS
GitLabCE 16.11.10CE 18.8.2
PostgreSQL14.1114.x → 自動升級
Runner4 台外部機器不需變動
資料量5 年專案、約 240 個 repositories

遷移策略

經過研究,我選擇的遷移策略是:

  1. 在 CentOS 7 上完整備份
  2. 在 Ubuntu 24 上安裝相同版本的 GitLab(16.11.10)
  3. 還原備份
  4. 按照官方升級路徑逐步升級到最新版

為什麼不直接在 CentOS 7 上升級再遷移?因為 CentOS 7 的 glibc 版本太舊,新版 GitLab 根本裝不上去。所以只能先搬家,再升級。


第一步:在 CentOS 7 上備份

備份 GitLab 資料

# 建立備份目錄
mkdir -p ~/gitlab_migration

# 備份配置檔(超級重要!)
cp /etc/gitlab/gitlab.rb ~/gitlab_migration/
cp /etc/gitlab/gitlab-secrets.json ~/gitlab_migration/

# 備份 SSH 主機金鑰(避免用戶端出現 host key changed 警告)
sudo cp /etc/ssh/ssh_host_* ~/gitlab_migration/

# 如果有 SSL 憑證也要備份
sudo cp -r /etc/gitlab/ssl ~/gitlab_migration/

建立 GitLab 完整備份

# 停止寫入服務,確保資料一致性
sudo gitlab-ctl stop puma
sudo gitlab-ctl stop sidekiq

# 建立備份
sudo gitlab-backup create

# 查看備份檔案
ls -lh /var/opt/gitlab/backups/
# 會產生類似這樣的檔案:1768921239_2026_01_20_16.11.10_gitlab_backup.tar

備份過程中如果看到 file changed as we read it 的警告,不用擔心,這只是因為有 CI job 在跑,不影響備份完整性。

gitlab-backup 包含什麼?

包含 ✅不包含 ❌
PostgreSQL 資料庫gitlab.rb(主配置)
所有 Git repositoriesgitlab-secrets.json(加密金鑰)
用戶上傳的檔案SSL 憑證
CI/CD ArtifactsSSH 主機金鑰
LFS 大型檔案
GitLab Pages
Packages

特別注意gitlab-secrets.json 遺失的話,2FA 會失效、CI/CD Variables 無法解密、Runner tokens 會失效,用戶可能需要重設密碼。這個檔案超級重要,一定要手動備份!


第二步:傳輸到新伺服器

# 傳輸備份檔案
scp /var/opt/gitlab/backups/*_gitlab_backup.tar user@ubuntu24:/tmp/

# 傳輸配置檔和金鑰
scp ~/gitlab_migration/* user@ubuntu24:/tmp/

# 傳輸 SSL 憑證(如果有的話)
scp -r ~/gitlab_migration/ssl user@ubuntu24:/tmp/

第三步:在 Ubuntu 24 上安裝 GitLab

遇到第一個坑:Ubuntu 24 沒有 GitLab 16.11.10

當我興沖沖地在 Ubuntu 24 上加好 GitLab 官方 repository,準備安裝 16.11.10 時,卻發現:

apt-cache madison gitlab-ce | grep 16.11
# 什麼都沒有!

查了一下才發現,Ubuntu 24 (noble) 的 GitLab repository 最舊只提供到 17.x 版本。但 GitLab 備份還原必須要相同版本,不能跨版本還原。

解決方案:使用 Ubuntu 22.04 的 Repository

# 安裝依賴
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl openssh-server ca-certificates tzdata perl postfix

# 添加 GPG key
curl -fsSL https://packages.gitlab.com/gitlab/gitlab-ce/gpgkey | sudo gpg --dearmor -o /usr/share/keyrings/gitlab-ce.gpg

# 關鍵:使用 jammy (Ubuntu 22.04) 的 repository
echo "deb [signed-by=/usr/share/keyrings/gitlab-ce.gpg] https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu jammy main" | sudo tee /etc/apt/sources.list.d/gitlab_gitlab-ce.list

sudo apt update

# 確認版本存在
apt-cache madison gitlab-ce | grep 16.11.10
# gitlab-ce | 16.11.10-ce.0 | https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu jammy/main amd64 Packages

# 安裝!
sudo apt install gitlab-ce=16.11.10-ce.0

遇到第二個坑:runsv not running

安裝過程中可能會卡在 runsv not running 的錯誤,一直重試。最簡單的解決方法:

sudo reboot

重開機後 runsv 服務就會正常啟動。


第四步:還原配置和備份

還原配置檔

# 複製配置檔
sudo cp /tmp/gitlab-secrets.json /etc/gitlab/
sudo cp /tmp/gitlab.rb /etc/gitlab/

# 設定正確權限(重要!)
sudo chmod 600 /etc/gitlab/gitlab.rb
sudo chmod 600 /etc/gitlab/gitlab-secrets.json

還原 SSH 主機金鑰

# 還原金鑰
sudo cp /tmp/ssh_host_* /etc/ssh/

# 設定權限
sudo chmod 600 /etc/ssh/ssh_host_*_key
sudo chmod 644 /etc/ssh/ssh_host_*.pub
sudo chown root:root /etc/ssh/ssh_host_*

# 重啟 SSH
sudo systemctl restart sshd

初始化 GitLab

sudo gitlab-ctl reconfigure

還原備份

# 移動備份檔案並設定權限
sudo cp /tmp/*_gitlab_backup.tar /var/opt/gitlab/backups/
sudo chown git:git /var/opt/gitlab/backups/*.tar

# 停止服務
sudo gitlab-ctl stop puma
sudo gitlab-ctl stop sidekiq

# 還原!
sudo gitlab-backup restore BACKUP=1768921239_2026_01_20_16.11.10

系統會問你要不要繼續,輸入 yes

遇到第三個坑:各種 ERROR 和 WARNING

還原過程中會看到很多嚇人的訊息:

ERROR: must be owner of extension pg_trgm
ERROR: must be owner of extension btree_gist

還有大量的:

rpc error: code = NotFound desc = repository not found
no refs in backup
skipped restore

不要慌!這些都是正常的!

訊息意思影響
must be owner of extensionPostgreSQL 擴展權限問題不影響
repository not found新系統上還沒有這個 repo會自動建立
no refs in backup空的 wiki跳過
skipped restoreDesign 功能沒用到跳過

這些 WARNING 的意思是:「找不到現有 repo → 改用完整還原建立新的」,不影響最終結果。

還原完成後會看到:

Restore task is done.

重啟並驗證

sudo gitlab-ctl restart

# 檢查服務狀態
sudo gitlab-ctl status

# 完整性檢查
sudo gitlab-rake gitlab:check SANITIZE=true

# 確認專案數量
sudo gitlab-rails runner "puts Project.count"

如果專案數量跟舊伺服器一致,恭喜你,還原成功了!


第五步:升級到最新版

GitLab 不能直接從 16.11.10 跳到 18.8.2,必須按照官方的升級路徑一步一步來。

升級路徑

16.11.10 → 17.1.8 → 17.3.7 → 17.5.5 → 17.8.7 → 17.11.7 → 18.2.8 → 18.5.5 → 18.8.2

你可以用 GitLab 官方的升級路徑工具 來查詢正確的升級路線。

切換回 Ubuntu 24 的 Repository

# 升級需要用 Ubuntu 24 (noble) 的 repository
echo "deb [signed-by=/usr/share/keyrings/gitlab-ce.gpg] https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu noble main" | sudo tee /etc/apt/sources.list.d/gitlab_gitlab-ce.list

sudo apt update

開始升級

# 升級到 17.1.8
sudo apt install gitlab-ce=17.1.8-ce.0
sudo gitlab-ctl reconfigure

# 升級到 17.3.7
sudo apt install gitlab-ce=17.3.7-ce.0
sudo gitlab-ctl reconfigure

# 升級到 17.5.5
sudo apt install gitlab-ce=17.5.5-ce.0
sudo gitlab-ctl reconfigure

# 升級到 17.8.7
sudo apt install gitlab-ce=17.8.7-ce.0
sudo gitlab-ctl reconfigure

# 升級到 17.11.7
sudo apt install gitlab-ce=17.11.7-ce.0
sudo gitlab-ctl reconfigure

# 升級到 18.2.8
sudo apt install gitlab-ce=18.2.8-ce.0
sudo gitlab-ctl reconfigure

# 升級到 18.5.5
sudo apt install gitlab-ce=18.5.5-ce.0
sudo gitlab-ctl reconfigure

# 升級到 18.8.2(最新)
sudo apt install gitlab-ce=18.8.2-ce.0
sudo gitlab-ctl reconfigure

每次升級後,建議檢查一下服務狀態:

sudo gitlab-ctl status

整個升級過程大約需要 1-2 小時,主要時間都花在資料庫遷移上。耐心等待就好,看到一堆 migrated 就是正常的。


第六步:收尾工作

修改 Domain(如果有需要)

sudo nano /etc/gitlab/gitlab.rb
# 修改 external_url

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

Runner 處理

如果 Domain 沒有改變,Runner 完全不用動,會自動連上新伺服器。

如果 Domain 有改變,需要到每台 Runner 機器上更新配置:

sudo nano /etc/gitlab-runner/config.toml
# 修改 url = "http://新的domain:port"

sudo gitlab-runner restart

最終驗證

# 測試 SSH 連線
ssh -T git@gitlab.yourdomain.com

# 測試 Git Clone
git clone git@gitlab.yourdomain.com:group/test-repo.git

# 測試 CI/CD
git commit --allow-empty -m "Test CI/CD"
git push

遇到的坑總結

問題解決方案
Ubuntu 24 沒有舊版 GitLab用 Ubuntu 22.04 (jammy) 的 repository
runsv not running重開機
備份檔案 Permission deniedsudo chown git:git *.tar
版本不匹配無法還原必須安裝相同版本
各種 rpc error可以忽略,不影響結果
extension owner 錯誤可以忽略,不影響結果

結語

整個遷移過程花了大約半天時間,其中大部分時間都在等待備份、還原和升級。最麻煩的部分其實是搞清楚 Ubuntu 24 沒有舊版 GitLab 這件事,以及理解那些嚇人的 ERROR 訊息其實可以忽略。

遷移完成後,不僅作業系統從 EOL 的 CentOS 7 升級到了最新的 Ubuntu 24.04 LTS(支援到 2034 年),GitLab 也從 16.11.10 升級到了 18.8.2,可以享受到最新的功能和安全更新。

如果你也有類似的需求,希望這篇文章能幫助你少走一些彎路。有問題歡迎在下方留言討論!


參考資源

404NOTE
404NOTE
文章: 48

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *