- 收集系统信息
- 显示许可证信息
- 检查配置
- 重建
authorized_keys
文件 - 清除 Redis 缓存
- 预编译 assets
- 检查与远程站点的 TCP 连接
- 清除 exclusive lease (危险)
- 显示数据库迁移的状态
- 运行不完整的数据库迁移
- 重建数据库索引
- Dump 数据库模式
- 检查数据库是否存在模式不一致的情况
- 故障排产
维护 Rake 任务
极狐GitLab 提供用于一般维护的 Rake 任务。
收集系统信息
此命令收集有关您的 GitLab 安装及其运行的系统的信息。这些在寻求帮助或报告问题时可能很有用。
-
Linux 软件包安装:
sudo gitlab-rake gitlab:env:info
-
自编译安装:
bundle exec rake gitlab:env:info RAILS_ENV=production
示例输出:
System information
System: Ubuntu 20.04
Proxy: no
Current User: git
Using RVM: no
Ruby Version: 2.6.6p146
Gem Version: 2.7.10
Bundler Version:1.17.3
Rake Version: 12.3.3
Redis Version: 5.0.9
Git Version: 2.27.0
Sidekiq Version:5.2.9
Go Version: unknown
GitLab information
Version: 13.2.2-ee
Revision: 618883a1f9d
Directory: /opt/gitlab/embedded/service/gitlab-rails
DB Adapter: PostgreSQL
DB Version: 11.7
URL: http://gitlab.example.com
HTTP Clone URL: http://gitlab.example.com/some-group/some-project.git
SSH Clone URL: git@gitlab.example.com:some-group/some-project.git
Elasticsearch: no
Geo: no
Using LDAP: no
Using Omniauth: yes
Omniauth Providers:
GitLab Shell
Version: 13.3.0
Repository storage paths:
- default: /var/opt/gitlab/git-data/repositories
GitLab Shell path: /opt/gitlab/embedded/service/gitlab-shell
显示许可证信息
此命令显示有关您的许可证以及使用的席位数量的信息。适用于极狐GitLab 安装实例。
这些在向支持部门提供证明或以编程方式检查许可证参数时可能很有用。
-
Linux 软件包安装:
sudo gitlab-rake gitlab:license:info
-
自编译安装:
bundle exec rake gitlab:license:info RAILS_ENV=production
示例输出:
Today's Date: 2020-02-29
Current User Count: 30
Max Historical Count: 30
Max Users in License: 40
License valid from: 2019-11-29 to 2020-11-28
Email associated with license: user@example.com
检查配置
gitlab:check
Rake 任务运行以下 Rake 任务:
gitlab:gitlab_shell:check
gitlab:gitaly:check
gitlab:sidekiq:check
gitlab:incoming_email:check
gitlab:ldap:check
gitlab:app:check
-
gitlab:geo:check
(仅在 Geo环境下)
它检查每个组件是否根据安装指南进行设置,并针对发现的问题提出修复建议。此命令必须从您的应用程序服务器运行,并且在 Gitaly 等组件服务器上无法正常运行。
您还可以查看我们的故障排查指南:
此外,您还应该验证数据库值可以使用当前 secrets 解密。
要运行 gitlab:check
,运行以下命令:
-
Linux 软件包安装:
sudo gitlab-rake gitlab:check
-
自编译安装:
bundle exec rake gitlab:check RAILS_ENV=production
如果要从输出中省略项目名称,请对 gitlab:check
使用 SANITIZE=true
。
示例输出:
Checking Environment ...
Git configured for git user? ... yes
Has python2? ... yes
python2 is supported version? ... yes
Checking Environment ... Finished
Checking GitLab Shell ...
GitLab Shell version? ... OK (1.2.0)
Repo base directory exists? ... yes
Repo base directory is a symlink? ... no
Repo base owned by git:git? ... yes
Repo base access is drwxrws---? ... yes
post-receive hook up-to-date? ... yes
post-receive hooks in repos are links: ... yes
Checking GitLab Shell ... Finished
Checking Sidekiq ...
Running? ... yes
Checking Sidekiq ... Finished
Checking GitLab ...
Database config exists? ... yes
Database is SQLite ... no
All migrations up? ... yes
GitLab config exists? ... yes
GitLab config outdated? ... no
Log directory writable? ... yes
Tmp directory writable? ... yes
Init script exists? ... yes
Init script up-to-date? ... yes
Redis version >= 2.0.0? ... yes
Checking GitLab ... Finished
重建 authorized_keys
文件
在某些情况下,有必要重建 authorized_keys
文件,例如,如果在升级后您在通过 SSH 推送时收到 Permission denied (publickey)
,和在 gitlab-shell.log
文件中查找 404 Key Not Found
错误。要重建 authorized_keys
,请运行:
-
Linux 软件包安装:
sudo gitlab-rake gitlab:shell:setup
-
自编译安装:
cd /home/git/gitlab sudo -u git -H bundle exec rake gitlab:shell:setup RAILS_ENV=production
示例输出:
This will rebuild an authorized_keys file.
You will lose any data stored in authorized_keys file.
Do you want to continue (yes/no)? yes
清除 Redis 缓存
如果由于某种原因仪表盘显示错误信息,您可能需要清除 Redis 的缓存。 为此,请运行:
-
Linux 软件包安装:
sudo gitlab-rake cache:clear
-
自编译安装:
cd /home/git/gitlab sudo -u git -H bundle exec rake cache:clear RAILS_ENV=production
预编译 assets
有时在版本升级期间,您可能最终会出现一些错误的 CSS 或丢失一些图标。在这种情况下,请尝试再次预编译 assets。
这仅适用于源安装,不适用于 Linux 软件包。
-
自编译安装
cd /home/git/gitlab sudo -u git -H bundle exec rake gitlab:assets:compile RAILS_ENV=production
对于 Linux 软件包版本,未优化的 assets(JavaScript、CSS)在上游 GitLab 发布时被冻结。Linux 软件包版本包括这些 assets 的优化版本。除非您在安装软件包后在生产机器上修改 JavaScript / CSS 代码,否则应该没有理由在生产机器上重做 rake gitlab:assets:compile
。如果您怀疑 assets 已损坏,则应重新安装 Linux 软件包。
检查与远程站点的 TCP 连接
有时您需要知道您的极狐GitLab 安装是否可以连接到另一台机器(例如 PostgreSQL 或 Web 服务器)上的 TCP 服务,以便对代理问题进行故障排除。包含一个 Rake 任务来帮助您完成此任务。
-
Linux 软件包安装:
sudo gitlab-rake gitlab:tcp_check[example.com,80]
-
自编译安装:
cd /home/git/gitlab sudo -u git -H bundle exec rake gitlab:tcp_check[example.com,80] RAILS_ENV=production
清除 exclusive lease (危险)
极狐GitLab 使用共享锁机制 ExclusiveLease
来防止共享资源中的同时操作。一个例子是在仓库上运行定期垃圾收集。
在非常特殊的情况下,被 Exclusive Lease 锁定的操作可能会在不释放锁的情况下失败。如果您等不及它过期,您可以运行此任务手动清除它。
要清楚所有 exclusive leases:
sudo gitlab-rake gitlab:exclusive_lease:clear
要指定 lease type
或 lease type + id
,请指定范围:
# to clear all leases for repository garbage collection:
sudo gitlab-rake gitlab:exclusive_lease:clear[project_housekeeping:*]
# to clear a lease for repository garbage collection in a specific project: (id=4)
sudo gitlab-rake gitlab:exclusive_lease:clear[project_housekeeping:4]
显示数据库迁移的状态
要检查特定迁移的状态,您可以使用以下 Rake 任务:
sudo gitlab-rake db:migrate:status
要检查 Geo 次要站点上的跟踪数据库,您可以使用以下 Rake 任务:
sudo gitlab-rake db:migrate:status:geo
这会为每个迁移 ID 输出一个 Status
为 up
或 down
的表。
database: gitlabhq_production
Status Migration ID Type Milestone Name
--------------------------------------------------
up 20240701074848 regular 17.2 AddGroupIdToPackagesDebianGroupComponents
up 20240701153843 regular 17.2 AddWorkItemsDatesSourcesSyncToIssuesTrigger
up 20240702072515 regular 17.2 AddGroupIdToPackagesDebianGroupArchitectures
up 20240702133021 regular 17.2 AddWorkspaceTerminationTimeoutsToRemoteDevelopmentAgentConfigs
up 20240604064938 post 17.2 FinalizeBackfillPartitionIdCiPipelineMessage
up 20240604111157 post 17.2 AddApprovalPolicyRulesFkOnApprovalGroupRules
从极狐GitLab 17.1 版本开始,迁移将按照与极狐GitLab 发布节奏相符的顺序执行。
运行不完整的数据库迁移
数据库迁移可能会停留在不完整的状态,在 sudo gitlab-rake db:migrate:status
命令的输出中会出现 down
状态。
-
要完成这些迁移,请使用以下 Rake 任务:
sudo gitlab-rake db:migrate
-
命令完成后,运行
sudo gitlab-rake db:migrate:status
检查所有迁移是否已完成(具有up
状态)。 -
热重载
puma
和sidekiq
服务:sudo gitlab-ctl hup puma sudo gitlab-ctl restart sidekiq
从极狐GitLab 17.1 版本开始,迁移将按照与极狐GitLab 发布节奏相符的顺序执行。
重建数据库索引
数据库索引可以定期重建,以便随着时间推移回收空间并将索引膨胀率维持在健康水平。重新索引操作也可以作为定期的定时任务来运行。“健康”的膨胀率在很大程度上取决于具体的索引,但一般来说应低于30%。
先决条件:
- 此功能需要 PostgreSQL 12 或更高版本。
- 不支持的索引类型:表达式索引、分区索引以及用于约束排除的索引。
要手动重建数据库索引:
-
可选。要想给 Grafana(4.6 或更高版本)端点发送注释,用自定义的环境变量启用注释(请参阅设置自定义环境变量):
-
GRAFANA_API_URL
- Grafana 的基本 URL,例如http://some-host:3000
。 -
GRAFANA_API_KEY
- 至少具有Editor role
的 Grafana API 密钥。
-
-
运行 Rake 任务,以重建预估膨胀率最高的两个索引:
sudo gitlab-rake gitlab:db:reindex
-
重建任务(
gitlab:db:reindex
) 重建两个索引。要重建更多索引,请再次运行该任务,直到所有所需的索引都已重建。
说明
- 重建数据库索引是一项磁盘密集型任务,因此您应该在使用低峰期时运行该任务。在高峰时段运行该任务可能会导致膨胀率上升,还可能使某些查询的执行速度变慢。
- 该任务需要为正在恢复的索引预留空闲磁盘空间。创建的索引会附加
_ccnew
后缀。如果重新索引任务失败,重新运行该任务会清理临时索引。 - 数据库索引重建完成所需的时间取决于目标数据库的大小,可能需要几个小时到几天不等。
Dump 数据库模式
在极少数情况下,即便所有数据库迁移均已完成,数据库模式仍可能与应用程序代码的预期不符。一旦出现这种情况,就可能在极狐GitLab 中引发异常错误。
要 Dump 数据库模式:
SCHEMA=/tmp/structure.sql gitlab-rake db:schema:dump
Rake 任务会创建一个包含数据库模式 dump 的 /tmp/structure.sql
文件。
要确定是否存在任何不同:
- 前往
gitlab
项目中的db/structure.sql
文件。选择与您的极狐GitLab 版本相匹配的分支。比如,极狐GitLab 16.2 的文件是:https://jihulab.com/gitlab-cn/gitlab/-/blob/16-2-stable-ee/db/structure.sql。 - 针对您的版本,对比
/tmp/structure.sql
和db/structure.sql
文件。
检查数据库是否存在模式不一致的情况
- 引入于极狐GitLab 15.11。
此 Rake 任务会检查数据库模式是否存在任何不一致之处,并将结果打印在终端上。该任务是一种诊断工具,需在极狐GitLab 技术支持的指导下使用。由于数据库不一致情况可能属预期之内,所以不应将此任务用于常规检查。
gitlab-rake gitlab:db:schema_checker:run
故障排产
Advisory lock 连接信息
运行 db:migrate
Rake 任务后,您可能会看到类似如下的输出:
main: == [advisory_lock_connection] object_id: 173580, pg_backend_pid: 5532
main: == [advisory_lock_connection] object_id: 173580, pg_backend_pid: 5532
返回的是信息性的消息,可以忽略。
当执行 gitlab:env:info
Rake 任务时出现 PostgreSQL socket 错误
在 Gitaly 或其他非 Rails 节点上运行 sudo gitlab-rake gitlab:env:info
后,您可能会看到如下错误:
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/opt/gitlab/postgresql/.s.PGSQL.5432"?
这是因为,在多节点环境中,gitlab:env:info
Rake 任务只能在运行 GitLab Rails 的节点上执行。