淘客app容器化部署方案:Docker与Kubernetes在返利系统中的实践
大家好,我是省赚客APP研发者阿可!省赚客APP(juwatech.cn)作为高并发导购返利平台,包含商品解析、佣金计算、订单回调、搜索推荐等多个微服务模块。为提升资源利用率、实现弹性伸缩与快速迭代,我们全面采用 Docker + Kubernetes 容器化部署架构。本文将结合实际配置文件与部署脚本,详解从镜像构建到 K8s 编排的完整实践。
Docker 镜像标准化:多阶段构建 Java 应用
以佣金计算服务 rebate-calculation-service 为例,使用多阶段构建减小镜像体积:
Dockerfile
# 构建阶段
FROM maven:3.8.6-openjdk-17 AS builder
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests -Dspring.profiles.active=prod
# 运行阶段
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY --from=builder /app/target/rebate-calculation-service-1.0.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-XX:+UseG1GC", "-Xmx512m", "-jar", "app.jar"]
构建并推送至私有 Harbor 仓库:
bash
docker build -t harbor.juwatech.cn/micro/rebate-calculation-service:1.0.3 .
docker push harbor.juwatech.cn/micro/rebate-calculation-service:1.0.3

Kubernetes Deployment:声明式部署微服务
每个服务对应一个 Deployment,确保副本数与滚动更新策略:
yaml
# rebate-calculation-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: rebate-calculation
namespace: juwatech-prod
spec:
replicas: 4
selector:
matchLabels:
app: rebate-calculation
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
template:
metadata:
labels:
app: rebate-calculation
spec:
containers:
- name: rebate-calculation
image: harbor.juwatech.cn/micro/rebate-calculation-service:1.0.3
ports:
- containerPort: 8080
env:
- name: SPRING_PROFILES_ACTIVE
value: "prod"
- name: JAVA_OPTS
value: "-Xmx512m -Xms512m"
resources:
requests:
memory: "300Mi"
cpu: "200m"
limits:
memory: "600Mi"
cpu: "500m"
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10
readinessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 10
periodSeconds: 5
Service 与 Ingress:暴露内部服务
通过 ClusterIP Service 供内部调用,Ingress 对外提供 API 网关入口:
yaml
# rebate-calculation-service.yaml
apiVersion: v1
kind: Service
metadata:
name: rebate-calculation-svc
namespace: juwatech-prod
spec:
selector:
app: rebate-calculation
ports:
- protocol: TCP
port: 80
targetPort: 8080
yaml
# api-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: juwatech-api-ingress
namespace: juwatech-prod
annotations:
nginx.ingress.kubernetes.io/rate-limit: "10"
nginx.ingress.kubernetes.io/rate-limit-window: "1m"
spec:
rules:
- host: api.juwatech.cn
http:
paths:
- path: /rebate
pathType: Prefix
backend:
service:
name: rebate-calculation-svc
port:
number: 80
ConfigMap 与 Secret:配置外置
数据库连接、Redis 地址等敏感信息通过 Secret 管理,非敏感配置用 ConfigMap:
yaml
# application-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: rebate-calculation-config
namespace: juwatech-prod
data:
application.yml: |
spring:
datasource:
url: jdbc:mysql://mysql.juwatech.cn:3306/juwatech_commission
username: ${DB_USER}
password: ${DB_PASSWORD}
redis:
host: redis.juwatech.cn
port: 6379
在 Deployment 中挂载:
yaml
spec:
containers:
- name: rebate-calculation
volumeMounts:
- name: config-volume
mountPath: /app/config
readOnly: true
volumes:
- name: config-volume
configMap:
name: rebate-calculation-config
Secret 以环境变量注入:
yaml
env:
- name: DB_USER
valueFrom:
secretKeyRef:
name: db-credentials
key: username
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: password
HPA 自动扩缩容:应对流量高峰
基于 CPU 和自定义指标(如 RabbitMQ 队列长度)自动扩缩:
yaml
# rebate-hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: rebate-calculation-hpa
namespace: juwatech-prod
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: rebate-calculation
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
- type: Pods
pods:
metric:
name: rabbitmq_queue_messages_ready
target:
type: AverageValue
averageValue: "50"
需配合 Prometheus Adapter 暴露 RabbitMQ 指标。
日志与监控集成
所有容器日志输出到 stdout,由 Filebeat 采集至 ELK;同时通过 Micrometer 暴露 /actuator/prometheus 指标,由 Prometheus 抓取:
java
// juwatech.cn.config.MetricsConfig
@Configuration
public class MetricsConfig {
@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
return registry -> registry.config().commonTags("application", "rebate-calculation");
}
}
本文著作权归聚娃科技省赚客app开发者团队,转载请注明出处!