Nexus3 容器部署3.70.x升级实践-非成功案例

docker 从 3.70.x 及以下版本升级 3.71.x 及以上版本出现旧数据库不支持错误

官方也很细心给了一份升级文档 upgrading-to-nexus-repository-3-71-0-and-beyond

由于我使用的也是官方镜像,社区也有人给出了解决方案 sonatype/nexus-public#51

友情提示,操作前需要备份持久化数据,避免升级失败,建议先阅读文尾后再参考我的升级

创建 nexus 备份文件

  • 设置-系统-任务-创建任务

  • 搜索 Backup,选择它

  • 创建一下手动触发的任务,备份路径 /nexus-data/backup 就行

  • 创建完任务后,点击 backup 任务运行

  • 触发备份

  • 等待备份完成

不放心,也可以进容器内,看一下:

19:56 ➜  ~ kubectl exec -it pods/nexus3-859bb76886-mc29s -n nat -- bash
bash-4.4$ ls
nexus  sonatype-work  start-nexus-repository-manager.sh
bash-4.4$ cd /nexus-data/
bash-4.4$ ls
backup	blobs  cache  db  elasticsearch  etc  generated-bundles  instances  javaprefs  kar  karaf.pid  keystores  lock	log  orient  port  restore-from-backup	tmp
bash-4.4$ cd backup/
bash-4.4$ ls
analytics-2025-05-16-11-56-22-3.70.1-02.bak  component-2025-05-16-11-56-22-3.70.1-02.bak  config-2025-05-16-11-56-22-3.70.1-02.bak  security-2025-05-16-11-56-22-3.70.1-02.bak
bash-4.4$ exit

示例是 k8s 方式,docker 部署也是类似。

数据迁移

查询对应 nexus-db-migrator 迁移工具

需要下载和你 Nexus 版本匹配的 nexus-db-migrator 工具, 最新版本下载 nexus-db-migrator, 目前最新版本是 3.70.4-02

大概格式如下:

https://download.sonatype.com/nexus/nxrm3-migrator/nexus-db-migrator-<version版本号>.jar

由于我的版本是 3.70.1-02, 选择下载

https://sonatype-download.global.ssl.fastly.net/repository/downloads-prod-group/nxrm3-migrator/nexus-db-migrator-3.70.1-03.jar

可能你对应的版本不存在,可以尝试改 patch 版本,实在不行你就先升级到 3.70.4 最新版本

再次提醒版本一定要匹配上

操作

进入容器内部操作,方便起见, 确保在 /nexus-data/backup 目录下

bash-4.4$ pwd
/nexus-data/backup

下载迁移工具,或者同步到容器内也行(网络不好的情况下)

curl -s -L -O https://sonatype-download.global.ssl.fastly.net/repository/downloads-prod-group/nxrm3-migrator/nexus-db-migrator-3.70.1-03.jar

停 nexus 服务

/opt/sonatype/nexus/bin/nexus stop

执行迁移操作,将数据迁移到 H2 数据库,根据数据量和配置灵活调整 -Xmx4G -Xms4G 值,这里条件有限默认 4G, 执行完成如下图

java -Xmx4G -Xms4G -XX:+UseG1GC -XX:MaxDirectMemorySize=28672M -jar nexus-db-migrator-3.70.1-03.jar --migration_type=h2

再次停 nexus 服务

bash-4.4$ /opt/sonatype/nexus/bin/nexus stop
Shutting down nexus
nexus is not running.

查看迁移的数据

bash-4.4$ ls -ahl nexus.mv.db
-rw-r--r-- 1 nexus nexus 320K May 16 12:19 nexus.mv.db

将迁移的数据文件复制到 db 文件夹中

cp nexus.mv.db /nexus-data/db

然后退出容器

销毁容器

  • compose 部署
docker compose down nexus3
  • k8s 部署副本改成 0
kubectl scale --replicas 0 deploy/nexus3 -n nat

编辑配置文件

为啥不在容器里编辑,啥工具都没有不方便,而且这个配置持久化了

  • 默认配置如下 old
root@nat3:/data/k8s/local/nexus3/etc# cat nexus.properties
# Jetty section
# application-port=8081
# application-host=0.0.0.0
# nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
# nexus-context-path=/${NEXUS_CONTEXT}

# Nexus section
# nexus-edition=nexus-pro-edition
# nexus-features=\
#  nexus-pro-feature

# nexus.hazelcast.discovery.isEnabled=true
  • 新增 nexus.datastore.enabled=true, 新配置如下
root@nat3:/data/k8s/local/nexus3/etc# cat nexus.properties
# Jetty section
# application-port=8081
# application-host=0.0.0.0
# nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
# nexus-context-path=/${NEXUS_CONTEXT}

# Nexus section
# nexus-edition=nexus-pro-edition
# nexus-features=\
#  nexus-pro-feature

# nexus.hazelcast.discovery.isEnabled=true
nexus.datastore.enabled=true

启动老服务,验证数据库切换成功

  • compose 部署
docker compose up -d
  • k8s 部署副本改成 0
kubectl scale --replicas 1 deploy/nexus3 -n nat

观察日志,搜索 H2

2025-05-16 12:33:15,378+0000 INFO  [FelixStartLevel] *SYSTEM org.sonatype.nexus.datastore.DataStoreConfigurationDefaultSource - Loaded 'nexus' data store configuration defaults (Embedded H2)
2025-05-16 12:33:15,710+0000 INFO  [FelixStartLevel] *SYSTEM com.zaxxer.hikari.HikariDataSource - nexus - Starting...
2025-05-16 12:33:16,018+0000 INFO  [FelixStartLevel] *SYSTEM com.zaxxer.hikari.HikariDataSource - nexus - Start completed.
2025-05-16 12:33:16,020+0000 INFO  [FelixStartLevel] *SYSTEM org.sonatype.nexus.datastore.mybatis.MyBatisDataStore - nexus - Loading MyBatis configuration from /opt/sonatype/nexus/etc/fabric/mybatis.xml
2025-05-16 12:33:16,147+0000 INFO  [FelixStartLevel] *SYSTEM org.sonatype.nexus.datastore.mybatis.MyBatisDataStore - nexus - MyBatis databaseId: H2
2025-05-16 12:33:16,346+0000 INFO  [FelixStartLevel] *SYSTEM org.sonatype.nexus.datastore.mybatis.MyBatisDataStore - nexus - Creating schema for UpgradeTaskDAO

升级版本失败

直接替换镜像升级就完成了,然后我的数据就没了 😂, 严格按照官方文档来的哇 😂

后续

  • 云缘生镜像站暂停营业几天
  • 虽然有备份数据,但是不想回滚了,重新部署,数据存储使用 PG 吧

Sponsor

Like this article? $1 reward

Comments