当前位置: 首页> 文旅> 美景 > 天元建设集团有限公司租赁公司_弄一个app要多少钱_友链交换平台源码_免费刷推广链接的网站

天元建设集团有限公司租赁公司_弄一个app要多少钱_友链交换平台源码_免费刷推广链接的网站

时间:2025/8/2 10:09:31来源:https://blog.csdn.net/LCY133/article/details/147199404 浏览次数:0次
天元建设集团有限公司租赁公司_弄一个app要多少钱_友链交换平台源码_免费刷推广链接的网站

在 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-appstrategy:rollingUpdate:maxSurge: 1       # 滚动更新时最大临时副本数maxUnavailable: 0 # 确保零停机更新type: RollingUpdatetemplate:metadata:labels:app: spring-appspec:containers:- name: spring-appimage: your-dockerhub-username/spring-app:1.0.0ports:- containerPort: 8080resources:requests:cpu: "100m"  # 最小 CPU 资源memory: "256Mi"limits:cpu: "500m"  # 最大 CPU 资源memory: "512Mi"livenessProbe:   # 存活检查httpGet:path: /actuator/healthport: 8080initialDelaySeconds: 30periodSeconds: 10readinessProbe:  # 就绪检查httpGet:path: /actuator/healthport: 8080initialDelaySeconds: 20periodSeconds: 5
(2) Service 配置(service.yaml
apiVersion: v1
kind: Service
metadata:name: spring-app-service
spec:selector:app: spring-appports:- protocol: TCPport: 80targetPort: 8080type: ClusterIP  # 或 LoadBalancer(云环境)

3. 动态管理 Pod 的关键配置

(1) 自动扩缩容(HPA)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:name: spring-app-hpa
spec:scaleTargetRef:apiVersion: apps/v1kind: Deploymentname: spring-app-deploymentminReplicas: 2   # 最小副本数maxReplicas: 10  # 最大副本数metrics:- type: Resourceresource:name: cputarget:type: UtilizationaverageUtilization: 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=8080spring.datasource.url=jdbc:mysql://mysql-service:3306/mydb

在 Deployment 中挂载 ConfigMap:

spec:containers:- name: spring-appvolumeMounts:- name: config-volumemountPath: /app/configvolumes:- name: config-volumeconfigMap: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_USERNAMEvalueFrom:secretKeyRef:name: db-secretkey: username

6. 监控与日志

(1) 查看 Pod 日志
kubectl logs -f <pod-name> --tail=100
(2) 集成 Prometheus + Grafana

在 Deployment 中启用 Spring Boot Actuator 的 Prometheus 端点:

management:endpoints:web:exposure:include: prometheusmetrics:tags:application: spring-app

常见问题排查

  1. Pod 无法启动
    • 检查镜像名称是否正确:kubectl describe pod <pod-name>
    • 查看容器日志:kubectl logs <pod-name>

  2. 服务无法访问
    • 确认 Service 的 selector 是否匹配 Pod 标签。
    • 检查端口映射:kubectl describe service spring-app-service

  3. HPA 不生效
    • 确认 Metrics Server 已安装:kubectl top nodes
    • 检查资源请求配置:kubectl describe hpa spring-app-hpa


总结

核心组件:Deployment 管理 Pod 生命周期,Service 暴露服务,HPA 实现自动扩缩容。
动态管理:通过 kubectl scalekubectl rollout 和 HPA 实现弹性伸缩。
最佳实践
• 使用健康检查(livenessProbe/readinessProbe)确保高可用。
• 通过 ConfigMap 和 Secret 分离配置与代码。
• 监控资源使用率以优化 HPA 策略。

关键字:天元建设集团有限公司租赁公司_弄一个app要多少钱_友链交换平台源码_免费刷推广链接的网站

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: