现在有一个springboot服务,如何将这个服务打包成镜像,并部署到k8s中,指定副本=3,设置每个副本cpu、内存的资源,如果cpu和内存资源达到80%,就增加副本,如果资源下降到60%,就缩减副本
1. 准备工作
1.1 创建 Spring Boot 应用
确保你有一个可用的 Spring Boot 应用,可以通过 Maven 或 Gradle 打包成 JAR 文件。
1.2 安装必要工具
Docker
kubectl (Kubernetes 命令行工具)
访问 Kubernetes 集群的权限
2. 创建 Docker 镜像
2.1 编写 Dockerfile
在 Spring Boot 项目根目录创建 Dockerfile:
# 使用 OpenJDK 11 作为基础镜像
FROM openjdk:11-jre-slim
# 设置工作目录
WORKDIR /app
# 复制打包好的 JAR 文件到镜像中
COPY target/your-springboot-app.jar app.jar
# 暴露应用端口
EXPOSE 8080
# 设置 JVM 参数
ENV JAVA_OPTS="-Xms256m -Xmx512m"
# 启动应用
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar app.jar"]2.2 构建 Docker 镜像
# 构建镜像
docker build -t your-username/springboot-app:1.0.0 .
# 测试镜像
docker run -p 8080:8080 your-username/springboot-app:1.0.0
# 推送到镜像仓库(如 Docker Hub)
docker push your-username/springboot-app:1.0.03. 创建 Kubernetes 部署文件
创建 springboot-deployment.yaml 文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-app
labels:
app: springboot-app
spec:
replicas: 3 # 初始副本数
selector:
matchLabels:
app: springboot-app
template:
metadata:
labels:
app: springboot-app
spec:
containers:
- name: springboot-app
image: your-username/springboot-app:1.0.0 # 你的镜像地址
ports:
- containerPort: 8080
resources:
requests:
memory: "512Mi" # 每个容器请求的内存
cpu: "250m" # 每个容器请求的CPU (250 milliCPU)
limits:
memory: "1Gi" # 每个容器内存上限
cpu: "500m" # 每个容器CPU上限
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: springboot-service
spec:
selector:
app: springboot-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer # 根据你的环境选择类型,可以是 ClusterIP、NodePort 或 LoadBalancer4. 创建 Horizontal Pod Autoscaler (HPA)
创建 springboot-hpa.yaml 文件来实现自动扩缩容:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: springboot-app-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: springboot-app
minReplicas: 3 # 最小副本数
maxReplicas: 10 # 最大副本数
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80 # CPU使用率达到80%时扩容
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80 # 内存使用率达到80%时扩容
behavior:
scaleDown:
policies:
- type: Pods
value: 1
periodSeconds: 60
stabilizationWindowSeconds: 300
selectPolicy: Min
scaleUp:
policies:
- type: Pods
value: 2
periodSeconds: 60
stabilizationWindowSeconds: 0
selectPolicy: Max5. 部署到 Kubernetes
5.1 应用配置
# 创建命名空间(可选)
kubectl create namespace springboot-app
# 部署应用
kubectl apply -f springboot-deployment.yaml
# 部署HPA
kubectl apply -f springboot-hpa.yaml5.2 验证部署
# 查看部署状态
kubectl get deployments
# 查看Pod状态
kubectl get pods
# 查看服务
kubectl get services
# 查看HPA状态
kubectl get hpa
# 查看Pod资源使用情况(需要metrics-server)
kubectl top pods6. 监控和调试
6.1 查看详细日志
# 查看特定Pod的日志
kubectl logs <pod-name>
# 实时查看日志
kubectl logs -f <pod-name>
# 查看所有Pod的日志(如果有多个副本)
kubectl logs -l app=springboot-app --all-containers=true6.2 监控HPA行为
# 查看HPA详情
kubectl describe hpa springboot-app-hpa
# 查看事件
kubectl get events --sort-by=.metadata.creationTimestamp7. 注意事项
Metrics Server:确保你的Kubernetes集群已安装Metrics Server,HPA需要它来获取资源使用指标:
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yamlSpring Boot Actuator:确保你的Spring Boot应用包含Actuator依赖,以便提供健康检查端点:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>资源限制:根据你的应用实际需求调整CPU和内存的请求和限制值。
就绪和存活探针:根据你的应用启动时间调整
initialDelaySeconds。自动扩缩容行为:HPA的扩缩容行为可能需要根据实际负载模式进行调整。
8. 扩展配置(可选)
8.1 配置Ingress(如果需要外部访问)
创建 springboot-ingress.yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: springboot-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: your-domain.com # 你的域名
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: springboot-service
port:
number: 80应用配置:
kubectl apply -f springboot-ingress.yaml8.2 配置ConfigMap和Secret(用于应用配置)
apiVersion: v1
kind: ConfigMap
metadata:
name: springboot-config
data:
application.properties: |
server.port=8080
spring.datasource.url=jdbc:mysql://mysql-service:3306/mydb
# 其他配置...然后在Deployment中引用:
# 在Deployment的spec.template.spec.containers部分添加
envFrom:
- configMapRef:
name: springboot-config总结
通过以上步骤,你已经成功将Spring Boot服务部署到Kubernetes,并配置了:
初始副本数为3
每个副本的CPU和内存资源限制
基于CPU和内存使用率(80%)的自动扩容
自动缩容策略
要验证自动扩缩容是否正常工作,你可以对应用进行压力测试,然后观察HPA的行为和Pod数量的变化。使用以下命令实时监控:
watch -n 2 "kubectl get hpa && echo && kubectl get pods"这将每2秒刷新一次HPA状态和Pod列表,方便你观察自动扩缩容的效果。