One-Click Deployment of Spring Cloud Microservices Using Jenkins, Docker, and Kubernetes
This guide walks through setting up a fully automated CI/CD pipeline that pulls code from Git, builds it with Maven, packages the resulting JAR into a Docker image, pushes the image to a registry, and finally deploys or updates the service on Kubernetes using Jenkins pipelines and supporting scripts.
The article demonstrates how to achieve one‑click deployment of a Spring Cloud microservice by leveraging Jenkins, Docker, and Kubernetes. The workflow consists of five main steps: developers push code to Git, Jenkins automatically pulls the code, Maven builds the project, the built JAR is packaged into a Docker image and pushed to a Docker registry, and the image is deployed or updated on a Kubernetes cluster.
1. Configure SSH‑KEY for Git access – Generate an ed25519 key pair with ssh-keygen -t ed25519 -C "[email protected]", add the public key to the Git platform (e.g., Gitee), and verify connectivity using ssh -T [email protected].
2. Create Jenkins pipeline – Define a pipeline job with a parameter for the Git branch. The pipeline script includes stages for pulling code, compiling with Maven, building the Docker image, and deploying to Kubernetes. Example snippet:
pipeline {
agent any
environment {
REPOSITORY = "[email protected]:xxxxxx/cloud-demo.git"
REPOSITORY_VERSION = "${params.BRANCH}"
}
stages {
stage('拉代码') {
steps {
deleteDir()
git branch: "${REPOSITORY_VERSION}", url: "${REPOSITORY}"
}
}
// ... other stages ...
}
}3. Maven build – Install Maven on Jenkins, then run a series of mvn -U clean install commands across multiple modules (project, dependencies, common, test modules, etc.) within the 编译代码 stage.
4. Build and push Docker image – Prepare a directory containing build.sh, Dockerfile, and Spring Cloud configuration files. The build.sh script tags the image with the Git revision and timestamp, builds it with docker build, pushes it to the registry, and writes the full image name to an IMAGE file.
#!/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}" > IMAGE5. Kubernetes deployment – Create a cloud-demo-m-test-dubbo-service.yaml file defining a Deployment and a NodePort Service. The placeholder IMAGE_AND_TAG is replaced with the actual image name read from the IMAGE file during the 发布 stage, then applied with kubectl apply -f.
apiVersion: apps/v1
kind: Deployment
metadata:
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:
- name: cloud-demo-m-test-dubbo-service
image: IMAGE_AND_TAG
ports:
- containerPort: 20881
---
apiVersion: v1
kind: Service
metadata:
name: cloud-demo-m-test-dubbo-service
spec:
type: NodePort
ports:
- port: 20881
targetPort: 20881
selector:
app: cloud-demo-m-test-dubbo-serviceAfter the pipeline runs, the image is available in the registry (e.g.,
cloud-demo/cloud-demo-m-test-dubbo-service:master-7012e1d-2023.05.01.10.16) and the service is reachable via the assigned NodePort. The article concludes with a brief promotional note encouraging readers to follow the author.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
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.
