一、SonarQube介绍
SonarQube是一个用于代码质量检测管理的开放平台,可以集成不同的检测工具,代码分析工具,以及持续集成工具。SonarQube 并不是简单地把不同的代码检查工具结果直接显示在 Web 页面上,而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化。<br /> SonarQube不仅提供了对 IDE 的支持,可以在Eclipse和IntelliJ IDEA这些工具里联机查看结果;同时 SonarQube 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用SonarQube,另外Sonar的插件还可以对Java以外的其他编程语言提供支持。
二、代码质量
1.编码规范:是否遵守了编码规范,遵循了最佳实践。
2.潜在的BUG:可能在最坏情况下出现问题的代码,以及存在安全漏洞的代码。
3.文档和注释:过少(缺少必要信息)、过多(没有信息量)、过时的文档或注释。
4.重复代码:违反了Don’tRepeat Yourself原则。
5.复杂度:代码结构太复杂(如圈复杂度高),难以理解、测试和维护。
6.测试覆盖率:编写单元测试,特别是针对复杂代码的测试覆盖是否足够。
7.设计与架构:是否高内聚、低耦合,依赖最少。
三、部署
3.1、部署MySQL
Mysql也可以部署在kubernets中,我这里由于本地虚拟机磁盘不够,所以部署在自己的云服务器上。
1、在官网下载rpm包:https://dev.mysql.com/downloads/repo/yum/
mysql57-community-release-el7-11.noarch.rpm
2、安装yum源
# rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
3、查看是否有我们需要的源
# yum repolist all | grep mysql
Repository epel is listed more than once in the configuration
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 131
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 100
mysql-tools-community-source MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - disabled
mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 384
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server disabled
mysql80-community-source MySQL 8.0 Community Server - disabled
4、安装
# yum install mysql-community-server
5、启动
# systemctl start mysqld
6、授权用户
mysql> grant all privileges on *.* to sonar@'%' identified by 'P@ssW0rd';
mysql> FLUSH PRIVILEGES;
7、创建数据库
mysql> create database sonar charset=utf8;
3.2、部署sonarqube
sonar-deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: sonarqube
namespace: devops
spec:
replicas: 1
template:
metadata:
labels:
app: sonarqube
spec:
containers:
- name: sonarqube
image: sonarqube:lts
imagePullPolicy: IfNotPresent
ports:
- name: port9000
containerPort: 9000
volumeMounts:
- name: sonar-storage
mountPath: "/data/sonar"
readOnly: false
env:
- name: JAVA_OPS
value: "-Duser.timezone=Asia/Shanghai -DsessionTimeout=10080 -Dpermissive-script-security.enabled=true"
- name: SONARQUBE_JDBC_USERNAME
value: sonar
- name: SONARQUBE_JDBC_PASSWORD
value: P@ssW0rd
- name: SONARQUBE_JDBC_URL
value: "jdbc:mysql://122.51.79.172:3306/sonar?useSSL=false&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance"
volumes:
- name: sonar-storage
persistentVolumeClaim:
claimName: sonar-pvc
sonar-storage.yaml
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: sonar-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Delete
nfs:
server: 172.16.1.128
path: /data/k8s/sonar
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sonar-pvc
namespace: devops
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 5Gi
sonar-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: sonar
namespace: devops
spec:
type: NodePort
ports:
- name: sonar
port: 9000
nodePort: 29000
targetPort: 9000
selector:
app: sonarqube
创建资源:
# kubectl apply -f sonar-storage.yaml
# kubectl apply -f sonar-svc.yaml
# kubectl apply -f sonar-deploy.yaml
查看资源结果:
# kubectl get svc -n devops
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
jenkins NodePort 10.68.196.41 <none> 8080:30002/TCP,50000:24073/TCP 6d23h
sonar NodePort 10.68.155.65 <none> 9000:29000/TCP 4m34s
# kubectl get pod -n devops
NAME READY STATUS RESTARTS AGE
jenkins-6595ddd5d-m5fvd 1/1 Running 0 6d23h
sonarqube-6b46dffc95-sv24l 1/1 Running 0 12m
四、用PostgreSQL
(1)、创建secret
kubectl create secret generic postgres-pwd --from-literal=password=P@ssword -n devops
(2)、创建PVC
sonar-storage.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-client-storageclass
provisioner: rookieops/nfs
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: claim-postgres
namespace: devops
spec:
accessModes: ["ReadWriteOnce"]
storageClassName: nfs-client-storageclass
resources:
requests:
storage: 5Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sonar-data
namespace: devops
spec:
accessModes:
- ReadWriteOnce
storageClassName: nfs-client-storageclass
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: sonar-extensions
namespace: devops
spec:
accessModes:
- ReadWriteOnce
storageClassName: nfs-client-storageclass
resources:
requests:
storage: 1Gi
(3)、部署PostgreSQL
sonar-postgres-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
name: sonar-postgres
name: sonar-postgres
namespace: devops
spec:
ports:
- port: 5432
selector:
name: sonar-postgres
sonar-postgres-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sonar-postgres
namespace: devops
spec:
selector:
matchLabels:
name: sonar-postgres
replicas: 1
template:
metadata:
name: sonar-postgres
labels:
name: sonar-postgres
spec:
containers:
- image: postgres:latest
name: sonar-postgres
env:
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-pwd
key: password
- name: POSTGRES_USER
value: sonar
- name: POSTGRES_DB
value: sonar
ports:
- containerPort: 5432
name: postgresport
volumeMounts:
# This name must match the volumes.name below.
- name: data-disk
mountPath: /var/lib/postgresql/data
volumes:
- name: data-disk
persistentVolumeClaim:
claimName: claim-postgres
(4)、部署sonar
sonarqube-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
name: sonar
name: sonar
namespace: devops
spec:
type: NodePort
ports:
- port: 80
targetPort: 9000
name: sonarport
selector:
name: sonarqube
sonarqube-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: sonarqube
namespace: devops
spec:
selector:
matchLabels:
name: sonarqube
replicas: 1
template:
metadata:
name: sonarqube
labels:
name: sonarqube
spec:
initContainers: #设置初始化镜像,执行 system 命令
- name: init-sysctl
image: busybox
imagePullPolicy: IfNotPresent
command: ["sysctl", "-w", "vm.max_map_count=262144"] #必须设置vm.max_map_count这个值调整内存权限,否则启动可能报错
securityContext:
privileged: true #赋予权限能执行系统命令
containers:
- image: sonarqube:latest
args:
- -Dsonar.web.context=/sonar
name: sonarqube
env:
- name: SONARQUBE_JDBC_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-pwd
key: password
- name: SONARQUBE_JDBC_URL
value: jdbc:postgresql://sonar-postgres:5432/sonar
- name: SONARQUBE_JDBC_USERNAME
value: sonar
ports:
- containerPort: 9000
name: sonarqube
volumeMounts:
- mountPath: "/opt/sonarqube/data/"
name: sonar-data
- mountPath: "/opt/sonarqube/extensions/"
name: sonar-extensions
volumes:
- name: sonar-data
persistentVolumeClaim:
claimName: sonar-data
- name: sonar-extensions
persistentVolumeClaim:
claimName: sonar-extensions
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: sonarqube
namespace: devops
spec:
entryPoints:
- web
routes:
- match: Host(`sonarqube-test.coolops.cn`)
kind: Rule
services:
- name: sonar
port: 9000
然后创建配置清单:
# kubectl apply -f .
# kubectl get pod -n devops
NAME READY STATUS RESTARTS AGE
jenkins-686445d7b-vznkg 1/1 Running 4 23h
sonar-postgres-76d98f6b85-nl4d9 1/1 Running 0 12m
sonarqube-5586984d6f-qncq9 1/1 Running 1 12m
五、集成到jenkins
安装插件:SonarQube Scanner
(1)、在Sonar上生产token
记得保存token,然后在jenkins中添加凭证。
(2)、Jenkins对接Sonar
(3)、Jenkins中配置sonar scanner