Loading
Loading...

Kubernetes 中部署代码质量检测工具 SonarQube

环境说明:

一、SonarQube 介绍

简介

       SonarQube 是一个用于代码质量管理的开源平台,用于管理源代码的质量。同时 SonarQube 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 SonarQube。此外 SonarQube 的插件还可以对 Java 以外的其他编程语言提供支持,对国际化以及报告文档化也有良好的支持。

特性

  • 多语言的平台: 支持超过20种编程语言,包括Java、Python、C#、C/C++、JavaScript等常用语言。
  • 自定义规则: 用户可根据不同项目自定义Quality Profile以及Quality Gates。
  • 丰富的插件: SonarQube 拥有丰富的插件,从而拥有强大的可扩展性。
  • 持续集成: 通过对某项目的持续扫描,可以对该项目的代码质量做长期的把控,并且预防新增代码中的不严谨和冗余。
  • 质量门: 在扫描代码后可以通过对“质量门”的比对判定此次“构建”的结果是否通过,质量门可以由用户定义,由多维度判定是否通过。

二、部署过程描述


       这里我们将 SonarQube 部署到 Kubernetes 中需要提前知道的是 SonarQube 需要依赖数据库存储数据,且 SonarQube7.9 及其以后版本将不再支持 Mysql,所以这里推荐设置 PostgreSQL 作为 SonarQube 的存储库,下面将记录在 Kubernetes 中部署 PostgreSQL 和 SonarQube 过程。


三、部署 PostgreSQL

存储驱动为 NFS,需要提前在 NFS 目录中创建对应存储文件夹并设置文件夹其它用户拥有一定权限,然后设置 PV 与 NFS 地址和目录绑定。

1、创建 PV & PVC

postgres-storage.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
name: postgres
labels:
app: postgres
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
mountOptions: #NFS挂在选项
- hard
- nfsvers=4.1 #NFS Server版本
nfs: #NFS设置
server: 192.168.2.11
path: /nfs/data/postgres
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: postgres
labels:
app: postgres
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi #设置大小为5G
selector:
matchLabels:
app: postgres

创建 PostgreSQL PV & PVC

  • -n:指定启动的 Namespace,这个值替换为自己集群的 Namespace
Terminal window
$ kubectl apply -f postgres-storage.yaml -n mydlqcloud

2、创建 PostgreSQL

postgres.yaml

apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
app: postgres
spec:
type: NodePort #指定为 NodePort 方式暴露出口
ports:
- name: server
port: 5432
targetPort: 5432
nodePort: 30543 #指定 Nodeport 端口
protocol: TCP
selector:
app: postgres
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
labels:
app: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:11.4
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB #PostgreSQL 数据库名称
value: "sonarDB"
- name: POSTGRES_USER #PostgreSQL 用户名
value: "sonarUser"
- name: POSTGRES_PASSWORD #PostgreSQL 密码
value: "123456"
resources:
limits:
cpu: 1000m
memory: 2048Mi
requests:
cpu: 500m
memory: 1024Mi
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgredb
volumes:
- name: postgredb
persistentVolumeClaim:
claimName: postgres #引用上面创建的 PVC

创建 PostgreSQL

  • -n:指定启动的 Namespace,这个值替换为自己集群的 Namespace
Terminal window
$ kubectl apply -f postgres.yaml -n mydlqcloud

四、部署 SonarQube

存储驱动为 NFS,需要提前在 NFS 下创建对应存储文件夹,然后设置 PV 与 NFS 地址和目录绑定。

1、创建 PV & PVC

注意:这里用的是 NFS 存储,请提前在 NFS 共享目录中创建该目录 SonarQube 为了安全是不允许使用 Root 用户启动,并且 Dockerfile 中设置了 sonarqube(999) 用户与 sonarqube(999) 用户组,所以为了确保成功启动,请提前设置文件夹权限,否则启动将提升无法加载 ES 或者 Log 等文件。

