Kubernetes SonarQube

Posted by Vito on February 2, 2024

Install

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
apiVersion: v1
kind: Service 
metadata:
  name: sonar-postgres
  namespace: devops
spec:
  selector: 
    app: sonar-postgres
  type: ClusterIP
  ports:
  - port: 5432 
    targetPort: 5432
    protocol: TCP
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: sonar-postgres
  namespace: devops
  labels:
    app: sonar-postgres
spec:
  serviceName: "sonar-postgres"
  replicas: 1
  selector:
    matchLabels:
      app: sonar-postgres
  template:
    metadata:
      labels:
        app: sonar-postgres
    spec:
      containers:
      - name: sonar-postgres
        image: postgres:16.1-alpine3.19
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 5432
        env:
        - name: POSTGRES_DB
          value: "sonar"
        - name: POSTGRES_USER
          value: "sonar"
        - name: POSTGRES_PASSWORD 
          value: "59m5p>@wYe"
        - name: ALLOW_IP_RANGE
          value: "0.0.0.0/0"
        volumeMounts:
          - name: sonar-postgres
            mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:
  - metadata:
      name: sonar-postgres
      labels:
        app: sonar-postgres
    spec:
      accessModes: [ "ReadWriteOnce" ]
      #storageClassName: "managed-nfs-storage"
      resources:
        requests:
          storage: 2Gi
---
apiVersion: v1
kind: Service 
metadata:
  name: sonarqube
  namespace: devops
spec:
  selector: 
    app: sonarqube
  type: ClusterIP
  ports:
  - port: 9000 
    targetPort: 9000
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: sonarqube
  namespace: devops
  labels:
    app: sonarqube
spec:
  serviceName: "sonarqube"
  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=524288"]
        #command: [ "/bin/sh", "-c", "--" ]
        #args: [ "sysctl -w vm.max_map_count=524288; sysctl -w fs.file-max=131072; ulimit -n 131072; ulimit -u 8192;" ]
        # ulimit -n 131072; ulimit -u 8192; 只在当前会话有效,因此从命令中去除了
        command: [ "/bin/sh", "-c", "--" ]
        args: [ "sysctl -w vm.max_map_count=524288; sysctl -w fs.file-max=131072;" ]
        securityContext:
          privileged: true
      containers:
      - name: sonarqube
        image: sonarqube:9.9.3-community
        ports:
        - containerPort: 9000
        env:
        - name: SONAR_JDBC_USERNAME
          value: "sonar"
        - name: SONAR_JDBC_PASSWORD
          value: "59m5p>@wYe"
        - name: SONAR_JDBC_URL
          value: "jdbc:postgresql://sonar-postgres:5432/sonar"
        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: 
          requests: 
            memory: 512Mi
            cpu: 10m
          limits: 
            memory: 1.5Gi
            cpu: 1000m
        volumeMounts:
        - name: sonarqube
          mountPath: /opt/sonarqube/data          
          subPath: data
        - name: sonarqube
          mountPath: /opt/sonarqube/extensions
          subPath: extensions
  volumeClaimTemplates:
  - metadata:
      name: sonarqube
      labels:
        app: sonarqube
    spec:
      accessModes: [ "ReadWriteOnce" ]
      #storageClassName: "managed-nfs-storage"
      resources:
        requests:
          storage: 2Gi
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: sonarqube
  namespace: devops
spec: 
  ingressClassName: nginx
  tls:
  - hosts:
    - sonar.zhch.lan
    secretName: zhch.lan
  rules:
  - host: sonar.zhch.lan
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: sonarqube
            port:
              number: 9000

使用

  • https://sonar.zhch.lan/ admin / admin

  • 安装中文插件:Administration -> Marketplace , 点击 ‘I understand the risk’ , 搜索 Chinese ,安装插件
  • 创建 token:我的账号 -> 安全 -> 创建令牌
    • 创建好后立即复制,该令牌不会显示第二次,本例创建的令牌是:sqa_3cf5259d5c4db59ba29de9f15f42e6a18f5545c8