GitLab → 搭建中常遇的问题与日常维护

本文围绕GitLab展开,介绍了其服务命令,如启动、停止、重启等。阐述搭建中常见问题,像安装卡住、502错误的原因及解决办法。还提及日常维护,包括重置管理员和其他用户密码,偏好设置里的中文设置,以及批量添加账号的方法和注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gitlab服务命令:

启动所有 gitlab 组件;sudo gitlab-ctl start
停止所有 gitlab 组件;sudo gitlab-ctl stop
重启所有 gitlab 组件;sudo gitlab-ctl restart
查看服务状态;sudo gitlab-ctl status
启动服务(再次加载配置);sudo gitlab-ctl reconfigure
修改默认的配置文件;sudo vim /etc/gitlab/gitlab.rb
检查gitlab;gitlab-rake gitlab:check SANITIZE=true --trace
查看日志;sudo gitlab-ctl tail

搭建中遇到的问题

安装卡住

这基本是内存不足导致

yi开始比较任性,没按官方的推荐(内存最少 4G)来,弄了个 2G 内存,一顿操作猛如虎,定睛一看原地杵,特么的竟然像泰山一样稳,进度在那一动不动

直接卡住是比较烦的,因为不能直观地看出是否真的是卡住了,还是在安装中,只能凭感觉、经验来判断;如果出现如下类似错误信息

* execute[clear the gitlab-rails cache] action run
================================================================================
Error executing action `run` on resource 'execute[clear the gitlab-rails cache]'
================================================================================

这也是内存不足所致

502

当我们成功安装、配置 GitLab 后,不一定就万事大吉了,访问 GitLab 的时候可能会出现如下错误
在这里插入图片描述
原因1:GitLab 所需内存最低为 4G,若服务器配置太低会出现 502 错误
原因2:端口冲突8080端口被tomcat占用,场景2:未安装tomcat,ss -ntlp查看nginx监听80端口但还是不能访问尝试变更端口:
修改下unicorn的默认端口,vim打开/etc/gitlab/gitlab.rb配置文件

external_url 'http://gitlab.xgclassroom.com:8090'
 unicorn['port'] = 8090

修改完成后:wq保存退出,执行gitlab -ctl reconfigure命令,让配置生效,再重新启动服务

gitlab-ctl reconfigure
gitlab-ctl stop
gitlab-ctl start

再次访问gitlab就可以了,
原因3:端口冲突80端口niginx未开启代理(问题2解决后是直接访问的gitlab,现在启动nginx进行代理,隐藏gitlab端口,更新的配置文档如下:)

################################################################################
## GitLab NGINX
##! Docs: https://docs.gitlab.com/omnibus/settings/nginx.html
################################################################################

nginx['enable'] = true
nginx['client_max_body_size'] = '250m'

nginx['listen_port'] = 80

修改完成后:wq保存退出,执行gitlab -ctl reconfigure命令,让配置生效,再重新启动服务

gitlab-ctl reconfigure
gitlab-ctl stop
gitlab-ctl start
GitLab 新装或重启后,需要等待 1 分钟才能使用

对于单核 CPU 的服务器,Unicorn and Sidekiq 服务启动最少需要一分钟
    
    这个问题楼主没有遇到,因为开始的时候,楼主就给虚拟机分了 4 核 CPU

那么说白了,只要我们的虚拟机(或真实机器)环境足够干净,硬件资源(内存和 CPU)足够好,就不太会出现如上的错误

日常维护

重置管理员的密码

由于 root 账户用的很少,所以我们容易忘记它的密码,但不代表它不重要,类似 linux 的 root 账户;一旦我们忘记了 root 账号的密码,我们需要知道重置的方法,方法如下

1、进入 GitLab 控制台

[root@localhost ~]# gitlab-rails console -e production

GitLab 版本不同,命令会有所不同(网上说的而基本都是gitlab-rails console production ),推荐大家直接上 GitLab 官网去找对应版本的命令
我测是使用gitlab-rails console production是进不去GitLab 控制台的

