Jenkins pipeline 集成 k8s 弹性slave的 CI/CD
文章目录前言环境配置Global Tool Configuration(全局工具配置)配置Pod Template创建Pipeline,并使用SCMPipeline script编写前言参考此教程时,需要提前准备好或了解以下内容:准备好k8s集群,请参考《使用kubeadm搭建(kubernetes)k8s集群》准备好Jenkins与k8s的集成,请参考《Jenkins 集成k8s 运行...
文章目录
前言
参考此教程时,需要提前准备好或了解以下内容:
- 准备好k8s集群,请参考《使用kubeadm搭建(kubernetes)k8s集群》
- 准备好Jenkins与k8s的集成,请参考《Jenkins 集成k8s 运行Jenkins slave(Jenkins 弹性slave)》
- 准备好harbor仓库,请参考《Harbor 安装》
- 了解maven构建相关知识,请参考《Maven新手入门》
- 了解docker镜像构建相关知识,请参考《Dockerfile构建springboot Image》
- 了解k8s如何部署应用,请参考《在k8s上部署Springboot》
- 了解jenkins pipeline的基本语法《Jenkins高级篇之Pipeline代码自动化》
环境配置
Global Tool Configuration(全局工具配置)
配置Pod Template
本次的教程使用到以下container images:
- jenkins/jnlp-slave:3.27-1-alpine
- maven:3.6-jdk-8-alpine
- docker:18.06
- lachlanevenson/k8s-kubectl:v1.8.15
- postman/newman:ubuntu
我们需要为每个images在Pod Template创建相对应的Container Template:
在Jenkins/configuration找到Cloud接着,找到Kubernetes,点击Add Pod Template
以下配置根据自己实际情况输入,
Name,输入:jenkins_slave
Namespace,输入:devops
Label: 输入:haimaxy-jnlp, label这个会在后面pipeline调用k8s时用到。
配置Container Template
- jenkins/jnlp-slave:3.27-1-alpine
点击Add Container
Name,填入:jnlp
Docker image,填入:jenkins/jnlp-slave:3.27-1-alpine
如下图:
2. maven:3.6-jdk-8-alpine
如下图:
3. docker:18.06
如下图:
4. lachlanevenson/k8s-kubectl:v1.8.15
如下图:
- postman/newman:ubuntu
如下图:
最后,Host Path Volume,挂载相关路径
点击Add Volume,选择Host Path Volume,
分别填入:
Host path:/root/.m2
Mount path:/root/.m2
和
Host path:/var/run/docker.sock
Mount path:/var/run/docker.sock
如下图:
创建Pipeline,并使用SCM
在Jenbkins里面创建Pipeline job
- 点击New Item
- 输入item name,并点击Pipeline,最后点OK
- 在Pipeline里面选择Pipeline script from SCM,可以使用我准备好的pipeline project: https://github.com/wenshangbei/jenkins_pipeline_CICD.git,如下图,点击保存
Pipeline script编写
pipeline {
agent {
node {
label 'haimaxy-jnlp'
}
}
stages {
stage('Init') {
steps{
script{
println "welcome to Nick learn"
println env.WORKSPACE
module_test = load env.WORKSPACE + "/groovy/jenkins-module.groovy"
sh 'mkdir workplace'
}
}
}
stage('Git Source') {
steps{
script{
dir("${env.WORKSPACE}/workplace"){
git branch: 'env/dev', credentialsId: '11111',url: 'git@192.168.108.132:root/jenkins_slave_k8s.git'
}
}
}
}
stage('Maven Build') {
steps {
script {
container('maven') {
dir("${env.WORKSPACE}/workplace"){
sh 'mvn clean package'
}
}
}
}
}
stage('Docker Build') {
steps {
script {
container('docker') {
sh 'docker login -u ' + HARBOR_CREDS_USR + ' -p ' + HARBOR_CREDS_PSW + ' ' + HARBOR_HOST
sh "docker build --build-arg JAR_FILE=`ls workplace/target/*.jar |cut -d '/' -f2` -t " + HARBOR_HOST + "/" + DOCKER_IMAGE + ":" + GIT_TAG + " --no-cache ."
sh "docker push " + HARBOR_HOST + "/" + DOCKER_IMAGE + ":" + GIT_TAG
sh "docker rmi " + HARBOR_HOST + "/" + DOCKER_IMAGE + ":" + GIT_TAG
}
}
}
}
stage('K8s Deploy') {
steps {
script {
container('kubectl') {
sh "sed -e 's#{IMAGE_URL}#" + HARBOR_HOST + "/" + DOCKER_IMAGE + "#g;s#{IMAGE_TAG}#" + GIT_TAG + "#g;s#{APP_NAME}#" + APP_NAME + "#g;s#{SPRING_PROFILE}#k8s-test#g' k8s-deployment.tpl > k8s-deployment.yml"
sh "sed -e 's#{APP_NAME}#" + APP_NAME + "#g;s#{NODE_PORT}#" + NODE_PORT + "#g' k8s-deployment-svc.tpl > k8s-deployment-svc.yml"
try{
sh "kubectl delete rc " + APP_NAME + " --namespace=" + K8S_NAMESPACE
}catch (Exception e) {
println e
}
try{
sh "kubectl delete svc " + APP_NAME + " --namespace=" + K8S_NAMESPACE
}catch (Exception e) {
println e
}
sh "kubectl apply -f k8s-deployment.yml --namespace=" + K8S_NAMESPACE
sh "kubectl apply -f k8s-deployment-svc.yml --namespace=" + K8S_NAMESPACE
}
}
}
}
stage('API Test') {
steps {
script {
container('newman') {
dir("${env.WORKSPACE}/workplace"){
try{
sleep 5
sh 'newman run postman/jenkins_demo_test.postman_collection.json'
}catch (Exception e) {
println e
}
}
}
}
}
}
}
post{
failure {
script {
module_test.send_email_results("Failed","Master","1006953115@qq.com")
}
}
success {
script {
module_test.send_email_results("Success","Master","1006953115@qq.com")
}
}
}
environment {
HARBOR_CREDS = credentials('jenkins-harbor-creds')
// K8S_CONFIG = credentials('jenkins-k8s-config')
GIT_TAG = sh(returnStdout: true,script: 'echo ' + BUILD_NUMBER ).trim()
}
parameters {
string(name: 'HARBOR_HOST', defaultValue: '192.168.108.131', description: 'harbor仓库地址')
string(name: 'DOCKER_IMAGE', defaultValue: 'library/jenkins_demo', description: 'docker镜像名')
string(name: 'APP_NAME', defaultValue: 'jenkinsdemo', description: 'k8s中标签名')
string(name: 'NODE_PORT', defaultValue: '30000', description: 'Service端口')
string(name: 'K8S_NAMESPACE', defaultValue: 'default', description: 'k8s的namespace名称')
}
}
运行Pipeline
最后点击自己创建的pipeline,选择Buiild with Parameters:
分别填入相关信息:
点击Build后:
更多推荐
所有评论(0)