One‑Click Deployment of Spring Cloud Microservices Using Jenkins, Docker, and Kubernetes
This guide walks through a complete one‑click Jenkins pipeline that pulls Spring Cloud source code from Git, builds it with Maven, packages the JAR into a Docker image, pushes the image to a registry, and finally deploys or updates the service on Kubernetes, covering SSH‑key setup, pipeline scripting, and Kubernetes resource definitions.
Deploy a Spring Cloud microservice with a one‑click Jenkins pipeline that pulls code from Git, builds it with Maven, packages the JAR into a Docker image, pushes the image to a Docker registry, and deploys the service on Kubernetes.
1. Push code to Git
Developers push their code to a Git repository; this step is not detailed further.
2. Jenkins pipeline configuration
2.1 Configure SSH‑KEY for Git access
Generate an SSH key on the Jenkins host, add the public key to the Git platform (e.g., Gitee), and test the connection.
ssh-keygen -t ed25519 -C "[email protected]"View the generated public key:
cat ~/.ssh/id_ed25519.pubAdd the public key to the repository’s deployment‑key settings.
ssh -T [email protected]Test cloning via SSH:
git clone [email protected]:xxxx.git2.2 Create Jenkins pipeline job
In Jenkins, create a new pipeline job, enable parameterized builds, and add a string parameter (e.g., REPOSITORY_VERSION ) to specify the Git branch or tag.
2.2.1 Pipeline script for code checkout and compilation
pipeline {
agent any
environment {
REPOSITORY = "[email protected]:xxxxxx/cloud-demo.git"
}
stages {
stage('拉代码') {
steps {
echo "start fetch code from git:${REPOSITORY}"
deleteDir()
git branch: "${REPOSITORY_VERSION}", url: "${REPOSITORY}"
}
}
stage('编译代码') {
steps {
echo "start compile"
sh "cd cloud-demo-project && mvn -U clean install"
// additional Maven build steps omitted for brevity
}
}
}
}2.2.2 Build Docker image
Prepare a directory on the Jenkins node to hold the build scripts and Dockerfile:
mkdir -p /usr/local/project/.env/cloud-demo-m-test-dubbo-service/Files placed in this directory:
build.sh
Dockerfile
application.properties
bootstrap.properties
Example build.sh script:
#!/usr/bin/env bash
REPOSITORY_VERSION=$1
GIT_REVISION=$(git log -1 --pretty=format:"%h")
TIME=$(date "+%Y.%m.%d.%H.%M")
IMAGE_NAME=192.168.31.100:5000/cloud-demo/cloud-demo-m-test-dubbo-service
IMAGE_TAG=${REPOSITORY_VERSION}-${GIT_REVISION}-${TIME}
docker build -t ${IMAGE_NAME}:${IMAGE_TAG} .
docker push ${IMAGE_NAME}:${IMAGE_TAG}
echo "${IMAGE_NAME}:${IMAGE_TAG}" > IMAGEExample Dockerfile :
FROM openjdk:8u342-jdk
MAINTAINER yanger [email protected]
COPY target/cloud-demo-m-test-dubbo-service-1.0-SNAPSHOT.jar /cloud-demo-m-test-dubbo-service.jar
COPY application.properties /application.properties
COPY bootstrap.properties /bootstrap.properties
ENTRYPOINT ["java","-jar","/cloud-demo-m-test-dubbo-service.jar"]Start a local Docker registry if none exists:
docker run -d -p 5000:5000 --name registry registryMake the build script executable:
chmod 755 build.sh2.2.3 Extend pipeline to build image and deploy
pipeline {
agent any
environment {
REPOSITORY = "[email protected]:xxxxxx/cloud-demo.git"
SCRIPT_PATH = "/usr/local/project/.env/cloud-demo-m-test-dubbo-service"
IMAGE = ""
}
stages {
// 拉代码 and 编译代码 stages omitted for brevity
stage('构建镜像') {
steps {
echo "start build image"
sh "cp ${SCRIPT_PATH}/build.sh cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cp ${SCRIPT_PATH}/Dockerfile cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cp ${SCRIPT_PATH}/application.properties cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cp ${SCRIPT_PATH}/bootstrap.properties cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "cd cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/ && ./build.sh ${REPOSITORY_VERSION}"
}
}
stage('发布') {
steps {
echo "start deploy"
script {
IMAGE = readFile "cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/IMAGE"
IMAGE = IMAGE.trim()
if (IMAGE == "") { throw new Exception("获取镜像名文件失败,请重试") }
}
echo "IMAGE: -- ${IMAGE}"
sh "cp ${SCRIPT_PATH}/cloud-demo-m-test-dubbo-service.yaml cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/"
sh "sed -i \"s#IMAGE_AND_TAG#${IMAGE}#g\" cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/cloud-demo-m-test-dubbo-service.yaml"
sh "kubectl apply -f cloud-demo-m-test/dubbo/cloud-demo-m-test-dubbo-service/cloud-demo-m-test-dubbo-service.yaml"
}
}
}
}3. Kubernetes resource definition
Create cloud-demo-m-test-dubbo-service.yaml under the script directory:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: cloud-demo-m-test-dubbo-service
name: cloud-demo-m-test-dubbo-service
spec:
replicas: 1
selector:
matchLabels:
app: cloud-demo-m-test-dubbo-service
template:
metadata:
labels:
app: cloud-demo-m-test-dubbo-service
spec:
containers:
- image: IMAGE_AND_TAG
name: cloud-demo-m-test-dubbo-service
volumeMounts:
- name: log-path
mountPath: /logs
volumes:
- name: log-path
hostPath:
path: /root/k8s/cloud-demo-m-test-dubbo-service/logs
---
apiVersion: v1
kind: Service
metadata:
labels:
app: cloud-demo-m-test-dubbo-service
name: cloud-demo-m-test-dubbo-service
spec:
ports:
- port: 20881
protocol: TCP
targetPort: 20881
selector:
app: cloud-demo-m-test-dubbo-service
type: NodePortReplace the placeholder IMAGE_AND_TAG with the actual image name and tag generated by the build script, then apply the manifest with kubectl apply -f … .
4. Verification
After the Jenkins job finishes, check the Docker registry to confirm the image was pushed, and run kubectl get all on the Kubernetes master to verify the deployment and service are running.
5. Additional notes
The article also includes promotional text for a backend technical community, which is not part of the technical guide.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.