Jenkins Plugin在一個(gè)k8 cluster中運(yùn)行動(dòng)態(tài)的agents.
基于Scaling Docker with Kubernetes们何,自動(dòng)調(diào)整Jenkins agents的規(guī)模萄焦。
這個(gè)plugin為每一個(gè)啟動(dòng)的agent創(chuàng)建k8 pod,在build結(jié)束后停掉它。
Agents用jnlp啟動(dòng)冤竹,所以預(yù)期這個(gè)agent可以自動(dòng)于jenkins master自動(dòng)連接拂封。
其中一些環(huán)境變量會(huì)自動(dòng)注入:
JENKINS_URL: Jenkins web interface url
JENKINS_SECRET: the secret key for authentication
JENKINS_AGENT_NAME: the name of the Jenkins agent
JENKINS_NAME: the name of the Jenkins agent
可以用jenkins/jnlp-slave 進(jìn)行測試, 查看源代碼可見Docker image source code.
Kubernetes Cloud Configuration
‘Jenkins-settings’ -> 'add cloud'-> select 'Kubernetes'-fill the information like Name, Kubernetes URL, Kubernetes server certificate key
如果 Kubernetes URL沒有設(shè)置,則這個(gè)connection項(xiàng)會(huì)從server account或者kube config文件中自動(dòng)配置鹦蠕。
Restricting what jobs can use your configured cloud
clouds可以配置冒签,使其僅允許一些job去用他們。
去enable這些钟病,在cloud的'advanced configuration'勾選'Restrict pipeline support' 去對(duì)‘folder box'進(jìn)行授權(quán)萧恕。如果后續(xù)有新的job想使該云,則可將其加入job folder.
Pipeline support
Nodes可以在pipeline中定義并被使用肠阱。然而票唆, 默認(rèn)執(zhí)行通常在jnlp container上執(zhí)行。你必須指定container去執(zhí)行你的任務(wù)
可從下兩圖進(jìn)行說明:
可以在examples dir中看到更多的答案屹徘。
默認(rèn)的jnlp agent? template如下:
containerTemplate(name:'jnlp',image:'jenkins/jnlp-slave:3.10-1-alpine',args:'${computer.jnlpmac} ${computer.name}'),
也可以用yaml syntax來顯示:
如果你想覆蓋原來默認(rèn)的jnlp走趋,保持名字不變,更新其他參數(shù)即可噪伊。
Container Group Support
我們可以針對(duì)agent pod, 基于共享資源例如mounts, 定義多組container簿煌。每個(gè)container中的ports都可以被K8的pod用localhost連接。
以下段落沒有理解:
The container statement allows to execute commands directly into each container. This feature is considered ALPHAas there are still some problems with concurrent execution and pipeline resumption
不知道要結(jié)合下圖干什么說明什么鉴吹。
Pod and container template configuration
Pod templates是用來創(chuàng)建agents的pod template.它可以使用user interface去配置姨伟,也可以使用pipeline去配置。
但是無論那種方式豆励,我們編輯的都是以下這些fileds:
cloud: Jenkins setting中的cloud name夺荒。 defaults: kubernetes
name: the name of the pod
namespace: the namespace of the pod
label: the label of the pod. 必須是唯一值從而防止builds之前的沖突。
yaml: pod的yaml representation, 可以可以任何的values除了這些支持的fileds.
containers: container templates用于創(chuàng)建pod上的containers
serviceAccount: 這個(gè)pod的service account
nodeSelector: 這個(gè)pod的在哪個(gè)node上
nodeUsageMode: NORMAL或者EXCLUSIVE,其控制了Jenkins僅schedule jobs當(dāng)label匹配還是盡可能多的用這個(gè)node.
volumes: pod volumes對(duì)所有container有效
envVars: 應(yīng)用于所有container的環(huán)境變量
secretEnvVar: 其值從kubernetes secret中獲得
imagePullSecrets: pull secret names的列表
annotation: 部署這個(gè)pod的說明
inheritFrom: 一個(gè)或多個(gè)pod templates的列表可以去繼承的
slaveConnectTimeout: 等待一個(gè)agent上線的時(shí)間
podRetention: keep pods行為的控制器般堆≡谛ⅲ可以是never(), onFailure(), always(), default(). 如果是空的話诚啃,這個(gè)pod在‘a(chǎn)ctiveDeadlineSeconds’過后就會(huì)被刪掉淮摔。
activeDeadlineSeconds如果podRentions設(shè)為‘never()’或者‘onFailure()’,pod就會(huì)在deadline過后刪除始赎。
idleMinutes允許pod保留一段時(shí)間防止復(fù)用直到這個(gè)時(shí)間過去秒拔。因?yàn)橹挥性谶@個(gè)時(shí)間過去后诉探,最后一個(gè)步驟才會(huì)被執(zhí)行。
和pod template對(duì)應(yīng)的是container template。container template支持用戶接口或者pipeline去配置陡叠。允許你配置的fields有:
name: container的name
image: container是基于哪個(gè)image啟動(dòng)的。
envVars: 配置container的環(huán)境變量特碳,也會(huì)覆蓋在pod level配置的環(huán)境變量蹋岩。也可以在一行內(nèi)用envVar配置單個(gè)環(huán)境變量。
secretEnvVar: 一個(gè)環(huán)境變量杆逗。這個(gè)環(huán)境變量從K8 secret中讀取乡翅。
command: 這個(gè)container可以執(zhí)行的命令
args: 傳給command的參數(shù)。?
ttyEnabled: 使能tty
livenessProbe: liveness探針罪郊。用以在container內(nèi)部去執(zhí)行exec liveness probe. 不支持httpGet liveness probes.
ports: 暴露container的端口蠕蚜。
Using yaml to Define Pod Templates
除了上面我們提到的一些fields,我們可以傳入yaml的文件去支持任意的值悔橄。注意靶累,yaml外所有的配置都會(huì)先生效。
下面這是一個(gè)container template
def label="mypod-${UUID.randomUUID().toString()}"
podTemplate(label: label, yaml:"""
apiVersion: v1
kind: Pod
metadata:?
? labels:? ? some-label: some-label-value
spec:
? containers:
? - name: busybox??
??? image: busybox? ?
??? command:? ? - cat? ?
???? tty: true
? """) {? ?
???? node (label) {? ??
?????????? container('busybox')
??????????? {? ? ? ? sh"hostname"}?
? }
}
你還可以用readFile或者readTrusted步驟去從文件中l(wèi)oad一個(gè)yaml癣疟。在jenkins console的該plugin的配置panel也可以看到readFile/readTrusted
containerTemplate(
name:'busybox',
image:'busybox',
ttyEnabled:true,
command:'cat',
livenessProbe:
containerLivenessProbe(execArgs:'some --command',initialDelaySeconds:30,timeoutSeconds:1,failureThreshold:3,periodSeconds:10,successThreshold:1))
我們可以從Defining a liveness command中獲取更多信息挣柬。
Pod template inheritance
一個(gè)podTemplate可以從一個(gè)已經(jīng)存在的template中繼承也可以不繼承。這意味著podTemplate會(huì)繼承node selector, service account, image pull secrets, containerTemplates和volume
加入到podTemplate的containerTemplae, 會(huì)在'parent' template中有一個(gè)匹配的containerTemplate睛挚, 會(huì)從parent containerTemplate中繼承配置邪蛔。如果沒有匹配的containerTemplate, 這個(gè)模版會(huì)被加到parent template中。
volume的繼承和container templates一樣竞川。Image Pull Secrets也會(huì)進(jìn)行合并(這些證書包括在parent和current)
在以下這個(gè)例子中店溢,我們會(huì)繼承我們創(chuàng)建的podTemplate。
如果你想更改集成的template, 可以如下圖所示:
如上圖委乌,我們只更換maven container的image床牧,而保留其他參數(shù)不變。
需要注意:我們在上圖中沒有指定golang container,但是實(shí)際上它已經(jīng)集成過來了遭贸。
Multiple Pod template inheritance
Field 'inheritFrom'指定從一個(gè)podTemplate或者多個(gè)以space間隔的podTemplate中繼承戈咳。
后者覆蓋前者,沒有找到的則自動(dòng)忽略。
Nesting Pod templates
‘inheritFrom’可以讓我們很好的整合已經(jīng)預(yù)先定義的podTemplates著蛙。另外一種整合podTemplate的方法是我們在pipeline中用groovy直接定義和整合删铃,即如直接嵌入podTemplates.
示例如下:
這樣,我們就把兩個(gè)pod template整合成了一個(gè)pod template.
這個(gè)feature的用處還有踏堡, piepline library developers認(rèn)為它允許你根據(jù)功能打包podTemplates, 讓用戶根據(jù)他們的意圖嵌套這些功能猎唁。
示例如下:
src/com/foo/utils/PodTemplates.groovy:
有了這個(gè)庫之后,我們就可以如下圖所展示顷蟆,可以很方便的使用了诫隅。
Using a different namespace
如果你需要你的pod在不同的namespace下運(yùn)行,則需要通過ui或者pipeline明確指出帐偎。
Specifying a different shell command other than /bin/sh
默認(rèn)的shell command是'/bin/sh', 如果你想用‘/bin/bash’逐纬,則需要明確指出。
By default, the shell command is /bin/sh. In some case, you would like to use another shell command like /bin/bash.
Container Configuration
示例:
Declarative Pipeline
Jenkins 2.66+ 才支持的feature
或者將yamlFile作為一個(gè)單獨(dú)的pod yaml供使用而不是直接定義削樊。如下圖所示:
Constraints
我們可以在一個(gè)pod里面定義多個(gè)containers.其中jnlp是自動(dòng)創(chuàng)建豁生,運(yùn)行的是JNLP agent service, 和${computer.jnlpmac}漫贞, ${computer.name}甸箱,會(huì)作為Jenkins agent運(yùn)行。
其他的containers必須是一個(gè)長時(shí)間的進(jìn)程绕辖。
另外摇肌,如果你想提供自己的docker image作為jnlp slave, 你必須命令這個(gè)container為jnlp來覆蓋the default one。不然的化仪际,就會(huì)出現(xiàn)兩個(gè)slaves同時(shí)和master進(jìn)行交互的情況围小。