1、背景
由于jenkins運(yùn)行在k8s上能夠更好的利用動(dòng)態(tài)agent進(jìn)行構(gòu)建。所以寫了個(gè)部署教程,親測(cè)無坑
2、部署
1、創(chuàng)建ns
kubectl create namespace devops
2、kubectl apply -f jenkins.yml
apiVersion: v1
kind: ServiceAccount
metadata:
name: jenkins
namespace: devops
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: jenkins
rules:
- apiGroups: ["extensions", "apps"]
resources: ["deployments", "ingresses"]
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", "events"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: jenkins
namespace: devops
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: jenkins
subjects:
- kind: ServiceAccount
name: jenkins
namespace: devops
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: jenkins
namespace: devops
spec:
selector:
matchLabels:
app: jenkins
template:
metadata:
labels:
app: jenkins
spec:
serviceAccount: jenkins
initContainers:
- name: fix-permissions
image: busybox:1.35.0
command: ["sh", "-c", "chown -R 1000:1000 /var/jenkins_home"]
securityContext:
privileged: true
volumeMounts:
- name: jenkinshome
mountPath: /var/jenkins_home
containers:
- name: jenkins
image: jenkins/jenkins:2.414.1-lts-jdk11
imagePullPolicy: IfNotPresent
env:
- name: JAVA_OPTS
value: -Dhudson.model.DownloadService.noSignatureCheck=true
ports:
- containerPort: 8080
name: web
protocol: TCP
- containerPort: 50000
name: agent
protocol: TCP
readinessProbe:
httpGet:
path: /login
port: 8080
initialDelaySeconds: 60
timeoutSeconds: 5
failureThreshold: 12
volumeMounts:
- name: jenkinshome
mountPath: /var/jenkins_home
- name: localtime
mountPath: /etc/localtime
volumes:
- name: jenkinshome
hostPath:
path: /opt/jenkins/jenkins_data
- name: localtime
hostPath:
path: /etc/localtime
---
apiVersion: v1
kind: Service
metadata:
name: jenkins
namespace: devops
labels:
app: jenkins
spec:
selector:
app: jenkins
ports:
- name: web
port: 8080
targetPort: web
- name: agent
port: 50000
targetPort: agent
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: jenkins
namespace: devops
spec:
ingressClassName: nginx
rules:
- host: jenkins.k8s.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: jenkins
port:
name: web
注意:鏡像建議使用最新版本,因?yàn)閖enkin平臺(tái)默認(rèn)提供了最新的插件,且無法選擇版本,所以如果jenkins版本過低會(huì)導(dǎo)致插件不兼容問題
3、本地電腦配置host解析后,就可以用域名訪問
4、查看pod日志獲取初始化密碼,也可以查看/opt/jenkins/jenkins_data/secrets/initialAdminPassword
5、安裝必要插件
中文插件: Localization: Chinese
pipeline插件:Pipeline
k8s插件: Kubernetes
代碼庫管理插件:Git
6、配置k8s連接信息
填寫 以下內(nèi)容 ,然后點(diǎn)擊測(cè)試。
k8s地址 :https://kubernetes.default.svc.cluster.local
命名空間:devops
jenkins地址:http://jenkins.devops.svc.cluster.local:8080
由于之前部署的時(shí)候已經(jīng)給jenkins用戶訪問k8s 的devops命名空間的權(quán)限,所以這里不需要配置kubeconfig認(rèn)證也可直接訪問文章來源:http://www.zghlxwxcb.cn/news/detail-741484.html
3、編寫一條pipeline
這里用一個(gè)java項(xiàng)目的ci過程作為案例文章來源地址http://www.zghlxwxcb.cn/news/detail-741484.html
def createVersion() {
// 定義一個(gè)版本號(hào)作為當(dāng)次構(gòu)建的版本,輸出結(jié)果 20191210175842_69
return new Date().format('yyyyMMddHHmmss') + "_${env.BUILD_ID}"
}
pipeline{
agent{
kubernetes{
defaultContainer 'maven'
yaml '''
apiVersion: v1
kind: Pod
spec:
containers:
- name: maven
image: maven:3.8.1-jdk-8
command: ["sleep"]
args: ["99d"]
- name: docker
image: docker
command: ["sleep"]
args: ["99d"]
volumeMounts:
- mountPath: /var/run/docker.sock
name: docker-socket
volumes:
- name: docker-socket
hostPath:
path: /var/run/docker.sock
'''
}
}
environment {
tag = createVersion()
}
stages{
stage("pull code"){
steps{
script{
git 'https://gitee.com/uuei/java-devops-demo.git'
}
}
}
stage("mvn"){
steps{
script{
sh 'mvn clean package'
}
container('docker') {
script {
sh 'docker build -t java-demo:${tag} .'
}
}
}
}
}
}
到了這里,關(guān)于基于jenkins+k8s實(shí)現(xiàn)devops的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!