前提
4台机器
一台是Jenkins服务器192.168..129.132 ,这台机器要有docker jdk环境
一台是harbor仓库服务器192.168..129.133
两台是k8s服务器,用来部署服务只需要关注主节点192.168..129.135
必要环境
需要配置k8s对harbor的链接
harbor服务器
harbor -- 下载安装_harbor-offline-installer下载-CSDN博客
打包上传镜像
docker -- Jenkins容器内使用宿主机的docker环境-CSDN博客
集群搭建
参考文章
构建流水线
配置pipeline
pipeline {
agent any
tools {
maven 'maven3.9.5'
jdk 'JDK 17'
}
environment {
MAVEN_OPTS = '-Xmx1024m'
APP_NAME = 'demoTest'
APP_VERSION = '0.0.1-SNAPSHOT'
}
options {
// 保留最近10次构建
buildDiscarder(logRotator(numToKeepStr: '10'))
// 添加时间戳到控制台输出
timestamps()
// 设置超时时间
timeout(time: 30, unit: 'MINUTES')
}
stages {
//获取代码,代码库地址https://gitee.com/diaoguihua/demo-test/tree/master
stage('Checkout') {
steps {
echo '🔍 Checking out code...'
sh 'git clone git@gitee.com:diaoguihua/demo-test.git'
echo 'code end'
sh 'ls'
}
}
//maven打包代码
stage('Build') {
steps {
echo '🔨 Building application...'
sh '''
cd demo-test
mvn clean package -DskipTests
pwd
ls
'''
}
post {
success {
echo '✅ Build completed successfully!'
archiveArtifacts artifacts: 'demo-test/target/*.jar', fingerprint: true
}
failure {
echo '❌ Build failed!'
}
}
}
//maven test执行单元测试
stage('Test') {
steps {
echo '🧪 Running tests...'
sh '''
cd demo-test
mvn test
'''
}
post {
always {
// 发布测试报告
junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml'
}
success {
echo '✅ All tests passed!'
}
failure {
echo '❌ Tests failed!'
}
}
}
//maven verify
stage('Code Quality') {
steps {
echo '📊 Analyzing code quality...'
sh '''
cd demo-test
mvn verify
'''
}
}
//doker构建镜像,dockerfile是代码库里面的
stage('Build docker') {
steps {
// 构建镜像,先构建,最后还要删
echo '🔨 Build docker...'
sh '''
cd demo-test
docker login -u admin -p Harbor12345 192.168.129.133:8066
docker build -t demotest -f docker/dockerfile .
docker images
docker tag demotest:latest 192.168.129.133:8066/repo/demotest:harbor1.0
docker images
docker push 192.168.129.133:8066/repo/demotest:harbor1.0
'''
}
post {
success {
echo '✅ Build docker completed successfully!'
sh 'docker rmi 192.168.129.133:8066/repo/demotest:harbor1.0'
sh 'docker rmi demotest:latest'
}
failure {
echo '❌ Build docker failed!'
sh 'docker rmi 192.168.129.133:8066/repo/demotest:harbor1.0'
sh 'docker rmi demotest:latest'
}
}
}
//远程部署,这里面的操作是连接到远程服务器后进行拉取镜像和启动操作
stage('Deploy') {
steps {
echo '🚀 Deploying application...'
script {
def remoteHost = 'kane@192.168.129.135'
def deployPath = '/home/kane/k8s'
def jarFile = "test-deployment.yaml"
sh """
echo 'Deploying ${jarFile} to ${remoteHost}'
cd demo-test/k8s
pwd
ls
# 上传 yaml 文件
scp ./test-deployment.yaml ${remoteHost}:${deployPath}/
# 远程部署
ssh -T ${remoteHost} << 'ENDSSH'
set -e
# k8s 启动pod
pwd
cd /home/kane/k8s
pwd
ls
kubectl apply -f test-deployment.yaml --insecure-skip-tls-verify
echo "ssh end"
ENDSSH
echo '✅ Deployment completed'
exit 0
"""
}
}
post {
success {
echo '✅ Deployment completed successfully!'
}
failure {
echo '❌ Deployment failed!'
sh 'kubectl delete -f ${jarFile}'
}
}
}
}
post {
always {
echo '🧹 Cleaning up workspace...'
cleanWs()
}
success {
echo '🎉 Pipeline completed successfully!'
// 可以添加通知,例如邮件或 Slack
// emailext subject: "✅ Build Success: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
// body: "The build was successful.",
// to: "team@example.com"
}
failure {
echo '💥 Pipeline failed!'
// emailext subject: "❌ Build Failed: ${env.JOB_NAME} #${env.BUILD_NUMBER}",
// body: "The build failed. Please check the logs.",
// to: "team@example.com"
}
unstable {
echo '⚠️ Pipeline is unstable!'
}
}
}
很多信息就图快,没有配置参数,凑合用,以后再考虑优化
日志(只截取了代码拉取,还有k8s部署的日志)
Started by user kane
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/k8s-test
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] timestamps
[Pipeline] {
[Pipeline] timeout
22:10:22 Timeout set to expire in 30 min
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Tool Install)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
22:10:22 🔍 Checking out code...
[Pipeline] sh
22:10:22 + git clone git@gitee.com:diaoguihua/demo-test.git
22:10:22 Cloning into 'demo-test'...
[Pipeline] echo
22:10:24 code end
[Pipeline] sh
22:10:24 + ls
22:10:24 demo-test
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy)
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] tool
[Pipeline] envVarsForTool
[Pipeline] withEnv
[Pipeline] {
[Pipeline] echo
22:10:24 🚀 Deploying application...
[Pipeline] script
[Pipeline] {
[Pipeline] sh
22:10:25 + echo Deploying test-deployment.yaml to kane@192.168.129.135
22:10:25 Deploying test-deployment.yaml to kane@192.168.129.135
22:10:25 + cd demo-test/k8s
22:10:25 + pwd
22:10:25 /var/jenkins_home/workspace/k8s-test/demo-test/k8s
22:10:25 + ls
22:10:25 test-deployment.yaml
22:10:25 + scp ./test-deployment.yaml kane@192.168.129.135:/home/kane/k8s/
22:10:25 + ssh -T kane@192.168.129.135
22:10:25 Welcome to Ubuntu 24.04 LTS (GNU/Linux 6.8.0-147-generic x86_64)
22:10:25
22:10:25 * Documentation: https://help.ubuntu.com
22:10:25 * Management: https://landscape.canonical.com
22:10:25 * Support: https://ubuntu.com/pro
22:10:25
22:10:25 System information as of Thu Nov 12 02:10:25 PM UTC 2026
22:10:25
22:10:25 System load: 0.54 Processes: 310
22:10:25 Usage of /: 76.1% of 9.75GB Users logged in: 1
22:10:25 Memory usage: 13% IPv4 address for ens33: 192.168.129.135
22:10:25 Swap usage: 0%
22:10:25
22:10:25
22:10:25 Expanded Security Maintenance for Applications is not enabled.
22:10:25
22:10:25 167 updates can be applied immediately.
22:10:25 13 of these updates are standard security updates.
22:10:25 To see these additional updates run: apt list --upgradable
22:10:25
22:10:25 Enable ESM Apps to receive additional future security updates.
22:10:25 See https://ubuntu.com/esm or run: sudo pro status
22:10:25
22:10:25 New release '26.04.1 LTS' available.
22:10:25 Run 'do-release-upgrade' to upgrade to it.
22:10:25
22:10:25
22:10:25 /home/kane
22:10:25 /home/kane/k8s
22:10:25 nginx-deployment.yaml
22:10:25 test-deployment.yaml
22:10:26 deployment.apps/test-deployment created
22:10:26 ssh end
22:10:26 + echo ✅ Deployment completed
22:10:26 ✅ Deployment completed
22:10:26 + exit 0
[Pipeline] }
[Pipeline] // script
Post stage
[Pipeline] echo
22:10:26 ✅ Deployment completed successfully!
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
22:10:26 🧹 Cleaning up workspace...
[Pipeline] cleanWs
22:10:26 [WS-CLEANUP] Deleting project workspace...
22:10:26 [WS-CLEANUP] Deferred wipeout is used...
22:10:26 [WS-CLEANUP] done
[Pipeline] echo
22:10:26 🎉 Pipeline completed successfully!
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // timeout
[Pipeline] }
[Pipeline] // timestamps
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
测试
点击"立即构建"
查看日志输出
查看k8s主节点服务器135
查看pod是否启动
kubectl get pod -A

pod:test-deployment-85bf7558ff-rxjtw
查看pod详细信息,集群ip,拉取镜像,启动日志等
kubectl describe pod test-deployment-85bf7558ff-rxjtw

集群ip 10.244.0.113,用这个ip测试pod内部启动的服务是否正常
访问应用
curl 10.244.0.113:8080

返回hello kane666, 返回成功
docker检查(非必须)
镜像情况(哪个节点启动的pod去哪个机器执行)
docker images

容器启动情况
docker ps
