Skip to main content

在 K8S 中使用 Helm 快速部署 Nexus3 全能私有仓库

准备条件

  • Kubernetes 1.23+
  • Helm 3.8.0+
  • Kubernetes Storage 存储动态供给 或 PV 存储卷
  • 二级域名,TLS 证书(可选)

获取 Chart

ArtifactHub 选择一个 Nexus3 的 Chart 版本,这里使用 Chart 4.42.1,对应版本 Nexus3 3.67.1 ,docker 镜像来自 DockerHub sonatype/nexus3:3.67.1

添加 Chart 仓库:

helm repo add stevehipwell https://stevehipwell.github.io/helm-charts/
helm repo update

查看 stevehipwell/nexus3 仓库历史版本的 Charts

$ helm search repo stevehipwell/nexus3 --versions
NAME CHART VERSION APP VERSION DESCRIPTION
stevehipwell/nexus3 4.45.1 3.70.2 Helm chart for Sonatype Nexus 3 OSS.
stevehipwell/nexus3 4.45.0 3.70.1 Helm chart for Sonatype Nexus 3 OSS.
stevehipwell/nexus3 4.44.0 3.69.0 Helm chart for Sonatype Nexus 3 OSS.
stevehipwell/nexus3 4.43.1 3.68.1 Helm chart for Sonatype Nexus 3 OSS.
stevehipwell/nexus3 4.43.0 3.68.0 Helm chart for Sonatype Nexus 3 OSS.
stevehipwell/nexus3 4.42.1 3.67.1 Helm chart for Sonatype Nexus 3 OSS.
stevehipwell/nexus3 4.41.1 3.66.0 Helm chart for Sonatype Nexus 3 OSS.
stevehipwell/nexus3 4.41.0 3.66.0 Helm chart for Sonatype Nexus 3 OSS.
....

获取 Chart 4.42.1

helm pull stevehipwell/nexus3 --version 4.42.1

Chart 4.42.1 获取 values.yaml

部署目录下仅保留 Chart 包和 values.yaml 文件:

$ ls -l
total 32
-rw-r--r-- 1 root root 24258 Mar 11 18:03 nexus3-4.42.1.tgz
-rw-r--r-- 1 root root 7119 Mar 11 18:03 values-nexus3.yaml

配置 values.yaml 默认参数

固定容器镜像版本号

image:
repository: sonatype/nexus3
tag: "3.67.1"
在生产环境中为什么要固定应用的版本号?

防止应用自动升级带来的不兼容,保证业务的稳定性,应用的升级变得可控。

使用 NodePort 暴露服务

为了前期访问方便,开启 NodePort 访问:

service:
type: NodePort
port: 8081

使用 ingress + TLS 暴露服务

以下配置分别对上传包体Size、自动发现监控与白名单安全进行了优化:

ingress:
enabled: true
annotations:
# 增加 body 用于上传大的包体
nginx.ingress.kubernetes.io/proxy-body-size: 1G
nginx.ingress.kubernetes.io/proxy-body-timeout: 300s
nginx.ingress.kubernetes.io/proxy-connect-timeout: 300s
nginx.ingress.kubernetes.io/proxy-read-timeout: 300s
nginx.ingress.kubernetes.io/proxy-send-timeout: 300s
# 开启 prometheus 自动发现监控
prometheus.io/http-probe: "true"
prometheus.io/http-probe-path: /
# 绑定白名单,仿制的
nginx.ingress.kubernetes.io/whitelist-source-range: "10.0.0.0/8,192.168.0.0/16"
ingressClassName: "clb"
hosts:
- repo.example.com
tls:
- hosts:
- repo.example.com
secretName: ssl-star-example-com

数据库持久化

这里使用了 腾讯云文件存储 CFS 持久化数据。

persistence:
enabled: true # 启用
accessMode: ReadWriteOnce
storageClass: cfs # 修改成指定存储类
size: 100Gi # 修改分配存储资源大小
Size

修改分配存储资源大小,在腾讯云 CFS 中这可以是个任意值,会按量进行扩缩容

限制资源配额

resources:
limits:
cpu: '2'
memory: 4Gi
requests:
cpu: '1'
memory: 2Gi

envVars:
jvmMinHeapSize: 2048m
jvmMaxHeapSize: 2048m
jvmAdditionalMemoryOptions: "-XX:MaxDirectMemorySize=2048m"
jvmAdditionalOptions: ""
限制资源配额

生产环境必须为每个应用限制资源配额,以防止资源抢占带来的不稳定。

安装 Chart

$ helm -n ${NAME_SPACE} install nexus3 nexus3-4.42.1.tgz -f values-nexus3.yaml
NAME: nexus3
LAST DEPLOYED: Tue Mar 11 18:26:06 2025
NAMESPACE: kube-ops
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
***********************************************************************
* Nexus3 *
***********************************************************************
Chart version: 4.42.1
App version: 3.67.1
Image tag: sonatype/nexus3:3.67.1-java11
***********************************************************************

登录 Nexus3

先通过 NodePort 访问:http://${NodeIP}:30167

kubectl -n kube-ops get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
jenkins NodePort 172.16.1.138 <none> 8080:30167/TCP,50000:31951/TCP 39d

到 Pod 内 /nexus-data/admin.password 获取 admin 的默认密码。

也在 PVC 的挂在卷下:

$ cd /data/cfs/kube-ops-nexus3-data-pvc-f07a6e7a-a7f7-4ca6-a6fb-ef33d98c2f37
$ ls -l
total 36
-rw-r--r-- 1 200 200 36 Mar 11 18:27 admin.password
drwxr-sr-x 3 200 200 20 Mar 11 18:27 blobs
drwxr-sr-x 353 200 200 12288 Mar 11 18:26 cache
drwxr-sr-x 7 200 200 123 Mar 11 18:27 db
drwxr-sr-x 3 200 200 18 Mar 11 18:27 elasticsearch
drwxr-sr-x 4 200 200 56 Mar 11 18:26 etc
drwxr-sr-x 2 200 200 6 Mar 11 18:26 generated-bundles
drwxr-sr-x 2 200 200 32 Mar 11 18:26 instances
drwxr-sr-x 3 200 200 18 Mar 11 18:26 javaprefs
-rw-r--r-- 1 200 200 1 Mar 11 18:26 karaf.pid
drwxr-sr-x 3 200 200 17 Mar 11 18:27 keystores
-rw-r--r-- 1 200 200 25 Mar 11 18:26 lock
drwxr-sr-x 3 200 200 106 Mar 11 18:27 log
drwxr-sr-x 2 200 200 6 Mar 11 18:27 orient
-rw-r--r-- 1 200 200 5 Mar 11 18:26 port
drwxr-sr-x 2 200 200 6 Mar 11 18:27 restore-from-backup
drwxr-sr-x 7 200 200 4096 Mar 11 18:27 tmp

$ cat admin.password
0d97f990-9392-416f-bb10-4779f974f15e

再次登录会要求修改默认密码:

开启 “匿名访问权限”

初始化完成:

更新 release

helm -n ${NAME_SPACE} upgrade --install nexus3 nexus3-4.42.1.tgz -f values-nexus3.yaml

参考文档