DevOps & KubeSphere
1 DevOps
1.1 概念
DevOps是Development和Operations两个词的组合。
DevOps是一组过程、方法与系统的统称,用于促进开发、技术运营和质量保障(QA)部门之间的沟通、协作与整合。
敏捷管理的流程是“设计--开发-测试-开发-测试...--部署”
而DevOps的流程是“设计--开发-测试-部署-开发-测试-部署...”,就是CI(Continuous Integration)/CD(Continuous Deployment)的一个过程
2 DevOps与容器技术
容器技术,微服务为DevOps提供了很好的前提条件,可以把大的项目分成多个划分为多个模块独立地进行开发测试,更迭,而容器技术可以把不同模块之间进行隔离,不会互相影响
3 KubeSphere
3.1 概念
4 KubeSphere部署DevOps
4.1 开启DevOps功能
4.2 准备工作
4.2.1 创建企业空间、项目、帐户和角色
出于安全考虑,强烈建议给不同的租户授予不同的权限在企业空间中进行协作
4.2.1.1 创建账户
users-manager--用户管理员,管理平台所有用户
步骤
登录控制台后,点击左上角的平台管理,然后选择访问控制


切换帐户使用 user-manager 重新登录,创建如下四个新帐户,这些帐户将在其他的教程中使用


4.2.1.2 创建企业空间
需要使用上一个步骤中创建的帐户 ws-manager 创建一个企业空间。作为管理项目、DevOps 工程和组织成员的基本逻辑单元,企业空间是 KubeSphere 多租户系统的基础
步骤
以 ws-manager 身份登录 KubeSphere,它具有管理平台上所有企业空间的权限。点击左上角的平台管理,选择访问控制,点击右侧的创建,将新企业空间命名为 demo-workspace,并将用户 ws-admin 设置为企业空间管理员


登出控制台,然后以 ws-admin 身份重新登录。在企业空间设置中,选择企业成员,然后点击邀请成员


4.2.1.3 创建项目
需要使用在上一步骤中创建的帐户 project-admin 来创建项目。KubeSphere 中的项目与 Kubernetes 中的命名空间相同,为资源提供了虚拟隔离
步骤
以 project-admin 身份登录 KubeSphere,在项目管理中,点击创建


4.2.2 将 SonarQube 集成到流水线
SonarQube 是一种主流的代码质量持续检测工具。您可以将其用于代码库的静态和动态分析。SonarQube 集成到 KubeSphere 流水线后,如果在运行的流水线中检测到问题,您可以直接在仪表板上查看常见代码问题,例如 Bug 和漏洞
4.2.2.1 安装 SonarQube 服务器
步骤
请先安装 Helm,以便后续使用该工具安装 SonarQube。例如,运行以下命令安装 Helm 3
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
查看helm version
helm version
version.BuildInfo{Version:"v3.4.1", GitCommit:"c4e74854886b2efe3321e185578e6db9be0a6e29", GitTreeState:"clean", GoVersion:"go1.14.11"}
执行以下命令安装 SonarQube 服务器
helm upgrade --install sonarqube sonarqube --repo https://charts.kubesphere.io/main -n kubesphere-devops-system --create-namespace --set service.type=NodePort
root@ubuntu:~# helm upgrade --install sonarqube sonarqube --repo https://charts.kubesphere.io/main -n kubesphere-devops-system --create-namespace --set service.type=NodePort
Release "sonarqube" does not exist. Installing it now.
NAME: sonarqube
LAST DEPLOYED: Wed Jun 9 14:45:39 2021
NAMESPACE: kubesphere-devops-system
STATUS: deployed
REVISION: 1
NOTES:
1\. Get the application URL by running these commands:
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
root@ubuntu:~#
4.2.2.2 配置 SonarQube 服务器
步骤
4.2.2.2.1 访问 SonarQube 控制台
在浏览器中访问 SonarQube 控制台 http://{Node IP}:{NodePort}

点击右上角的 Log in,然后使用默认帐户 admin/admin 登录
4.2.2.2.2 创建 SonarQube 管理员令牌 (Token)
点击右上角字母 A,然后从菜单中选择 My Account 以转到 Profile 页面
点击 Security 并输入令牌名称,例如 kubesphere


4.2.2.2.3 创建 Webhook 服务器
执行以下命令获取 SonarQube Webhook 的地址
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/
依次点击 Administration、Configuration 和 Webhooks 创建一个 Webhook


在弹出对话框中输入 Name 和 Jenkins Console URL(即 SonarQube Webhook 地址)。点击 Create 完成操作

4.2.2.2.4 将 SonarQube 配置添加到 ks-installer
执行以下命令编辑 ks-installer
kubectl edit cc -n kubesphere-system ks-installer
搜寻至 devops。添加字段 sonarqube 并在其下方指定 externalSonarUrl 和 externalSonarToken
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.77.1.201:31377 # The SonarQube IP address.
externalSonarToken: 00ee4c512fc987d3ec3251fdd7493193cdd3b91d # The SonarQube admin token created above.
完成操作后保存此文件
4.2.2.2.5 将 SonarQube 服务器添加至 Jenkins
执行以下命令获取 Jenkins 的地址
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
请使用地址 http://{Public IP}:30180 访问 Jenkins。安装 KubeSphere 时,默认情况下也会安装 Jenkins 仪表板。此外,Jenkins 还配置有 KubeSphere LDAP,这意味着您可以直接使用 KubeSphere 帐户(例如 admin/P@88w0rd)登录 Jenkins

点击左侧的系统管理



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


点击apply应用
4.2.2.2.6 将 sonarqubeURL 添加到 KubeSphere 控制台
需要指定 sonarqubeURL,以便可以直接从 KubeSphere 控制台访问 SonarQube
执行以下命令
kubectl edit cm -n kubesphere-system ks-console-config
搜寻到 client,添加 devops 字段并指定 sonarqubeURL
client:
version:
kubesphere: v3.0.0
kubernetes: v1.17.9
openpitrix: v0.3.5
enableKubeConfig: true
devops: # Add this field manually.
sonarqubeURL: http://10.77.1.201:31377 # The SonarQube IP address.
保存
4.2.2.2.7 重启服务
执行以下命令
kubectl -n kubesphere-system rollout restart deploy ks-apiserver
kubectl -n kubesphere-system rollout restart deploy ks-console
4.2.2.3 为新工程创建 SonarQube Token
需要一个 SonarQube 令牌,以便您的流水线可以在运行时与 SonarQube 通信

输入工程密钥,例如 java-demo,然后点击 Set Up




4.3 创建DevOps工程
步骤
以 project-admin 身份登录控制台,在 DevOps 工程中,点击创建



4.4 使用DevOps
4.4.1 使用图形编辑面板创建流水线
在 KubeSphere 中使用图形编辑面板创建流水线。KubeSphere 在整个过程中将根据您在编辑面板上的设置自动生成 Jenkinsfile,您无需手动创建 Jenkinsfile。待流水线成功运行,它会相应地在您的开发环境中创建一个部署 (Deployment) 和一个服务 (Service),并将镜像推送至 Docker Hub
网友评论