1:Gitlab 部署

Gitlab 实现的是 CI/CD 环节中的 CI 环节,即持续集成(Continuous Integration)。
“持续”意味着代码的频繁提交,能将各版本的代码分阶段保存;
“集成”一方面意味着支持多名开发人员协同,将多名开发人员的开发的不同功能的代码进行合并,实现整体功能;另一方面意味着将通过测试的分支合并到主分支,完成软件的稳定版本的迭代。

1.1:下载 gitlab-ce 程序包

一般安装的是Gitlab-ce,即社区版本,采用程序包的方式进行安装,下载地址为:

  • Gitlab官方:https://packages.gitlab.com/gitlab/gitlab-ce
  • 清华大学镜像站:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/

选择对应的gitlab-ce版本、Linux发行版以及系统版本进行下载。

gitlab 集群 gitlab集成_gitlab

这里使用的系统是Ubuntu1804,即Ubuntu bionic;gitlab-ce版本为11.11.8。

gitlab 集群 gitlab集成_git_02

1.2:安装 gitlab-ce

将下载的gitlab-ce的deb程序包上传至服务器,进行安装。

安装 gitlab-ce-11.11.8:

root@gitlab:/usr/local/src# dpkg -i gitlab-ce_11.11.8-ce.0_amd64.deb

安装完成:

gitlab 集群 gitlab集成_gitlab_03

1.3:配置 gitlab-ce

1.3.1:编辑配置文件

gitlab的配置文件为/etc/gitlab/gitlab.rb,配置gitlab的访问地址,以及邮件配置。

root@gitlab:~# grep '^[a-Z]' /etc/gitlab/gitlab.rb
external_url 'http://192.168.1.121'						#Gitlab URL,即Gitlab能够被访问到的地址
gitlab_rails['smtp_enable'] = true						#打开smtp邮件发送
gitlab_rails['smtp_address'] = ""			#Gitlab调用的smtp服务器地址(这里使用QQ邮箱作为Gitlab的邮件发送服务器)
gitlab_rails['smtp_port'] = 465							#smtp服务器的端口(使用tls时,QQ邮箱的smtp端口为465,否则为25)
gitlab_rails['smtp_user_name'] = "20251839@"		#Gitlab调用smtp服务器时所使用的用户名
gitlab_rails['smtp_password'] = "hwefsagwdgqcbdf"		#Gitlab调用smtp服务器时的认证密码(这里是QQ邮箱进行第三方登录时的授权码,具体获取方式在zabbix邮件通知的文档中)
gitlab_rails['smtp_domain'] = ""					#smtp服务器的域名
gitlab_rails['smtp_authentication'] = :login			#smtp服务器的认证方式(这里是login)
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true							#使用tls协议(较为安全)
gitlab_rails['gitlab_email_from'] = "20251839@"	#Gitlab发送邮件时的发件人
user["git_user_email"] = "20251839@"				#Git用户邮箱地址

1.3.2:执行 reconfigure

在每次更改gitlab配置后,都要执行reconfigure,以使配置生效并启动/重启gitlab。

root@gitlab:~# gitlab-ctl reconfigure

reconfigure完成,目前gitlab已启动。

gitlab 集群 gitlab集成_git_04

1.4:验证

1.4.1:验证 gitlab 启动

查看gitlab进程:

root@gitlab:~# ps -ef | grep gitlab

gitlab-ctl status可以看到gitlab各组件的进程启动情况:

root@gitlab:~# gitlab-ctl status
run: alertmanager: (pid 24285) 881s; run: log: (pid 20797) 2200s
run: gitaly: (pid 24299) 880s; run: log: (pid 20007) 2301s
run: gitlab-monitor: (pid 24324) 880s; run: log: (pid 20671) 2218s
run: gitlab-workhorse: (pid 24338) 879s; run: log: (pid 20436) 2244s
run: logrotate: (pid 24356) 879s; run: log: (pid 20501) 2232s
run: nginx: (pid 24362) 878s; run: log: (pid 20468) 2238s
run: node-exporter: (pid 24447) 878s; run: log: (pid 20600) 2225s
run: postgres-exporter: (pid 24455) 877s; run: log: (pid 20862) 2194s
run: postgresql: (pid 24467) 877s; run: log: (pid 20171) 2291s
run: prometheus: (pid 24475) 877s; run: log: (pid 20756) 2206s
run: redis: (pid 24490) 876s; run: log: (pid 19882) 2308s
run: redis-exporter: (pid 24494) 876s; run: log: (pid 20711) 2212s
run: sidekiq: (pid 24504) 874s; run: log: (pid 20403) 2250s
run: unicorn: (pid 24516) 873s; run: log: (pid 20376) 2256s

Gitlab启动时会绑定80端口进行监听:

