k8s下部署高可用jenkins
1、部署jenkins使用的pv文件[root@k8s-node1 jenkins]# cat jenkins_pv.yamlapiVersion: v1kind: PersistentVolumemetadata:name: opspvspec:capacity:storage: 20GiaccessModes:- ReadWriteManyp...
加入公众号一起发现美好的技术
一、在k8s中部署Jenkins的优点
1、传统Jenkins集群架构一些问题
● Master发生故障时,整个流程都不可用
● Slave集群的环境配置不一样,来完成不同语言的编译打包,但是这些差异化的配置导致管理起来不方便,维护麻烦
● 资源分配不均衡,有的slave要运行的job出现排队等待,而有的salve处于空闲状态
● 资源浪费,每台slave可能是物理机或者虚拟机,当slave处于空闲状态时,也不能完全释放掉资源
2、K8s中Jenkins集群架构优点
● 当Jenkins Master接受到Build请求后,会根据配置的Label动态创建一个运行在Pod中的Jenkins Slave并注册到Master上,当运行完Job后,这个Slave会被注销并且这个Pod也会自动删除,恢复到最初的状态(这个策略可以设置)
● 服务高可用,当Jenkins Master出现故障时,Kubernetes会自动创建一个新的Jenkins Master容器,并且将Volume分配给新创建的容器,保证数据不丢失,从而达到集群服务高可用的作用
● 动态伸缩,合理使用资源,每次运行Job时,会自动创建一个Jenkins Slave,Job完成后,Slave自动注销并删除容器,资源自动释放,并且Kubernetes会根据每个资源的使用情况,动态分配slave到空闲的节点上创建,降低出现因某节点资源利用率高,降低出现因某节点利用率高出现排队的情况
● 扩展性好,当Kubernetes集群的资源严重不足导致Job排队等待时,可以很容器的添加一个Kubernetes Node到集群,从而实现扩展
二、k8s 部署Jenkins
0、创建命名空间
kubectl create namespace demon
1、为Jenkins数据持久化存储创建一个PV
[root@k8s-node1 jenkins]# cat jenkins_pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: opspv
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: Delete
nfs:
server: 192.168.29.175
path: /app/k8s-vloume #注意:需要给此目录jenkins权限 chown -R jenkins:jenkins /app/k8s-vloume
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: opspvc
namespace: demon
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Gi
2、创建Jenkins集群权限serviceAccount文件
[root@k8s-node1 jenkins]# cat jenkins_rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: jenkins
namespace: demon
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: jenkins
rules:
- apiGroups: ["extensions", "apps"]
resources: ["deployments"]
verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
- apiGroups: [""]
resources: ["services"]
verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]
- apiGroups: [""]
resources: ["pods"]
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"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: jenkins
namespace: demon
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: jenkins
subjects:
- kind: ServiceAccount
name: jenkins
namespace: demon
3、创建Jenkins SVC
[root@k8s-node1 jenkins]# cat jenkins_svc.yaml
apiVersion: v1
kind: Service
metadata:
name: jenkins
namespace: demon
labels:
app: jenkins
spec:
selector:
app: jenkins
type: NodePort
ports:
- name: web
port: 8080
targetPort: web
nodePort: 9000
- name: agent
port: 50000
targetPort: agent
4、创建Jenkins Deployment
[root@k8s-node1 jenkins]# cat jenkins_deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins #deployment名称
namespace: demon #命名空间
spec:
selector:
matchLabels:
app: jenkins
template:
metadata:
labels:
app: jenkins
spec:
terminationGracePeriodSeconds: 10 #优雅停止pod
serviceAccount: jenkins #后面还需要创建服务账户
containers:
- name: jenkins
image: jenkins/jenkins:lts #镜像版本
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8080 #外部访问端口
name: web
protocol: TCP
- containerPort: 50000 #jenkins save发现端口
name: agent
protocol: TCP
resources:
limits:
cpu: 1000m
memory: 1Gi
requests:
cpu: 500m
memory: 512Mi
livenessProbe:
httpGet:
path: /login
port: 8080
initialDelaySeconds: 60 #容器初始化完成后,等待60秒进行探针检查
timeoutSeconds: 5
failureThreshold: 12 #当Pod成功启动且检查失败时,Kubernetes将在放弃之前尝试failureThreshold次。放弃生存检查意味着重新启动Pod。而放弃就绪检查,Pod将被标记为未就绪。默认为3.最小值为1
readinessProbe:
httpGet:
path: /login
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 5
failureThreshold: 12
volumeMounts: #需要将jenkins_home目录挂载出来
- name: jenkinshome
subPath: jenkins
mountPath: /var/jenkins_home
env:
- name: LIMITS_MEMORY
valueFrom:
resourceFieldRef:
resource: limits.memory
divisor: 1Mi
- name: JAVA_OPTS
value: -Xmx$(LIMITS_MEMORY)m -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85 -Duser.timezone=Asia/Shanghai
securityContext:
fsGroup: 1000
volumes:
- name: jenkinshome
persistentVolumeClaim:
claimName: opspvc #这里将上面创建的pv关联到pvc上
5、依次创建
[root@k8s-node1 jenkins]# ll
-rw-r--r-- 1 root root 404 12月 27 14:57 jenkins_pv.yaml
-rw-r--r-- 1 root root 1108 12月 27 14:58 jenkins_rbac.yaml
-rw-r--r-- 1 root root 285 12月 27 14:59 jenkins_svc.yaml
-rw-r--r-- 1 root root 2281 12月 27 14:59 jenkins_deployment.yaml
三、Jenkins中配置k8s
1、系统管理->系统配置
2、配置拉到最下面找到Kubernetes插件
Name 配置的名称
Kubernetes URL 这里的URL是K8s内部的URL,实际上就是svcname https://kubernetes.default.svc.cluster.local
Kubernetes Namespace k8s的命名空间 (实际上就是Jenkins所在的命名空间)
3、Jenkins URL配置
Jenkins URL 这里的URL是jenkins的svc名称加上命名空间,实际上就是在k8s集群内部访问jenkins的一个方法,这里也不需要修改
http://jenkins.demon.svc.cluster.local:8080
4、配置添 Jenkins Slave Pod模板
Name = Pod 名称 Namespave = Pod命名空间 Labels = Pod标签
5、容器的模板配置
6、创建volume的配置
Jenkins Master收到Build请求时,会根据配置的Label动态创建一个运行在Pod中的Jenkins Slave并注册到Master上,当Job运行完,这个Slave会被注销并且这个Pod也会自动删除,恢复到最初状态
四、测试验证
1、新建Job选择流水线
2、流水线Pipeline
def label = "jenkins-slave"
podTemplate(label: label, cloud: 'kubernetes')
{
node(label) {
stage('pull code') {
echo "拉取代码"
}
stage('build') {
echo "代码编译"
}
stage('SonarQube') {
echo "质量扫描"
}
}
}
3、执行效果
五、git–>jenkins–>k8s结合实现自动化部署jenkinsfile文件
def label = "jenkins-slave"
podTemplate(label: label, cloud: 'kubernetes')
{
node(label) {
stage('pull code') {
git credentialsId: '732e79e2-7085-4882-ae31-4b50c7f7c4ea', url: 'git@192.168.29.182:demon/java-demon.git'
}
stage('build') {
sh "/usr/bin/mvn clean package -Dmaven.test.skip=ture"
}
stage('SonarQube') {
echo "质量扫描"
}
stage('制作镜像'){
sh '''
cat >Dockerfile <<EOF
FROM 192.168.29.175:9003/demon/tomcat:v1
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
EOF
'''
}
stage('构建镜像'){
sh '''
docker build -t 192.168.29.175:9003/demon/test-demon:v1 .
'''
}
stage('上传到镜像仓库'){
sh '''
docker login -u admin -p Harbor12345 192.168.29.175:9003
docker push 192.168.29.175:9003/demon/test-demon:v1
'''
}
stage('Deployment Services'){
sh '''
cat > java-demo.yaml <<EOF
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
app: java-demo
name: java-demo
spec:
replicas: 1
selector:
matchLabels:
app: java-demo
template:
metadata:
labels:
app: java-demo
spec:
containers:
- image: 192.168.29.175:9003/demon/test-demon:v1
name: java-demo
---
apiVersion: v1
kind: Service
metadata:
name: java-demo
spec:
type: NodePort
selector:
app: java-demo
ports:
- protocol: TCP
nodePort: 9006
port: 8080
targetPort: 8080
EOF
'''
}
stage('deploy to k8s'){
sh 'kubectl create -f java-demo.yaml'
}
}
}
执行后流水线
更多推荐
所有评论(0)