搭建之前,需要安装相应的依赖包,并且要启动sshd服务
(1).安装policycoreutils-python openssh-server openssh-clients
[root@VM-0-2-centos ~]# sudo yum install -y curl policycoreutils-python openssh-server openssh-clients
[root@VM-0-2-centos ~]# sudo systemctl enable sshd
[root@VM-0-2-centos ~]# sudo systemctl start sshd
(2).在安装时可能会报错
在安装yum install -y curl policycoreutils-python openssh-server openssh-clients时可能会报如下错误:
Error: Unable to find a match: policycoreutils-python
产生这个错误的原因是未配置yum源,所以需要安装 EPEL 源,命令如下:
[root@VM-0-2-centos ~]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
然后运行
[root@VM-0-2-centos ~]# yum install -y curl policycoreutils-python3 openssh-server openssh-clients
(3).安装postfix
gitlab配置需要用到邮件发送,所以需要安装postfix
[root@VM-0-2-centos ~]# yum -y install postfix
#安装完之后,启动postfix
[root@VM-0-2-centos ~]# systemctl start postfix
启动过程中如果报错:Job for postfix.service failed because the control process exited with error code. See "systemctl status postfix.service" and "journalctl -xe" for details.
解决办法,vim打开/etc/postfix/main.cf, 修改如下两项,修改之前可以先备份:
#修改 /etc/postfix/main.cf的设置
inet_protocols = ipv4
inet_interfaces = all
修改完成后,再次启动,就不会报错了,设置postfix为开机自启动
[root@VM-0-2-centos ~]# systemctl enable postfix
查看启动状态
[root@VM-0-2-centos ~]# systemctl status postfix
2.gitlab安装
centos系统的下载地址: https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
找最新版去下载 gitlab-ce-15.8.3-ce.0.el7.x86_64.rpm
下载rpm包并安装
[root@VM-0-2-centos ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-15.8.3-ce.0.el7.x86_64.rpm
[root@VM-0-2-centos ~]# mkdir /usr/local/gitlab
[root@VM-0-2-centos ~]# mv gitlab-ce-15.8.3-ce.0.el7.x86_64.rpm /usr/local/gitlab/
[root@VM-0-2-centos ~]# cd /usr/local/gitlab/
[root@VM-0-2-centos ~]# rpm -i itlab-ce-15.8.3-ce.0.el7.x86_64.rpm
[root@VM-0-2-centos ~]# warning: itlab-ce-15.8.3-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
[root@VM-0-2-centos ~]# error: Failed dependencies:
policycoreutils-python is needed by itlab-ce-15.8.3-ce.0.el7.x86_64.rpm
# 如果出现上面这个报错就执行yum install policycoreutils-python
如果运行上面的yum install policycoreutils-python命令还是报错,解决办法:
可以在安装rpm包命令的后面加两个参数,如:
[root@VM-0-2-centos ~]# rpm -i itlab-ce-15.8.3-ce.0.el7.x86_64.rpm --nodeps --force
--force是强制安装
--nodeps是不依赖关系
加上那两个参数的意义就在于,
安装时不再分析包之间的依赖关系而直接安装,
也就不会再提示error: Failed dependencies:这样的错误了
上述 rpm -i itlab-ce-15.8.3-ce.0.el7.x86_64.rpm --nodeps --force命令成功后展示如下:
warning: gitlab-ce-10.0.0-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
It looks like GitLab has not been configured yet; skipping the upgrade script.
*. *.
*** ***
***** *****
.****** *******
******** ********
,,,,,,,,,***********,,,,,,,,,
,,,,,,,,,,,*********,,,,,,,,,,,
.,,,,,,,,,,,*******,,,,,,,,,,,,
,,,,,,,,,*****,,,,,,,,,.
,,,,,,,****,,,,,,
.,,,***,,,,
,*,.
_______ __ __ __
/ ____(_) /_/ / ____ _/ /_
/ / __/ / __/ / / __ `/ __ \
/ /_/ / / /_/ /___/ /_/ / /_/ /
\____/_/\__/_____/\__,_/_.___/
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
For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
安装完成之后,会出现gitlab官方文档地址https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
3.配置并启动gitlab-ce
gitlab安装完成后,需要设置一个访问地址(或域名),打开/etc/gitlab/gitlab.rb,将默认的external_url = 'http://git.example.com'修改为自己的IP地址:http://xxx.xx.xxx
[root@VM-0-2-centos ~]# vim /etc/gitlab/gitlab.rb
原来默认的external_url
## GitLab URL
##! URL on which GitLab will be reachable.
##! For more details on configuring external_url see:
##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
external_url 'http://git.example.com'
修改成自己定义的url地址,端口自己设置一个,别和已有的冲突了
## GitLab URL
##! URL on which GitLab will be reachable.
##! For more details on configuring external_url see:
##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
external_url 'http://152.26.xx.xx:8181'
修改完成后:wq保存退出,执行以下命令,让配置生效
[root@VM-0-2-centos ~]# gitlab-ctl reconfigure
启动Gitlab
[root@VM-0-2-centos ~]# gitlab-ctl start
[root@VM-0-2-centos ~]# gitlab-ctl start
ok: run: gitaly: (pid 6638) 186s
ok: run: gitlab-monitor: (pid 6656) 186s
ok: run: gitlab-workhorse: (pid 6659) 186s
ok: run: logrotate: (pid 6703) 185s
ok: run: nginx: (pid 6709) 185s
ok: run: node-exporter: (pid 6715) 184s
ok: run: postgres-exporter: (pid 6720) 184s
ok: run: postgresql: (pid 7324) 44s
ok: run: prometheus: (pid 6752) 171s
ok: run: redis: (pid 6761) 171s
ok: run: redis-exporter: (pid 6765) 170s
ok: run: sidekiq: (pid 7299) 45s
ok: run: unicorn: (pid 7476) 18s
启动完成后,在浏览器输入http://152.26.xx.xx:8181,就是gitlab的登录首页了
重启下服务,刷新页面就可以访问了
[root@VM-0-2-centos ~]# gitlab-ctl restart
为了避免8080端口冲突问题,可以修改下unicorn的默认端口,vim打开/etc/gitlab/gitlab.rb配置文件,新增一项unicorn['port'] = 8101
## GitLab URL
##! URL on which GitLab will be reachable.
##! For more details on configuring external_url see:
##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
external_url 'http://152.26.xx.xx:8181'
unicorn['port'] = 8101
修改完成后:wq保存退出,执行gitlab-ctl reconfigure命令,让配置生效,再重新启动服务
[root@VM-0-2-centos ~]# gitlab-ctl reconfigure
[root@VM-0-2-centos ~]# gitlab-ctl stop
[root@VM-0-2-centos ~]# gitlab-ctl start
gitlab常用命令
启动服务:gitlab-ctl start
查看状态:gitlab-ctl status
停掉服务:gitlab-ctl stop
重启服务:gitlab-ctl restart
让配置生效:gitlab-ctl reconfigure
当运行命令gitlab-ctl reconfigure时,如果报错:STDERR: initdb: error: invalid locale settings; check LANG and L...,这是默认字符集问题
There was an error running gitlab-ctl reconfigure:
execute[/opt/gitlab/embedded/bin/initdb -D /var/opt/gitlab/postgresql/data -E UTF8] (postgresql::enable line 49) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '1'
---- Begin output of /opt/gitlab/embedded/bin/initdb -D /var/opt/gitlab/postgresql/data -E UTF8 ----
STDOUT: The files belonging to this database system will be owned by user "gitlab-psql".
This user must also own the server process.
STDERR: initdb: error: invalid locale settings; check LANG and LC_* environment variables
---- End output of /opt/gitlab/embedded/bin/initdb -D /var/opt/gitlab/postgresql/data -E UTF8 ----
Ran /opt/gitlab/embedded/bin/initdb -D /var/opt/gitlab/postgresql/data -E UTF8 returned 1
解决办法
确保服务器内存在4G以上
修改配置
vim /etc/profile
# 将以下内容添加到配置文件末尾
export LC_CTYPE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# 重读环境变量
[root@localhost src]# source /etc/profile
# 重新加载
[root@localhost src]# gitlab-ctl reconfigure
第一次进入最好修改密码
gitlab初次安装后,登录gitlab网页的管理员账号和密码各是什么?
在安装完启动 gitlab 后,应该就可以在网页上访问到 gitlab 的登录界面了,不过 gitlab已事先创建了一个账号了,这个账号就是管理员账号,这个管理员账号的账号名为 root,而密码在一个自动生成的文件 /etc/gitlab/initial_root_password 中(密码不会含空格),且会在 24 小时后自动被删除,实际上,在第一次使用命令 gitlab-ctl reconfigure 初始化 gitlab 配置时,gitlab其实已经提示过这些信息
Notes:
Default admin account has been configured with following details:
Username: root
Password: You didn't opt-in to print initial root password to STDOUT.
Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours.
4.搭建GItLab以后出现卡顿现象
原因一
(1).原因分析
gitlab 启动运行占用了大量的内存,2G内存在启动后已经所剩无几,想再进行git 相关操作自然也就会出现卡顿现象.正常 centos 应该是启用 swap 分区的,但是阿里云,腾讯云的服务器却没有swap分区
通过以下命令查看swap分区情况:
cat /proc/swaps
(2).解决方法
1).创建 swap 分区(这里需要等待几秒)
dd if=/dev/zero of=/swap bs=512 count=8388616
注意:创建swap大小为bs*count=4294971392(4G)
2).通过mkswap命令将上面新建出的文件做成swap分区
mkswap /swap
3).查看内核参数vm.swappiness中的数值是否为0,如果为0则根据实际需要调整成60
# 查看参数
cat /proc/sys/vm/swappiness
# 设置参数
# sysctl -w vm.swappiness=60
4).启用 swap 分区
swapon /swap
echo “/swap swap swap defaults 0 0” >> /etc/fstab
5).再次使用cat /proc/swaps 查看swap分区是否启动
可以看到,swap分区已经启用,现在通过 gitlab 进行操作会发现很流畅
原因二
查看是否有进程在消耗内存,最耗内存的是哪一个 杀死进程或者优化后启动
是不是磁盘已经满了,塞不下了 ,然后在回收垃圾?
(1).查看是否有进程在消耗内存,最耗内存的是哪一个 杀死进程或者优化后启动
通过top -c 命令查看cpu消耗情况, -c:显示 完整命令:启动进程的命令语句
# top -C
root@zph-IdeaCentre-GeekPro-14IOB:/home/zph# top -c
top - 09:36:10 up 25 min, 1 user, load average: 1.06, 1.37, 1.19
任务: 499 total, 1 running, 497 sleeping, 0 stopped, 1 zombie
%Cpu(s): 3.0 us, 0.5 sy, 0.0 ni, 96.4 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 15783.1 total, 5079.5 free, 5819.9 used, 4883.7 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 9379.0 avail Mem
进程号 USER PR NI VIRT RES SHR %CPU %MEM TIME+ COMMAND
6076 zph 20 0 9103720 290768 131948 S 27.2 1.8 4:29.96 /opt/有道云笔记/ynote-desktop --type=renderer --no-sandbox --field-trial-handle=13907500021522840268,10117407446205598093,131+
5891 zph 20 0 4503324 167160 58796 S 5.3 1.0 0:12.30 /opt/有道云笔记/ynote-desktop /opt/有道云笔记/resources/app.asar/dist/server.js
4969 zph 20 0 16.6g 191040 168444 S 4.6 1.2 1:48.35 /opt/google/chrome/chrome --type=gpu-process --enable-crashpad --crashpad-handler-pid=4529 --enable-crash-reporter=12ee17c8-6041-4+
5408 zph 20 0 3669912 321492 79652 S 4.0 2.0 0:45.83 /usr/local/sunlogin/bin/sunloginclient
9713 zph 20 0 24.7g 269076 142628 S 3.3 1.7 0:32.19 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=4529 --enable-crash-reporter=12ee17c8-6041-4093+
5868 zph 20 0 300016 77896 58992 S 2.6 0.5 0:25.76 /opt/有道云笔记/ynote-desktop --type=gpu-process --field-trial-handle=13907500021522840268,10117407446205598093,131072 --enab+
4005 zph 20 0 7205848 520776 193396 S 2.0 3.2 7:13.90 /usr/bin/gnome-shell
9855 zph 20 0 24.5g 115028 78792 S 2.0 0.7 0:03.77 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=4529 --enable-crash-reporter=12ee17c8-6041-4093+
9895 zph 20 0 24.5g 129396 88480 S 2.0 0.8 0:04.54 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=4529 --enable-crash-reporter=12ee17c8-6041-4093+
877 root 20 0 613132 22012 19416 S 1.0 0.1 0:18.90 /usr/local/sunlogin/bin/sunloginclient -m service
3803 root 20 0 480332 268624 141112 S 1.0 1.7 1:08.16 /usr/lib/xorg/Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3
5150 zph 20 0 160.8g 342600 117848 S 1.0 2.1 7:01.54 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=4529 --enable-crash-reporter=12ee17c8-6041-4093+
4518 zph 20 0 413220 39464 28612 S 0.7 0.2 0:03.56 /opt/sogoupinyin/files/bin/sogoupinyinService-watchdog
4668 zph 20 0 24.6g 181684 104888 S 0.7 1.1 1:33.21 /opt/google/chrome/chrome --type=renderer --enable-crashpad --crashpad-handler-pid=4529 --enable-crash-reporter=12ee17c8-6041-4093+
5191 zph 20 0 9892.6m 1.4g 73460 S 0.7 8.9 3:20.14 /opt/PhpStorm-203.7717.64/jbr/bin/java -classpath /opt/PhpStorm-203.7717.64/lib/bootstrap.jar:/opt/PhpStorm-203.7717.64/lib/extens+
11957 root 20 0 15764 4744 3600 R 0.7 0.0 0:00.05 top -c
172 root 20 0 0 0 0 I 0.3 0.0 0:02.70 [kworker/u32:1-phy0]
303 root 20 0 0 0 0 D 0.3 0.0 0:04.97 [kworker/u32:6+phy0]
314 root 20 0 0 0 0 I 0.3 0.0 0:00.12 [kworker/2:2-events]
816 root 20 0 87260 6264 5788 S 0.3 0.0 0:03.41 /usr/local/sunlogin/bin/oray_rundaemon -m server
908 root 20 0 2305604 47128 29132 S 0.3 0.3 0:02.76 /usr/bin/containerd
使用kill -9 杀死进程
(2).查看磁盘是否已经满了,释放磁盘空间
#查看磁盘使用情况
df -h
root@zph-IdeaCentre-GeekPro-14IOB:/home/zph# df -h
文件系统 容量 已用 可用 已用% 挂载点
udev 7.7G 0 7.7G 0% /dev
tmpfs 1.6G 2.7M 1.6G 1% /run
/dev/nvme0n1p5 92G 67G 20G 78% /
tmpfs 7.8G 356M 7.4G 5% /dev/shm
发现磁盘/dev/nvme0n1p5 92G 67G 20G 78% /,清除一些没用的垃圾,或者给服务器扩容
再次使用top查看cpu使用情况,发现磁盘空间占用下降,刷新gitlab页码,速度流畅
原因三
(1).原因分析
cpu占用不高,但是内存占用高,也许是gitlab配置原因
(2).解决办法
修改gitlab配置,具体根据自己服务器情况酌情修改,案例如下:
# vim /etc/gitlab/gitlab.rb
$ vim /etc/gitlab/gitlab.rb
gitlab_rails['time_zone'] = 'Asia/Shanghai'
#减少进程数,默认是被注释掉的,官方建议该值是CPU核心数加一,可以提高服务器的响应速度,如果内存只有4G,或者服务器上有其它业务,就不要改了,以免内存不足,另外,这个参数最小值是2,设为1,服务器可能会卡死
unicorn['worker_processes'] = 2
#修改内存占用
unicorn['worker_memory_limit_min'] = "100 * 1 << 20"
unicorn['worker_memory_limit_max'] = "250 * 1 << 20"
#减少sidekiq并发数,sidekiq这两个参数都改成1
sidekiq['max-concurrency'] = 1
#减少sidekiq并发
sidekiq['min-concurrency'] = 1
#减少数据库缓存(默认为256MB 改为128MB)
postgresql['shared_buffers'] = "128MB"
#减少数据库并发数(默认为8 改为4)
postgresql['max_worker_processes'] = 4
#禁用prometheus_monitoring 网络监控
prometheus_monitoring['enable'] = false
修改完后一定要重启服务
gitlab-ctl reconfigure
gitlab-ctl restart
之后查看内存情况
root@zph-IdeaCentre-GeekPro-14IOB:/home/zph# free -h
总计 已用 空闲 共享 缓冲/缓存 可用
内存: 15Gi 5.5Gi 5.2Gi 323Mi 4.8Gi 9.4Gi
交换: 2.0Gi 0B 2.0Gi
设置后内存占用还是比较多,建议创建swap虚拟内存