hello云胜

技术与生活

0%

  1. 使用helm安装

    1
    2
    3
    helm repo add kubesphere https://charts.kubesphere.io/main

    helm upgrade --install sonarqube sonarqube --repo https://charts.kubesphere.io/main -n kubesphere-devops-system --create-namespace --set service.type=NodePort

    http://1x.xxx.151.208:31884

  2. 配置SonarQube

    默认帐户 admin/admin 登录,改密码xxx,123

    创建管理员令牌

    SonarQube 配置-1

SonarQube 配置-2

1657d796c5cfd32e8ffabb9f0e2d378f16477028

  1. 创建webHook服务器

    获取 SonarQube Webhook 的地址。

    image-20220420141759179

    1
    2
    3
    4
    export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services ks-jenkins)
    export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonpath="{.items[0].status.addresses[0].address}")
    echo http://$NODE_IP:$NODE_PORT/sonarqube-webhook/

http://1x.xxx.151.208:30180/sonarqube-webhook/

依次点击 AdministrationConfigurationWebhooks 创建一个 Webhook。

SonarQube Webhook-1

Webhook 页面信息

将 SonarQube 配置添加到 ks-installer

  1. 执行以下命令编辑 ks-installer

    1
    kubectl edit cc -n kubesphere-system ks-installer
  2. 搜寻至 devops。添加字段 sonarqube 并在其下方指定 externalSonarUrlexternalSonarToken

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    devops:
    enabled: true
    jenkinsJavaOpts_MaxRAM: 2g
    jenkinsJavaOpts_Xms: 512m
    jenkinsJavaOpts_Xmx: 512m
    jenkinsMemoryLim: 2Gi
    jenkinsMemoryReq: 1500Mi
    jenkinsVolumeSize: 8Gi
    sonarqube: # Add this field manually.
    externalSonarUrl: http://1x.xx.1.201:31377 # The SonarQube IP address.
    externalSonarToken: 00ee4c512fc987d3ec3251fdd7493193cdd3b91d # The SonarQube admin token created above.

    sonarqube:
    externalSonarUrl: http://1x.xxx.151.208:31884
    externalSonarToken: 1657d796c5cfd32e8ffabb9f0e2d378f16477028

  3. 完成操作后保存此文件。

步骤 5:将 SonarQube 服务器添加至 Jenkins

  1. 执行以下命令获取 Jenkins 的地址。

    1
    2
    3
    export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services ks-jenkins)
    export NODE_IP=$(kubectl get nodes --namespace kubesphere-devops-system -o jsonpath="{.items[0].status.addresses[0].address}")
    echo http://$NODE_IP:$NODE_PORT
  2. 您可以获得以下输出,获取 Jenkins 的端口号。

    1
    http://1x.xxx.151.208:30180
  3. 请使用地址 http://<Node IP>:30180 访问 Jenkins。安装 KubeSphere 时,默认情况下也会安装 Jenkins 仪表板。此外,Jenkins 还配置有 KubeSphere LDAP,这意味着您可以直接使用 KubeSphere 帐户(例如 admin/P@88w0rd)登录 Jenkins。有关配置 Jenkins 的更多信息,请参见 Jenkins 系统设置

  4. 点击左侧导航栏中的系统管理

  5. 向下翻页找到并点击系统配置

  6. 搜寻到 SonarQube servers,然后点击 Add SonarQube

  7. 输入 NameServer URL (http://<Node IP>:<NodePort>)。点击添加,选择 Jenkins,然后在弹出的对话框中用 SonarQube 管理员令牌创建凭证(如下方第二张截图所示)。创建凭证后,从 Server authentication token 旁边的下拉列表中选择该凭证。点击应用完成操作。

    sonarqube-jenkins-settings

    add-credentials

    如果点击添加按钮无效(Jenkins 已知问题),您可以前往系统管理下的 Manage Credentials 并点击 Stores scoped to Jenkins 下的 Jenkins,再点击全局凭据 (unrestricted),然后点击左侧导航栏的添加凭据,参考上方第二张截图用 SonarQube 管理员令牌添加凭证。添加凭证后,从 Server authentication token 旁边的下拉列表中选择该凭证。

步骤 6:将 sonarqubeURL 添加到 KubeSphere 控制台

您需要指定 sonarqubeURL,以便可以直接从 KubeSphere 控制台访问 SonarQube。

  1. 执行以下命令:

    1
    kubectl edit  cm -n kubesphere-system  ks-console-config
  2. 搜寻到 data.client.enableKubeConfig,在下方添加 devops 字段并指定 sonarqubeURL

    1
    2
    3
    4
    client:
    enableKubeConfig: true
    devops: # 手动添加该字段。
    sonarqubeURL: http://1x.xx.1.201:31377 # SonarQube IP 地址。
  3. 保存该文件。

步骤 7:重启服务

执行以下命令。

1
2
kubectl -n kubesphere-devops-system rollout restart deploy devops-apiserver
kubectl -n kubesphere-system rollout restart deploy ks-console

为新项目创建 SonarQube Token

您需要一个 SonarQube 令牌,以便您的流水线可以在运行时与 SonarQube 通信。

  1. 在 SonarQube 控制台上,点击 Create new project

    SonarQube 创建项目

  2. 输入项目密钥,例如 java-demo,然后点击 Set Up

    Jenkins 项目密钥

  3. 输入项目名称,例如 java-sample,然后点击 Generate

    创建令牌

    mqcloud: f97624b945c3707077053928534206b89db4bc1c

  4. 创建令牌后,点击 Continue

    令牌已创建

  5. 分别选择 JavaMaven。复制下图所示绿色框中的序列号,如果要在流水线中使用,则需要在凭证中添加此序列号。

    sonarqube-example