参考链接:
https://www.jenkins.io/doc/book/scaling/scaling-jenkins-on-kubernetes/
https://www.jenkins.io/doc/book/installing/kubernetes/#install-jenkins-with-yaml-files
备注:
基于Kuboard和已经自己搭建的docker私有仓库
一、构建Jenkins Controller镜像
Dockerfile
FROM jenkins/jenkins:lts-slim
# Pipelines with Blue Ocean UI and Kubernetes
RUN jenkins-plugin-cli --plugins blueocean kubernetes
docker build -t 192.168.1.169:80/jyyun/jenkins-k8s:1.0 .
docker push 192.168.1.169:80/jyyun/jenkins-k8s:1.0
二、部署Jenkins到k8s
参考yaml:https://raw.githubusercontent.com/jenkins-infra/jenkins.io/master/content/doc/tutorials/kubernetes/installing-jenkins-on-kubernetes/jenkins-deployment.yaml
http://192.168.1.161:32608/jenkins
这里使用了社区推荐的插件往后走
这里没创建,直接用admin继续了,然后修改了admin的密码
admin默认就是上面的截图那个密码
可以修改为自己想修改的密码
三、Jenkins Agents的配置
查看k8s集群:
kubectl cluster-info
Jenkins 地址:主节点8080端口通过nodeport暴露出来的,地址:端口
Jenkins 通道:主节点50000端口通过nodeport暴露出来的,地址:端口
保存
碰到的问题
问题1:
参考:https://github.com/george-sre/jenkins-master/pull/2/files#diff-f187381d0c0cbb0ac50ec161868cec82898d5b65d141f613af009ee05dbffa10
https://segmentfault.com/a/1190000017388120
https://blog.csdn.net/weixin_42562106/article/details/114697462
显示说devops的defalut无法list jenkins这个namespace的资源
vi service-reader-cluster-role.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
namespace: devops
name: service-reader
rules:
- apiGroups: [""]
resources: ["services"]
verbs: ["get", "watch", "list"]
- apiGroups: [""]
resources: ["pods"]
verbs: ["create","delete","get","list","patch","update","watch"]
- apiGroups: [""]
resources: ["pods/exec"]
verbs: ["create","delete","get","list","patch","update","watch"]
kubectl create -f jenkins-account.yaml
如果报用户角色存在,则
kubectl delete -f jenkins-account.yaml
kubectl apply -f service-reader-cluster-role.yaml
kubectl create clusterrolebinding service-reader-pod --clusterrole=service-reader --serviceaccount=devops:default
如果已经存在,则:
kubectl delete clusterrolebinding service-reader-pod
验证:
kubectl -n devops get clusterrolebinding|grep service-reader-pod
四、使用Jenkins Agent
现在可以构建计划了
如果之前配置正确,那么在构建的时候就会看到jenkins-agent开头的计划
构建计划参考:https://www.freesion.com/article/8898268508/
构建:
欢迎大家关注我的公众号“神游坐忘峰大话IT”,一起多多交流!