本文介绍生产环境 GitLab 及内置 PostgreSQL 的升级思路
升级背景
GitLab 系统漏洞
CVE-2021-22205
- An issue has been discovered in GitLab CE/EE affecting all versions starting from 11.9. GitLab was not properly validating image files that were passed to a file parser which resulted in a remote command execution.
- 在GitLab CE/EE中存在远程代码执行漏洞,由于GitLab没有正确验证传递到文件解析器的image文件,导致远程命令可以执行。

- 影响范围:
- [11.9, 13.10.3)
- 当前部署版本为12.9,准确命中...
一、升级规划
1、GitLab 升级
- GitLab 不支持版本无缝升级,会产生兼容性问题
- 跨大版本升级需要遵循以下步骤:
- 确定受支持的升级路径,对于成功的大版本升级至关重要
- 需要升级到先前主要版本的最新次要版本
- 升级到下一个主要版本的"点零"版本(X.0.Z)
- 继续升级到该主要版本的较新版本
升级路线

路线制定
-
12.9.9 -> 12.10.14 -> 13.0.14 -> 13.1.11 -> 13.8.8 -> 13.12.15
-
升级故障排查参考: docs.gitlab.com/ee/update/p...
2、PostgreSQL 升级
- GitLab 版本迭代过程中,其 PostgreSQL 的要求版本也在不断更新。出于谨慎性考虑,在 GitLab 升级过程中手动升级 PostgreSQL 。
升级路线

路线制定
GitLab 版本 | PostgreSQL 版本 |
---|---|
12.9.9 | 升级至 PostgreSQL 11 |
13.8.8 | 升级至 PostgreSQL 12 |
二、升级前后检查事项
1、系统版本
cat /etc/redhat-release
2、系统内存
free -h
3、磁盘空间
df -h
4、系统内核
uname -r
5、查看端口占用
netstat -nlpt
6、当前 GitLab 版本
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
7、确认 GitLab 安装方式
rpm -qa gitlab-ce
8、查看 GitLab 服务状态
gitlab-ctl status
9、检查状态和常规配置
gitlab-rake gitlab:check SANITIZE=true --trace
gitlab-rake gitlab:check
gitlab-rake gitlab:check SANITIZE=true
10、确认可以解密数据库值(13.1版本以上)
gitlab-rake gitlab:doctor:secrets
11、在GitLab UI中查看
- 查看事项:
- 用户可以登录
- 项目列表可见
- 可以访问项目问题和合并请求
- 用户可以从 GitLab 克隆存储库
- 用户可以将提交推送到 GitLab
三、版本升级
1、升级 PostgreSQL 11
前置准备
- 正在运行支持升级后PostgreSQL版本的GitLab
- 如果是升级后的GitLab,确保成功执行过sudo gitlab-ctl reconfigure
- 确保有足够的磁盘空间用于数据库的两个副本(13.3及以后版本会对可用磁盘空间检查并在不满足要求时中止升级)
- 检查数据库大小:
du -sh /var/opt/gitlab/postgresql/data
- 检查可用空间:
df -h
- 检查数据库大小:
- 查看当前版本:
rpm -qa gitlab-ce
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
cat /var/opt/gitlab/postgresql/data/PG_VERSION
/opt/gitlab/embedded/bin/pg_ctl --version
/opt/gitlab/embedded/bin/psql --version

- 查看GitLab UI

升级命令
- 前台执行:
gitlab-ctl pg-upgrade -V 11
- 后台执行(数据库空间占用大的情况):
nohup gitlab-ctl pg-upgrade -V 11 > /log/gitlab_pg-upgrade.log 2>&1 &

- gitlab-ctl pg-upgrade 执行以下步骤:
- 检查以确保数据库处于已知的良好状态。
- 检查是否有足够的可用磁盘空间,否则中止。可以通过附加--skip-disk-check标志来跳过它。
- 关闭现有数据库、任何不必要的服务,并启用 GitLab 部署页面。
- 更改 PostgreSQL 中的符号链接 /opt/gitlab/embedded/bin/ 以指向更新版本的数据库。
- 创建一个新目录,其中包含一个新的空数据库,其区域设置与现有数据库匹配。
- 使用该 pg_upgrade 工具将数据从旧数据库复制到新数据库。
- 将旧数据库移开。
- 将新数据库移动到预期位置。
- 调用 gitlab-ctl reconfigure 以进行所需的配置更改并启动新的数据库服务器。
- 运行 ANALYZE 以生成数据库统计信息。
- 启动其余服务并删除部署页面
rm -f /opt/gitlab/embedded/service/gitlab-rails/public/index.html
。 - 如果在此过程中检测到任何错误,它将恢复到旧版本的数据库。
重启服务
gitlab-ctl reconfigure
gitlab-ctl status
生成数据库统计信息
gitlab-psql -c "SELECT relname, last_analyze, last_autoanalyze FROM pg_stat_user_tables WHERE last_analyze IS NULL AND last_autoanalyze IS NULL;"
gitlab-psql -c 'SET statement_timeout = 0; ANALYZE VERBOSE;'
验证 GitLab
cat /var/opt/gitlab/postgresql/data/PG_VERSION
/opt/gitlab/embedded/bin/pg_ctl --version
/opt/gitlab/embedded/bin/psql --version
gitlab-ctl status