sonarqube-storage.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
name: sonarqube
labels:
app: sonarqube
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
mountOptions: #NFS挂在选项
- hard
- nfsvers=4.1 #NFS Server版本
nfs: #NFS设置
server: 192.168.2.11
path: /nfs/data/sonarqube
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: sonarqube
labels:
app: sonarqube
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi #设置大小为5G
selector:
matchLabels:
app: sonarqube

创建 Sonarqube PV & PVC

  • -n:指定启动的 Namespace,这个值替换为自己集群的 Namespace
Terminal window
$ kubectl apply -f sonarqube-storage.yaml -n mydlqcloud

2、创建 Service & Deployment

sonarqube.yaml

apiVersion: v1
kind: Service
metadata:
name: sonarqube
labels:
app: sonarqube
spec:
type: NodePort #指定 NodePort 端口
ports:
- name: sonarqube
port: 9000
targetPort: 9000
nodePort: 30900 #指定 NodePort 端口
protocol: TCP
selector:
app: sonarqube
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sonarqube
labels:
app: sonarqube
spec:
replicas: 1
selector:
matchLabels:
app: sonarqube
template:
metadata:
labels:
app: 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:
- name: sonarqube
image: "sonarqube:7.9-community"
ports:
- containerPort: 9000
env:
- name: SONARQUBE_JDBC_USERNAME
value: "sonarUser" #引用 PostgreSQL 配置中设置的用户名
- name: SONARQUBE_JDBC_PASSWORD
value: "123456" #引用 PostgreSQL 配置中设置的密码
- name: SONARQUBE_JDBC_URL
value: "jdbc:postgresql://postgres:5432/sonarDB" #指定 PostgreSQL 在 Kubernetes 中的地址
livenessProbe:
httpGet:
path: /sessions/new
port: 9000
initialDelaySeconds: 60
periodSeconds: 30
readinessProbe:
httpGet:
path: /sessions/new
port: 9000
initialDelaySeconds: 60
periodSeconds: 30
failureThreshold: 6
resources:
limits:
cpu: 2000m
memory: 2048Mi
requests:
cpu: 1000m
memory: 1024Mi
volumeMounts:
- mountPath: /opt/sonarqube/conf
name: sonarqube
subPath: conf
- mountPath: /opt/sonarqube/data
name: sonarqube
subPath: data
- mountPath: /opt/sonarqube/extensions
name: sonarqube
subPath: extensions
volumes:
- name: sonarqube
persistentVolumeClaim:
claimName: sonarqube #绑定上面创建的 PVC

创建 SonarQube

  • -n:指定启动的 Namespace,这个值替换为自己集群的 Namespace
Terminal window
$ kubectl apply -f sonarqube.yaml -n mydlqcloud

注意:如果项目启动失败,或者显示无法执行 initContainers 操作,那么可能是没有 RBAC 权限导致,请创建一个有一定权限的 ServiceAccount 后,再在 SonarQube 部署文件中指定这个 ServiceAccount 即可。

五、配置 SonarQube

1、登录 SonarQube

程序启动完成后,进入 SonarQube 页面,上面 SonarQube Service 以 NodePort 方式暴露 SonarQube 到集群外,并且本人 Kubernetes 集群地址为”192.168.2.11”,所以这里输入地址 http://192.168.2.11:30900 访问 SonarQube UI 界面。

输入默认用户名”admin”,密码与用户名一致,进而登录到 SonarQube 控制台

2、SonarQube 安装插件

我们按照 SonarQube 后需要安装许多插件来完善这个工具,这里我们安装 Chinese 和 SonarJava 俩个插件,其中第一个插件为界面提供汉化,第二个插件是 Java 语言分析插件。当然,也可以选择你自己想安装的插件进行安装。

待安装插件列表:

  • Chinese
  • SonarJava

(1)、安装 Chinese:

