SonarQube 容器化部署

SonarQube是一个开源的代码质量管理系统,可用来快速定位代码中的Bug、漏洞以及不优雅的代码。它支持几乎所有的常见编程语言,例如Java、JavaScript、TypeScript、Kotlin、Ruby、Go, Scala等。并且还有插件机制,利用插件,可以让SonarQube更加强大,例如可以整合Findbugs、PMD、Checkstyle等。可以说,SonarQube是一款提升项目代码质量必备的根据。

系统需求:

  • k8s:1.13.5以上版本
  • pgsql 10及以上

docker-compose

官方提供了docker-compose的文件,把data、log、extensions目录挂载到了本机目录,以便管理文件以及查看日志。

#docker-compose.yml
version: "3"

services:
  sonarqube:
    image: sonarqube:lts-community #8.9.2
    depends_on:
      - db
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://pgsql/sonarqube?currentSchema=my_schema
      SONAR_JDBC_USERNAME: postgres
      SONAR_JDBC_PASSWORD: passwd
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
      #将data、log、extensions目录挂载到本机目录
    ports:
      - "9000:9000"
volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:

kubernetes-yaml

通过kompose将转换为k8s的yaml文件,需要创建三个pv以及pvc,用于挂载文件。

#sonarqube-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert -f docker-compose.yml
    kompose.version: 1.26.0 (40646f47)
  creationTimestamp: null
  labels:
    io.kompose.service: sonarqube
  name: sonarqube
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: sonarqube
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert -f docker-compose.yml
        kompose.version: 1.26.0 (40646f47)
      creationTimestamp: null
      labels:
        io.kompose.service: sonarqube
    spec:
      containers:
        - env:
            - name: SONAR_JDBC_PASSWORD
              value: passwd
            - name: SONAR_JDBC_URL
              value: jdbc:postgresql://pgsql/sonarqube?currentSchema=public
            - name: SONAR_JDBC_USERNAME
              value: postgres
          image: sonarqube:lts-community
          name: sonarqube
          ports:
            - containerPort: 9000
          resources: {}
          volumeMounts:
          #将data、log、extensions目录挂载到机器目录
            - mountPath: /opt/sonarqube/data
              name: sonarqube-data
            - mountPath: /opt/sonarqube/extensions
              name: sonarqube-extensions
            - mountPath: /opt/sonarqube/logs
              name: sonarqube-logs
      restartPolicy: Always
      volumes:
        - name: sonarqube-data
          persistentVolumeClaim:
            claimName: sonarqube-data
        - name: sonarqube-extensions
          persistentVolumeClaim:
            claimName: sonarqube-extensions
        - name: sonarqube-logs
          persistentVolumeClaim:
            claimName: sonarqube-logs
status: {}

#sonarqube-service.yaml


apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert -f docker-compose.yml
    kompose.version: 1.26.0 (40646f47)
  creationTimestamp: null
  labels:
    io.kompose.service: sonarqube
  name: sonarqube
spec:
  ports:
    - name: "9000"
      port: 9000
      targetPort: 9000
  selector:
    io.kompose.service: sonarqube
status:
  loadBalancer: {}


# sonarqube-extensions-persistentvolumeclaim.yaml


apiVersion: v1
kind: PersistentVolume
metadata:
  name: sonarqube-extensions
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: sonarqube-extensions
  nfs:
    path: /nfs/sonarqube-extensions
    server: nfs-server-ip
    # nfs路径



kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: sonarqube-extensions
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
  storageClassName: sonarqube-extensions
# sonarqube-logs-persistentvolumeclaim.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: sonarqube-logs
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: sonarqube-logs
  nfs:
    path: /nfs/sonarqube-logs
    server: nfs-server-ip
    # nfs路径


kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: sonarqube-logs
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: sonarqube-logs



# sonarqube-data-persistentvolumeclaim.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: sonarqube-data
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: sonarqube-data
  nfs:
    path: /nfs/sonarqube-data
    server: nfs-server-ip
    # nfs路径


kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: sonarqube-data
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
  storageClassName: sonarqube-data

image.png

   
  • Docker

    Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的镜像中,然后发布到任何流行的 Linux或Windows 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口

    29 引用
  • k8s

    10 引用