- 查看GitLab UI 是否正常

清理旧的数据库文件
rm -rf /var/opt/gitlab/postgresql/data.10
rm -f /var/opt/gitlab/postgresql-version.old
关闭自动升级
- 在之后 GitLab 升级期间退出 PostgreSQL 自动升级:
touch /etc/gitlab/disable-postgresql-upgrade
2、12.9.9 升级 12.10.14
升级前检查
rpm -qa gitlab-ce
查看当前状态和配置
gitlab-ctl status
gitlab-rake gitlab:check SANITIZE=true --trace
gitlab-rake gitlab:check
gitlab-rake gitlab:check SANITIZE=true
停止服务
gitlab-ctl stop
gitlab-ctl status
安装新版本
方式一
- 卸载旧包:
rpm -evh `rpm -qa gitlab-ce`
- 安装新包:
rpm -ivh gitlab-ce-12.10.14-ce.0.el7.x86_64.rpm
方式二
- 启动 PostgreSQL:
gitlab-ctl start postgresql
- 更新安装:
rpm -Uvh gitlab-ce-12.10.14-ce.0.el7.x86_64.rpm
检查配置文件
vi /etc/gitlab/gitlab.rb
重启服务
gitlab-ctl restart
重新配置
gitlab-ctl reconfigure
检查状态
gitlab-ctl status
版本验证
rpm -qa gitlab-ce
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION

- 验证GitLab UI

3、12.10.14 升级 13.0.14
升级前检查
rpm -qa gitlab-ce
查看当前状态和配置
gitlab-ctl status
gitlab-rake gitlab:check SANITIZE=true --trace
gitlab-rake gitlab:check
gitlab-rake gitlab:check SANITIZE=true
哈希存储转换问题
问题说明
- 在版本检查中发现检查未通过事项:
- All projects are in hashed storage? ... no

- 官方说明:docs.gitlab.com/ee/administ...
- 在 GitLab 13.0 中,默认启用哈希存储,并且弃用散列存储,所以出现上述事项。不过旧存储在 GitLab 13.x 版本依旧支持,继续使用GitLab 13.x 版本可以略过该事项。
- GitLab 14.0 取消了对散列存储的支持,也就是说 GitLab 13.x 在升级 GitLab 14.x 前必须进行哈希存储转换。
问题解决
- 为了以绝后患,这里对该事项进行处置。
- 存储库迁移:
gitlab-rake gitlab:storage:migrate_to_hashed
- 执行成功后,再次执行,会提示:
- There are no projects requiring storage migration. Nothing to do!
- 如果执行过程中反复出现类似说明:
-
Enqueuing migration of XX projects in batches of 200. Done!
-
解决:
- 更新令牌重新转换
- 进入数据库终端:
gitlab-rails dbconsole
ini# 执行清空命令 UPDATE projects SET runners_token = null, runners_token_encrypted = null; # 退出 exit;
- 重新迁移存储库
-
- 再次检查:

停止服务
gitlab-ctl stop
gitlab-ctl status
安装新版本
方式一
- 卸载旧包:
rpm -evh `rpm -qa gitlab-ce`
- 安装新包:
rpm -ivh gitlab-ce-13.0.14-ce.0.el7.x86_64.rpm
方式二
- 启动 PostgreSQL:
gitlab-ctl start postgresql
- 更新安装:
rpm -Uvh gitlab-ce-13.0.14-ce.0.el7.x86_64.rpm
检查配置文件
vi /etc/gitlab/gitlab.rb
重启服务
gitlab-ctl restart
重新配置
gitlab-ctl reconfigure
检查状态
gitlab-ctl status
版本验证
rpm -qa gitlab-ce
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION

- 验证GitLab UI

