記一次 GitLab 的遷移過程

1. 遷移背景

公司的機房須要遷移,因此代碼倉庫也要跟着遷。這邊先簡單介紹下以前 GitLab 的搭建和使用狀況。html

在遷移以前 GitLab 已經已經運行了一段時間,整個代碼倉庫的數據量大約在 22G 左右,GitLab 基於 rpm 的方式進行安裝,代碼數據天天進行全量備份,備份數據保留 7 天,備份的數據沒有申請 NAS 備份,而是備份在系統的一個數據分區上面。系統沒有加監控,我接手的時候,上面的數據備份分區都已經滿了,可是系統運維人員都不知道,致使後面的數據備份都是失敗的。python

上面就是老 GitLab 大體的狀況。遷移的方案:git

  • 選擇同版本的rpm安裝包:若是是新安裝 GitLab 的話建議使用 Docker 的安裝形式,可是網上有不少地方都說不一樣版本的 GitLab 對數據有兼容性問題,GitLab 的數據又很是重要,因此爲了謹慎起見仍是選擇使用同版本的 rpm 包進行安裝;
  • 備份策略:備份數據選擇保留 7 天意義不大,會佔用大量的磁盤空間,因此選擇保留2天的備份數據,並將數據備份到nas盤上面;
  • 監控策略:磁盤,內存和 CPU 等基本監控指標歸入監控。

2. GitLab 總體架構介紹

在開始安裝配置前,先來介紹下 GitLab 的總體架構,熟悉以後對後續的安裝、配置和運維都頗有幫助。sql

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 服務器的硬件作合理的配置選擇。shell

關於內存和 CPU 的配置,官網給出的推薦配置:數據庫

  • 4核 4GB內存 支持 500 個用戶
  • 8核 8GB內存 支持 1000 個用戶

根據本身的用戶量,作出合理的選擇。vim

關於存儲的配置,我這邊根據現有的GitLab 上的數據量選擇了1T的存儲緩存

  • 1個T的存儲空間
  • 1個T的NAS盤坐數據備份

安裝方式選擇

GitLab 官網有詳細介紹安裝方式。由於咱們的機器是在內網區域,不能鏈接互聯網,也爲了和以前的安裝方式保持一致,因此選擇 RPM 包的安裝方式。(若是是初次安裝的話能夠考慮使用 Docker 的安裝方式)bash

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

解決方法:
一、按住CTRL+C強制結束
二、先中止 gitlab ,命令: sudo gitlab-ctl stop
三、執行命令: sudo chmod 755 /var/opt/gitlab/postgresql
四、執行命令:sudo systemctl restart gitlab-runsvdir
五、再次配置:sudo gitlab-ctl reconfigure
六、啓動便可:sudo gitlab-ctl restart

6. 參考

相關文章
相關標籤/搜索