kubesphere 集成 sonar

文章目录

  • 安装 helm
  • 通过 helm 安装 sonar
  • 配置 SonarQube 服务器
    • 创建 SonarQube 管理员令牌
    • SonarQube 配置添加到 ks-installer
    • 创建 Webhook 服务器
    • 将 SonarQube 服务器添加至 Jenkins
    • 将 sonarqubeURL 添加到 KubeSphere 控制台
    • 重启服务
  • 为新项目创建 SonarQube Token

官方文档: 将 SonarQube 集成到流水线

安装 helm

先安装helm,可参考文章 CentOS7 安装 helm

通过 helm 安装 sonar

安装命令

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

安装成功
在这里插入图片描述

查看 SonarQube 的状态

kubectl get pod -n kubesphere-devops-system

在这里插入图片描述

输出 sonar 访问地址

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

在这里插入图片描述

默认账号:admin
默认密码:admin

成功访问,ip:32525
kubesphere 集成 sonar_第1张图片

 

配置 SonarQube 服务器

创建 SonarQube 管理员令牌

点击右上角字母 A,然后从菜单中选择 My Account 以转到 Profile 页面。
kubesphere 集成 sonar_第2张图片

点击 Security 并输入令牌名称,例如 kubesphere
kubesphere 集成 sonar_第3张图片

复制 token 并保存好

SonarQube 配置添加到 ks-installer

编辑 ks-installer,或在自定义资源中搜索 ClusterConfiguration,点击 ks-installer 右侧的按钮 ,选择编辑 YAML

kubectl edit cc -n kubesphere-system ks-installer
  • externalSonarUrl:sonar 的访问地址
  • externalSonarToken:上一步生成的 token
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://10.xx.xx.xx:32525
    externalSonarToken: xxxxxx

检查安装过程

kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f

创建 Webhook 服务器

获取 SonarQube Webhook 的地址。

export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services devops-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/

在这里插入图片描述

依次点击 Administration、Configuration 和 Webhooks 创建一个 Webhook
kubesphere 集成 sonar_第4张图片

新建
在这里插入图片描述

  • name:自定义
  • url:上面获取的 Webhook

kubesphere 集成 sonar_第5张图片

将 SonarQube 服务器添加至 Jenkins

获取 Jenkins 的地址

export NODE_PORT=$(kubectl get --namespace kubesphere-devops-system -o jsonpath="{.spec.ports[0].nodePort}" services devops-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

在这里插入图片描述
默认账号:admin
默认密码:P@88w0rd

如果报错

WARNING o.s.c.s.ResourceBundleMessageSource#getResourceBundle: ResourceBundle [org.acegisecurity.messages] not found for MessageSource: Can’t find bundle for base name org.acegisecurity.messages, locale en
WARNING o.a.p.l.a.BindAuthenticator2#handleBindException: Failed to bind to LDAP: userDnuid=admin,ou=Users,dc=kubesphere,dc=io username=admin
javax.naming.AuthenticationException: [LDAP: error code 49 - Invalid Credentials]

可以修改密码,参考文章 KubeSphere3.0我在部署devops服务之后,jenkins无法使用ldap账号登录

kubectl patch users admin -p '{"spec":{"password":"Admin123456"}}' --type='merge' && kubectl annotate users admin iam.kubesphere.io/password-encrypted-

系统管理 - Manage Credentials - Jenkins - 全局凭据
kubesphere 集成 sonar_第6张图片

kubesphere 集成 sonar_第7张图片

Secret text - 全局 - 上面创建的 token
kubesphere 集成 sonar_第8张图片

点击 系统管理 - 系统配置 - SonarQube servers - Add SonarQube - 选择sonarqube

kubesphere 集成 sonar_第9张图片

将 sonarqubeURL 添加到 KubeSphere 控制台

编辑 config

kubectl edit  cm -n kubesphere-system  ks-console-config

搜寻到 data.client.enableKubeConfig,在下方添加 devops 字段并指定 sonarqubeURL。
kubesphere 集成 sonar_第10张图片

重启服务

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

为新项目创建 SonarQube Token

创建项目
kubesphere 集成 sonar_第11张图片
创建 token
kubesphere 集成 sonar_第12张图片

得到 mvn 发布命令
kubesphere 集成 sonar_第13张图片

执行命令
在这里插入图片描述

成功发布到 sonar

kubesphere 集成 sonar_第14张图片

你可能感兴趣的:(JAVA,运维,java)