CentOS下GitLab的安裝部署

轉載來源 :https://mp.weixin.qq.com/s/kUwZja0xK1IfqGU6R2f1EA

一 GitLab Server的搭建

參考:https://about.gitlab.com/install/

1.準備工作

以centos7爲例,準備一臺至少內存爲4G的機器。
系統版本:CentOS Linux release 7.3.1611 (Core)
軟件版本:Gitlab-ce-11.10.1
硬件要求:最低2核4GB,建議4核8GB

2.安裝依賴軟件

[root@localhost ~]# sudo yum install -y git vim gcc glibc-static telnet
[root@localhost ~]# sudo yum install -y curl policycoreutiels-python openssh-server
[root@localhost ~]# sudo systemctl enable sshd
[root@localhost ~]# sudo systemctl start sshd
[root@localhost ~]# sudo yum install postfix -y 
[root@localhost ~]# sudo systemctl enable postfix
[root@localhost ~]# sudo systemctl start postfix                                                => 啓動SSH遠程服務
[root@localhost ~]# systemctl stop firewalld                                              => 停止Firewalld防火牆服務
[root@localhost ~]# systemctl disable firewalld                                           => 禁用Firwalld防火牆服務開機自啓
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/sysconfig/selinux => 關閉SeLinux(重啓主機生效)
[root@localhost ~]# setenforce 0

3.設置gitlab安裝源

國內的話就使用清華大學源,內容爲:

[root@localhost ~]# vim /etc/yum.repos.d/gitlab-ce.repo
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
[root@localhost ~]# yum makecache

4、安裝Gitlab

[root@localhost ~]# yum install -y gitlab-ce
可以訪問"https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/"查看Gitlab-ce的版本。
安裝歷史版本請使用下面命令:
[root@localhost ~]# yum install -y gitlab-ce-{VERSION}

5、配置Gitlab

建議使用HTTPS。

[root@localhost ~]# vim /etc/gitlab/gitlab.rb
### 基礎配置 ###
external_url 'http://gitlab.example.com/'
#用戶訪問所使用的URL,域名或者IP地址
gitlab_rails['time_zone'] = 'Asia/Shanghai'
#時區

### SSH配置 ###
gitlab_rails['gitlab_shell_ssh_port'] = 10222
#使用SSH協議拉取代碼所使用的連接端口。

### 郵箱配置 ###
gitlab_rails['smtp_enable'] = true
#啓用SMTP郵箱功能,綁定一個第三方郵箱,用於郵件發送
gitlab_rails['smtp_address'] = "smtp.exmail.qq.com"
#設置SMTP服務器地址
gitlab_rails['smtp_port'] = 465
#設置SMTP服務器端口
gitlab_rails['smtp_user_name'] = "[email protected]"
#設置郵箱賬號
gitlab_rails['smtp_password'] = "xxx"
#設置郵箱密碼
gitlab_rails['smtp_authentication'] = "login"
#設置郵箱賬號密碼身份驗證方式,"login"表示採用賬號密碼的方式登陸
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
#設置開啓SMTP郵件使用TLS傳輸加密協議傳輸郵件,以保證郵件安全傳輸
gitlab_rails['gitlab_email_from'] = '[email protected]'
#設置Gitlab來源郵箱地址,設置登陸所使用的郵箱地址

### WEB配置 ###
nginx['enable'] = true
#啓用Nginx服務
nginx['client_max_body_size'] = '250m'
#設置客戶端最大文件上傳大小
nginx['redirect_http_to_https'] = true
#設置開啓自動將HTTP跳轉到HTTPS
nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.xxx.cn.pem"
#設置HTTPS所使用的證書
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.xxx.cn.key"
#設置HTTPS所使用的證書密碼
nginx['ssl_protocols'] = "TLSv1.1 TLSv1.2"
#設置HTTPS所使用的TLS協議版本
nginx['ssl_session_cache'] = "builtin:1000  shared:SSL:10m"
#設置開啓SSL會話緩存功能
nginx['ssl_session_timeout'] = "5m"
#設置SSL會話超時時間
nginx['listen_addresses'] = ['*', '[::]']
#設置Nginx監聽地址,"*"表示監聽主機上所有網卡的地址
nginx['gzip_enabled'] = true
#設置開啓Nginx的傳輸壓縮功能,以節約傳輸帶寬,提高傳輸效率

6、上傳SSL證書到指定目錄

[root@localhost ~]# ll /etc/gitlab/ssl/
total 28
drwxr-xr-x 2 root root 4096 Apr 25 11:48 ./
drwxrwxr-x 4 root root 4096 Apr 25 12:50 ../
-rw-r--r-- 1 root root 1675 Apr 25 11:45 gitlab.xxx.cn.key
-rw-r--r-- 1 root root 3671 Apr 25 11:45 gitlab.xxx.cn.pem

7、刷新配置

當配置文件發生變化時,或者是第一次啓動時,我們需要刷新配置。
[root@localhost ~]# systemctl restart gitlab-runsvdir
[root@localhost ~]# gitlab-ctl reconfigure

8、啓動服務

