- 0x00 前言
- 0x01 環境安裝
- 0x02 操作說明
- gitlab-ctl 指令
- gitlab-rails 指令
- gitlab-rake 指令
- 0x03 基礎配置
- Gitlab初始化設定
- Gitlab資料備份恢複
- Gitlab添加發信郵箱
- Gitlab添加LDAP認證
- 0x04 入坑解決
- 0x05 腳本附錄
0x00 前言
在開源世界中,是沒有終結的盡頭!
描述:GitLab 是一個非常優秀的項目。這是一個開源項目,允許使用者在自己的伺服器上運作類似于 GitHub 的項目管理系統。
可以使用 GitLab 在公開釋出之前開發私有項目。
官網位址:https://about.gitlab.com/
幫助文檔:https://about.gitlab.com/install/
元件參考:https://docs.gitlab.com/ce/development/architecture.html
GitLab 采用傳統的開源商業模式,他們有兩種産品:
- 免費的開源軟體,使用者可以在自己的伺服器上安裝,以及類似于 GitHub 的托管服務。
- 免費的社群版
和付費企業版Gitlab CE
Gitlab EE
- 企業版基于社群版但附帶針對企業客戶的其他功能,它或多或少與 WordPress.org 或 Wordpress.com 提供的服務類似。
Gitlab優點:
- 有開源免費的版本,可以進行私有開發上傳與拉取;
- 社群版具有高度可擴充性,可以在單個伺服器或群集上支援 25000 個使用者
- GitLab 的一些功能包括:Git 倉庫管理,代碼評論,問題跟蹤,活動源和維基
- 它配備了 GitLab CI,用于持續內建和傳遞
0x01 環境安裝
Gitlab 基礎要求:至少4GB的空閑RAM來運作GitLab
- 系統:CentOS Linux release 8.0.1905 (Core)
GitLab 12.9.2 (ac5568eb5d8)
GitLab Shell 12.0.0
GitLab Workhorse v8.25.1
GitLab APIv 4
Ruby 2.6.5p114
Rails 6.0.2
PostgreSQL 10.12
Gitaly Servers
Gitlab 安裝方式:
- 官方推薦使用Omnibus快速安裝(采用rpm軟體包進行安裝部署(國内推薦直接鏡像源下載下傳));
#此處以社群版本為例
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo EXTERNAL_URL="https://gitlab.example.com" dnf install -y gitlab-ee
- 采用yum進行安裝部署;
安裝流程:
Step1.采用rpm進行安裝社群版gitlab(替換更新源自己選擇即可):
sudo dn8 install -y curl policycoreutils openssh-server wget
#可以使用 wget 的方式把 rpm 包下載下傳下來安裝
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el8/gitlab-ce-12.9.2-ce.0.el8.x86_64.rpm
#dnf install gitlab-ce-12.9.2-ce.0.el8.x86_64.rpm
rpm -ivh gitlab-ce-12.9.2-ce.0.el8.x86_64.rpm
Step2.安裝完成後顯示以下則說明安裝成功:
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
Step3.修改 gitlab 預設配置
$vim /etc/gitlab/gitlab.rb
# 修改為你自己的域名或者 IP,是單引号,而且前面的 http 不要改 (并且将該域名加入到hosts中)
external_url 'http://gitlab.weiyigeek.top'
# 郵件配置,沒有郵件伺服器可以關閉郵件服務功能
# gitlab_rails['smtp_enable'] = false
# gitlab_rails['smtp_address'] = ""
# gitlab_rails['smtp_port'] = 587
# gitlab_rails['smtp_user_name'] = ""
# gitlab_rails['smtp_password'] = ""
# gitlab_rails['smtp_authentication'] = ""
# gitlab_rails['smtp_enable_starttls_auto'] =
# gitlab_rails['smtp_tls'] =
# gitlab_rails['gitlab_email_from'] = ''
Step4.完成修改後需要重新加載配置然後重新開機即可;
$ gitlab-ctl reconfigure
# [2020-04-15T10:33:44+08:00] WARN: Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support. #如果看着不舒服自己設定系統語系即可
# [2020-04-15T10:33:45+08:00] WARN: Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support.
# Starting Chef Client, version 14.14.29
# resolving cookbooks for run list: ["gitlab"]
# Synchronizing Cookbooks:
# - redis (0.1.0)
# - package (0.1.0)
# - postgresql (0.1.0)
# - mattermost (0.1.0)
# - consul (0.1.0)
# - letsencrypt (0.1.0)
# - gitlab (0.0.1)
# - runit (4.3.0)
# - monitoring (0.1.0)
# - praefect (0.1.0)
# - gitaly (0.1.0)
# - registry (0.1.0)
# - acme (4.1.1)
# - nginx (0.1.0)
# - crond (0.1.0)
# Running handlers:
# Running handlers complete
# Chef Client finished, 541/1460 resources updated in 03 minutes 14 seconds
# gitlab Reconfigured!
$ gitlab-ctl restart
# [2020-04-15T10:37:51+08:00] WARN: Please install an English UTF-8 locale for Chef to use, falling back to C locale and disabling UTF-8 support.
# ok: run: alertmanager: (pid 21410) 1s
# ok: run: gitaly: (pid 21422) 0s
# ok: run: gitlab-exporter: (pid 21429) 1s
# ok: run: gitlab-workhorse: (pid 21444) 0s
# ok: run: grafana: (pid 21463) 0s
# ok: run: logrotate: (pid 21474) 0s
# ok: run: nginx: (pid 21480) 0s
# ok: run: node-exporter: (pid 21563) 1s
# ok: run: postgres-exporter: (pid 21569) 0s
# ok: run: postgresql: (pid 21580) 0s
# ok: run: prometheus: (pid 21589) 0s
# ok: run: redis: (pid 21601) 0s
# ok: run: redis-exporter: (pid 21708) 0s
# ok: run: sidekiq: (pid 21716) 0s
# ok: run: unicorn: (pid 21727) 0s
Step5.如果 reconfigure 失敗,則需要
systemctl enable gitlab-runsvdir && systemctl restart gitlab-runsvdir
重新開機一下 gitlab-runsvdir 服務
Step6.打開浏覽器進行初始化賬戶設定密碼,這個密碼為 root 管理者賬戶的密碼。設定完密碼之後會自動跳轉到登入頁面。
應用:http://gitlab.weiyigeek.top/
賬号:root
密碼:WeiyiGeek #8位及以上

