SonarQube 容器化部署
SonarQube是一个开源的代码质量管理系统,可用来快速定位代码中的Bug、漏洞以及不优雅的代码。它支持几乎所有的常见编程语言,例如Java、JavaScript、TypeScript、Kotlin、Ruby、Go, Scala等。并且还有插件机制,利用插件,可以让SonarQube更加强大,例如可以整合Findbugs、PMD、Checkstyle等。可以说,SonarQube是一款提升项目代码质量必备的根据。
系统需求:
- k8s:1.13.5以上版本
- pgsql 10及以上
docker-compose
官方提供了docker-compose的文件,把data、log、extensions目录挂载到了本机目录,以便管理文件以及查看日志。
#docker-compose.yml
version: "3"
services:
sonarqube:
image: sonarqube:lts-community #8.9.2
depends_on:
- db
environment:
SONAR_JDBC_URL: jdbc:postgresql://pgsql/sonarqube?currentSchema=my_schema
SONAR_JDBC_USERNAME: postgres
SONAR_JDBC_PASSWORD: passwd
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_extensions:/opt/sonarqube/extensions
- sonarqube_logs:/opt/sonarqube/logs
#将data、log、extensions目录挂载到本机目录
ports:
- "9000:9000"
volumes:
sonarqube_data:
sonarqube_extensions:
sonarqube_logs:
kubernetes-yaml
通过kompose将转换为k8s的yaml文件,需要创建三个pv以及pvc,用于挂载文件。
#sonarqube-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose convert -f docker-compose.yml
kompose.version: 1.26.0 (40646f47)
creationTimestamp: null
labels:
io.kompose.service: sonarqube
name: sonarqube
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: sonarqube
strategy:
type: Recreate
template:
metadata:
annotations:
kompose.cmd: kompose convert -f docker-compose.yml
kompose.version: 1.26.0 (40646f47)
creationTimestamp: null
labels:
io.kompose.service: sonarqube
spec:
containers:
- env:
- name: SONAR_JDBC_PASSWORD
value: passwd
- name: SONAR_JDBC_URL
value: jdbc:postgresql://pgsql/sonarqube?currentSchema=public
- name: SONAR_JDBC_USERNAME
value: postgres
image: sonarqube:lts-community
name: sonarqube
ports:
- containerPort: 9000
resources: {}
volumeMounts:
#将data、log、extensions目录挂载到机器目录
- mountPath: /opt/sonarqube/data
name: sonarqube-data
- mountPath: /opt/sonarqube/extensions
name: sonarqube-extensions
- mountPath: /opt/sonarqube/logs
name: sonarqube-logs
restartPolicy: Always
volumes:
- name: sonarqube-data
persistentVolumeClaim:
claimName: sonarqube-data
- name: sonarqube-extensions
persistentVolumeClaim:
claimName: sonarqube-extensions
- name: sonarqube-logs
persistentVolumeClaim:
claimName: sonarqube-logs
status: {}
#sonarqube-service.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose convert -f docker-compose.yml
kompose.version: 1.26.0 (40646f47)
creationTimestamp: null
labels:
io.kompose.service: sonarqube
name: sonarqube
spec:
ports:
- name: "9000"
port: 9000
targetPort: 9000
selector:
io.kompose.service: sonarqube
status:
loadBalancer: {}
# sonarqube-extensions-persistentvolumeclaim.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: sonarqube-extensions
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: sonarqube-extensions
nfs:
path: /nfs/sonarqube-extensions
server: nfs-server-ip
# nfs路径
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: sonarqube-extensions
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: sonarqube-extensions
# sonarqube-logs-persistentvolumeclaim.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: sonarqube-logs
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: sonarqube-logs
nfs:
path: /nfs/sonarqube-logs
server: nfs-server-ip
# nfs路径
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: sonarqube-logs
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: sonarqube-logs
# sonarqube-data-persistentvolumeclaim.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: sonarqube-data
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: sonarqube-data
nfs:
path: /nfs/sonarqube-data
server: nfs-server-ip
# nfs路径
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: sonarqube-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: sonarqube-data
大神啊
渣渣罢了😭 ,记录一下,折腾一上午才折腾出来,不然下次忘了就gg了
最近我们在弄SonarQube和bitbucket的集成
巧了,我们是SonarQube和Gitlab,但是还在测试阶段,不知道能不能用
直接
SonarQube
+ IDEA‘s PluginSonarLint
个人用的话这样确实最简便。但是人太多了,不太好推动,主要是上面非要搞这个,我们下面只能照着做😭
我们还买了lincense,主要是把这个代码检查加入到这个cicd的流程里面来
已经实践了吗。强啊,我们还在测试阶段。