![42af8b3baad9bd602974d16179c51a5c.png](https://i-blog.csdnimg.cn/blog_migrate/4b255b48db4269d7f97c897242f86c00.jpeg)
Kubernetes布署SonarQube检查Java项目代码质量
在上一篇教程中,使用Docker快速布署了SonarQube。这次将其布署到kubernetes集群中
![915e3da2e81679681aa355ab2291dc03.png](https://i-blog.csdnimg.cn/blog_migrate/34a833c1d31fa0c0ef5e1b650ed41bd6.jpeg)
布署Postgresql
SonarQube在未来将不支持Mysql,而内置H2数据库只供测试使用。如果我们要正式布署使用SonarQube,则需要布署Postgresql。
Postgresql布署清单
apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: postgresql namespace: sonarqubespec: selector: matchLabels: app: postgresql template: metadata: name: postgresql labels: app: postgresql spec: volumes: - name: postgres-data persistentVolumeClaim: claimName: postgres-data restartPolicy: Always containers: - name: postgres image: postgres:12 imagePullPolicy: IfNotPresent env: - name: POSTGRES_PASSWORD value: postgres - name: POSTGRES_USER value: sonar - name: POSTGRES_DB value: sonarqube volumeMounts: - name: postgres-data mountPath: /var/lib/postgresql/data ports: - name: default containerPort: 5432 protocol: TCP ---apiVersion: v1kind: PersistentVolumeClaimmetadata: name: postgres-data namespace: sonarqubespec: accessModes: - ReadWriteOnce storageClassName: managed-nfs-storage resources: requests: storage: 5Gi---apiVersion: v1kind: Servicemetadata: name: sonar-postgres namespace: sonarqubespec: type: NodePort selector: app: postgresql ports: - name: default port: 5432 targetPort: 5432 protocol: TCP
布署SonarQube服务
重要: SonarQube官方镜像在配置文件中配置使用Postgresql无效(?可能是我方法不对)
查看官方镜像启动脚本
![3151f0eb6c8fa60c1a8e2e3f96bf7c7e.png](https://i-blog.csdnimg.cn/blog_migrate/a3042870ecea6ee26d276d46140c4381.jpeg)
需要设置三个环境变量
- SONARQUBE_JDBC_USERNAME
- SONARQUBE_JDBC_PASSWORD
- SONARQUBE_JDBC_URL
以下是SonarQube布署清单
apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: sonar-web-deployment namespace: sonarqubespec: selector: matchLabels: app: sonar-web template: metadata: name: sonar-web labels: app: sonar-web spec: volumes: - name: sonar-extensions persistentVolumeClaim: claimName: sonar-extensions - name: sonar-data persistentVolumeClaim: claimName: sonar-data - name: sonar-logs persistentVolumeClaim: claimName: sonar-logs containers: - name: sonar image: sonarqube:7.9.1-community imagePullPolicy: IfNotPresent env: - name: SONARQUBE_JDBC_USERNAME value: sonar - name: SONARQUBE_JDBC_PASSWORD value: postgres - name: SONARQUBE_JDBC_URL value: "jdbc:postgresql://sonar-postgres:5432/sonarqube?currentSchema=public" volumeMounts: - name: sonar-data mountPath: /opt/sonarqube/data - name: sonar-logs mountPath: /opt/sonarqube/logs - name: sonar-extensions mountPath: /opt/sonarqube/extensions ports: - name: http containerPort: 9000
Ingress
apiVersion: extensions/v1beta1kind: Ingressmetadata: name: sonar-web-ingress namespace: sonarqubespec: rules: - host: sonar.mydomain.com http: paths: - backend: serviceName: sonar-web servicePort: 9000 path: /
登录查看系统信息
![2fedfa964302a6da982fe87c83094323.png](https://i-blog.csdnimg.cn/blog_migrate/422d061a4e134ad563d9111aaa396285.jpeg)
SonarQube中文插件及配置请参考上篇文章