Kubernetes部署SonarQube无法连接外部PostgreSQL数据库
问题:Kubernetes部署SonarQube后仍连接默认H2数据库,配置的PostgreSQL参数未生效
我正尝试在Kubernetes上部署带有外部PostgreSQL数据库的SonarQube,部署过程无报错,但SonarQube仍连接默认的H2数据库,配置的数据库参数未生效。
我的deployment.yaml文件
apiVersion: v1 kind: ConfigMap metadata: name: sonar-config namespace: sonarqube labels: app: sonar data: SONARQUBE_JDBC_URL: "jdbc:postgresql://10.145.165.23:5432/sonar_db" SONARQUBE_JDBC_USERNAME: "postgres" SONARQUBE_JDBC_PASSWORD: "passwds" JAVA_OPTS: "-Duser.timezone=Asia/Jakarta -Xmx2048m" --- apiVersion: apps/v1 kind: Deployment metadata: name: sonar namespace: sonarqube labels: app: sonar spec: replicas: 1 strategy: type: Recreate selector: matchLabels: app: sonar template: metadata: labels: app: sonar spec: initContainers: - name: init image: busybox command: - sysctl - -w - vm.max_map_count=262144 imagePullPolicy: IfNotPresent securityContext: privileged: true containers: - name: sonarqube image: sonarqube:10.3.0-community imagePullPolicy: IfNotPresent ports: - containerPort: 9000 envFrom: - configMapRef: name: sonar-config volumeMounts: - name: app-pvc mountPath: "/opt/sonarqube/data/" subPath: data - name: app-pvc mountPath: "/opt/sonarqube/extensions/" subPath: extensions resources: requests: memory: "1024Mi" limits: memory: "2048Mi" volumes: - name: app-pvc persistentVolumeClaim: claimName: sonar-pvc --- apiVersion: v1 kind: Service metadata: name: sonar-svc namespace: sonarqube labels: app: sonar spec: ports: - port: 9000 name: sonar selector: app: sonar ---
Pod日志
2023.12.21 06:19:22 INFO web[][o.s.s.p.Platform] Web Server is operational WARNING: A terminally deprecated method in java.lang.System has been called WARNING: System::setSecurityManager has been called by org.sonar.process.PluginSecurityManager (file:/opt/sonarqube/lib/sonar-application-10.3.0.82913.jar) WARNING: Please consider reporting this to the maintainers of org.sonar.process.PluginSecurityManager WARNING: System::setSecurityManager will be removed in a future release 2023.12.21 06:19:22 INFO ce[][o.s.p.ProcessEntryPoint] Starting Compute Engine 2023.12.21 06:19:22 INFO ce[][o.s.ce.app.CeServer] Compute Engine starting up... 2023.12.21 06:19:23 INFO ce[][o.s.d.DefaultDatabase] Create JDBC data source for jdbc:h2:tcp://127.0.0.1:9092/sonar;NON_KEYWORDS=VALUE 2023.12.21 06:19:23 INFO ce[][c.z.h.HikariDataSource] HikariPool-1 - Starting... 2023.12.21 06:19:23 INFO ce[][c.z.h.p.HikariPool] HikariPool-1 - Added connection conn0: url=jdbc:h2:tcp://127.0.0.1:9092/sonar user= 2023.12.21 06:19:23 INFO ce[][c.z.h.HikariDataSource] HikariPool-1 - Start completed. 2023.12.21 06:19:23 WARN ce[][o.s.db.dialect.H2] H2 database should be used for evaluation purpose only. 2023.12.21 06:19:24 INFO ce[][o.s.s.p.ServerFileSystemImpl] SonarQube home: /opt/sonarqube 2023.12.21 06:19:24 INFO ce[][o.s.c.c.CePluginRepository] Load plugins 2023.12.21 06:19:26 INFO ce[][o.s.c.c.ComputeEngineContainerImpl] Running Community edition 2023.12.21 06:19:26 INFO ce[][o.s.ce.app.CeServer] Compute Engine is started 2023.12.21 06:19:26 INFO app[][o.s.a.SchedulerImpl] Process[ce] is up 2023.12.21 06:19:26 INFO app[][o.s.a.SchedulerImpl] SonarQube is operational
问题分析与解决办法
核心问题:环境变量名称不匹配
SonarQube 10.x版本已将数据库相关环境变量的前缀从SONARQUBE_JDBC_*改为SONAR_JDBC_*,你当前使用的是旧版本变量名,导致SonarQube无法识别配置,自动回退到默认的H2数据库。
修改步骤
更新ConfigMap中的环境变量名称:
将ConfigMap内的SONARQUBE_JDBC_URL、SONARQUBE_JDBC_USERNAME、SONARQUBE_JDBC_PASSWORD分别改为SONAR_JDBC_URL、SONAR_JDBC_USERNAME、SONAR_JDBC_PASSWORD。修改后的ConfigMap片段如下:apiVersion: v1 kind: ConfigMap metadata: name: sonar-config namespace: sonarqube labels: app: sonar data: SONAR_JDBC_URL: "jdbc:postgresql://10.145.165.23:5432/sonar_db" SONAR_JDBC_USERNAME: "postgres" SONAR_JDBC_PASSWORD: "passwds" JAVA_OPTS: "-Duser.timezone=Asia/Jakarta -Xmx2048m"重新应用配置并重启Pod:
- 执行命令更新配置:
kubectl apply -f deployment.yaml -n sonarqube - 删除现有Pod触发重启:
kubectl delete pod -l app=sonar -n sonarqube
- 执行命令更新配置:
验证结果:
查看新Pod的日志,确认是否出现类似Create JDBC data source for jdbc:postgresql://10.145.165.23:5432/sonar_db的条目,以此确认已连接到目标PostgreSQL数据库。
额外注意事项
- 确保外部PostgreSQL数据库已创建
sonar_db库,且Kubernetes集群节点能够访问10.145.165.23的5432端口 - 确认PostgreSQL用户
postgres拥有sonar_db数据库的创建、读写等完整权限
内容的提问来源于stack exchange,提问作者Breaking News
相关产品推荐
相关产品推荐