打开 Administration->Marktplace ,然后搜索栏输入 “Chinese” ,等待一段时间(国内访问国外地址比较慢)后弹出相关插件列表,选择 Chinese Pack 进行安装。

安装完成后会弹出重启 SonarQube 服务的窗口,点击一下进行容器,再次进入后可以看到界面已经汉化

(2)、安装 SonarJava:

安装过程跟上面类似,搜索 “Java” 选择 SonarJava 插件安装

到此完成在 Kubernetes 部署 SonarQube 这个过程。

六、安装过程中遇到的问题

(1)、对 Mysql 不再支持

在 SonarQube7.9 及以后版本将不再支持 Mysql,请使用 Postgresql 数据库

Terminal window
2019.07.04 04:40:50 INFO web[][o.s.p.ProcessEntryPoint] Starting web
2019.07.04 04:40:50 INFO web[][o.a.t.u.n.NioSelectorPool] Using a shared selector for servlet write/read
2019.07.04 04:40:51 INFO web[][o.e.p.PluginsService] no modules loaded
2019.07.04 04:40:51 INFO web[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.join.ParentJoinPlugin]
2019.07.04 04:40:51 INFO web[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.percolator.PercolatorPlugin]
2019.07.04 04:40:51 INFO web[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
2019.07.04 04:40:53 INFO web[][o.s.s.e.EsClientProvider] Connected to local Elasticsearch: [127.0.0.1:9001]
2019.07.04 04:40:53 INFO web[][o.s.s.p.LogServerVersion] SonarQube Server / 7.9.0.26994 / 687d8007c0f62bfaaa44a890d93397de0a588119
2019.07.04 04:40:53 ERROR web[][o.s.s.p.Platform] Web server startup failed:
#############################################################################################################
# End of Life of MySQL Support : SonarQube 7.9 and future versions do not support MySQL. #
# Please migrate to a supported database. Get more details at #
# https://community.sonarsource.com/t/end-of-life-of-mysql-support #
# and https://github.com/SonarSource/mysql-migrator #
#############################################################################################################

(2)、 vm.max_map_count 错误

Terminal window
2019.07.04 04:39:13 INFO app[][o.e.p.PluginsService] loaded plugin [org.elasticsearch.transport.Netty4Plugin]
ERROR: [1] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
2019.07.04 04:39:19 WARN app[][o.s.a.p.AbstractManagedProcess] Process exited with exit value [es]: 78

这个错误主要是镜像系统中设置的内存权限过小,需要初始化容器时候执行”sysctl -w vm.max_map_count=262144”命令,可以按下面方法解决:

apiVersion: apps/v1
kind: Deployment
metadata:
name: sonarqube-server
labels:
app: sonarqube-server
spec:
replicas: 1
selector:
matchLabels:
app: sonarqube-server
template:
metadata:
labels:
app: sonarqube-server
spec:
initContainers: #设置初始化镜像,执行 system 命令
- name: init-sysctl
image: busybox
imagePullPolicy: IfNotPresent
command: ["sysctl", "-w", "vm.max_map_count=262144"]
securityContext:
privileged: true
......

(3)、exited with exit value [es] 错误

Terminal window
2019.07.04 02:40:53 WARN app[][o.s.a.p.AbstractProcessMonitor] Process exited with exit value [es]: 1

发现这个错误主要原因是,Sonarqube 要求以非 root 用户启动,如果设置容器安全”runAsUser: 0”等参数,以 root 用户运行就会抛出以下错误,去掉 root 用户权限,设置镜像默认用户启动即可。

---END---
如果本文对你有帮助,可以关注我的公众号 "小豆丁技术栈" 了解最新动态,顺便也请帮忙 Github 点颗星哦,感谢~

本文作者:超级小豆丁 @ 小豆丁技术栈

本文链接:http://www.mydlq.club/article/25/

本文标题:Kubernetes 中部署代码质量检测工具 SonarQube

本文版权:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!