Kubernetes安装部署SonarQube 9.x并安装中文插件;

文章目录

  • 所需镜像
  • 一、安装postgres
    • 1.postgres PVC
    • 2.postgres Deployment
    • 3.postgres Service
    • 4.启动postgres
  • 二、安装sonarqube
    • 1.sonarqube PVC
    • 2.sonarqube Deployment
    • 3.sonarqube Service
    • 4.sonarqube Ingress
    • 5.启动sonarqube
  • 三、sonarqube设置中文
    • 1.登录sonarqube
    • 2.下载中文插件


所需镜像

镜像 版本
postgres 11.6
sonarqube 9.0.0-community
buxybox 1.27.1

一、安装postgres

sonarqube7.8以上的版本都需要使用postgres为底层数据库

1.postgres PVC

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: sonar-postgres
  namespace: sonar
spec:
  storageClassName: "managed-nfs-storage" #nfs动态供给存储类的名字,可以使用kubectl get sc 查看到;
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

2.postgres Deployment

kind: Deployment
apiVersion: apps/v1
metadata:
  name: sonar-db
  namespace: sonar
  labels:
    app: sonar-db
    name: sonar-db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sonar-db
      name: sonar-db
  template:
    metadata:
      labels:
        app: sonar-db
        name: sonar-db
    spec:
      volumes:
        - name: sonar-postgres
          persistentVolumeClaim:
            claimName: sonar-postgres
      containers:
        - name: postgres
          image: 'postgres:11.6'
          ports:
            - containerPort: 5432
              protocol: TCP
          env:
            - name: POSTGRES_DB
              value: sonardb
            - name: POSTGRES_USER #数据库的用户名
              value: sonar
            - name: POSTGRES_PASSWORD #数据库的密码
              value: sonar
          resources:
            limits:
              cpu: '1'
              memory: 2Gi
            requests:
              cpu: 800m
              memory: 2Gi
          volumeMounts:
            - name: sonar-postgres
              mountPath: /var/lib/postgresql/data
          imagePullPolicy: IfNotPresent
      restartPolicy: Always

3.postgres Service

kind: Service
apiVersion: v1
metadata:
  name: sonar-db
  namespace: sonar
  labels:
    name: sonar-db
spec:
  ports:
    - name: sonardb
      protocol: TCP
      port: 5432
      targetPort: 5432
  selector:
    name: sonar-db
  type: ClusterIP

4.启动postgres

这里我是把上述的内容合到一个yaml文件里了

#创建sonar命名空间
[root@k8s-master sonar_workspace]# kubectl create ns sonar 
namespace/sonar created
[root@k8s-master sonar_workspace]# kubectl apply -f postgres.yaml 
deployment.apps/sonar-db created
persistentvolumeclaim/sonar-postgres created
service/sonar-db created
[root@k8s-master sonar_workspace]# kubectl get pod -n sonar
NAME                         READY   STATUS            RESTARTS   AGE
sonar-db-545676f4c6-zvjw8    1/1     Running           0          4m26s


二、安装sonarqube

1.sonarqube PVC

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: sonar-data
  namespace: sonar
spec:
  storageClassName: "managed-nfs-storage" #nfs动态供给存储类的名字,可以使用kubectl get sc 查看到;
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: sonar-extensions
  namespace: sonar
spec:
  storageClassName: "managed-nfs-storage" #nfs动态供给存储类的名字,可以使用kubectl get sc 查看到;
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 1Gi


2.sonarqube Deployment

ind: Deployment
apiVersion: apps/v1
metadata:
  name: sonarqube
  namespace: sonar
  labels:
    app: sonarqube
    name: sonarqube
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sonarqube
      name: sonarqube
  template:
    metadata:
      labels:
        app: sonarqube
        name: sonarqube
    spec:
      volumes:
        - name: sonar-extensions
          persistentVolumeClaim:
            claimName: sonar-extensions
        - name: sonar-data
          persistentVolumeClaim:
            claimName: sonar-data
      initContainers:
        - name: init-sysctl
          image: 'busybox:1.27.1'
          command:   # elasticsearch用户拥有的内存权限太小,es启动会报错,此命令保证es正常启动
            - sysctl
            - '-w'
            - vm.max_map_count=262144
          ports:
            - containerPort: 80
              protocol: TCP
          resources:
            limits:
              cpu: '1'
              memory: 2Gi
            requests:
              cpu: 300m
              memory: 2Gi
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
          securityContext:   # 配置特权
            privileged: true
      containers:
        - name: sonarqube
          image: 'sonarqube:9.0.0-community'
          ports:
            - containerPort: 9000
              protocol: TCP
          env:
            - name: SONARQUBE_JDBC_USERNAME
              value: sonar
            - name: SONARQUBE_JDBC_PASSWORD
              value: sonar
            - name: SONARQUBE_JDBC_URL #数据库的连接地址;格式为:jdbc:postgresql://数据库实例名称.命名空间:5432/数据库名称
              value: 'jdbc:postgresql://sonar-db.sonar:5432/sonardb'
          resources:
            limits:
              cpu: '1'
              memory: 4Gi
            requests:
              cpu: 400m
              memory: 1Gi
          volumeMounts:
            - name: sonar-extensions
              mountPath: /opt/sonarqube/extensions
            - name: sonar-data
              mountPath: /opt/sonarqube/data
          readinessProbe:
            httpGet:
              path: /
              port: 9000
              scheme: HTTP
            initialDelaySeconds: 240
            timeoutSeconds: 5
            periodSeconds: 5
            successThreshold: 1
            failureThreshold: 3
          imagePullPolicy: IfNotPresent
          securityContext:
            privileged: false
      restartPolicy: Always

