在 Kubernetes 中部署 Spring Boot 项目并实现 动态管理 Pod(自动扩缩容、滚动更新等),需要结合 Docker 镜像构建、Deployment 配置、Service 暴露和 HPA(Horizontal Pod Autoscaler) 等组件。以下是完整操作步骤:
1. 构建 Spring Boot 项目的 Docker 镜像
(1) 项目打包
确保 Spring Boot 项目已编译生成可执行的 JAR 文件(如 target/app.jar
)。
(2) 编写 Dockerfile
# 使用 OpenJDK 作为基础镜像
FROM openjdk:11-jre-slim
# 设置工作目录
WORKDIR /app
# 复制 JAR 文件到镜像中
COPY target/app.jar /app/app.jar
# 暴露端口(与 Spring Boot 的 server.port 一致)
EXPOSE 8080
# 启动命令
CMD ["java", "-jar", "app.jar"]
(3) 构建镜像
# 在项目根目录执行
docker build -t your-dockerhub-username/spring-app:1.0.0 .
(4) 推送镜像到仓库(可选)
docker push your-dockerhub-username/spring-app:1.0.0
2. 编写 Kubernetes 部署文件
(1) Deployment 配置(deployment.yaml
)
apiVersion: apps/v1
kind: Deployment
metadata:
name: spring-app-deployment
spec:
replicas: 3 # 初始副本数
selector:
matchLabels:
app: spring-app
strategy:
rollingUpdate:
maxSurge: 1 # 滚动更新时最大临时副本数
maxUnavailable: 0 # 确保零停机更新
type: RollingUpdate
template:
metadata:
labels:
app: spring-app
spec:
containers:
- name: spring-app
image: your-dockerhub-username/spring-app:1.0.0
ports:
- containerPort: 8080
resources:
requests:
cpu: "100m" # 最小 CPU 资源
memory: "256Mi"
limits:
cpu: "500m" # 最大 CPU 资源
memory: "512Mi"
livenessProbe: # 存活检查
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe: # 就绪检查
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 20
periodSeconds: 5
(2) Service 配置(service.yaml
)
apiVersion: v1
kind: Service
metadata:
name: spring-app-service
spec:
selector:
app: spring-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP # 或 LoadBalancer(云环境)
3. 动态管理 Pod 的关键配置
(1) 自动扩缩容(HPA)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: spring-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: spring-app-deployment
minReplicas: 2 # 最小副本数
maxReplicas: 10 # 最大副本数
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50 # CPU 使用率超过 50% 时扩容
(2) 触发部署
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f hpa.yaml
4. 动态管理操作示例
(1) 手动扩缩容
# 调整副本数
kubectl scale deployment spring-app-deployment --replicas=5
(2) 滚动更新镜像
# 修改 deployment.yaml 中的镜像版本
kubectl set image deployment/spring-app-deployment spring-app=your-dockerhub-username/spring-app:2.0.0
(3) 回滚到上一版本
kubectl rollout undo deployment/spring-app-deployment
(4) 查看 HPA 状态
kubectl get hpa
5. 配置管理进阶
(1) 使用 ConfigMap 管理 Spring Boot 配置
apiVersion: v1
kind: ConfigMap
metadata:
name: spring-app-config
data:
application.properties: |
server.port=8080
spring.datasource.url=jdbc:mysql://mysql-service:3306/mydb
在 Deployment 中挂载 ConfigMap:
spec:
containers:
- name: spring-app
volumeMounts:
- name: config-volume
mountPath: /app/config
volumes:
- name: config-volume
configMap:
name: spring-app-config
(2) 使用 Secret 管理敏感信息
apiVersion: v1
kind: Secret
metadata:
name: db-secret
type: Opaque
data:
username: dXNlcm5hbWU= # Base64 编码
password: cGFzc3dvcmQ=
在 Deployment 中引用 Secret:
env:
- name: DB_USERNAME
valueFrom:
secretKeyRef:
name: db-secret
key: username
6. 监控与日志
(1) 查看 Pod 日志
kubectl logs -f <pod-name> --tail=100
(2) 集成 Prometheus + Grafana
在 Deployment 中启用 Spring Boot Actuator 的 Prometheus 端点:
management:
endpoints:
web:
exposure:
include: prometheus
metrics:
tags:
application: spring-app
常见问题排查
Pod 无法启动:
• 检查镜像名称是否正确:kubectl describe pod <pod-name>
。
• 查看容器日志:kubectl logs <pod-name>
。服务无法访问:
• 确认 Service 的selector
是否匹配 Pod 标签。
• 检查端口映射:kubectl describe service spring-app-service
。HPA 不生效:
• 确认 Metrics Server 已安装:kubectl top nodes
。
• 检查资源请求配置:kubectl describe hpa spring-app-hpa
。
总结
• 核心组件:Deployment 管理 Pod 生命周期,Service 暴露服务,HPA 实现自动扩缩容。
• 动态管理:通过 kubectl scale
、kubectl rollout
和 HPA 实现弹性伸缩。
• 最佳实践:
• 使用健康检查(livenessProbe
/readinessProbe
)确保高可用。
• 通过 ConfigMap 和 Secret 分离配置与代码。
• 监控资源使用率以优化 HPA 策略。