本地docker搭建gitlab, 並配置ldap認證

基於Docker在Mac OS X系統中的部署和設置GitLab的具體過程以下:
 
1. 安裝Docker for Mac (參見 https://docs.docker.com/docker-for-mac/install/
 
2. 下載GitLab鏡像
     docker pull gitlab/gitlab-ce
 
    文檔可參見 https://docs.gitlab.com/omnibus/docker/
 
3. 運行GitLab實例
  sudo docker run -d \
    --hostname xxxx.xxxx.xx \
    --name gitlab \
    --restart always \
    --publish 30001:22 --publish 30000:80 --publish 30002:443 \
    --volume $HOME/gitlab/data:/var/opt/gitlab \
    --volume $HOME/gitlab/logs:/var/log/gitlab \
    --volume $HOME/gitlab/config:/etc/gitlab \
    gitlab/gitlab-ce
 
其中volume選項將gitlab的目錄掛載爲用戶當地目錄,以避免容器在中止或被刪除的時候丟失數據。publish選項將宿主機器的30000、30001和30002映射爲容器的80(http)、22(ssh)和443(https)端口。
 
4.配置GitLab實例
 
GitLab的配置存儲在/etc/gitlab/gitlab.rb文件中。
 
利用命令「 sudo docker exec -it gitlab /bin/bash」能夠在gitlab容器中進入bash,也能夠用命令「 sudo docker exec -it gitlab vi /etc/gitlab/gitlab.rb」直接執行vi編輯GitLab配置文件。因爲在運行GitLab實例時,已經用volume選項將gitlab的目錄掛載爲用戶當地目錄,所以gitlab.rb文件實際位於$HOME/gitlab/config目錄下。
 
修改gitlab.rb文件後,在容器中執行命令gitlab-ctl reconfigure從新配置gitlab。
 
4.1 配置GitLab服務器的訪問地址
 
4.1.1 配置http協議所使用的訪問地址
將external_url修改成GitLab服務器的訪問地址:
    external_url ' http://xxxx.xxxx.xxx:30000'
 
因爲定義的url中有端口號,須要將nginx監聽的端口號改回80,不然nginx將監聽容器的30000端口,形成GitLab沒法使用:
    nginx['listen_port'] = 80
 
4.1.2 配置ssh協議所使用的訪問地址和端口
    gitlab_rails['gitlab_ssh_host'] = ‘ data.comdyn.cn'
    gitlab_rails['gitlab_shell_ssh_port'] =30001
 
4.2 配置郵箱
 
爲了採用SMTP服務器而不是缺省的Sendmail發送郵件,須要修改gitlab.rb中的內容,
 
下面以網易163郵箱爲例配置郵箱:
 
    gitlab_rails['gitlab_email_from'] = " xxxx@163.com
    gitlab_rails['gitlab_email_reply_to'] = ‘ xxxx@163.com'
    
    gitlab_rails['smtp_enable'] = true
    gitlab_rails['smtp_address'] = " smtp.163.com"
    gitlab_rails['smtp_port'] = 465
    gitlab_rails['smtp_user_name'] = " xxxx@163.com"
    gitlab_rails['smtp_password'] = "xxxxpassword"
    gitlab_rails['smtp_domain'] = " 163.com"
    gitlab_rails['smtp_authentication'] = "login"       
    gitlab_rails['smtp_enable_starttls_auto'] = true
    gitlab_rails['smtp_tls'] =true
 
    gitlab_rails['smtp_openssl_verify_mode'] = "peer"
 
最後,用命令docker restart gitlab重啓GitLab,或者在容器中執行命令gitlab-ctl reconfigure從新配置gitlab。
 
5. 導入現有的git倉庫
 
將現有的git倉庫(如test)導入gitlab須要三步:
 
5.1 將已有的全部git倉庫的裸庫(如test.git)都拷貝到gitlab目錄中。
 
前面在運行gitlab實例時,利用volume選項已經將gitlab的數據目錄指定爲$HOME/gitlab/data/,所以git倉庫將存放在目錄$HOME/gitlab/data/git-data/repositories/下。
 
gitlab容許建立組,將多個倉庫歸入到一個組中,每一個組在repositories下面對應於一個與組名稱相同的子目錄。將全部裸庫拷貝到與其組所對應的子目錄中。例如,若是要要將test倉庫導入到sub組中的話,則應執行:
    sudo cp -r test.gig $HOME/gitlab/data/git-data/repositories/sub/
 
5.2 利用sudo chown命令,將test.git的擁有者的用戶名和組名改成和repositories目錄的擁有者相同,以保證gitlab能夠訪問該倉庫。
 
5.3 導入倉庫
 
在gitlab容器中執行gitlab-rake命令:
    sudo docker exec -it gitlab gitlab-rake gitlab:import:repos['$HOME/gitlab/data/git-data/repositories’]
導入repositories下的全部倉庫,並自動建立組。若是某倉庫在gitlab中已存在,則會跳過該倉庫。

 

 

Gitlab 11.0.3漢化完成後,須要集成域控,現將域控的配置過程描述以下:html

gitlab目錄:/etc/gitlabnginx

gitlab配置文件:/etc/gitlab/gitlab.rbgit

編輯gitlab.rb文件:docker

在gitlab.rb文件中修改下面配置,其中:shell

1.host,是搭建的ldap的ip,根據實際狀況修改;windows

2.uid,能夠配置cn,也能夠配置uid,都能識別(具體爲何沒深刻研究);bash

3.bind_dn,ldap的管理員帳號,根據實際狀況修改;服務器

4.password,ldap的管理員,根據實際狀況修改;dom

5.active_directory,彷佛是針對鏈接是不是ad域控的標示,由於這部分是ldap的配置,故爲false;ssh

6.allow_username_or_email_login,用戶登陸是否用戶名和郵箱均可以,方便用戶故配置true;

7.base,用戶列表所在的目錄,由於新增的用戶都在ldap的People下,故這麼配置,根據實際狀況修改;

個人配置以下:

  gitlab_rails['ldap_enabled'] = true 

###! **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: '**.**.**.**' 
     port: 389 
     uid: 'sAMAccountName' 
     bind_dn: 'cn=***,cn=Users,dc=***,dc=****,dc=COM' 
     password: '*****' 
     encryption: 'plain' # "start_tls" or "simple_tls" or "plain" 
     verify_certificates: true 
     active_directory: true 
     allow_username_or_email_login: true 
     lowercase_usernames: false 
     block_auto_created_users: false 
     base: 'OU=Users,OU=*****,DC=***,DC=*****,DC=COM' 
     user_filter: '' 
     ## EE only 
#     group_base: '' 
#     admin_group: '' 
#     sync_ssh_keys: false 

#   secondary: # 'secondary' is the GitLab 'provider ID' of second LDAP server 
#     label: 'LDAP' 
#     host: '_your_ldap_server' 
#     port: 389 
#     uid: 'sAMAccountName' 
#     bind_dn: '_the_full_dn_of_the_user_you_will_bind_with' 
#     password: '_the_password_of_the_bind_user' 
#     encryption: 'plain' # "start_tls" or "simple_tls" or "plain" 
#     verify_certificates: true 
#     active_directory: true 
#     allow_username_or_email_login: false 
#     lowercase_usernames: false 
#     block_auto_created_users: false 
#     base: '' 
#     user_filter: '' 
#     ## EE only 
#     group_base: '' 
#     admin_group: '' 
#     sync_ssh_keys: false 
 EOS 

 

 

重要配置參數解釋(仔細閱讀上一篇svn集成LDAP認證的文章這些參數會更好理解):
- host:LDAP服務器地址
- port:LDAP服務端口
- uid:以哪一個屬性做爲驗證屬性,能夠爲uid、cn等,咱們使用uid
- method:若是開啓了tls或ssl則填寫對應的tls或ssl,都沒有就填寫plain
- bind_dn:search搜索帳號信息的用戶完整bind(須要一個有read權限的帳號驗證經過後搜索用戶輸入的用戶名是否存在)
- password:bind_dn用戶的密碼,bind_dn和password兩個參數登陸LDAP服務器搜索用戶
- active_directory:LDAP服務是不是windows的AD,咱們是用的OpenLDAP,這裏寫false
- allow_username_or_email_login:是否容許用戶名或者郵箱認證,若是是則用戶輸入用戶名或郵箱均可
- base:從哪一個位置搜索用戶,例如容許登陸GitLab的用戶都在ou gitlab裏,name這裏能夠寫ou=gitlab,dc=domain,dc=com
- filter:添加過濾屬性,例如只過濾employeeType爲developer的用戶進行認證(employeeType=developer)

 

配置完成後進行配置更新:

  gitlab-ctl reconfigure

檢查是否配置成功:

  gitlab-rake gitlab:ldap:check(列出前100個用戶)

重啓gitlab服務:

  gitlab-ctl restart

 

注意:  確認該用戶不處於特殊狀態

 

1.不處於「禁用帳戶」的狀態;

 

2.不處於「須要首次登陸修改密碼」的狀態;

 

(處於上面任意一種狀態的帳戶在登陸gitlab時都會報「Cloud not authenticate you from Ldapmain because "Invalid creadentials".」)

下圖是發出的search 用戶狀態的請求:

判斷用戶狀態碼:

http://www.javashuo.com/article/p-woikbxpm-dq.html

相關文章
相關標籤/搜索