https://www.jianshu.com/p/b35674bd2fad
https://www.cnblogs.com/ligang0357/p/14722277.html
https://blog.51cto.com/u_13397153/2713421?articleABtest=1
Sonar (SonarQube)是一个开源平台,用于管理源代码的质量。 Sonar 不只是一个质量数据报告工具,更是代码质量管理平台。 支持java, JavaScrip, Scala 等等二十几种编程语言的代码质量管理与检测。 SonarQube®是一种自动代码审查工具,用于检测代码中的错误,漏洞和代码异味。它可以与您现有的工作流程集成,以便在项目分支和拉取请求之间进行连续的代码检查。
机器选择 192.168.31.226
root@debian:~# apt install nfs-common nfs-kernel-server
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
nfs-common 已经是最新版 (1:1.3.4-2.5+deb10u1)。
nfs-kernel-server 已经是最新版 (1:1.3.4-2.5+deb10u1)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 77 个软件包未被升级。
root@debian:~# ls
kuboard-data
root@debian:~# mkdir /data/sonarqube -p
mkdir /data/postgres -p #数据库目录
mkdir /data/sonarqube -p
/data/sonarqube *(rw,sync,root_squash,no_subtree_check)
/data/postgres *(rw,sync,root_squash,no_subtree_check)
systemctl status nfs-kernel-server
showmount -e 192.168.31.226
root@aike:~# showmount -e 192.168.31.226
Export list for 192.168.31.226:
/data/sonarqube *
root@aike:~#
1)创建名称空间
kubectl create namespace sonarqube-k8s
pv.yaml 文件内容如下:
apiVersion: v1
kind: PersistentVolume
metadata:
name: sonarqube-k8s-pv
spec:
capacity:
storage: 50Gi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.31.226
path: /data/postgres
4)创建一个 sa 账号
kubectl create sa sonarqube-k8s-sa -n sonarqube-k8s
5)把上面的 sa 账号做 rbac 授权
kubectl create clusterrolebinding sonarqube-k8s-sa-cluster -n \
sonarqube-k8s --clusterrole=cluster-admin --serviceaccount=sonarqube-k8s:sonarqube-k8s-sa
6)通过 deployment 部署 jenkins
3)创建 postgres—pvc
pvc.yaml 文件内容如下:
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: postgres-pvc
namespace: sonarqube-k8s
spec:
resources:
requests:
storage: 20Gi
accessModes:
- ReadWriteMany
#更新资源清单文件
kubectl apply -f pvc.yaml
#查看 pvc 是否创建成功
crictl pull postgres:latest
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres-sonar
namespace: sonarqube-k8s
labels:
app: postgres-sonar
k8s.eip.work/name: postgres-sonar
k8s.eip.work/layer: db
spec:
replicas: 1
selector:
matchLabels:
app: postgres-sonar
template:
metadata:
labels:
app: postgres-sonar
spec:
containers:
- name: postgres-sonar
image: docker.io/library/postgres:latest #这里注意
imagePullPolicy: Always
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: sonar
- name: POSTGRES_USER
value: sonar
- name: POSTGRES_PASSWORD
value: sonar
resources:
limits:
cpu: 1000m
memory: 2048Mi
requests:
cpu: 500m
memory: 1024Mi
volumeMounts:
- name: data
mountPath: /var/lib/postgresql
volumes:
- name: data
persistentVolumeClaim:
claimName: postgres-pvc
#报错显示没有权限操作/var/jenkins_home/copy_reference_file.log 文件,解决办法如下:
kubectl delete -f
chown -R 1000.1000 /data/sonarqube/
chmod -R 777 /data/sonarqube/
kubectl apply -f
#查看 pod 是否创建成功:
7)把 postgres. 前端加上 service,提供外部网络访问
#更新资源清单文件
postgres-service.yaml 文件内容如下:
---
apiVersion: v1
kind: Service
metadata:
name: postgres-service
namespace: sonarqube-k8s
labels:
app: postgres-sonar
spec:
type: NodePort
ports:
- port: 5432
targetPort: 5432
nodePort: 32333
selector:
app: postgres-sonar
sonarqube-pv.yaml 文件内容如下:
apiVersion: v1
kind: PersistentVolume
metadata:
name: sonarqube2-k8s-pv
spec:
capacity:
storage: 50Gi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.31.226
path: /data/sonarqube
sonarqube-pvc.yaml 文件内容如下:
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: sonarqube2-pvc
namespace: sonarqube-k8s
spec:
resources:
requests:
storage: 20Gi
accessModes:
- ReadWriteMany
#更新资源清单文件
先拉取镜像
docker.io/library/busybox 这个
crictl pull sonarqube:latest
apiVersion: apps/v1
kind: Deployment
metadata:
name: sonarqube
namespace: sonarqube-k8s
labels:
app: sonarqube
spec:
replicas: 1
selector:
matchLabels:
app: sonarqube
template:
metadata:
labels:
app: sonarqube
spec:
initContainers:
- name: init-sysctl
image: docker.io/library/busybox:latest
imagePullPolicy: IfNotPresent
command: ["sysctl", "-w", "vm.max_map_count=262144"]
securityContext:
privileged: true
containers:
- name: sonarqube
image: docker.io/library/sonarqube:latest
ports:
- containerPort: 9000
env:
- name: SONARQUBE_JDBC_USERNAME
value: "sonarUser"
- name: SONARQUBE_JDBC_PASSWORD
value: "123456"
- name: SONARQUBE_JDBC_URL
value: "jdbc:postgresql://postgres-sonar:5432/sonarDB"
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: data
subPath: conf
- mountPath: /opt/sonarqube/data
name: data
subPath: data
- mountPath: /opt/sonarqube/extensions
name: data
subPath: extensions
volumes:
- name: data
persistentVolumeClaim:
claimName: sonarqube2-pvc
---
apiVersion: v1
kind: Service
metadata:
name: sonarqube
labels:
app: sonarqube
spec:
type: NodePort
ports:
- name: sonarqube
port: 9000
targetPort: 9000
nodePort: 30003
protocol: TCP
selector:
app: sonarqube
root@debian:/data/sonarqube# ls
conf data extensions
root@debian:/data/sonarqube# chown -R 1000.1000 conf/
root@debian:/data/sonarqube# chown -R 1000.1000 data/
root@debian:/data/sonarqube# chown -R 1000.1000 extensions/
root@debian:/data/sonarqube# chmod -R 777 conf/
root@debian:/data/sonarqube# chmod -R 777 data/
root@debian:/data/sonarqube# chmod -R 777 extensions/
root@debian:/data/sonarqube#
7)把 postgres. 前端加上 service,提供外部网络访问
#更新资源清单文件
postgres-service.yaml 文件内容如下:
---
apiVersion: v1
kind: Service
metadata:
name: sonarqube
namespace: sonarqube-k8s
labels:
app: sonarqube
spec:
type: NodePort
ports:
- name: sonarqube
port: 9000
targetPort: 9000
nodePort: 30003
protocol: TCP
selector:
app: sonarqube
http://192.168.31.236:30003/sessions/new?return_to=%2F
这里选择手动安装 离线安装包路径:https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.0.2311-linux.zip
5,在jenkins-》系统-》系统配置 中配置 SonarQube servers
链图片转存中…(img-KSQmrbNn-1680265936388)]
[外链图片转存中…(img-DM26R9Vp-1680265936389)]
[外链图片转存中…(img-XfEDuEA4-1680265936389)]
[外链图片转存中…(img-kiumqgab-1680265936390)]
这里选择手动安装 离线安装包路径:https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.0.2311-linux.zip
5,在jenkins-》系统-》系统配置 中配置 SonarQube servers