Step7.登入成功的界面,是不是有種瘋狂的想寫代碼的沖動;
Step8.英文可能對英語不好的新手可能不友好,我們進行漢化(
注意:高于12.3.5的版本無需漢化,直接在使用者設定裡面進行設定自定義語言選擇簡體中文
);
#停止gitlab
gitlab-ctl stop
# 擷取目前安裝的版本更新檔
git clone https://gitlab.com/xhang/gitlab.git
cd gitlab
# 檢視全部分支版本
git branch -a
gitlab_version=$(cat /opt/gitlab/embedded/service/gitlab-rails/VERSION)
# 比較漢化标簽和原标簽,導出patch用的diff檔案
#git diff v${gitlab_version} v${gitlab_version}-zh > ../${gitlab_version}-zh.diff
git diff remotes/origin/12-3-stable remotes/origin/12-3-stable-zh > ../${gitlab_version}-zh.diff
#打更新檔的時候會提示一些更新檔檔案不存在,一定要跳過這些檔案,不然後面reconfig的時候會報錯的。
patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 < ../${gitlab_version}-zh.diff
#重新編譯和啟動
gitlab-ctl reconfigure
gitlab-ctl start
0x02 操作說明
Gitlab相關操作及說明:
#檢視Gitlab版本
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
/etc/gitlab/gitlab.rb #gitlab配置檔案
/opt/gitlab #gitlab的程式安裝目錄
/var/opt/gitlab #gitlab目錄資料目錄(通過gitlab.rb修改的子子產品配置檔案存放的)
/var/opt/gitlab/git-data #存放倉庫資料
/var/opt/gitlab/backups #預設備份目錄
#Gitlab的服務構成:
gitlab-shell:用于處理Git指令和修改authorized keys清單
gitlab-workhorse:輕量級的反向代理伺服器
logrotate:日志檔案管理工具
nginx:靜态web伺服器
postgresql:資料庫
redis:緩存資料庫
sidekiq:用于在背景執行隊列任務(異步執行)
unicorn:GitLab Rails應用是托管在這個伺服器上面的。
gitlab-ctl 指令
基礎指令:
#常用指令
gitlab-ctl reconfigure #生成配置并重新啟動服務
gitlab-ctl start #啟動所有 gitlab 元件:
gitlab-ctl status #檢視目前gitlab所有服務運作狀态
gitlab-ctl stop #停止gitlab服務
gitlab-ctl pg-upgrade #更新PostgreSQL最新版本
#單獨啟動|停止|重新開機某個服務
gitlab-ctl start nginx #啟動nginx元件
gitlab-ctl stop postgresql #停止所有 gitlab postgresql 元件:
gitlab-ctl restart unicorn #重新開機相關資料連接配接服務
gitlab-ctl restart sidekiq #重新開機相關資料連接配接服務
gitlab-ctl restart gitlab-workhorse # 重新開機所有 gitlab gitlab-workhorse 元件:
#日志管理
gitlab-ctl tail #檢視所有服務的日志
gitlab-ctl tail redis #實時檢查redis的日志(延伸某個服務也是一樣得)
gitlab-rails 指令
基礎示例:
#gitlab重置密碼
gitlab-rails console
> u=User.where(id:1).first #這個是管理者的,也可以用email等
> u.password = 'your_password' #密碼有格式限制,我隻知道8位以上否則會儲存失敗
> u.password_confirmation = 'your_password'
> u.save
gitlab-rake 指令
基礎示例:
gitlab-rake gitlab:check SANITIZE=true --trace # 檢查gitlab
gitlab-rake db:migrate # 資料庫關系更新
gitlab-rake cache:clear # 清理redis緩存
0x03 基礎配置
Gitlab初始化設定
0) 使用者主題與語言設定
流程:右上角個人頭像->settings->Preferences->
主題設定|文法高亮主題|自定義語言和區域相關設定
1) 關閉Gravatar頭像功能進行網絡加速
Setting -> Gravatar enabled(展開Expand) ->
not Checked Gravatar enabled
-> Sava Changes
2) 使用者管理與注冊相關設定
描述:是否使用者自己進行注冊以及設定密碼政策和注冊郵箱域名(白|黑名單)等資訊,如果關閉後首頁将不顯示注冊;
- 建立使用者:
-> User -> New User -> 項目限制(建立得數量預設即可) ->Admin Area
(企業内部建議取消) -> 建立使用者 (使用者郵箱将會收到一份注冊郵件)Can create group
- 模仿使用者 impersonate: 可以模仿使用者登入,不用退出再登入;
GitLab安裝與基礎使用
3) 代碼推送提醒功能
描述:添加一個項目有代碼送出後發郵件給指定的郵箱提醒通知,這個對于代碼稽核還是有幫助的,至少知道誰什麼時候推送新代碼了,更新了哪些内容等;
管理者登陸到gitlab界面
Admin area-> Service Templates -> Emails on push
, 勾選Active,Recipients填寫本項目更新需要推送知曉的人員,多個用空格或者逗号隔開;
4) 設定ssh shell的端口
用于處理Git指令和修改authorized keys清單
gitlab_rails['gitlab_shell_ssh_port'] = 9022
5) 修改gitlab的倉庫(repositories)存放目錄
描述:安裝好後要将倉庫(repositories)放在一個大硬碟上,是以需要修改倉庫對應的目錄。
(注:我是先将是以的repositories删除再作以下操作的,還把
/var/opt/gitlab/git-data/repositories/使用者名
目錄下所有使用者名下的檔案全删除,目錄删的隻剩下空檔案夾)
#檢視磁盤空間大小(可以根據您自己添加的磁盤進行操作)
$df -h
/dev/mapper/cl-home 24G 954M 24G 4% /home
#建立新倉庫目錄
$mkdir -p /home/gitlab/git-data
#配置gitlab的資料存儲位置為/home目錄下
$vi /etc/gitlab/gitlab.rb
git_data_dirs({ "default" => { "path" => "/home/gitlab/git-data" } })
#重新加載配置
$sudo gitlab-ctl reconfigure
#重載後發現我們更改的倉庫位址下面有資料存放了
$ls /home/gitlab/git-data/
repositories
6) 建立一個開發組
在Gitlab登入後的首頁面上點選
Create a group
->輸入相關組資訊建立即可;
邀請成員加入到組:
Members
->
Group members
->
Invite memeber
-> 選擇使用者加入到剛建立的組以及角色權限;
7) 項目建立與初始化
Gitlab的git位址組成與github是一緻(别告訴我您不知道):
gitlab位址+使用者/群組+自定義名字
Gitlab項目的可見類型有三種級别。
- Private project: 該級别是隻有項目擁有者或者已經得到授權的人可以通路該項目,或者這些人是該項目組的成員。
- Internal project: 隻要有使用者名和密碼,
,均可通路該項目。可以登陸該項目所在的Gitlab伺服器的
- public projects: 隻要知道該項目的具體位置就是路徑,都可以通路該項目,
。它們預設的使用的是guest權限
比如我們在上面的開發組項目中進行建立項目操作流程如下:
1.進入開發組->New Project->設定項目名稱->建立項目
2.建議添加SSH密鑰到Gitlab中免密碼推送與拉取
[email protected]:newproject/secopsdev.git
:Setting -> SSH;
$ ls ~/.ssh/
id_ed25519 id_ed25519.pub id_rsa id_rsa.pub known_hosts
3.項目的初始化:
#基礎設定(前提你需要下載下傳git)
git config --global user.name "WeiyiGeek"
git config --global user.email "[email protected]"
#建立一個新的存儲庫
git clone [email protected]:newproject/secopsdev.git
cd secopsdev
touch README.md
git add README.md
git commit -m "add README"
#推送現有檔案夾
cd existing_folder
git init
git remote add origin http://gitlab.weiyigeek.top/newproject/secopsdev.git
git add .
git commit -m "Initial commit"
#推動現有的Git存儲庫
cd existing_repo
git remote rename origin old-origin #将本地分支改名
git remote add origin http://gitlab.weiyigeek.top/newproject/secopsdev.git
4.需要注意預設是無法進行送出的這是由于Master主分支被保護所導緻的,如果想直接對主分支進行更改而又不想建立子分支可以采用以下方式,前提是必須采用
Maintainers的使用者上傳Master分支後進行設定
;
Project -> Settings -> Repository -> Brach (
分支權限設定
) 或者 删除保護;
8) 使用者項目權限控制
權限管理了解:
- (1).建立使用者的時候選擇一個普通權限和管理者權限之分即可
- (2).建git庫的時候可以關聯一個組或者一個成員
- (3).添加到組裡面的人員可以設定Guest Reporter Developer Master Owner
- (4).不同git庫裡面的關聯同一個成員可以設定不同權限
- (5).不同git庫裡面的關聯同一個組無法設定不同權限
- (6).一個git庫隻能關聯一個組成員
- (7).一個組成員可以被多個git庫關聯,且權限一樣
- (8).一個成員可以被多個git庫或組成員關聯,且權限可以不一樣
- (9).Reporter以上才有下代碼權限
- (10).企業權限控制:
- 開發人員:developer權限
- 代碼稽核MDE:master權限
項目組&x項目權限管理:
- Project Name -> Memebers-> 使用者成員與
Choose a role permission
-> 以及失效日期設定
參考官方文檔網站列出了
對應的權限,比如上面我們設定的Dev預設對保護分支是沒有push權限的;Guest,Reporter,Developer,Maintainer(Master),Owner
9) 廣播資訊設定
描述: 廣播資訊會顯示給全部使用者,可以用于通知使用者系統定期維護、近期計劃更新等資訊,
在使用者登陸界面以及使用者送出代碼的時候會有顯示
。
Area Admin -> Messages -> Broadcast Information (設定即可)
Gitlab資料備份恢複
描述:GitLab作為公司項目代碼的版本管理系統,資料非常重要是以必須做好備份。
Gitlab提供了兩種備份:
- 本地備份
- 遠端備份
本地備份
#1.GitLab備份的預設目錄是
/var/opt/gitlab/backups
#2.修改改備份目錄
$vim /etc/gitlab/gitlab.rb
gitlab_rails['backup_path'] = '/home/backups'
#備份保留7天
gitlab_rails['backup_keep_time'] = 604800
#3.修改配置後重載配置
$gitlab-ctl reconfigure
#4.執行備份指令
$gitlab-rake gitlab:backup:create
# 2020-04-16 09:44:52 +0800 -- Dumping database ...
# Dumping PostgreSQL database gitlabhq_production ... [DONE]
# 2020-04-16 09:44:53 +0800 -- done
# 2020-04-16 09:44:53 +0800 -- Dumping repositories ...
# 2020-04-16 09:44:53 +0800 -- done
# 2020-04-16 09:44:53 +0800 -- Dumping uploads ...
# 2020-04-16 09:44:53 +0800 -- done
# 2020-04-16 09:44:53 +0800 -- Dumping builds ...
# 2020-04-16 09:44:53 +0800 -- done
# 2020-04-16 09:44:53 +0800 -- Dumping artifacts ...
# 2020-04-16 09:44:53 +0800 -- done
# 2020-04-16 09:44:53 +0800 -- Dumping pages ...
# 2020-04-16 09:44:53 +0800 -- done
# 2020-04-16 09:44:53 +0800 -- Dumping lfs objects ...
# 2020-04-16 09:44:53 +0800 -- done
# 2020-04-16 09:44:53 +0800 -- Dumping container registry images ...
# 2020-04-16 09:44:53 +0800 -- [DISABLED]
# Creating backup archive: 1587001493_2020_04_16_12.9.2_gitlab_backup.tar ... done
# Uploading backup archive to remote storage ... skipped
#5.檢視備份檔案,壓縮包包括GitLab整個的完整部分,大概格式xxxxxxxx_gitlab_backup.tar
$ls /var/opt/gitlab/backups
1587001493_2020_04_16_12.9.2_gitlab_backup.tar
補充說明:
- 通過任務計劃crontab 實作自動備份
#輸入指令crontab -e
sudo crontab -e
0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create
#重新開機服務
systemctl restart crond
- 值得注意的是由于
包含敏感資料,采用上面備份的形式不能将它們進行備份,隻能通過gitlab.rb and gitlab-secrets.json
的方式;手動備份
遠端備份
描述:實際上就是在gitlab伺服器上添加備份伺服器的pub公鑰,然後備份伺服器通路Gitlab伺服器進行拉取,當然您也可以采用rsync進行備份;
#假設環境如下:
#備份伺服器: 192.168.1.2
#GitLab伺服器: 192.168.1.250
#1.備份伺服器建立authorized_keys檔案
mkdir -p /root/.ssh
touch /root/.ssh/authorized_keys
chmod 400 /root/.ssh/authorized_keys
#2.Gitlab伺服器生成rsa公鑰并将證書上傳到備份伺服器(實際環境中建議最小權限使用者)
$ssh-keygen -t rsa -C "[email protected]"
# Enter file in which to save the key (/root/.ssh/id_rsa): 報錯路徑
# Enter passphrase (empty for no passphrase): 公鑰密碼
# Enter same passphrase again: 驗證密碼
# Your public key has been saved in /root/.ssh/id_rsa.pub. #儲存路徑
# The key fingerprint is:
# SHA256:YahioDs7XZ9No********RS9jWX1NNXo04i4ZHOLY [email protected]
# The key's randomart image is:
# +---[RSA 2048]----+
# | . . ++|
# | .+ . o o.+|
# |. o.oo= . + .|
# |.. ..=.E.+ + |
# |o o..+ oS= . . |
# |.=+.+ o . . |
# |+*.. = + |
# |oo= o o . |
# |.o.+ |
# +----[SHA256]-----+
$ls /root/.ssh/
id_rsa id_rsa.pub
$scp /root/.ssh/id_rsa.pub [email protected]:/root/.ssh/
#3.備份伺服器将id_rsa.pub檔案内容追加到authorized_keys 檔案中,并且修改authorized_keys檔案的權限
$cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
#4.gitlab備份腳本(主要執行權限呼籲)将備份檔案拷貝到伺服器端
$cat > /home/gitlab/auto_backup_to_remote.sh<<'END'
DATE=`date +"%Y_%m_%d"`
BackupIP=10.10.107.245
BackupUser=root
BackupPATH=/var/opt/gitlab/backups
#删除本地7天前得備份
find ${BackupPATH} -type f -mtime +7 -name '*.tar' -exec rm {} \;
#如果不需要備份檔案您也可以備份倉房目錄
find ${BackupPATH} -name *${DATE}* -exec scp -r {} ${root}@${BackupIP}:/tmp/ \;
END
$chmod +x /home/gitlab/auto_backup_to_remote.sh
#5.添加定時計劃(每天淩晨1點執行),重新開機crond服務
cat >> /etc/crontab <<END
0 1 * * * /home/gitlab/auto_backup_to_remote.sh
END
systemctl restart crond
Gitlab資料恢複
比如:上面我們得備份:
/var/opt/gitlab/backups/1587001493_2020_04_16_12.9.2_gitlab_backup.tar
;
操作流程如下:
#停止相關資料連接配接服務(保證資料庫沒有新的連接配接不會有寫資料情況)
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
#指定恢複檔案會自動去備份目錄找格式:1587001493_2020_04_16_12.9.2
cd /var/opt/gitlab/backups/ && chmod -R 777 *
gitlab-rake gitlab:backup:restore BACKUP=1587001493_2020_04_16_12.9.2
#啟動Gitlab
gitlab-ctl restart
Gitlab添加發信郵箱
郵件找回密碼:
1)因為gitlab一直都在運作着,是以要修改前記錄備份一下配置檔案;
cp /etc/gitlab/gitlab.rb{,.bak}
2)修改配置檔案:/etc/gitlab/gitlab.rb 這裡使用公司的企業郵箱來發郵件;
$egrep -v "^$|^#" /etc/gitlab/gitlab.rb
external_url 'http://gitlab.weiyigeek.top'
gitlab_rails['time_zone'] = 'Asia/Shanghai'
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = '[email protected]'
gitlab_rails['gitlab_email_display_name'] = 'Gitlab CE'
gitlab_rails['gitlab_email_reply_to'] = '[email protected]'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.exmail.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "[email protected]"
gitlab_rails['smtp_password'] = "郵箱密碼"
gitlab_rails['smtp_domain'] = "weiyigeek.top"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
user['git_user_email'] = "[email protected]"
3)儲存修改,更新配置;
#隻是把修改過的配置檔案資訊,重新整理到配置檔案中
gitlab-ctl reconfigure
其實上面修改一個配置檔案gitlab.rb 它裡面的配置會被此指令調用編寫成多個配置檔案到安裝目錄下:
/var/opt/gitlab/gitlab-rails/etc/
$ls /var/opt/gitlab/gitlab-rails/etc/
database.yml gitlab_shell_secret gitlab.yml resque.yml smtp_settings.rb #也可以直接修改
gitlab_pages_secret gitlab_workhorse_secret rack_attack.rb secrets.yml unicorn.rb
如果直接修改
smtp_settings.rb
檔案就隻要重新開機一下
gitlab-ctl restart
即可。
4)控制台檢視配置與測試發信
$gitlab-rails console
--------------------------------------------------------------------------------
GitLab: 12.9.2 (ac5568eb5d8) FOSS
GitLab Shell: 12.0.0
PostgreSQL: 10.12
--------------------------------------------------------------------------------
Loading production environment (Rails 6.0.2)
irb(main):001:0> ActionMailer::Base.delivery_method
=> :smtp
irb(main):002:0> ActionMailer::Base.smtp_settings
=> {:authentication=>:login, :address=>"smtp.exmail.qq.com", :port=>465, :user_name=>"[email protected]", :password=>"Pa****20", :domain=>"weiyigeek.top", :enable_starttls_auto=>true, :tls=>true, :ca_file=>"/opt/gitlab/embedded/ssl/certs/cacert.pem"}
irb(main):003:0> Notify.test_email('[email protected]', 'GitLab測試郵件', 'GitLab 頁面資訊').deliver_now
Notify#test_email: processed outbound mail in 2.0ms
Delivered mail [email protected] (1543.7ms)
Date: Wed, 15 Apr 2020 21:12:37 +0800
From: Gitlab CE <[email protected]>
Reply-To: Gitlab CE <[email protected]>
完成上面操作後,回到登陸頁面,選擇Forgot your password? 輸入你帳号的郵箱位址即可收到郵件,根據郵件位址修改新密碼即可;
補充說明:
- 在某些ECS專有網絡主機上配置了一個gitlab倉庫,因為預設這些專有網絡的ECS主機都是禁用了25端口的,是以這個gitlab.rb的配置就有做相應的改變,使用465協定。
#隻修改以下兩行即可,修改完要更新配置
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_tls'] = true
Gitlab添加LDAP認證
LDAP配置說明:http://gitlab.weiyigeek.top/help/administration/auth/ldap
Step1.修改gitlab配置檔案設定連接配接LDAP伺服器參數即DN于bindPass
vim /etc/gitlab/gitlab.rb
gitlab_rails['ldap_enabled'] = true
# gitlab_rails['prevent_ldap_sign_in'] = false
###! **remember to close this block with 'EOS' below**
gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
main: # 'main' is the GitLab 'provider ID' of this LDAP server
label: 'LDAP'
host: '10.10.107.245'
port: 389
timeout: 10
uid: 'uid'
bind_dn: 'cn=admin,dc=WeiyiGeek,dc=com,dc=cn'
password: 'WeiyiGeek'
encryption: 'plain' # "start_tls" or "simple_tls" or "plain"
verify_certificates: false
smartcard_auth: false
active_directory: true
allow_username_or_email_login: true
lowercase_usernames: false
block_auto_created_users: false
base: 'dc=WeiyiGeek,dc=com,dc=cn'
#user_filter: '(&(objectclass=inetOrgPerson)(memberof=cn=gitlab,ou=Group,dc=WeiyiGeek,dc=com,dc=cn))'
attribute:
username: ['uid']
email: ['mail']
name: 'cn'
first_name: ['sn']
EOS
Step2.LDAP建立
groupOfUniqueNames屬性
得使用者組以及綁定使用者;
dn: cn=gitlab,ou=Group,dc=WeiyiGeek,dc=com,dc=cn
uniqueMember: uid=gituser,ou=People,dc=WeiyiGeek,dc=com,dc=cn
dn: uid=gituser,ou=People,dc=WeiyiGeek,dc=com,dc=cn
objectClass: posixAccount
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
loginShell: /bin/bash
homeDirectory: /home/gituser
userPassword:: e1NTSEF9UGVyM21xc1dJcnV3K1d2bWRiVmVpd3RWZHVVeVN6Tks=
uid: gituser
cn: gituser
uidNumber: 10000
gidNumber: 10000
sn: gituser
mail: [email protected]
Step3.登陸測試如果LDAP使用者不存在或者filter的條目不存在會報錯
Could not authenticate you from Ldapmain because "Invalid credentials for jenkuser".
Step4.登陸成功界面如下:
0x04 入坑解決
(0) 使用者pull與push代碼到gitlab常見錯誤
- 錯誤1:
The requested URL returned error: 403
- 原因:表示我們沒有權限來pull/push相關代碼需要修改相關gitlab賬号的權限為
;Development或者Maintianer
- 錯誤2:
To gitlab.weiyigeek.top:newproject/secopsdev.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '[email protected]:newproject/secopsdev.git'
- 原因:由于上傳代碼的gitlab使用者權限為開發者而預設建立的是私有的項目,預設是對master分支開啟了保護機制,需要對其項目的Branch分支設定
為Development+Maintiner;Allowed to merge | Allowed to push
#最終主分支以及dev分支都測試上次成功
remote:
To gitlab.weiyigeek.top:newproject/secopsdev.git
* [new branch] dev -> dev
Branch 'dev' set up to track remote branch 'dev' from 'origin'.
(1) gitlab密碼忘記找回重置方法
- 方式1:通過gitlab-rails進行重置
$gitlab-rails console
Loading production environment (Rails 6.0.2)
> u=User.where(id:1).first #這個是管理者的,也可以用email等
> u.password = 'your_password' #密碼有格式限制,我隻知道8位以上否則會儲存失敗
> u.password_confirmation = 'your_password'
> u.save
- 方式2:通過初始化設定郵件進行找回管理者或者在本地 postfix 中檢視未發送得郵件,此處以root使用者的[email protected]為例,一般預設都沒改(隻是一種方式實際中沒多大意義);
此處假設服務不能連結到外網之中(為了複現下列操作);
#(1)檢視無法發送出去的郵件隊列
$postqueue -p
-Queue ID- --Size-- ----Arrival Time---- -Sender/Recipient-------
#因為不存在此位址就無法發送出去,隻能停留在郵件的列隊中。
908DCCAC62 6871 Wed Apr 15 19:54:32 [email protected] #(實際就是我們找回密碼郵件)
(Host or domain name not found. Name service error for name=example.com type=MX: Host not found, try again)
[email protected]
#(2)郵件隊列存儲位置
$postconf -d | grep queue_directory #預設情況下是沒有修改的
queue_directory = /var/spool/postfix
#(3)進到目錄下,查找上面那封郵件的隊列ID号
$find /var/spool/postfix/ -name 908DCCAC62
/var/spool/postfix/defer/9/908DCCAC62
/var/spool/postfix/deferred/9/908DCCAC62
# (4) 利用postfix自帶的工具檢視郵件的内容,找到修改密碼的地
$postcat /var/spool/postfix/deferred/9/908DCCAC62
*** ENVELOPE RECORDS /var/spool/postfix/deferred/9/908DCCAC62 ***
message_size: 6871 274 1 0 6871 0
message_arrival_time: Wed Apr 15 19:54:32 2020
create_time: Wed Apr 15 19:54:32 2020
named_attribute: rewrite_context=local
sender: [email protected]
named_attribute: dsn_orig_rcpt=rfc822;[email protected]
original_recipient: [email protected]
.....
If you did not perform this request, you can safely ignore this email.
Otherwise, click the link below to complete the process:
#找到如下密碼修改連接配接,并将其複制出來到浏覽器中重置密碼
http://gitlab.weiyigeek.top/users/password/edit?reset_password_token=gd9MyL7FaSt5R2F3_qA_
(2)GitLab資料庫引起的502錯誤問題及解決方案
描述:打開GitLab的首頁位址http:/gitlab.weiyigeek.top/報錯502,重新開機或檢查狀态發現都正常
sudo gitlab-ctl status
,并且檢查了端口号并沒被占用,檢查重新整理配置
gitlab-ctl reconfigure
發現了錯誤;
錯誤資訊如下:
#gitlab-ctl reconfigure
Running handlers:
There was an error running gitlab-ctl reconfigure:
bash[migrate gitlab-rails database] (gitlab::database_migrations line 51) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
---- Begin output of "bash" "/tmp/chef-script20190308-65247-12ck9rp" ----
STDOUT: rake aborted!
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:52:in `block (3 levels) in <top (required)>
/opt/gitlab/embedded/bin/bundle:23:in `load'
/opt/gitlab/embedded/bin/bundle:23:in `<main>'
Tasks: TOP => gitlab:db:configure
(See full trace by running task with --trace)
STDERR:
---- End output of "bash" "/tmp/chef-script20190308-65247-12ck9rp" ----
Ran "bash" "/tmp/chef-script20190308-65247-12ck9rp" returned 1
#gitlab-ctl tail
(3) gitlab-ce版本更新記錄
我們為了保證資料安全,一步步來慢慢更新,使用官方的源可能比較慢,可以使用國内的源(采用Omnibus方式進行更新直接rpm包部署):https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum
更新方法:先更新到目前大版本最後一版(這裡是我的建議,一般差不多最後幾版就可以了),接下來就是跨大版本的更新,先從大版本的低版更新,再到最後版本更新,以此類推;因為我中間嘗試過直接跨太多個版本更新,出現過異常,其實主要是資料庫的資料導入備份問題,如果新版本跟舊版相關太大的話就會報錯了。
比如我的更新過程很長,同時也做了簡單的測試
#更新版本
8.3.1-->8.3.7-->8.8.5-->8.17.4-->9.5.6-->10.0.6-->10.8.5-->11.0.3
#停止服務
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-ctl stop nginx
#建立資料備份,防止更新失敗(非常重要)
gitlab-rake gitlab:backup:create
#安裝更新包
#yum update gitlab-ce # 更新GitLab-ce 版本(不建議采用此種方式)
rpm -Uvh gitlab-7.4.2_omnibus-1.el6.x86_64.rpm
#重新加載配置&重新開機Gitlab服務
gitlab-ctl reconfigure
gitlab-ctl restart
比如:項目備份的資料的是否有異常,使用者資訊,響應速度等等,個人認為還是比較靠普的。
其實隻要我們之前有更新過就不會現在這樣了,一下子從這麼低的版本更新上來。一般建議保持在相同的大版本号就好了。太新可能也會有Bug,太舊了也會有很多曆史遺留的問題。
更新完成後你會發現多了好幾個程序。可能會對系統硬體要求更高了,比如記憶體會要求更高了。
建議:如果非必要的功能需求,隻是簡單的要一個代碼倉庫的話,個人建立使用在
11.x.-12.3.x
之間也不錯了。
(4) gitlab-rails database初始化失敗
環境說明
Centos7: 3.10.0-327.el7.x86_64
gitlab版本:原來是8.8.5版本現更新到較新的版本;
問題:在同一台Centos7機器上解除安裝了舊版本的gitlab後,接着又重新安裝新版本的gitlab-ce 在安裝完後修改配置,初始化配置時出現以下錯誤:
Running handlers:
There was an error running gitlab-ctl reconfigure:
bash[migrate gitlab-rails database] (gitlab::database_migrations line 49) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
---- Begin output of "bash" "/tmp/chef-script20190628-7065-vx17en" ----
STDOUT: rake aborted!
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 `<main>'
Tasks: TOP => gitlab:db:configure
(See full trace by running task with --trace)
STDERR:
---- End output of "bash" "/tmp/chef-script20190628-7065-vx17en" ----
Ran "bash" "/tmp/chef-script20190628-7065-vx17en" returned 1
原因:因為在解除安裝舊版本的時候沒有把舊的資料删除,是以直接安裝時,程式檢測到配置檔案,資料檔案時就會跳過沒有覆寫,新版本使用舊版本的資料檔案[因為版本相差太多了]就會無法識别導緻。
解決方法:解除安裝完舊版本後,記得删除舊的安裝資料
配置檔案,安裝目錄,資料目錄等
,重新安裝新版的gitlab-ce即可正常啟動。
rm -rf /var/opt/gitlab/ /opt/gitlab/ /etc/gitlab/
(5) Gitlab的資料庫postgresql更新帳号資訊
問題描述:由于内部的Ldap認證伺服器硬碟壞掉了,導緻在上面跑的Ldap服務無法對Jenkins和Gitlab平台做集中認證了,導緻在Gitlab上的帳号無法登陸到平台上,也就無法送出拉取代碼了。
解決思路:
- 方法一、重新配置一個Ldap服務把之前的資料庫導進來,遷移到新的機器上然後修改Gitlab的認證服務位址。
- 方法二、直接修改Gitlab的資料庫,對帳号的認證方式修改。
此處選擇方式2進行操作其流程如下:
- Step1.修改gitlab的資料庫配置,開啟遠端通路(預設本機通路)
#1.修改配置檔案添加最後兩行
egrep -v "^#|^$" /var/opt/gitlab/postgresql/data/pg_hba.conf
local all all peer map=gitlab
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 trust
#2.修改postgresql配置檔案将listen_addresses把它改成*号
$vim /var/opt/gitlab/postgresql/data/postgresql.conf
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost', '*' = all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 200 # (change requires restart)
# Note: Increasing max_connections costs ~400 bytes of shared memory per
# connection slot, plus lock space (see max_locks_per_transaction).
#superuser_reserved_connections = 3 # (change requires restart)
unix_socket_directories = '/var/opt/gitlab/postgresql' # (change requires restart)
#3.重新開機postgresql服務
gitlab-ctl restart postgresql
#4.使用Navicat工具連postgresql接資料庫,下邊的初始資料庫gitlabhq_production和使用者名都是預設的,密碼為空
連接配接上後找到對應的identities表修改對應的字段,其實就是把字段中的user_id修改掉,我這裡做法就是把使用者的user_id改成了負數,這樣在Ldap認證時就找不到了,進而走标準認證(這是在不重新開機Gitlab的情況下進行)。
- 更新後如果使用者還存在登陸不了的問題,這時候可以讓使用者自己去Web端登陸gitlab,選擇忘記密碼,通過忘記密碼來重新設定一個密碼。(前提是你實作了密碼找回功能)
- 完成所有操作後記得要把遠端的配置注釋掉或者删除了,然後重新開機postgresql服務即可。
0x05 腳本附錄
#!/bin/bash
#Desc: Gitlab代碼伺服器自動化部署
#Author:WeiyiGeek
#SupportOS:CentOS7 / CentOS8
GITLAB_BASEDOMAIN=weiyigeek.top
GITLAB_VERSION=12.9.2
GITlABOS7=gitlab-ce-${GITLAB_VERSION}-ce.0.el7.x86_64.rpm
GITlABOS8=gitlab-ce-${GITLAB_VERSION}-ce.0.el8.x86_64.rpm
GITLABRUNNER_VERSION=12.9.0-1
GITLABRUNNER_NAME=gitlab-runner-${GITLABRUNNER_VERSION}.x86_64.rpm
CheckOSVersion=$(uname -r | grep -c el8)
## [鏡像源設定]
function repoChange(){
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.$(date +"%Y%m%d").backup
if [ $CheckOSVersion -eq 1 ];then
# CentOS8 源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
# 安裝 epel 配置包并位址替換為阿裡雲鏡像站位址
dnf install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
sed -i 's|^#baseurl=https://download.fedoraproject.org/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
dnf clean all
dnf makecache
else
# CentOS7 源
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#添加信任 GitLab 裡的 GPG 公鑰
sudo cat > /etc/yum.repos.d/gitlab-ce.repo <<EOF
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
gpgcheck=0
enabled=1
gpgkey=https://packages.gitlab.com/gpg.key
EOF
yum clean all
yum makecache
fi
}
#[yum 方式安裝]
function yumInstall(){
# 檢視可用的版本neng'b
yum list gitlab-ce --showduplicates
# 預設安裝最新的版本
yum install -y gitlab-ce
# 安裝指定版本 12.3.5
# yum install gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm
}
#[rpm 方式安裝-推薦方式]
function OmnibusInstall(){
if [ $CheckOSVersion -eq 1 ];then
wget -O $GITlABOS8 https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/${GITlABOS8}
rpm -i $GITlABOS8
else
wget -O $GITlABOS7 https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/${GITlABOS7}
rpm -i $GITlABOS7
fi
}
function gitlabSetting(){
sed -i "s#example.com#${GITLAB_BASEDOMAIN}#g" /etc/gitlab/gitlab.rb
echo "127.0.0.1 gitlab.${GITLAB_BASEDOMAIN}" > /etc/hosts
}
function useage(){
echo -e "\e[32m# Description: Gitlab 自動化安裝部署腳本"
echo -e "usage: $0 [rpm|yum] #指定rpm安裝還是yum安裝"
echo -e "Author:WeiyiGeek\e[0m"
}
#[低于 12.3.x 版本的才進行設定]
function Chinesization(){
#停止gitlab
gitlab-ctl stop
#擷取目前安裝的版本更新檔
git clone https://gitlab.com/xhang/gitlab.git
cd gitlab
gitlab_version=$(cat /opt/gitlab/embedded/service/gitlab-rails/VERSION)
# 生成對應版本更新檔檔案
git diff remotes/origin/12-3-stable remotes/origin/12-3-stable-zh > ../${gitlab_version}-zh.diff
# 打更新檔的時候會提示一些更新檔檔案不存在,一定要跳過這些檔案,不然後面reconfig的時候會報錯的。
patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 < ../${gitlab_version}-zh.diff
gitlab-ctl reconfigure
gitlab-ctl restart
}
## [安裝配置腳本入口函數]
function main(){
#關閉Sellinux
echo "目前Selinux: $(getenforce)"
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
echo "設定Selinux: $(getenforce)"
#環境依賴安裝
repoChange
sudo yum install -y curl policycoreutils openssh-server wget postfix git htop ncdu net-tools
systemctl enable postfix
systemctl start postfix
#防護牆設定
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo systemctl reload firewalld
#選擇安裝方式
if [ $1 == "rpm" ];then
OmnibusInstall
elif [ $1 == "yum" ];then
yumInstall
else
usage
fi
gitlabSetting
gitlab-ctl reconfigure
gitlab-ctl start
gitlab-ctl status
}
#[參數驗證]
if [ $# -ne 1 ];then
usage
else
main $1
fi