root@gitlab:~# lsof -i:80
COMMAND   PID       USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   24362       root    7u  IPv4 102076      0t0  TCP *:http (LISTEN)
nginx   24363 gitlab-www    7u  IPv4 102076      0t0  TCP *:http (LISTEN)
nginx   24364 gitlab-www    7u  IPv4 102076      0t0  TCP *:http (LISTEN)
nginx   24365 gitlab-www    7u  IPv4 102076      0t0  TCP *:http (LISTEN)
nginx   24366 gitlab-www    7u  IPv4 102076      0t0  TCP *:http (LISTEN)

1.4.2:访问 Gitlab URL

访问Gitlab URL(http://192.168.1.121/)

gitlab 集群 gitlab集成_ci/cd_05

1.4.3:登录 Gitlab

首次登录需要设置root用户的密码(至少8位)。

gitlab 集群 gitlab集成_nginx_06

密码设置完成后,使用root用户登录:

gitlab 集群 gitlab集成_gitlab_07

gitlab 集群 gitlab集成_git_08

1.5:Gitlab 的相关目录

1.5.1:安装目录

root@gitlab:~# ll /opt/gitlab/
total 3028
drwxr-xr-x 10 root root    4096 Apr 23 09:24 ./
drwxr-xr-x  3 root root    4096 Apr 23 09:04 ../
drwxr-xr-x  2 root root    4096 Apr 23 09:05 bin/
-rw-r--r--  1 root root  164357 Aug 10  2019 dependency_licenses.json
drwxr-xr-x 18 root root    4096 Apr 23 09:23 embedded/
drwxr-xr-x 10 root root    4096 Apr 23 09:25 etc/
drwxr-xr-x  2 root root    4096 Apr 23 09:25 init/
-rw-r--r--  1 root root 2809938 Aug 10  2019 LICENSE
drwxr-xr-x  2 root root   36864 Apr 23 09:05 LICENSES/
drwxr-xr-x  2 root root    4096 Apr 23 09:25 service/
drwxr-xr-x 16 root root    4096 Apr 23 09:25 sv/
drwxr-xr-x  3 root root    4096 Apr 23 09:24 var/
-rw-r--r--  1 root root   26944 Aug 10  2019 version-manifest.json
-rw-r--r--  1 root root   12451 Aug 10  2019 version-manifest.txt

1.5.2:配置目录

root@gitlab:~# tree /etc/gitlab/
/etc/gitlab/
├── gitlab.rb
├── gitlab-secrets.json
└── trusted-certs

1.5.3:运行目录

root@gitlab:~# tree /run/gitlab/
/run/gitlab/
├── sidekiq
│   ├── counter_process_id_20382-0.db
│   └── gauge_livesum_process_id_20382-0.db
└── unicorn
    ├── counter_process_id_20362-0.db
    ├── counter_worker_id_0-0.db
    ├── counter_worker_id_1-0.db
    ├── counter_worker_id_2-0.db
    ├── counter_worker_id_3-0.db
    ├── counter_worker_id_4-0.db
    ├── gauge_livesum_worker_id_0-0.db
    ├── gauge_livesum_worker_id_1-0.db
    ├── gauge_livesum_worker_id_2-0.db
    ├── gauge_livesum_worker_id_3-0.db
    ├── gauge_livesum_worker_id_4-0.db
    ├── gauge_max_worker_id_0-0.db
    ├── gauge_max_worker_id_1-0.db
    ├── gauge_max_worker_id_2-0.db
    ├── gauge_max_worker_id_3-0.db
    ├── gauge_max_worker_id_4-0.db
    ├── histogram_process_id_20362-0.db
    ├── histogram_worker_id_0-0.db
    ├── histogram_worker_id_1-0.db
    ├── histogram_worker_id_2-0.db
    ├── histogram_worker_id_3-0.db
    └── histogram_worker_id_4-0.db

1.5.4:数据目录(重要)

数据目录是Gitlab存放代码的目录,是最重要的目录。

root@gitlab:~# ll /var/opt/gitlab/ 
total 96
drwxr-xr-x 20 root              root       4096 Apr 23 09:25 ./
drwxr-xr-x  3 root              root       4096 Apr 23 09:23 ../
drwxr-x---  3 gitlab-prometheus root       4096 Apr 23 09:25 alertmanager/
drwx------  2 git               root       4096 Apr 23 09:23 backups/
-rw-------  1 root              root         38 Apr 23 09:24 bootstrapped
drwxr-xr-x  2 git               git        4096 Apr 23 09:23 .bundle/
drwx------  2 git               root       4096 Apr 23 09:25 gitaly/
-rw-r--r--  1 git               git         290 Apr 23 09:23 .gitconfig
drwx------  3 git               root       4096 Apr 23 09:23 git-data/
drwxr-xr-x  3 git               root       4096 Apr 23 09:23 gitlab-ci/
drwxr-xr-x  2 git               root       4096 Apr 23 09:24 gitlab-monitor/
drwxr-xr-x  9 git               root       4096 Apr 23 09:24 gitlab-rails/
drwx------  2 git               root       4096 Apr 23 09:23 gitlab-shell/
drwxr-x---  2 git               gitlab-www 4096 Apr 23 09:25 gitlab-workhorse/
drwx------  3 root              root       4096 Apr 23 09:24 logrotate/
drwxr-x---  9 root              gitlab-www 4096 Apr 23 09:24 nginx/
drwxr-xr-x  3 root              root       4096 Apr 23 09:24 node-exporter/
drwx------  2 gitlab-psql       root       4096 Apr 23 09:25 postgres-exporter/
drwxr-xr-x  3 gitlab-psql       root       4096 Apr 23 09:23 postgresql/
drwxr-x---  4 gitlab-prometheus root       4096 Apr 23 09:24 prometheus/
-rw-r--r--  1 root              root        226 Apr 23 09:25 public_attributes.json
drwxr-x---  2 gitlab-redis      git        4096 Apr 23 09:28 redis/
drwx------  2 git               git        4096 Apr 23 09:23 .ssh/
-rw-r--r--  1 root              root         40 Apr 23 09:23 trusted-certs-directory-hash

1.5.5:日志目录

root@gitlab:~# ll /var/log/gitlab
total 76
drwxr-xr-x 19 git               root       4096 Apr 23 09:25 ./
drwxrwxr-x  9 root              syslog     4096 Apr 23 09:04 ../
drwx------  2 gitlab-prometheus root       4096 Apr 23 09:25 alertmanager/
drwx------  2 git               root       4096 Apr 23 09:23 gitaly/
drwx------  2 git               root       4096 Apr 23 09:24 gitlab-monitor/
drwx------  2 git               root       4096 Apr 23 09:24 gitlab-rails/
drwx------  2 git               root       4096 Apr 23 09:23 gitlab-shell/
drwx------  2 git               root       4096 Apr 23 09:24 gitlab-workhorse/
drwx------  2 root              root       4096 Apr 23 09:24 logrotate/
drwxr-x---  2 root              gitlab-www 4096 Apr 23 09:24 nginx/
drwx------  2 gitlab-prometheus root       4096 Apr 23 09:24 node-exporter/
drwx------  2 gitlab-psql       root       4096 Apr 23 09:25 postgres-exporter/
drwx------  2 gitlab-psql       root       4096 Apr 23 09:23 postgresql/
drwx------  2 gitlab-prometheus root       4096 Apr 23 09:25 prometheus/
drwxr-xr-x  2 root              root       4096 Apr 23 09:22 reconfigure/
drwx------  2 gitlab-redis      root       4096 Apr 23 09:23 redis/
drwx------  2 gitlab-redis      root       4096 Apr 23 09:24 redis-exporter/
drwx------  2 git               root       4096 Apr 23 09:24 sidekiq/
drwx------  2 git               root       4096 Apr 23 09:24 unicorn/

打开http://192.168.1.121

第一次要设密码:

账号是root;密码设成了12345678

gitlab 集群 gitlab集成_nginx_09

gitlab 集群 gitlab集成_gitlab 集群_10

1.6:使用前的初始配置

1.6.1:关闭用户注册功能

Gitlab默认开启了用户帐号注册功能,用户可以自行注册帐号,为用户管理带来不便,在不对外开放的Gitlab中,需要关闭此功能。

gitlab 集群 gitlab集成_git_11

使用root用户登录Gitlab,点击上方的“扳手”图标,进入管理界面,然后点击settings:

gitlab 集群 gitlab集成_nginx_12

找到 Sign-up restrictions(注册限制),将 Sign-up enabled 的勾选去掉,关闭用户注册功能,最后点击保存。

gitlab 集群 gitlab集成_nginx_13

此时Gitlab登录界面就只有登录,没有注册了。

gitlab 集群 gitlab集成_gitlab 集群_14

1.6.2:更改 root 用户的邮箱

将Gitlab的root用户绑定的邮箱更改为Gitlab的服务邮箱(或其它可达邮箱),默认的admin@example.com无法接收Gitlab发送的邮件通知,只会在Gitlab邮件服务配置的邮箱中收到退信:

gitlab 集群 gitlab集成_ci/cd_15

root用户登录后,在Dashboard界面的Users栏中,点击 Users,进入用户列表:

gitlab 集群 gitlab集成_gitlab 集群_16

点击admin用户对应的编辑按钮,进入编辑页面:

gitlab 集群 gitlab集成_gitlab 集群_17

将邮箱地址设为一个可达的邮箱:

这里直接设置为Gitlab的服务邮箱,一般设为Gitlab的管理员工的邮箱,或固定的Gitlab管理员邮箱。

gitlab 集群 gitlab集成_git_18

这样,Gitlab系统发送给root用户的邮件通知就可以接收到了。