devops与kubesphere的关系如下图, 详细的组件介绍
devops与jenkins集成紧密且优雅,从构建、部署到使用维护纯云原生方式实现
用户使用kuberspere平台的devops功能时,调用devops-api发送请求,devops收到请求后,部分请求直接调用jenkins进行操作,部分请求通过更新devops-controller监听的资源,通过devops-controller来操作jenkins。
运行流水线阶段,jenkins配置了kubernetes动态slave
jenkins本身是一个Java应用,当前也没有提供官方的云原生方案,kubesphere通过下面几个项目定制了自己的镜像
ks-devops项目中的formulas安装了所有需要的jenkins插件主要有
这个项目里面主要有三个chart
部署 devops-api 和 devops-controller
注意⚠️ 这里有一个cronjob 作用为清理执行过的流水线记录,定期执行
ks pip gc
主要部署的资源有
kubernetes插件配置 charts/ks-devops/charts/jenkins/templates/config.yaml
kubesphere-user
所有资源只读 并绑定到authenticated
用户Kubernetes service account
deployment charts/ks-devops/charts/jenkins/templates/jenkins-master-deployment.yaml
到这里 jenkins pod就创建出来了,我们可以直接开始使用jenkins运行流水线了
source to image 这个组件没怎么用
kubesphere通过ks-install和helm都配置好了,无需单独配置
以流水线为例,groovy中添加以下字段会按照 'base'
去匹配pod的lable,匹配到了会使用这个label的pod模板启动pod运行流水线,下面有两个pipeline 脚本,第一个是选定了pod的模板的会启动一个pod来执行,第二个any, 如果设置了master节点为 Only build jobs with label expressions matching this node
将会启动base pod来运行,如果选择 Use this node as much as possible
则会在jenkins自身的容器/服务器上运行, 如果是普通job的话 勾选Restrict where this project can be run
且填写 Label Expression
选择要运行的label,和pipeline类似
pipeline {
agent {
node {
label 'base'
}
}
stages {
stage('Run shell') {
steps {
sh 'echo hello world'
}
}
}
}
pipeline {
agent any
stages {
stage('Run shell') {
steps {
sh 'echo hello world'
}
}
}
}
Manage Node ——> Configure Clouds
kubernetes service account
pod所使用的maven配置是挂载进去的,可以通过Jenkins->Configuration->Maven Project Configuration 配置
kubesphere-token-auth-plugin
集成kubesphere的认证体系,在kubesphere调用jenkins时,都需要经过ks-apiserver进行token的review, 通过之后再调用jenkins执行实际动作serviceAccountName
devops-jenkins
/var/run/secrets/kubernetes.io/serviceaccount/token
如果是外置jenkins则无法通过读取token来连接kubernetes,需要手动创建serviceAccount、clusterRole、clusterRoleBinding, 然后将token以Secret text或者将ca证书以Secret file形式或将kubconfig以Secret file形式写入credentials
kubectl delete --all apiservice
参照 https://github.com/kubernetes/kubernetes/issues/75704
mv /etc/kubernetes/manifests/kube-apiserver.yaml /etc/kubernetes/
kubesphere v3.3.0
install failed, ks-controller CrashLoopBackOff
E1116 00:55:15.113761 1 notification_controller.go:113] get /, Kind= informer error, no matches for kind "Config" in version "notification.kubesphere.io/v2beta1"
F1116 00:55:15.113806 1 server.go:340] unable to register controllers to the manager: no matches for kind "Config" in version "notification.kubesphere.io/v2beta1"
参照 kubectl apply -f https://raw.githubusercontent.com/kubesphere/notification-manager/master/config/bundle.yaml
kubectl apply -f https://raw.githubusercontent.com/kubesphere/notification-manager/master/config/bundle.yaml
jnlp是jenkin的远程调用协议
JNLP(JAVA NETWORK LAUNCH PROTOCOL) is used to Connect to/launch your java application( here Jenkins) from a remote location
[root@k8s-1 ~]# kubectl logs -f -n kubesphere-devops-worker base-w9dpq jnlp
Warning: SECRET is defined twice in command-line arguments and the environment variable
Warning: AGENT_NAME is defined twice in command-line arguments and the environment variable
Sep 14, 2022 11:29:43 AM hudson.remoting.jnlp.Main createEngine
INFO: Setting up agent: base-w9dpq
Sep 14, 2022 11:29:44 AM hudson.remoting.jnlp.Main$CuiListener <init>
INFO: Jenkins agent is running in headless mode.
Sep 14, 2022 11:29:44 AM hudson.remoting.Engine startEngine
INFO: Using Remoting version: 4.10
Sep 14, 2022 11:29:44 AM org.jenkinsci.remoting.engine.WorkDirManager initializeWorkDir
INFO: Using /home/jenkins/agent/remoting as a remoting work directory
Sep 14, 2022 11:29:44 AM org.jenkinsci.remoting.engine.WorkDirManager setupLogging
INFO: Both error and output logs will be printed to /home/jenkins/agent/remoting
Sep 14, 2022 11:29:44 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Locating server among [http://172.16.80.38:8080/]
Sep 14, 2022 11:29:44 AM hudson.remoting.jnlp.Main$CuiListener error
SEVERE: http://172.16.80.38:8080/tcpSlaveAgentListener/ is invalid: 404 Not Found
java.io.IOException: http://172.16.80.38:8080/tcpSlaveAgentListener/ is invalid: 404 Not Found
at org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver.resolve(JnlpAgentEndpointResolver.java:219)
at hudson.remoting.Engine.innerRun(Engine.java:724)
at hudson.remoting.Engine.run(Engine.java:540)
Sep 14, 2022 11:33:41 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Locating server among [http://devops-jenkins.kubesphere-devops-system:80/]
Sep 14, 2022 11:33:42 AM org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver resolve
INFO: Remoting server accepts the following protocols: [JNLP4-connect, Ping]
Sep 14, 2022 11:33:42 AM org.jenkinsci.remoting.engine.JnlpAgentEndpointResolver resolve
INFO: Remoting TCP connection tunneling is enabled. Skipping the TCP Agent Listener Port availability check
Sep 14, 2022 11:33:42 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Agent discovery successful
Agent address: devops-jenkins-agent.kubesphere-devops-system
Agent port: 50000
Identity: 13:ea:2b:ab:b5:16:70:70:89:58:d1:66:2b:62:b1:16
Sep 14, 2022 11:33:42 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Handshaking
Sep 14, 2022 11:33:42 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Connecting to devops-jenkins-agent.kubesphere-devops-system:50000
Sep 14, 2022 11:33:42 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Trying protocol: JNLP4-connect
Sep 14, 2022 11:33:42 AM org.jenkinsci.remoting.protocol.impl.BIONetworkLayer$Reader run
INFO: Waiting for ProtocolStack to start.
Sep 14, 2022 11:33:46 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Remote identity confirmed: 13:ea:2b:ab:b5:16:70:70:89:58:d1:66:2b:62:b1:16
Sep 14, 2022 11:33:46 AM hudson.remoting.jnlp.Main$CuiListener status
INFO: Connected
Sep 14, 2022 11:33:58 AM org.csanchez.jenkins.plugins.kubernetes.KubernetesSlave$SlaveDisconnector call
INFO: Disabled agent engine reconnects.
jcli
jcli使用手册
custom-war-packager
jenkins kubernetes插件
KubeSphere DevOps 3.0 流水线开发指南
Jenkins 基于Kubernetes动态创建pod
Can I use Jenkins kubernetes plugin when Jenkins server is outside of a kubernetes cluster?
kubernetes-jenkins-integration