Migrate Nexus to Longhorn and reduce its memory
Overview¶
This guide covers migrating Nexus from local-path storage to Longhorn for High Availability (HA), and permanently resolving memory issues.
Before:
Nexus → local-path PVC → single node (nuc-a)
data loss if nuc-a dies
stuck on one node
2.6GB memory, no limit
After:
Nexus → Longhorn PVC → replicated across nuc-a + castis-desktop
auto-failover if any node dies
can run on any node
memory optimized + limited
Prerequisites¶
- k3s cluster with 3 nodes
- Longhorn installed and healthy
- Nexus running on local-path storage
Part 1 — Memory Optimization¶
Why Nexus uses so much memory¶
Nexus runs several components in the same JVM: - OrientDB / H2 (metadata database) - Elasticsearch (search index) - Jetty (web server) - OSGi bundle cache
Default JVM settings allocate ~2.7GB heap which is pre-reserved regardless of actual usage.
JVM Tuning¶
Edit the Nexus deployment:
env:
- name: INSTALL4J_ADD_VM_PARAMS
value: "-Xms2703m -Xmx2703m -XX:MaxDirectMemorySize=2703m -Djava.util.prefs.userRoot=/nexus-data/javaprefs"
resources:
requests:
cpu: 500m
memory: 3Gi
limits:
cpu: "2"
memory: 5Gi
Warning: Setting
-Xmxtoo low (below 1536m) causes Nexus to crash with OOMKilled. Keep at 2703m for stability unless you have a small, lightly used instance.
Delete Unused Repositories¶
Default Nexus install includes many unused repos. Delete what you don't need:
maven-central (if not using Java)
maven-public (if not using Java)
maven-releases (if not using Java)
maven-snapshots (if not using Java)
nuget-group (if not using .NET)
nuget-hosted (if not using .NET)
nuget.org-proxy (if not using .NET)
Go to: Administration → Repositories → Delete
After deleting, run compaction tasks:
Part 2 — Longhorn HA Migration¶
Why Longhorn over local-path¶
| Feature | local-path | Longhorn |
|---|---|---|
| Node failure | data inaccessible | auto-failover |
| Pod scheduling | pinned to one node | any node |
| Snapshots | manual | automated |
| Resize | complex | easy |
| Replication | none | across nodes |
Step 1 — Verify Longhorn Health¶
# All pods should be Running
kubectl get pods -n longhorn-system | grep -v Running
# All nodes should have ALLOWSCHEDULING: true
kubectl get nodes.longhorn.io -n longhorn-system
If a node shows ALLOWSCHEDULING: false:
kubectl patch nodes.longhorn.io <node-name> \
-n longhorn-system \
--type='json' \
-p='[{"op":"replace","path":"/spec/allowScheduling","value":true}]'
Step 2 — Create Longhorn PVC¶
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nexus-data-longhorn
namespace: nexus
annotations:
longhorn.io/replica-count: "2"
spec:
accessModes:
- ReadWriteOnce
storageClassName: longhorn-retain
resources:
requests:
storage: 30Gi
Set replica count to 2 (sufficient for RWO workloads):
kubectl patch volume <longhorn-volume-name> \
-n longhorn-system \
--type='merge' \
-p='{"spec":{"numberOfReplicas":2}}'
Step 3 — Scale Down Nexus¶
kubectl scale deployment nexus -n nexus --replicas=0
# Confirm no pods running
kubectl get pods -n nexus
# Expected: No resources found
Critical: Never copy data while Nexus is running — risk of corruption.
Step 4 — Copy Data via Migration Pod¶
apiVersion: v1
kind: Pod
metadata:
name: nexus-migrate
namespace: nexus
spec:
containers:
- name: migrate
image: busybox
command: ["sh", "-c", "cp -av /source/. /dest/ && echo DONE"]
volumeMounts:
- name: source
mountPath: /source
- name: dest
mountPath: /dest
volumes:
- name: source
persistentVolumeClaim:
claimName: nexus-data
- name: dest
persistentVolumeClaim:
claimName: nexus-data-longhorn
restartPolicy: Never
kubectl apply -f nexus-migrate.yaml
# Watch progress — wait for DONE
kubectl logs -f nexus-migrate -n nexus
# Verify completed
kubectl get pod nexus-migrate -n nexus
# Expected: Completed
Step 5 — Clean Up Lock Files¶
After migration pod completes, remove Nexus lock files to prevent startup issues:
apiVersion: v1
kind: Pod
metadata:
name: remove-lock
namespace: nexus
spec:
containers:
- name: remove-lock
image: busybox
command: ["sh", "-c", "rm -f /nexus-data/lock /nexus-data/karaf.pid && echo DONE && ls /nexus-data/"]
volumeMounts:
- name: nexus-data
mountPath: /nexus-data
volumes:
- name: nexus-data
persistentVolumeClaim:
claimName: nexus-data-longhorn
restartPolicy: Never
kubectl apply -f remove-lock.yaml
kubectl logs remove-lock -n nexus
kubectl delete pod remove-lock -n nexus
kubectl delete pod nexus-migrate -n nexus
Step 6 — Update Deployment to Use Longhorn PVC¶
Update your nexus deployment yaml:
spec:
template:
spec:
nodeSelector:
kubernetes.io/hostname: nuc-b # pin to node with most free memory
volumes:
- name: nexus-data
persistentVolumeClaim:
claimName: nexus-data-longhorn # ← change from nexus-data
kubectl apply -f nexus-deployment.yaml
kubectl scale deployment nexus -n nexus --replicas=1
kubectl get pods -n nexus -w
Step 7 — Verify Nexus is Healthy¶
# Pod should be 1/1 Running
kubectl get pods -n nexus
# API should respond
curl -s https://nexus.castis.io/service/rest/v1/status
# Test docker pull
docker pull registry.nexus.castis.io/nginx:latest
docker pull registry.nexus.castis.io/playtelly/documentations:prod-latest
Step 8 — Clean Up Old local-path Resources¶
Only after confirming Nexus works:
# Delete old PVC
kubectl delete pvc nexus-data -n nexus
# Delete old PV
kubectl delete pv pvc-a2bdb9ff-5a3c-4209-b8b5-4d8f5d4691ba
# Remove data from old node (nuc-a)
ssh castis@<NUC_A_LAN_IP>
sudo rm -rf /var/lib/rancher/k3s/storage/pvc-a2bdb9ff-5a3c-4209-b8b5-4d8f5d4691ba_nexus_nexus-data
# Remove data from nuc-b local-path copy
sudo rm -rf /var/lib/rancher/k3s/storage/pvc-a2bdb9ff-5a3c-4209-b8b5-4d8f5d4691ba_nexus_nexus-data
Part 3 — Node Pinning Strategy¶
Since Nexus is RWO (ReadWriteOnce), it can only run on one node at a time. Pin it to the node with the most available memory:
Check node memory before choosing:
| Node | Memory | Best for |
|---|---|---|
| nuc-b | 16GB, ~48% used | Nexus (most free) |
| nuc-a | 16GB, ~81% used | too full |
| castis-desktop | 4GB, ~69% used | too small |
Troubleshooting¶
Volume stuck as faulted after node crash¶
# Force salvage
kubectl patch volume <volume-name> \
-n longhorn-system \
--type='merge' \
-p='{"spec":{"lastSalvageRequestedAt":"'$(date -u +%Y-%m-%dT%H:%M:%SZ)'"}}'
Multi-Attach error (volume already attached)¶
# Scale down to 0 first
kubectl scale deployment nexus -n nexus --replicas=0
# Delete any stuck pods
kubectl delete pod <pod-name> -n nexus --force --grace-period=0
# Scale back up
kubectl scale deployment nexus -n nexus --replicas=1
OOMKilled (Exit Code 137)¶
Increase memory limit in deployment:
Exit Code 0 / CrashLoopBackOff with empty logs¶
Remove lock files using the remove-lock pod (see Step 5).
Nexus slow to start¶
Normal — Nexus loads ~100+ OSGi bundles on startup. Takes 3-5 minutes. Watch logs:
Wait for:
Final Architecture¶
registry.nexus.castis.io
│
▼
Apisix (reverse proxy)
│
▼
Nexus Pod (nuc-b)
│
▼
Longhorn PVC (30Gi)
├── Replica 1: nuc-a
└── Replica 2: castis-desktop
If nuc-b dies:
→ Longhorn detaches volume
→ Pod rescheduled on another node
→ Volume reattaches automatically
→ Nexus back online
Summary¶
| Item | Before | After |
|---|---|---|
| Storage | local-path (single node) | Longhorn (replicated) |
| HA | no | auto-failover |
| Storage size | 50Gi | 30Gi (optimized) |
| Replicas | 1 (nuc-a only) | 2 (nuc-a + castis-desktop) |
| Memory limit | 4Gi (no JVM limit) | 5Gi (with JVM tuning) |
| Node | nuc-a (91% memory) | nuc-b (48% memory) |