容器版Jenkins连接K8s

目的

本文详解介绍了容器版jenkins连接k8s的配置和使用详情。特别注意:必须用谷歌浏览器,而且非容器版jenkins是无法安装kubernetes插件的,所以无法连接k8s。搭建k8s集群教程

环境

操作系统版本: Ubuntu 18.04 TLS

软件版本: Jenkins 2.121.2

教程

安装k8s插件

系统管理->管理插件->可选插件, 搜索kubernetes plugin(没有选择kubernetes)并选择安装。

配置插件连接k8s集群

点击系统管理->系统设置->添加一个云, 在下拉菜单中选择kubernets并添加,如下图所示:

云kubernetes配置

注:Name值任意添加,Kubernetes URL值添加K8S apiserver连接地址和端口,jenkins URL值添加jenkins UI访问地址和端口,如下图所示:

添加云pod template并配置

参考教程

配置云连接K8S集群的验证文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#在jenkins所在的node节点上操作

#创建目录
mkdir -p /opt/crt/

#获取K8S的/root/.kube/config文件
#获取/root/.kube/config中certificate-authority-data的内容并转化成base64 encoded文件

[root@test2 ~]# echo LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUR2akNDQXFhZ0F3SUJBZ0lVYTdJRGZlOENZdmc0ZGpBR0cxQS81WHBLZVRNd0RRWUpLb1pJaHZjTkFRRUwKQlFBd1pURUxNQWtHQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdUQjBKbGFVcHBibWN4RURBT0JnTlZCQWNUQjBKbAphVXBwYm1jeEREQUtCZ05WQkFvVEEyczRjekVQTUEwR0ExVUVDeE1HVTNsemRHVnRNUk13RVFZRFZRUURFd3ByCmRXSmxjbTVsZEdWek1CNFhEVEU0TURjeE9UQTBNVEl3TUZvWERUSXpNRGN4T0RBME1USXdNRm93WlRFTE1Ba0cKQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdUQjBKbGFVcHBibWN4RURBT0JnTlZCQWNUQjBKbGFVcHBibWN4RERBSwpCZ05WQkFvVEEyczRjekVQTUEwR0ExVUVDeE1HVTNsemRHVnRNUk13RVFZRFZRUURFd3ByZFdKbGNt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1FVQ3dQK0lQNjlxK0FMazJIWWRrNVB0SmpOMnpOSEw5SUppWHVQUGViZGtSYVNnMGIvZzMKc3BNeFZTY05jNDR3MGFyQ0ZFU2Foa0tMbHJFRGNjbjcxSVpPb2dEMStaMHgycFB4RDc5OWNlQjlxMUtsNnhOWQp6T2s9Ci0tLS0tRU5EIENFUlRJRklDQVRFLS0tLS0K | base64 -d > /opt/crt/ca.crt

#获取/root/.kube/config中client-certificate-data和client-key-data的内容并转化成base64 encoded文件

[root@test2 ~]# echo LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUQzVENDQXNXZ0F3SUJBZ0lVSkw2RzR5YjlsaGwvWlIyT01laENmbVBOK2s4d0RRWUpLb1pJaHZjTkFRRUwKQlFBd1pURUxNQWtHQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdUQjBKbGFVcHBibWN4RURBT0JnTlZCQWNUQjBKbAphVXBwYm1jeEREQUtCZ05WQkFvVEEyczRjekVQTUEwR0ExVUVDeE1HVTNsemRHVnRNUk13RVFZRFZRUURFd3ByCmRXSmxjbTVsZEdWek1CNFhEVEU0TURjeE9UQTJORFF3TUZvWERUSTRNRGN4TmpBMk5EUXdNRm93YXpFTE1Ba0cKQTFVRUJoTUNRMDR4RURBT0JnTlZCQWdUQjBKbGFVcHBibWN4RURBT0JnTlZCQWNUQjBKbGFVcHBibWN4RnpBVgpCZ05WQkFvVERuTjVjM1JsYlRwdFlYTjBaWEp6TVE4d0RRWURWUVFMRXdaVGVYTjBaVzB4RGpBTUJnTlZCQU1UCkJXRmtiV2x1TUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUF2Ui9GZ2krKzJMdmUKU0ttNU11TG9DeVZ6aUVsOEVCVFhIS0tacWczMG5iOWgvUHAxZEV2a3JFbHFtOFJHN3JOOGFFRkkyZi9mcHF4SwpMdFlTWmVYTjEybWFRQTBiV1JlSG1nYWxnTzBZbVpYV1psc0FOR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12cW1aZmZ0cmdxK1BmOW54WmRGaHc1WmxjbDRtZTltalB6UFc5WFMyVHVsandGZytabDNrNjRESEo3VwpmM2VwOGIvSlpHTmFOWFQ3akF2ZnNhclhYNVQzNUJpS3J4Z0tFOTdvMStFbVhBVENaMXprCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg== | base64 -d > /opt/crt/client.key

生产Client P12认证文件cert.pfx,并下载至本地windows

1
2
3
4
5
6
7
openssl pkcs12 -export -out /opt/crt/cert.pfx -inkey /opt/crt/client.key -in /opt/crt/client.crt -certfile /opt/crt/ca.crt
Enter Export Password: 123
Verifying - Enter Export Password: 123

sz /opt/crt/cert.pfx

#注:自定义一个password并牢记

在云k8s中添加凭证

添加凭证–> 首先密码填写123–> 类型 –> Certificate –> Upload PKCS#12 certificate –> 上传证书 –> 选择文件–> cert –> 打开 –> 上传 –> 添加 –> 选择凭证

注:Upload certificate上次刚生成并下载至本地的cert.pfx文件,Password值添加生成cert.pfx文件时输入的密钥

添加命名空间

Kubernetes 命名空间中的值添加/root/.kube/config文件中cluster部分中name的内容(否则连接失败)

测试连接kubernetes集群

配置jenkins jnlp代理端口

系统管理->全局安全配置中的”代理”项,指定端口为50000

拍错

显示证书不对

Error testing connection https://192.168.0.91:6443: Get Key failed: Given final block not properly padded. Such issues can arise if a bad key is used during decryption

解决:

是因为添加平局那一界面的下面没有输入之前设置的密码(123)。输入密码、再次导入即可解决

显示无法连接

解决:

cert.pfx 可能没有生成好 ;或者ca.cert没有生成好,有空格, 重新生成一遍就好

-------------本文结束感谢您的阅读-------------

本文标题:容器版Jenkins连接K8s

文章作者:WanDouDuoDuo

发布时间:2020年07月02日 - 17:07

最后更新:2020年08月04日 - 14:08

原始链接:https://wandouduoduo.github.io/articles/3cfe518.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

原创技术分享,您的支持将鼓励我继续创作