需要在k8s集群上搭建Jenkins,并且Jenkins在k8s中动态创建代理。K8s集群搭建就不写了。本文就用上次文档搭建的单master集群来 *** 作。
Jenkins官方提供了镜像:Docker Hub
使用Deployment来部署这个镜像,会暴露两个端口:8080 Web访问端口,50000 Slave通信端口,容器启动后Jenkins数据存储在/var/jenkins_home目录,所以需要将该目录使用PV持久化存储。
持久化存储就使用上次搭建的ceph集群,此处不详解了。
一、修改Jenkins yaml 文件,并搭建JenkinsapiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins
labels:
name: jenkins
spec:
replicas: 1
selector:
matchLabels:
name: jenkins
template:
metadata:
name: jenkins
labels:
name: jenkins
spec:
terminationGracePeriodSeconds: 10
serviceAccountName: jenkins
containers:
- name: jenkins
image: jenkins/jenkins
imagePullPolicy: Always
ports:
- containerPort: 8080
- containerPort: 50000
resources:
limits:
cpu: 2
memory: 2Gi
requests:
cpu: 1
memory: 1Gi
env:
- name: JAVA_OPTS
value: -Xmx1g
volumeMounts:
- name: jenkins-home
mountPath: /var/jenkins_home
securityContext:
fsGroup: 1000
runAsUser: 0
volumes:
- name: jenkins-home
persistentVolumeClaim:
claimName: jenkins-home
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: jenkins-home
spec:
storageClassName: rook-cephfs
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 10Gi
---
apiVersion: v1
kind: Service
metadata:
name: jenkins
spec:
selector:
name: jenkins
type: NodePort
ports:
- name: http
port: 80
targetPort: 8080
protocol: TCP
nodePort: 30006
- name: agent
port: 50000
protocol: TCP
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: jenkins
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: jenkins
rules:
- apiGroups: [""]
resources: ["pods","events"]
verbs: ["create","delete","get","list","patch","update","watch"]
- apiGroups: [""]
resources: ["pods/exec"]
verbs: ["create","delete","get","list","patch","update","watch"]
- apiGroups: [""]
resources: ["pods/log"]
verbs: ["get","list","watch"]
- apiGroups: [""]
resources: ["secrets","events"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: jenkins
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: jenkins
subjects:
- kind: ServiceAccount
name: jenkins
上图画圈的部分可以根据自身情况更改
#执行yaml文件搭建Jenkins
kubectl apply -f jenkins.yml
执行成功后可以查看
二、登陆Jenkins
- 查看日志,获取初始密码
pod名称按实际来写
kubectl logs -f jenkins-6bbd797dfd-jgd6b
2.访问页面
jenkins启动有点慢,需要等几分钟才能访问
http://NodeIP:svcPort
3.初始化安装
选择 选择插件安装 来最小安装
选择 无 先不安装插件
创建一个管理员用户 然后点 保存并完成
三、配置Jenkins,并下载所需的插件
- 更新Jenkins源地址
找到cephfs挂载的目录,然后修改default.json
cephfs挂载的目录可以在ceph管理页面中查看
sed -i 's/http:\/\/updates.jenkins.io\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json
sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
# 重建Pod生效(Pod名称改成你实际的)
2.安装所需的插件
本文安装git、Git Parameter、Pipeline、kubernetes、Config File Provider、Extended Choice Parameter等插件,个插件的作用如下:
• Git:拉取代码
• Git Parameter:Git参数化构建
• Pipeline:流水线
• kubernetes:连接Kubernetes动态创建Slave代理
• Config File Provider:存储配置文件
• Extended Choice Parameter:扩展选择框参数,支持多选
安装步骤:
管理Jenkins->系统配置-->管理插件-->分别搜索Git Parameter/Git/Pipeline/kubernetes/Config File Provider/Extended Choice Parameter
选中点击安装。
安装完成点击重启就行
四、Jenkins 主从架构
当触发Jenkins任务时,Jenkins会调用Kubernetes API 创建Slave Pod,Pod启动后会连接Jenkins,接受任务并处理,任务处理完成后pod会销毁。
五、Jenkins配置slave- 配置kubernetes插件
管理Jenkins->管理Nodes和云->管理云->添加Kubernetes
上图画红线部分配置就行,其他的按需配置
2.制作jenkins slave镜像vim Dockerfile
FROM centos:7
LABEL maintainer sudu
RUN yum install -y java-1.8.0-openjdk maven curl git libtool-ltdl-devel && \
yum clean all && \
rm -rf /var/cache/yum/* && \
mkdir -p /usr/share/jenkins
COPY agent.jar /usr/share/jenkins/agent.jar
COPY jenkins-agent /usr/bin/jenkins-agent
COPY settings.xml /etc/maven/settings.xml
RUN chmod +x /usr/bin/jenkins-agent
COPY helm kubectl /usr/bin/
ENTRYPOINT ["jenkins-agent"]
• Dockerfile:构建镜像
• jenkins-agent:shell脚本启动slave.jar,下载地址:https://github.com/jenkinsci/docker-inbound-agent
• settings.xml:maven配置文件。修改maven官方源为阿里云源
• slave.jar:agent程序,接受master下发的任务,下载地址:http://你的jenkins地址:端口/jnlpJars/agent.jar
• helm和kubectl客户端工具,最好跟集群中的版本一致
Helm下载地址:https://get.helm.sh/helm-v3.8.2-linux-amd64.tar.gz
Kubectl参考文档(最好用集群在用的):https://kubernetes.io/zh/docs/tasks/tools/install-kubectl-linux/
3.构建镜像并推送到harbor中docker login 192.168.20.208
docker build -t 192.168.20.208/library/jenkins-slave-jdk:1.8 .
docker push 192.168.20.208/library/jenkins-slave-jdk:1.8
六、测试验证
在jenkins中创建测试项目验证Jenkins master/slave 架构。步骤如下:
新建项目->流水线->Pipeline脚本(可生成示例)
名字可以自定义
pipeline {
agent {
kubernetes {
label "jenkins-slave"
yaml '''
apiVersion: v1
kind: Pod
metadata:
name: jenkins-slave
spec:
containers:
- name: jnlp
image: "192.168.20.208/library/jenkins-slave-jdk:1.8"
'''
}
}
stages {
stage('Main'){
steps {
sh 'hostname'
echo 'jenkins master/slave the architecture test is successful'
}
}
}
}
构建镜像
查看输出内容:
到此kubernetes上搭建jenkins 动态代理完成,实际环境中的使用后期更新
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)