記一次 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: [email protected]
  DN: CN=李二,OU=技術運維部,DC=kevin,DC=com   userPrincipalName: [email protected]
  DN: CN=lier1,OU=技術運維部,DC=kevin,DC=com   userPrincipalName: [email protected]
  DN: CN=test,OU=技術運維部,DC=kevin,DC=com   userPrincipalName: [email protected]
  DN: CN=王一,OU=技術運維部,DC=kevin,DC=com  userPrincipalName: [email protected]
  DN: CN=張三,OU=技術運維部,DC=kevin,DC=com   userPrincipalName: [email protected]
  DN: CN=張三,OU=網絡,OU=技術運維部,DC=kevin,DC=com   userPrincipalName: [email protected]
  DN: CN=趙四,OU=網絡,OU=技術運維部,DC=kevin,DC=com   userPrincipalName: [email protected]
 
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. 參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章