一、SonarQube介绍

  1. SonarQube是一个用于代码质量检测管理的开放平台,可以集成不同的检测工具,代码分析工具,以及持续集成工具。SonarQube 并不是简单地把不同的代码检查工具结果直接显示在 Web 页面上,而是通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化。<br />  SonarQube不仅提供了对 IDE 的支持,可以在EclipseIntelliJ 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/

  1. mysql57-community-release-el7-11.noarch.rpm

2、安装yum源

  1. # rpm -Uvh mysql57-community-release-el7-11.noarch.rpm

3、查看是否有我们需要的源

  1. # yum repolist all | grep mysql
  2. Repository epel is listed more than once in the configuration
  3. mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
  4. mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
  5. mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
  6. mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
  7. mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 131
  8. mysql-connectors-community-source MySQL Connectors Community - disabled
  9. mysql-tools-community/x86_64 MySQL Tools Community enabled: 100
  10. mysql-tools-community-source MySQL Tools Community - Sourc disabled
  11. mysql-tools-preview/x86_64 MySQL Tools Preview disabled
  12. mysql-tools-preview-source MySQL Tools Preview - Source disabled
  13. mysql55-community/x86_64 MySQL 5.5 Community Server disabled
  14. mysql55-community-source MySQL 5.5 Community Server - disabled
  15. mysql56-community/x86_64 MySQL 5.6 Community Server disabled
  16. mysql56-community-source MySQL 5.6 Community Server - disabled
  17. mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 384
  18. mysql57-community-source MySQL 5.7 Community Server - disabled
  19. mysql80-community/x86_64 MySQL 8.0 Community Server disabled
  20. mysql80-community-source MySQL 8.0 Community Server - disabled

4、安装

  1. # yum install mysql-community-server

5、启动

  1. # systemctl start mysqld

6、授权用户

  1. mysql> grant all privileges on *.* to sonar@'%' identified by 'P@ssW0rd';
  2. mysql> FLUSH PRIVILEGES;

7、创建数据库

  1. mysql> create database sonar charset=utf8;

3.2、部署sonarqube

sonar-deploy.yaml

  1. apiVersion: extensions/v1beta1
  2. kind: Deployment
  3. metadata:
  4. name: sonarqube
  5. namespace: devops
  6. spec:
  7. replicas: 1
  8. template:
  9. metadata:
  10. labels:
  11. app: sonarqube
  12. spec:
  13. containers:
  14. - name: sonarqube
  15. image: sonarqube:lts
  16. imagePullPolicy: IfNotPresent
  17. ports:
  18. - name: port9000
  19. containerPort: 9000
  20. volumeMounts:
  21. - name: sonar-storage
  22. mountPath: "/data/sonar"
  23. readOnly: false
  24. env:
  25. - name: JAVA_OPS
  26. value: "-Duser.timezone=Asia/Shanghai -DsessionTimeout=10080 -Dpermissive-script-security.enabled=true"
  27. - name: SONARQUBE_JDBC_USERNAME
  28. value: sonar
  29. - name: SONARQUBE_JDBC_PASSWORD
  30. value: P@ssW0rd
  31. - name: SONARQUBE_JDBC_URL
  32. value: "jdbc:mysql://122.51.79.172:3306/sonar?useSSL=false&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance"
  33. volumes:
  34. - name: sonar-storage
  35. persistentVolumeClaim:
  36. claimName: sonar-pvc

sonar-storage.yaml

  1. ---
  2. apiVersion: v1
  3. kind: PersistentVolume
  4. metadata:
  5. name: sonar-pv
  6. spec:
  7. capacity:
  8. storage: 5Gi
  9. accessModes:
  10. - ReadWriteMany
  11. persistentVolumeReclaimPolicy: Delete
  12. nfs:
  13. server: 172.16.1.128
  14. path: /data/k8s/sonar
  15. ---
  16. apiVersion: v1
  17. kind: PersistentVolumeClaim
  18. metadata:
  19. name: sonar-pvc
  20. namespace: devops
  21. spec:
  22. accessModes:
  23. - ReadWriteMany
  24. resources:
  25. requests:
  26. storage: 5Gi

sonar-svc.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. name: sonar
  5. namespace: devops
  6. spec:
  7. type: NodePort
  8. ports:
  9. - name: sonar
  10. port: 9000
  11. nodePort: 29000
  12. targetPort: 9000
  13. selector:
  14. app: sonarqube

创建资源:

  1. # kubectl apply -f sonar-storage.yaml
  2. # kubectl apply -f sonar-svc.yaml
  3. # kubectl apply -f sonar-deploy.yaml

查看资源结果:

  1. # kubectl get svc -n devops
  2. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  3. jenkins NodePort 10.68.196.41 <none> 8080:30002/TCP,50000:24073/TCP 6d23h
  4. sonar NodePort 10.68.155.65 <none> 9000:29000/TCP 4m34s
  5. # kubectl get pod -n devops
  6. NAME READY STATUS RESTARTS AGE
  7. jenkins-6595ddd5d-m5fvd 1/1 Running 0 6d23h
  8. sonarqube-6b46dffc95-sv24l 1/1 Running 0 12m

四、用PostgreSQL

(1)、创建secret

  1. kubectl create secret generic postgres-pwd --from-literal=password=P@ssword -n devops

