Jenkins Pipeline 部署程序到K8S
前置步骤: Nexus Docker私服配置+K8S拉取私服镜像
# 创建KubeConfig文件
复制集群内的~/.kube/config
在Jenkins中Managed files创建文件,类型MyCustom
# 创建部署模板文件
在Jenkins中Managed files创建文件,类型MyCustom
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
k8s.kuboard.cn/name: #{appname}
name: #{appname}
namespace: iot-dev
spec:
selector:
matchLabels:
k8s.kuboard.cn/name: #{appname}
template:
labels:
k8s.kuboard.cn/name: #{appname}
spec:
containers:
- name: #{appname}
image: '192.168.1.177:30303/#{appname}:#{apptag}'
ports:
- containerPort: 8080
name: http
protocol: TCP
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: mydocker
restartPolicy: Always
env:
- name: TZ
value: Asia/Shanghai
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Pipeline
env.appName = "myApp"
env.deploymentTemplateId = "部署模板文件id"
stage("k8s deploy") {
//复制kube集群配置文件 使kubectl能够连接集群 *我的Docker是带kubectl的,如果是官方Agent需要再安装
configFileProvider([configFile(fileId: 'kubeconfig', variable: 'KUBE_CONFIG')]) {
sh 'mkdir ~/.kube'
sh 'cp $KUBE_CONFIG ~/.kube/config'
sh 'kubectl get nodes'
}
//引入部署模板
configFileProvider([configFile(fileId: env.deploymentTemplateId, variable: 'DEPLOYMENT')]) {
//替换模板字段 生成部署文件
sh "sed 's/#{appname}/${env.appName}/;s/#{apptag}/${env.BUILD_TAG}/' $DEPLOYMENT > deployment.yaml"
//部署
sh "kubectl apply -f deployment.yaml"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
上次更新: 10/23/2024