1、准备脚本
1.1、pipeline-auto.yml脚本
完整的pipeline-auto.yml脚本如下
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: test
name: pipeline-auto
labels:
app: pipeline-auto
spec:
replicas: 2
selector:
matchLabels:
app: pipeline-auto
template:
metadata:
labels:
app: pipeline-auto
spec:
containers:
- name: pipeline-auto
#指定下载的镜像
image: 192.168.88.125:80/repo/jenkins-kubernetes-auto:dev
#镜像拉取策略,不管内容变不变都重新拉取
imagePullPolicy: Always
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
namespace: test
#注意:这里要和Deployment的名称一致
name: pipeline-auto
labels:
app: pipeline-auto
spec:
selector:
#注意:这里要和Deployment的app名称一致
app: pipeline-auto
ports:
#Service的端口
- port: 8081
#容器内的端口
targetPort: 8080
type: NodePort
---
#apiVersion值必须写成networking.k8s.io/v1
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
namespace: test
name: pipeline-auto
spec:
#可视化界面配置的ingress名称
ingressClassName: ingress
rules:
#映射的域名
- host: k8s.pipeline-auto.com
http:
paths:
#访问什么样的路径
- path: /
#匹配方式,前缀匹配,即匹配上path配置的斜杠就可以
pathType: Prefix
backend:
#自定用哪个service
service:
name: pipeline-auto
port:
#映射的端口
number: 8081
1.2、Jenkinsfile脚本
完整的Jenkinsfile脚本如下
//所有的脚本命令都放在pipeline中
pipeline {
//指定任务在哪个集群节点中执行
agent any
//声明全局变量,方便后面使用
environment {
harborUser = 'admin'
harborPassword = 'Harbor12345'
harborAddress = '192.168.88.125:80'
harborRepo = 'repo'
}
stages {
stage('拉取代码') {
steps {
echo '开始拉取git仓库代码......'
checkout scmGit(branches: [[name: 'dev']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.88.125:8929/root/devops-test.git']])
echo '开始拉取git仓库代码完毕。'
}
}
stage('构建项目') {
steps {
echo '开始通过maven构建项目......'
sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
echo '通过maven构建项目完毕'
}
}
stage('代码检查') {
steps {
echo 'TODO 通过SonarQube做代码质量检测'
}
}
stage('制作镜像') {
steps {
echo '通过Docker制作自定义镜像......'
sh '''mv ./target/*.jar ./docker/
docker build -t ${JOB_NAME}:dev ./docker/'''
echo '通过Docker制作自定义镜像完毕'
}
}
stage('推送镜像') {
steps {
echo '将自定义对象推送到Harbor仓库......'
sh '''docker login -u ${harborUser} -p ${harborPassword} ${harborAddress}
docker tag ${JOB_NAME}:dev ${harborAddress}/${harborRepo}/${JOB_NAME}:dev
docker push ${harborAddress}/${harborRepo}/${JOB_NAME}:dev
docker image prune -f'''
echo '将自定义对象推送到Harbor仓库完成'
}
}
stage('将yml文件传到k8smaster') {
steps {
echo '将yml文件传到k8smaster......'
sshPublisher(publishers: [sshPublisherDesc(configName: 'k8smaster', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'pipeline-auto.yml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo '将yml文件传到k8smaster完毕'
}
}
stage('远程执行k8smaster的kubectl命令') {
steps {
echo '远程执行k8smaster的kubectl命令......'
sshPublisher(publishers: [sshPublisherDesc(configName: 'k8smaster', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''kubectl apply -f /usr/local/k8s/pipeline-auto.yml
//强制重新部署容器
kubectl rollout restart deployment pipeline-auto -n test''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
echo '远程执行k8smaster的kubectl命令完毕'
}
}
}
}
2、Jenkins配置
2.1、安装GItLab插件
在Jenkins插件管理中搜索GitLab插件进行安装。
2.2、配置构建触发器
进入Jenkins项目配置里的构建触发器,勾选如下选项:
2.3、取消Jenkins的/project路径验证
从系统管理->系统配置->Gitlab将验证去掉,生产最好配置保证安全。
3、Gitlab配置
3.1、设置允许本地发送
如果是GitLab和Jenkins在同一台服务器,需要开启允许请求发送到本地网络
Admin Area->Settings->Network
把Allow requests to the local network from webhooks and integrations选项勾上,如下:
3.2、配置webhooks
在Gitlab中,配置Settings->Webhooks,其中URL填写步骤2中的GitLab webhook URL 对应的值,并且勾选Trigger下的Push evetns选项,并指定推送分支,例如dev。
接着点击最下方的Add webhook按钮
3.3、测试推送代码
如下在Gitlab的Webhook下方点击Test按钮的Push event触发Jenkins构建操作。
到Jenkins里可以看到触发了构建
4、验证自动化部署
修改dev分支的代码,并提交,可以在Jenkins项目里看到构建
构建完成之后,到Kuboard的容器组里看到,原来的容器下线,新的容器部署。
最后我们请求浏览,如下,至此完成了整个自动化部署。