^[[A[git@bogon ~]$ gitlab-rails console production
Traceback (most recent call last):
	8: from bin/rails:4:in `<main>'
	7: from bin/rails:4:in `require'
	6: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/commands.rb:18:in `<top (required)>'
	5: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/command.rb:46:in `invoke'
	4: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/command/base.rb:69:in `perform'
	3: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
	2: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
	1: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor/command.rb:27:in `run'
/opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/commands/console/console_command.rb:95:in `perform': wrong number of arguments (given 1, expected 0) (ArgumentError)
	9: from bin/rails:4:in `<main>'
	8: from bin/rails:4:in `require'
	7: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/commands.rb:18:in `<top (required)>'
	6: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/command.rb:46:in `invoke'
	5: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/railties-6.0.3.3/lib/rails/command/base.rb:69:in `perform'
	4: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor.rb:387:in `dispatch'
	3: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor/invocation.rb:126:in `invoke_command'
	2: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor/command.rb:20:in `run'
	1: from /opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor/command.rb:34:in `rescue in run'
/opt/gitlab/embedded/lib/ruby/gems/2.7.0/gems/thor-0.20.3/lib/thor/base.rb:506:in `handle_argument_error': ERROR: "rails console" was called with arguments ["production"] (Thor::InvocationError)
Usage: "rails console [options]"

当出现如下信息表示我们成功登入控制台

[root@localhost ~]# gitlab-rails console -e production
--------------------------------------------------------------------------------
 GitLab:       12.8.7 (b679f55a199) FOSS
 GitLab Shell: 11.0.0
 PostgreSQL:   10.12
--------------------------------------------------------------------------------
Loading production environment (Rails 6.0.2)
irb(main)

执行命令: user = User.where(id: 1).first,此 user 则表示 root 用户

3、修改密码

执行命令:user.password = 'secret_pass'修改密码, user.password_confirmation = 'secret_pass' 确认密码

4、保存密码

执行命令: user.save!

5、退出控制台

执行命令: exit

完整过程类似如下:
在这里插入图片描述

官方示例:How to reset your root password

重置成功后,我们就可以用新密码来登录 root 账号了

重制其他用户密码:

1.登陆控制台

root@gitlab:/# gitlab-rails console -e production
Loading production environment (Rails 4.2.10)

2.通过邮箱找到用户

irb(main):003:0> user=User.where(email:'jenkins@domian.com').first
=> #<User id:12 @jenkins>

3.重置密码并保存

irb(main):005:0> user.password=12345678
=> 12345678
irb(main):006:0> user.password_confirmation=12345678
=> 12345678
irb(main):007:0> user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: 53d8c7ea-c523-43a1-a5e6-032c836f4870) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", gid://gitlab/User/22
=> true

完整过程类似如下:
在这里插入图片描述

偏好设置

设置中文

GitLab 提供了很多的设置,楼主这里偷个懒,只教大家如何设置语言(虽然楼主知道大家的英语都很棒,但我相信大家的中文更棒),当语言变成我们最擅长的中文时,相信大家都能很好的进行偏好设置了;语言设置过程如下
在这里插入图片描述

看着熟悉的中文,倍感亲切,但是要注意: 此功能当初处于实验阶段,翻译尚未全部完成 ,也就是说语言功能尚未全部完成

批量添加账号

我们用 SVN 或 Git 的时候,账号往往不是自己注册的,而是统一分配的;而我们搭建的又是私服,不对外提供服务,所以无需注册功能,那么我们用 root 账号来关闭注册功能,很简单,流程如下
在这里插入图片描述
取消 Sign-up enabled前的复选框勾选,保存就好了

退出登录后可以发现,登录页的注册功能没了,既然不能注册了,那么就需要通过 root 用户来添加账号了

添加单个账号的话,可以直接通过管理中心来添加,具体如下图
在这里插入图片描述
单个账号的添加还可以通过 api 来实现,具体可查看:Users API

单个用户新增:(待测试)

如果账号少的话,通过单个添加的方式来添加是可以的,如果账号特别多的话,那就有点费时了,所以需要通过脚本的方式来批量添加账号了,具体我们往下看

1、创建账户文本

/opt目录下创建account_info.txt

12345678 test1@qq.com test1 测试账号1
12345678 test2@qq.com test2 测试账号2
12345678 1597031275@qq.com test3 测试账号3

4 列分别代表:登录密码  邮箱  用户名  别名

一行代表一个账户

2、获取 root 用户的 private_token

获取方式比较简单,如下图所示
在这里插入图片描述

如果访问令牌已经存在,则不需要新建;示例中的令牌:cwUxu3-2kiamVyKJQpW5

3、创建执行脚本

/opt目录下创建 batch_add_account.sh

#!/bin/bash
#gitlab用户文本
accountinfo="account_info.txt"
while read line 
do
    password=`echo $line | awk '{print $1}'`
    mail=`echo $line | awk '{print $2}'`
    username=`echo $line | awk '{print $3}'`
    name=`echo $line | awk '{print $4}'`
    curl -d "password=$password&email=$mail&username=$username&name=$name&private_token=cwUxu3-2kiamVyKJQpW5" "http://192.168.0.115/api/v4/users"
done < $accountinfo

private_token 的值就是上面 root 的访问令牌

4、执行脚本

赋予 batch_add_account.sh 可执行权限: [root@localhost opt]# chmod +x /opt/batch_add_account.sh

再执行 batch_add_account.sh:[root@localhost opt]# sh -x /opt/batch_add_account.sh

然后我们就可以在用户列表界面看到我们刚刚添加的账户了

在这里插入图片描述

批量创建用户,还是通过 Users API 来完成的,也会是说底层还是一个一个添加的

5、新账户登录

==我们用账号:测试账户3 登录下,发现登录不了,提示: You have to confirm your email address before continuing.(待测试) ==

提示需要邮箱验证,登录下邮箱,还真有一封验证邮件

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值