(2)、创建PVC
sonar-storage.yaml

  1. apiVersion: storage.k8s.io/v1
  2. kind: StorageClass
  3. metadata:
  4. name: nfs-client-storageclass
  5. provisioner: rookieops/nfs
  6. ---
  7. apiVersion: v1
  8. kind: PersistentVolumeClaim
  9. metadata:
  10. name: claim-postgres
  11. namespace: devops
  12. spec:
  13. accessModes: ["ReadWriteOnce"]
  14. storageClassName: nfs-client-storageclass
  15. resources:
  16. requests:
  17. storage: 5Gi
  18. ---
  19. apiVersion: v1
  20. kind: PersistentVolumeClaim
  21. metadata:
  22. name: sonar-data
  23. namespace: devops
  24. spec:
  25. accessModes:
  26. - ReadWriteOnce
  27. storageClassName: nfs-client-storageclass
  28. resources:
  29. requests:
  30. storage: 1Gi
  31. ---
  32. apiVersion: v1
  33. kind: PersistentVolumeClaim
  34. metadata:
  35. name: sonar-extensions
  36. namespace: devops
  37. spec:
  38. accessModes:
  39. - ReadWriteOnce
  40. storageClassName: nfs-client-storageclass
  41. resources:
  42. requests:
  43. storage: 1Gi

(3)、部署PostgreSQL
sonar-postgres-service.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. name: sonar-postgres
  6. name: sonar-postgres
  7. namespace: devops
  8. spec:
  9. ports:
  10. - port: 5432
  11. selector:
  12. name: sonar-postgres

sonar-postgres-deployment.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: sonar-postgres
  5. namespace: devops
  6. spec:
  7. selector:
  8. matchLabels:
  9. name: sonar-postgres
  10. replicas: 1
  11. template:
  12. metadata:
  13. name: sonar-postgres
  14. labels:
  15. name: sonar-postgres
  16. spec:
  17. containers:
  18. - image: postgres:latest
  19. name: sonar-postgres
  20. env:
  21. - name: POSTGRES_PASSWORD
  22. valueFrom:
  23. secretKeyRef:
  24. name: postgres-pwd
  25. key: password
  26. - name: POSTGRES_USER
  27. value: sonar
  28. - name: POSTGRES_DB
  29. value: sonar
  30. ports:
  31. - containerPort: 5432
  32. name: postgresport
  33. volumeMounts:
  34. # This name must match the volumes.name below.
  35. - name: data-disk
  36. mountPath: /var/lib/postgresql/data
  37. volumes:
  38. - name: data-disk
  39. persistentVolumeClaim:
  40. claimName: claim-postgres

(4)、部署sonar
sonarqube-service.yaml

  1. apiVersion: v1
  2. kind: Service
  3. metadata:
  4. labels:
  5. name: sonar
  6. name: sonar
  7. namespace: devops
  8. spec:
  9. type: NodePort
  10. ports:
  11. - port: 80
  12. targetPort: 9000
  13. name: sonarport
  14. selector:
  15. name: sonarqube

sonarqube-deployment.yaml

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: sonarqube
  5. namespace: devops
  6. spec:
  7. selector:
  8. matchLabels:
  9. name: sonarqube
  10. replicas: 1
  11. template:
  12. metadata:
  13. name: sonarqube
  14. labels:
  15. name: sonarqube
  16. spec:
  17. initContainers: #设置初始化镜像,执行 system 命令
  18. - name: init-sysctl
  19. image: busybox
  20. imagePullPolicy: IfNotPresent
  21. command: ["sysctl", "-w", "vm.max_map_count=262144"] #必须设置vm.max_map_count这个值调整内存权限,否则启动可能报错
  22. securityContext:
  23. privileged: true #赋予权限能执行系统命令
  24. containers:
  25. - image: sonarqube:latest
  26. args:
  27. - -Dsonar.web.context=/sonar
  28. name: sonarqube
  29. env:
  30. - name: SONARQUBE_JDBC_PASSWORD
  31. valueFrom:
  32. secretKeyRef:
  33. name: postgres-pwd
  34. key: password
  35. - name: SONARQUBE_JDBC_URL
  36. value: jdbc:postgresql://sonar-postgres:5432/sonar
  37. - name: SONARQUBE_JDBC_USERNAME
  38. value: sonar
  39. ports:
  40. - containerPort: 9000
  41. name: sonarqube
  42. volumeMounts:
  43. - mountPath: "/opt/sonarqube/data/"
  44. name: sonar-data
  45. - mountPath: "/opt/sonarqube/extensions/"
  46. name: sonar-extensions
  47. volumes:
  48. - name: sonar-data
  49. persistentVolumeClaim:
  50. claimName: sonar-data
  51. - name: sonar-extensions
  52. persistentVolumeClaim:
  53. claimName: sonar-extensions
  54. ---
  55. apiVersion: traefik.containo.us/v1alpha1
  56. kind: IngressRoute
  57. metadata:
  58. name: sonarqube
  59. namespace: devops
  60. spec:
  61. entryPoints:
  62. - web
  63. routes:
  64. - match: Host(`sonarqube-test.coolops.cn`)
  65. kind: Rule
  66. services:
  67. - name: sonar
  68. port: 9000

然后创建配置清单:

  1. # kubectl apply -f .
  2. # kubectl get pod -n devops
  3. NAME READY STATUS RESTARTS AGE
  4. jenkins-686445d7b-vznkg 1/1 Running 4 23h
  5. sonar-postgres-76d98f6b85-nl4d9 1/1 Running 0 12m
  6. sonarqube-5586984d6f-qncq9 1/1 Running 1 12m

image.png

五、集成到jenkins

安装插件:SonarQube Scanner

(1)、在Sonar上生产token
image.png
image.png
记得保存token,然后在jenkins中添加凭证。

(2)、Jenkins对接Sonar
image.png

(3)、Jenkins中配置sonar scanner
image.png