zoukankan      html  css  js  c++  java
  • 在k8s中搭建sonarqube并集成到jenkins

    前提:已经搭建好k8s环境并生成存储类

    1,搭建postgres数据库

    编写yaml文件

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: postgres-pvc
      namespace: devops
    spec:
      accessModes:
        - ReadWriteMany
      storageClassName: nfs-jenkins
      resources:
        requests:
          storage: 10Gi
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: postgres-sonar
      namespace: devops
      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: 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/data
          volumes:
            - name: data
              persistentVolumeClaim:
                claimName: postgres-pvc
    
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: postgres-sonar
      namespace: devops
      labels:
        app: postgres-sonar
    spec:
      type: NodePort
      ports:
      - port: 5432
        targetPort: 5432
        nodePort: 32333
      selector:
        app: postgres-sonar

    执行

    kubectl  apply -f postgres.yaml

    2,安装sonarqube服务

    编写yaml

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: sonar-data
      namespace: devops
    spec:
      accessModes:
        - ReadWriteMany
      storageClassName: nfs-jenkins
      resources:
        requests:
          storage: 100Gi
    ---
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: sonarqube
      namespace: devops
      labels:
        app: sonarqube
        k8s.eip.work/name: sonarqube
        k8s.eip.work/layer: web
    spec:
      serviceName: sonar
      replicas: 1
      selector:
        matchLabels:
          app: sonarqube
      template:
        metadata:
          labels:
            app: sonarqube
        spec:
          initContainers:
          - name: init-sysctl
            image: busybox
            imagePullPolicy: IfNotPresent
            command: ["sysctl", "-w", "vm.max_map_count=262144"]
            securityContext:
              privileged: true
          containers:
          - name: sonarqube
            image: sonarqube:latest
            ports:
            - containerPort: 9000
            env:
            - name: SONARQUBE_JDBC_USERNAME
              value: sonar
            - name: SONARQUBE_JDBC_PASSWORD
              value: sonar
            - name: SONARQUBE_JDBC_URL
              value: jdbc:postgresql://postgres-sonar:5432/sonar
            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: sonar-data
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: sonarqube
      namespace: devops
      labels:
        app: sonarqube
    spec:
      ports:
        - name: sonarqube
          port: 9000
          targetPort: 9000
          protocol: TCP
      selector:
        app: sonarqube
    ---
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: sonar-ingress
      namespace: devops
    spec:
      rules:
      - host: test.sonarqube.3yx.cn
        http:
          paths:
          - path: /
            backend:
              serviceName: sonarqube
              servicePort: 9000

    执行

    kubectl  apply -f postgres.yaml

    3,在Jenkins中安装插件

    4,在jenkins-》系统-》全局工具配置中配置sonarscanner

     这里选择手动安装 离线安装包路径:https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.0.2311-linux.zip

    5,在jenkins-》系统-》系统配置 中配置 SonarQube servers

     

    这里的token在sonar中生成

    6,在sonarqube 中设置回调

    如果没有create

     name 随便写 url 前面部分是jenkins地址 后面的是 固定的地址 /sonarqube-webhook/

    7,配置nodejs

    (1)在jenkins插件管理搜索nodejs 插件安装

    (2)在系统-》全局工具中配置

     8,配置 k8s jenkins-slave 环境变量(否则中文路径会报错)

    9,pipeline 新增 需要放到构建步骤后面

            stage('sonarqube-analysis') {
                steps{
                    script {
                        env.PATH = "${jdk_home}/bin:${mvnHome}/bin:${env.PATH}:${nodejs_home}/bin"
                        sonarqubeScannerHome = tool name: 'sonarScanner4.6', type: 'hudson.plugins.sonar.SonarRunnerInstallation'
                    }
                    withSonarQubeEnv('sonarqube') {
                        // Optionally use a Maven environment you've configured already
                        sh "${sonarqubeScannerHome}/bin/sonar-scanner -Dsonar.projectName=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME}"
                    }
                    timeout(time: 10) {
                        // Parameter indicates whether to set pipeline to UNSTABLE if Quality Gate fails
                        // true = set pipeline to UNSTABLE, false = don't
                        waitForQualityGate abortPipeline: true
                    }
                }
            }

    前面配置好环境变量

            def mvnHome = tool 'maven339'
            def jdk_home = tool 'jdk8'
            def nodejs_home = tool 'nodejs16'

    10,在项目根目录新建  sonar-project.properties 文件

    sonar.projectKey=mypjname
    sonar.projectName=mypjname
    sonar.projectVersion=1.0

    sonar.language=java
    sonar.sourceEncoding=UTF-8
    sonar.java.source=1.8
    sonar.sources=src
    sonar.java.binaries=target/classes
    sonar.java.libraries=target/*/WEB-INF/lib/*.jar

    到这里配置完了在Jenkins中点击构建即可

  • 相关阅读:
    C++默认参数
    C++中对象初始化
    类设计者的核查表
    函数返回数组
    UVA439 骑士的移动 Knight Moves
    P2415 集合求和(一道洛谷好题鸭)(虽然可以水过,但有必研究DP)
    最小生成树(Kruskal)
    并查集(许多东西的基本哦)
    堆模板(STL版)
    线段树模板(贼慢的版本)
  • 原文地址:https://www.cnblogs.com/ligang0357/p/14722277.html
Copyright © 2011-2022 走看看