4、13.0.14 升级 13.1.11
升级前检查
rpm -qa gitlab-ce
查看当前状态和配置
gitlab-ctl status
gitlab-rake gitlab:check SANITIZE=true --trace
gitlab-rake gitlab:check
gitlab-rake gitlab:check SANITIZE=true
停止服务
gitlab-ctl stop
gitlab-ctl status
安装新版本
方式一
- 卸载旧包:
rpm -evh `rpm -qa gitlab-ce`
- 安装新包:
rpm -ivh gitlab-ce-13.1.11-ce.0.el7.x86_64.rpm
方式二
- 启动 PostgreSQL:
gitlab-ctl start postgresql
- 更新安装:
rpm -Uvh gitlab-ce-13.1.11-ce.0.el7.x86_64.rpm
检查配置文件
vi /etc/gitlab/gitlab.rb
重启服务
gitlab-ctl restart
重新配置
gitlab-ctl reconfigure
检查状态
gitlab-ctl status
版本验证
rpm -qa gitlab-ce
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION

- 验证GitLab UI

5、13.1.11 升级 13.8.8
升级前检查
rpm -qa gitlab-ce
查看当前状态和配置
gitlab-ctl status
gitlab-rake gitlab:check SANITIZE=true --trace
gitlab-rake gitlab:check
gitlab-rake gitlab:check SANITIZE=true
停止服务
gitlab-ctl stop
gitlab-ctl status
安装新版本
方式一
- 卸载旧包:
rpm -evh `rpm -qa gitlab-ce`
- 安装新包:
rpm -ivh gitlab-ce-13.8.8-ce.0.el7.x86_64.rpm
方式二
- 启动 PostgreSQL:
gitlab-ctl start postgresql
- 更新安装:
rpm -Uvh gitlab-ce-13.8.8-ce.0.el7.x86_64.rpm
检查配置文件
vi /etc/gitlab/gitlab.rb
重启服务
gitlab-ctl restart
重新配置
gitlab-ctl reconfigure
检查状态
gitlab-ctl status
版本验证
rpm -qa gitlab-ce
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION

- 验证GitLab UI

6、升级 PostgreSQL 12
前置准备
- 正在运行支持升级后PostgreSQL版本的GitLab
- 如果是升级后的GitLab,确保成功执行过sudo gitlab-ctl reconfigure
- 确保有足够的磁盘空间用于数据库的两个副本(13.3及以后版本会对可用磁盘空间检查并在不满足要求时中止升级)
- 检查数据库大小:
du -sh /var/opt/gitlab/postgresql/data
- 检查可用空间:
df -h
- 检查数据库大小:
- 查看当前版本:
rpm -qa gitlab-ce
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
cat /var/opt/gitlab/postgresql/data/PG_VERSION
/opt/gitlab/embedded/bin/pg_ctl --version
/opt/gitlab/embedded/bin/psql --version

升级命令
- 前台执行:
gitlab-ctl pg-upgrade -V 12
- 后台执行(数据库空间占用大的情况):
nohup gitlab-ctl pg-upgrade -V 12 > /log/gitlab_pg-upgrade2.log 2>&1 &

重启服务
gitlab-ctl reconfigure
gitlab-ctl status
生成数据库统计信息
gitlab-psql -c "SELECT relname, last_analyze, last_autoanalyze FROM pg_stat_user_tables WHERE last_analyze IS NULL AND last_autoanalyze IS NULL;"
gitlab-psql -c 'SET statement_timeout = 0; ANALYZE VERBOSE;'
验证 GitLab
cat /var/opt/gitlab/postgresql/data/PG_VERSION
/opt/gitlab/embedded/bin/pg_ctl --version
/opt/gitlab/embedded/bin/psql --version
gitlab-ctl status

- 查看GitLab UI 是否正常

清理旧的数据库文件
rm -rf /var/opt/gitlab/postgresql/data.11
rm -f /var/opt/gitlab/postgresql-version.old
关闭自动升级
- 在之后 GitLab 升级期间退出 PostgreSQL 自动升级:
touch /etc/gitlab/disable-postgresql-upgrade
7、13.8.8 升级 13.12.15
升级前检查
rpm -qa gitlab-ce
查看当前状态和配置
gitlab-ctl status
gitlab-rake gitlab:check SANITIZE=true --trace
gitlab-rake gitlab:check
gitlab-rake gitlab:check SANITIZE=true
停止服务
gitlab-ctl stop
gitlab-ctl status
安装新版本
方式一
- 卸载旧包:
rpm -evh `rpm -qa gitlab-ce`
- 安装新包:
rpm -ivh gitlab-ce-13.12.15-ce.0.el7.x86_64.rpm
方式二
- 启动 PostgreSQL:
gitlab-ctl start postgresql
- 更新安装:
rpm -Uvh gitlab-ce-13.12.15-ce.0.el7.x86_64.rpm
检查配置文件
vi /etc/gitlab/gitlab.rb
重启服务
gitlab-ctl restart
重新配置
gitlab-ctl reconfigure
检查状态
gitlab-ctl status
版本验证
rpm -qa gitlab-ce
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION

- 查看GitLab UI 是否正常
