序言

SonarQube需要依赖数据库存储数据,且SonarQube新版本将不再支持Mysql,所以这里推荐设置PostgreSQL作为SonarQube的数据库。本次镜像版本为SonarQube8.9以及PostgreSQL11.4

一、PGSQL部署:

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-data 
  namespace: sonar
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: "nfs"
  resources:
    requests:
      storage: 10Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgresql
  namespace: sonar
  labels:
    app: postgresql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgresql
  template:
    metadata:
      labels:
        app: postgresql
    spec:
      containers:
      - name: postgresql-for-sonar
        image: postgres:11.4
        imagePullPolicy: "IfNotPresent"
        ports:
        - containerPort: 5432
        env:                                        # 这里设置 PostgreSQL 启动时候所需要的环境变量
          - name: POSTGRES_DB                       # 定义要创建的数据库名称
            value: sonarDB
          - name: POSTGRES_USER                     # 定义要创建访问数据库的用户
            value: sonarUser
          - name: POSTGRES_PASSWORD                 # 定义数据库的密码
            value: "***"
        resources:
          limits:
            cpu: 1000m
            memory: 2048Mi
          requests:
            cpu: 500m
            memory: 1024Mi
        volumeMounts:
          - name: data
            mountPath: /var/lib/postgresql/data
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: postgres-data
---
apiVersion: v1
kind: Service
metadata:
  name: postgresql
  namespace: sonar
  labels:
    app: postgresql
spec:
  type: NodePort
 #type: ClusterIP
  ports:
    - port: 5432
      targetPort: 5432
      nodePort: 30543
      protocol: TCP
  selector:
    app: postgresql

二、SonarQUBE部署:

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sonar-pvc
  namespace: sonar
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 20Gi
  storageClassName: nfs
  volumeMode: Filesystem
  ---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sonarqube
  namespace: sonar
  labels:
    app: sonarqube
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sonarqube
  template:
    metadata:
      labels:
        app: sonarqube
    spec:
      volumes:
        - name: sonarqube
          persistentVolumeClaim:
            claimName: sonar-pvc
        - name: sonarqube-config
          configMap:
            name: sonarqube-config
      initContainers:
        - name: init-sysctl
          image: busybox
          command:
            - sysctl
            - '-w'
            - vm.max_map_count=262144
          imagePullPolicy: IfNotPresent
          securityContext:
            privileged: true
      containers:
        - name: sonarqube
          image: mc1arke/sonarqube-with-community-branch-plugin:8.9-community
          ports:
            - containerPort: 9000
              protocol: TCP
          env:
            - name: SONARQUBE_JDBC_USERNAME
              value: sonarUser
            - name: SONARQUBE_JDBC_PASSWORD
              value: ******
            - name: SONARQUBE_JDBC_URL
              value: >-
                jdbc:postgresql://postgresql:5432/sonarDB?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=flase
          resources:
            limits:
              cpu: '2'
              memory: 2Gi
            requests:
              cpu: '1'
              memory: 1Gi
          volumeMounts:
            - name: sonarqube-config
              mountPath: /opt/sonarqube/conf
              subPath: conf
            - name: sonarqube
              mountPath: /opt/sonarqube/data
              subPath: data
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      dnsPolicy: ClusterFirst
---
apiVersion: v1
kind: Service
metadata:
  name: sonarqube
  namespace: sonar
  labels:
    app: sonarqube
spec:
  ports:
    - name: sonarqube
      protocol: TCP
      port: 9000
      targetPort: 9000
      nodePort: 31900 #看需求
  selector:
    app: sonarqube 
  type: NodePort
 #type: ClusterIP
---
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
  name: sonarqube
  namespace: sonar
spec:
  ingressClassName: nginx
  rules:
    - host: sonarqube-sonar.123.cn
      http:
        paths:
          - path: /
            pathType: ImplementationSpecific
            backend:
              service:
                name: sonarqube
                port:
                  number: 9000

三、访问web页面:

可以使用nodeport的端口,比如文中的web页面就是公网:31900

也可以用ingress,配置hosts,访问域名

账号密码都是admin,第一次登录会提示更改密码

如果是英文也别慌,在配置里下载一个汉化插件就可以

四、sonar scanner的使用:宿主机

#下载最新的scanner
wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.0.0.702-linux.zip
#解压缩
unzip sonar-scanner-cli-3.0.0.702-linux.zip /data/
#设置软连接
ln -sv /data/sonar-scanner-cli-3.0.0.702-linux/ /usr/local/sonar-scanner
#使用扫描 请先进入相关项目目录
sonar-scanner -X '-Dsonar.projectKey=项目KEY' '-Dsonar.projectName=项目名' '-Dsonar.sourceEncoding=UTF-8' '-Dsonar.branch.name=分支 '-Dsonar.language=编程语言' '-Dsonar.projectVersion=可以写commitid' #java参数('-Dsonar.sources=./' '-Dsonar.java.binaries=target/classes')#go参数('-Dsonar.exclusions=docs/**,api/**/*.go,**/*.pb.go' )#前端参数(-Dsonar.coverage.dtdVerification=false -Dsonar.sources=src -Dsonar.exclusions=**/node_modules/**,**/*.spec.js,**/test-data/*.js,**/testing/*.js,**/assets/js/*.js  -Dsonar.tests=src -Dsonar.test.inclusions=**/*.spec.js -Dsonar.test.exclusions=**/multiselect-dropdown/*.js) '-Dsonar.host.url=sonar地址' '-Dsonar.login=上面生成的token'

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