3.sonarqube Service

kind: Service
apiVersion: v1
metadata:
  name: sonarqube
  namespace: sonar
  labels:
    name: sonarqube
spec:
  ports:
    - name: sonarqube
      protocol: TCP
      port: 9000
      targetPort: 9000
  selector:
    name: sonarqube
  type: ClusterIP

4.sonarqube Ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  namespace: sonar
  name: sonarqube
  annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: "1024m"
spec:
  ingressClassName: "nginx"
  rules:
  - host: 'internal.sonarqube.com'
    http:
      paths:
      - path: /
        backend:
          serviceName: sonarqube
          servicePort: 9000

5.启动sonarqube

这里我是把上述的内容合到一个yaml文件里了

[root@k8s-master sonar_workspace]# kubectl apply -f sonarqube.yaml 
deployment.apps/sonarqube created
persistentvolumeclaim/sonar-data created
persistentvolumeclaim/sonar-extensions created
service/sonarqube created
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
ingress.extensions/sonarqube created
[root@k8s-master sonar_workspace]# kubectl get pod -n sonar
NAME                         READY   STATUS    RESTARTS   AGE
sonar-db-545676f4c6-zvjw8    1/1     Running   0          7m46s
sonarqube-56cb8748b9-zmp97   1/1     Running   0          4m34s


三、sonarqube设置中文

1.登录sonarqube

默认用户名密码
admin
admin
Kubernetes安装部署SonarQube 9.x并安装中文插件;_第1张图片
Kubernetes安装部署SonarQube 9.x并安装中文插件;_第2张图片

2.下载中文插件

上方菜单栏的【Administration】➡【Marketplace】➡【Plugins】
Kubernetes安装部署SonarQube 9.x并安装中文插件;_第3张图片


需要先同意霸王条款才能进行安装插件
Kubernetes安装部署SonarQube 9.x并安装中文插件;_第4张图片


搜索并下载中文插件 “Chinese Pack
Kubernetes安装部署SonarQube 9.x并安装中文插件;_第5张图片


安装完插件之后需要重启一下sonarqube才能生效
上方菜单栏的【Administration】➡【System】➡【Restart Rerver】
Kubernetes安装部署SonarQube 9.x并安装中文插件;_第6张图片


重启完之后就是中文的了
Kubernetes安装部署SonarQube 9.x并安装中文插件;_第7张图片
以上就是sonarqube在k8s内的安装配置了;




补充:
登录sonar-db(postgres)容器查看sonardb数据库的数据

[root@k8s-master sonar_workspace]# kubectl get pod -n sonar
NAME                         READY   STATUS    RESTARTS   AGE
sonar-db-545676f4c6-zvjw8    1/1     Running   0          175m
sonarqube-56cb8748b9-zmp97   1/1     Running   0          171m
[root@k8s-master sonar_workspace]# kubectl exec -it pod/sonar-db-545676f4c6-zvjw8 -n sonar sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
#切换到Linux用户postgres,并执行psql,使用sonar用户登录
# su - postgres 
postgres@sonar-db-545676f4c6-zvjw8:~$ psql -d sonardb -U sonar 
psql (11.6 (Debian 11.6-1.pgdg90+1))
Type "help" for help.
#切换到sonardb数据库
sonardb=# \c sonardb
You are now connected to database "sonardb" as user "sonar".
#查看库内的表
sonardb=# \dt
                 List of relations
 Schema |           Name            | Type  | Owner 
--------+---------------------------+-------+-------
 public | active_rule_parameters    | table | sonar
 public | active_rules              | table | sonar
 public | alm_pats                  | table | sonar
 public | alm_settings              | table | sonar
....

更多postgresql语句可查询下面这个连接:
https://www.sjkjc.com/postgresql/describe-table/

你可能感兴趣的:(SonarQube,代码检测平台,kubernetes,运维,容器,jenkins)