Kubernetes环境下将SonarQube插件目录挂载至NFS实现持久化
Got it, let's solve this plugin retention headache for your Kubernetes-hosted SonarQube instance. Mounting the plugins directory to NFS is exactly the right move—especially critical since you've got custom plugins that aren't available in the official marketplace. Here's a straightforward, step-by-step breakdown:
1. Prep Your NFS Shared Directory First
Before touching Kubernetes configs, make sure your NFS server is set up correctly:
- Create a dedicated shared directory on your NFS server (e.g.,
/nfs/sonarqube/plugins) - Set permissions so the SonarQube pod can read/write to it. SonarQube runs as user
uid=1000by default, so run this on your NFS server:chown -R 1000:1000 /nfs/sonarqube/plugins chmod -R 755 /nfs/sonarqube/plugins - Verify the NFS share is accessible from your Kubernetes nodes (test with a
mountcommand on a node to confirm connectivity)
2. Update SonarQube Deployment Config
Modify your existing SonarQube Deployment to add the NFS volume and mount it to the plugin directory. Here's how the relevant sections should look:
Example Deployment Snippet
apiVersion: apps/v1 kind: Deployment metadata: name: sonarqube spec: replicas: 1 template: spec: containers: - name: sonarqube image: sonarqube:latest # Replace with your actual image tag volumeMounts: - name: sonar-plugins mountPath: /opt/sonarqube/plugins # Default plugin path; adjust if you've customized it # ... rest of your container config (ports, env vars, resources, etc.) volumes: - name: sonar-plugins nfs: server: <your-nfs-server-ip-or-hostname> # Replace with your NFS server address path: /nfs/sonarqube/plugins # Match the shared directory on your NFS server # ... rest of your Deployment config (selector, strategy, etc.)
3. Migrate Existing Plugins to NFS
If you already have plugins installed in the current SonarQube pod, copy them over to the NFS share so they're available immediately after the mount:
- Copy plugins from the running pod to your local machine:
kubectl cp <your-sonarqube-pod-name>:/opt/sonarqube/plugins ./sonar-plugins-temp - Transfer the copied plugins to your NFS server's shared directory (use
scp,rsync, or directly move them if you have access to the NFS server):rsync -av ./sonar-plugins-temp/ <nfs-server-user>@<nfs-server-ip>:/nfs/sonarqube/plugins/
4. Apply the Config and Verify
- Apply the updated Deployment:
kubectl apply -f your-sonarqube-deployment.yaml - Wait for the new pod to start, then verify the NFS mount is working:
You should see the NFS server's path listed as the mount source.kubectl exec -it <new-sonarqube-pod-name> -- df -h /opt/sonarqube/plugins - Log into SonarQube and confirm all your plugins are present and active—no more reinstalling after pod restarts!
Key Notes to Avoid Headaches
- Permission Checks: Double-check that the NFS directory permissions match SonarQube's runtime user (uid=1000). Mismatched permissions will cause plugin loading failures.
- NFS Stability: Ensure your NFS server is highly available (or use a managed NFS service) to avoid SonarQube pod crashes if the NFS share goes down.
- Plugin Updates: When adding new plugins in the future, you can either:
- Upload them via SonarQube's UI (they'll be saved to the NFS share automatically)
- Copy them directly to the NFS shared directory and restart the SonarQube pod
内容的提问来源于stack exchange,提问作者DC.Skells