[root@localhost ~]# gitlab-ctl restart
[root@localhost ~]# gitlab-ctl status
run: alertmanager: (pid 13541) 2171s; run: log: (pid 13221) 2192s
run: gitaly: (pid 13557) 2170s; run: log: (pid 12463) 2266s
run: gitlab-monitor: (pid 13580) 2169s; run: log: (pid 13103) 2208s
run: gitlab-workhorse: (pid 13602) 2169s; run: log: (pid 12887) 2226s
run: logrotate: (pid 13617) 2168s; run: log: (pid 12959) 2218s
run: nginx: (pid 13628) 2168s; run: log: (pid 12927) 2222s
run: node-exporter: (pid 13714) 2168s; run: log: (pid 13002) 2214s
run: postgres-exporter: (pid 13720) 2167s; run: log: (pid 13270) 2188s
run: postgresql: (pid 13740) 2167s; run: log: (pid 12669) 2258s
run: prometheus: (pid 13748) 2166s; run: log: (pid 13181) 2198s
run: redis: (pid 13761) 2166s; run: log: (pid 11907) 2293s
run: redis-exporter: (pid 13800) 2165s; run: log: (pid 13143) 2202s
run: sidekiq: (pid 13821) 2163s; run: log: (pid 12872) 2227s
run: unicorn: (pid 13833) 2162s; run: log: (pid 12832) 2233s

9、測試郵件發送

我們在啓動完成後測試一下郵件發送功能是否正常工作。

[root@localhost ~]# gitlab-rails console
irb(main):001:0> Notify.test_email('郵箱地址', '標題', '內容').deliver_now
irb(main):002:0> exit

10、第一次訪問登陸

本地hosts中加入域名解析gitlab.example.com,然後瀏覽器中輸入域名訪問,第一次需要輸入新的超級管理員(root)密碼。
修改成功後,我們使用超級管理員用戶“root”賬號登錄Gitlab管理平臺。
在這裏插入圖片描述

11、關閉用戶註冊功能

爲了避免用戶隨便註冊賬號,我們將註冊功能關閉。
在這裏插入圖片描述

11、設置語言爲"簡體中文"

在這裏插入圖片描述
保存後重啓登陸即可。

二、docker部署gitlab

1 環境描述
在這裏插入圖片描述

2 確保安裝順利,linux先關閉selinux服務,否則容器內部可能權限不足

vi /etc/selinux/config
-------------------------------
SELINUX=enforcing  #註釋掉
SELINUXTYPE=targeted #註釋掉
SELINUX=disabled #增加
:wq! #保存退出
-------------------------------
setenforce 0 #使配置立即生效

3 搜索和下載gitlab鏡像

#搜索鏡像
docker search gitlab
#下載鏡像
sudo docker pull gitlab/gitlab-ce:latest

4 創建docker中的網絡

docker network create gitlab_net

5 使用鏡像創建容器,並且使重要數據外部掛載到宿主機

docker run --name='gitlab' -d \
--net=gitlab_net \
--publish 443:443 --publish 80:80 \
--restart always \
--volume ~/docker/gitlab/config:/etc/gitlab \
--volume ~/docker/gitlab/logs:/var/log/gitlab \
--volume ~/docker/gitlab/data:/var/opt/gitlab \
--privileged=true \
gitlab/gitlab-ce:latest
## 查看容器是否運行起來
docker ps | grep gitlab

參數解析1.http端口使用 80
2.網絡使用 gitlab_net網絡
3.將容器內部 /etc/gitlab,/var/log/gitlab,/var/opt/gitlab - 掛載到宿主機的 /root/docker/gitlab/config,logs,data 下,防止容器被刪除數據丟失
4.privileged=true 使用特權,怕什麼地方權限不足,安裝不順
5./root/docker/gitlab下的config,logs,data沒有的話,創建容器會一併創建

6 修改配置文件中的訪問域名

vim ~/docker/gitlab/config/gitlab.rb
...
external_url 'http://gitlab.example.com/'
#用戶訪問所使用的URL,域名或者IP地址
...

7 打開瀏覽器看成效

瀏覽器輸入 http://gitlab.example.com/ 進行訪問,第一次登陸需要修改密碼 這樣子就安裝OK了,輸入賬號密碼進行註冊
在這裏插入圖片描述

三、常見問題

1、訪問瀏覽器被拒絕,不要慌 使用 docker logs gitlab 查看日誌,看報什麼錯,進行解決
2、訪問返回502,一般情況下是端口衝突
修改gitlab.rb文件,設置端口,重啓容器,稍等一會訪問

#編輯文件
vi /root/docker/gitlab/config/gitlab.rb
#找到 unicorn['port'] = 8080 的地方,修改爲不會被佔用的端口
unicorn['port'] = 8888
#保存
:wq!
#重啓容器 
docker restart gitlab

3、訪問比較緩慢
因爲鏡像就有一個多G,每次啓動容器,重啓,需要花一段時間等待。
4、訪問還是502
看看CPU佔用率,電腦容量,有些情況是因爲CPU、內存耗盡導致

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