介紹
GitLab核心是內建管理Git存儲庫的工具。比如你希望建立一個提供服務的平台,那麼GitLab将提供強大的身份驗證和授權機制、工作組、問題跟蹤、wiki和片段,除此之外還有公有、内部和私有存儲庫。
GitLab強大之處在于,它包含強大的持續內建(CI)引擎和Docker容器鏡像倉庫,讓使用者從開發到釋出都使用相同的實用工具。它還有兩個更強大的開源軟體實用工具:Prometheus負責監控,Mattermost負責和團隊溝通。該平台有着堅實的API并能和多個現有第三方系統內建,如:JIRA,Bugzilla,Pivotal,Slack,HipChat,Bamboo等。
這裡就有這樣一個疑問:為什麼使用GitLab而不是直接使用SaaS 服務?答案隻是個人品味而已。對大多數人來說,向SaaS提供商購買其提供的服務是一個很好的解決方案。你可以專注于搭建你的應用程式,讓他們去操心維護這些工具。而如果你已擁有的基礎設施有備用容量怎麼辦?如果你隻想私有化存儲庫而不想為該權限付費時該怎麼辦?如果你想運用你的數學頭腦由自己托管來省錢該怎麼辦?如果你隻想擁有你自己的資料該怎麼辦?
由内部提供服務可以讓你有更多的時間管理它們并讓它們之間互相通信來消除風險,這就是GitLab的出衆之處。僅需要一次點選和幾分鐘的配置,你就可以啟動并運作一個完整的解決方案。
部署GitLab
Rancer的Catalog包含了安裝最新版GitLab CE的條目。它假設你有一個主機,希望為HTTP/HTTPS直接打開80和443端口,并且打開一個端口映射到容器内的22端口。
Catalog條目會根據你提供的值設定環境變量GITLAB_OMNIBUS_CONFIG。然後GitLab在釋出時将這些值并入配置。對于非常基本的GitLab部署,Catalog提供的選擇是完全足夠的,不過我仍希望向你展示更多的内容…
在本教程中,我們将部署GitLab CE,不過我們不會打開任何端口。主機端口非常昂貴,是以稍後我們會使用一個負載均衡器。我們将配置HTTPS和Docker Registry,并将其與Rancher配合使用。
1. 建立一個名為gitlab的新應用
2. 向gitlab應用添加一個服務
Image: gitlab/gitlab-ce:latest
Volumes:
gitlab-etc:/etc/gitlab
gitlab-opt:/var/opt/gitlab
gitlab-log:/var/log/gitlab
Networking
Set a specific host name: git
Health Check
HTTP: Port 80
Path: GET /HTTP/1.0
3. 向postfix服務中添加一個配對
Image: tozd/postfix
Environment:
MY_NETWORKS:10.42.0.0/16, 127.0.0.0/8
ROOT_ALIAS: [email protected]
postfix-log:/var/log/postfix
postfix-spool:/var/spool/postfix
Health Check:
TCP: Port 25
運作之前,你還需完成幾個選項來配置GitLab:
将所有的GitLab變量添加到GITLAB_OMNIBUS_CONFIG
稍後設定所有變量
對于首次使用的使用者來說,我建議選擇第二項。GitLab提供的gitlab.rb檔案在預設設定下文檔已經很豐富,如果你之前沒有接觸過GitLab,參考這份檔案就可以得到大量功能的說明介紹。
接着,單擊Launch鍵,Rancher将抓取鏡像并呈現給你。
設定SSL解除安裝
Rancher在抓取鏡像的時候,我們來用HTTPS添加一個負載均衡器。為此,我們首先要建立一個LetsEncrypt容器,然後将其添加到負載均衡器中,等待證書注冊。注冊完成後,将GitLab的配置添加到負載均衡器上。
在這個例子中,我将使用域名“example.com”,GitLab的主機名設定為“git”,Docker Registry的主機名設定為“regitstry”。在執行下一步前需確定你已經将相應的記錄添加到DNS區域檔案中,且這些記錄均指向運作均衡器的主機。(PS:這裡需要簽的域名必須是公網可解析)
部署LetsEncrypt
從Rancher社群Catalog中,選擇LetsEncrypt服務。接受第一個下拉清單中的TOS,然後按以下設定準備HTTP驗證:
你的Email位址: [email protected]
證書名: gitlab
域名: git.example.com,registry.example.com
域驗證方法: HTTP
單擊Launch以釋出容器。現在開始你有120秒來完成下一步。
部署負載均衡器
在gitlab棧中,單擊“添加服務”旁邊的下拉菜單,然後選擇添加負載均衡器。給它取個名字,接着添加下面的服務選擇器。另外,如果你已經有了一個負載均衡器的環境,編輯它,添加下面的服務。
Public / HTTP
Port: 80
Path: /.well-known/acme-challenge
Target: letsencrypt
單擊“編輯”儲存你所做的變更。
監控LetsEncryt容器的日志,兩分鐘後,就可以獲得它已經注冊了兩個域的證書的報告。如果你收到狀态403或503的錯誤報告,那麼需要檢查負載均衡器配置,确認設定無誤。LetsEncrypt容器将重新啟動并繼續嘗試注冊證書。注冊成功後,你就可以在Rancher界面中的基礎設施頁籤中找到該證書。
到這為止我們已經準備好通過負載均衡器向GitLab添加SSL支援:
1. 編輯負載均衡器
2. 添加以下服務規則:
Host: git.example.com
Target: gitlab
Public / HTTPS
Port: 443
Host: registry.example.com
Public / TCP
Port: 2222
Port: 22
3. 單擊“編輯”儲存你所做的更改。
配置GitLab
GitLab的配置儲存在容器中的/etc/gitlab/gitlab.rb下。當我們啟動服務時,我們建立了一個Docker卷用于持久化存儲這些資料。在Rancher中,找到你的GitLab容器,使用Execute Shell登入。将存儲位址改為/etc/gitlab,然後編輯gitlab.rb。
在gitlab.rb中有很多變量可以調整GitLab的行為。這裡每一個部分都包含了一個指向GitLab文檔的連結,文檔描述了該服務的功能以及每個變量的調整。
在本教程中,需要找到以下變量,更改或者取消它們的注釋:
<code>external_url ‘https:</code><code>//git.example.com’</code>
<code>gitlab_rails[</code><code>'gitlab_ssh_host'</code><code>] = 'git.example.com’</code>
<code>gitlab_rails[</code><code>'gitlab_email_enabled'</code><code>] = </code><code>true</code>
<code>gitlab_rails[</code><code>'gitlab_email_from'</code><code>] = </code><code>'[email protected]'</code>
<code>gitlab_rails[</code><code>'gitlab_email_display_name'</code><code>] = </code><code>'Gitlab'</code>
<code>gitlab_rails[</code><code>'gitlab_email_reply_to'</code><code>] = </code><code>'[email protected]'</code>
<code>gitlab_rails[</code><code>'gravatar_plain_url'</code><code>] = </code><code>'https://secure.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon'</code>
<code>gitlab_rails[</code><code>'gravatar_ssl_url'</code><code>] = </code><code>'https://secure.gravatar.com/avatar/%{hash}?s=%{size}&d=identicon'</code>
<code>gitlab_rails[</code><code>'gitlab_shell_ssh_port'</code><code>] = </code><code>2222</code>
<code>gitlab_rails[</code><code>'smtp_enable'</code><code>] = </code><code>true</code>
<code>gitlab_rails[</code><code>'smtp_address'</code><code>] = </code><code>'postfix'</code>
<code>gitlab_rails[</code><code>'smtp_port'</code><code>] = </code><code>25</code>
<code>gitlab_rails[</code><code>'smtp_domain'</code><code>] = </code><code>'yourdomain.com'</code>
<code>gitlab_rails[</code><code>'smtp_authentication'</code><code>] = </code><code>false</code>
<code>gitlab_rails[</code><code>'smtp_enable_starttls_auto'</code><code>] = </code><code>false</code>
<code>gitlab_rails[</code><code>'manage_backup_path'</code><code>] = </code><code>true</code>
<code>gitlab_rails[</code><code>'backup_path'</code><code>] = </code><code>'/var/opt/gitlab/backups'</code>
<code>gitlab_rails[</code><code>'backup_archive_permissions'</code><code>] = </code><code>0644</code>
<code>gitlab_rails[</code><code>'backup_pg_schema'</code><code>] = </code><code>'public'</code>
<code>gitlab_rails[</code><code>'backup_keep_time'</code><code>] = </code><code>604800</code>
<code>registry_external_url 'https:</code><code>//registry.example.com’</code>
<code>gitlab_rails[</code><code>'registry_enabled'</code><code>] = </code><code>true</code>
<code>gitlab_rails[</code><code>'registry_host'</code><code>] = </code><code>'registry.example.com'</code>
<code>gitlab_rails[</code><code>'registry_api_url'</code><code>] = </code><code>'http://localhost:5000'</code>
<code>gitlab_rails[</code><code>'registry_key_path'</code><code>] = </code><code>'/var/opt/gitlab/gitlab-rails/certificate.key'</code>
<code>gitlab_rails[</code><code>'registry_path'</code><code>] = </code><code>'/var/opt/gitlab/gitlab-rails/shared/registry'</code>
<code>gitlab_rails[</code><code>'registry_issuer'</code><code>] = </code><code>'omnibus-gitlab-issuer'</code>
<code>registry[</code><code>'enable'</code><code>] = </code><code>true</code>
<code>registry[</code><code>'token_realm'</code><code>] = </code><code>'https://git.example.com'</code>
<code>nginx[</code><code>'listen_port'</code><code>] = </code><code>80</code>
<code>nginx[</code><code>'listen_https'</code><code>] = </code><code>false</code>
<code>nginx[</code><code>'proxy_set_headers'</code><code>] = {</code>
<code> </code><code>'Host'</code> <code>=> </code><code>'$http_host_with_default'</code><code>,</code>
<code> </code><code>'X-Real-IP'</code> <code>=> </code><code>'$remote_addr'</code><code>,</code>
<code> </code><code>'X-Forwarded-For'</code> <code>=> </code><code>'$proxy_add_x_forwarded_for'</code><code>,</code>
<code> </code><code>'X-Forwarded-Proto'</code> <code>=> </code><code>'https'</code><code>,</code>
<code> </code><code>'X-Forwarded-Ssl'</code> <code>=> </code><code>'on'</code><code>,</code>
<code> </code><code>'Upgrade'</code> <code>=> </code><code>'$http_upgrade'</code><code>,</code>
<code> </code><code>'Connection'</code> <code>=> </code><code>'$connection_upgrade'</code>
<code>}</code>
<code>registry_nginx[</code><code>'enable'</code><code>] = </code><code>true</code>
<code>registry_nginx[</code><code>'listen_port'</code><code>] = </code><code>80</code>
<code>registry_nginx[</code><code>'listen_https'</code><code>] = </code><code>false</code>
<code>registry_nginx[</code><code>'proxy_set_headers'</code><code>] = {</code>
<code> </code><code>'Host'</code> <code>=> </code><code>'$http_host'</code><code>,</code>
<code> </code><code>'X-Forwarded-Ssl'</code> <code>=> </code><code>'on'</code>
<code>registry_nginx[</code><code>'custom_gitlab_server_config'</code><code>] = </code><code>'proxy_cache_convert_head off;'</code>
在這些變量變更之後,意味着以下工作你已經完成了:
為你的Git URLs設定主機名
配置GitLab将HTTP反向到HTTPS
啟用HTTP和HTTPS兩者的HTTPS gravatar URLs(在避免内容混合的錯誤時是必要的)
将報告的SSH端口設為2222
激活來自GitLab的郵件
通過Postfix助手啟動郵件傳遞
激活一個星期保留期的夜間備份
啟用容器registry
激活GitLab的配置和必需的标題,讓GitLab知道它是在SSL負載均衡器之後
儲存此檔案,接着輸入gitlab-ctl reconfigure,按Enter鍵重新配置GitLab。GitLab将重建它的配置,重新開機那些需要的服務。
登入
現在你已經準備好了!在你的浏覽器中輸入https://git.example.com,會出現一個要求你輸入密碼的界面。這裡預設使用者是root,如果你設定了密碼,系統将要求你重新登入。
恭喜你!你有了一個正在運作的GitLab執行個體!
現在還有一些需要我們從GitLab内部做的工作來鞏固它,請你接着讀下去。
鎖定它
我建議你去做以下變更操作:
更改root使用者名
以root身份登入任何内容都是不安全的,因為該使用者名是一個衆所周知的目标。現在你是以唯一的使用者身份登入進來的,那麼第一件事就是更改你的使用者名。
點選位于右上角、在搜尋欄旁邊的扳手圖示
在中間列的底部選擇管理者
選擇右上角的編輯按鈕
更改你的名字、使用者名和郵箱位址
向下滾動并單擊“儲存更改”
管理者賬戶的舊郵箱位址是[email protected],更改此資訊隻是嘗試向該賬戶發送電子郵件來通知這些更改。我敢相信example.com郵箱的人會吃驚于他們收到的電子郵件數。
傳回Rancher,找到你的postfix容器和Execute Shell
輸入mailq,按下Enter鍵。你應該看到延遲的郵件在隊列中,注意ID
輸入postsuper –d<id>,按下Enter鍵,将從隊列中删除該消息。
禁止公開注冊
下一步的更改将使Internet不再接管你的新GitLab執行個體,也不能再将其用于惡意的目的。
再次點選扳手圖像,傳回到管理控制台
點選右上角齒輪圖示的下拉菜單,選擇設定
你可以根據需要調整其中任意一項,但你需要禁用在Sign-up Restrictions下的Sign-up enabled預設值
檢查你的端口
在這個例子中,我們使用了80、443、2222端口。GitLab不需要主機上的其他端口,不過2222端口并不是通用端口。你需要确認你已經在防火牆中打開了它(2222端口)。
這會有一個很棒的GitLab安裝過程。你可以立即為你的項目啟動它。是的,在GitLab中還有很多事情要做!
添加你的SSH密鑰
盡管你可以通過HTTPS使用GitLab,然而使用SSH指令執行則更為常見。在執行此操作之前,需要将你的SSH公鑰添加到GitLab,這樣它會識别你的身份。如果你沒有SSH密鑰,你可以用下列代碼制作一個(Linux或Mac系統上):
<code>ssh-keygen -b </code><code>2048</code>
盡可能使用密碼密碼確定安全——你的密鑰有以特權使用者身份登入的權限,如果筆記本電腦遭到入侵,你也不願為攻擊者提供通路級别。(如果你不想使用密碼密碼或處理SSH代理,請通路https://krypt.co上優秀的Kryptonite項目)
使用預設值(或選擇新的密鑰名稱)儲存,接着在GitLab中:
點選右上角的頭像旁邊的下拉菜單,選擇設定,然後選擇SSH密鑰
将你的公鑰(.pub檔案中的内容)粘貼到打開頁面的框中
很快我們将釋出該系列的第二部分,其中将介紹如何使用GitLab CI Multi-Runner建構容器,以及如何使用GitLab容器registry配置項目。除此之外,我們還将涉及如何用GitLab CI建立容器并部署到Rancher上。
本文轉自 RancherLabs 51CTO部落格,原文連結:http://blog.51cto.com/12462495/1953088