淘客app容器化部署方案:Docker与Kubernetes在返利系统中的实践

淘客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开发者团队,转载请注明出处!

相关推荐
木童66213 分钟前
K8s 组网方案深度解析:Flannel vs Calico 原理与选型
云原生·容器·kubernetes
魂之木32 分钟前
【零基础教程】基于Docker的RabbitMQ部署方案
分布式·docker·微服务·rabbitmq
星哥说事33 分钟前
零成本上线!用 Hugging Face免费服务器+Docker 快速部署HertzBeat 监控平台
运维·服务器·docker
Mr.徐大人ゞ2 小时前
Docker 详解与部署微服务实战
docker·微服务·容器
mr_orange_klj2 小时前
关于k8s pod状态的AI问答(chatGPT)
云原生·容器·kubernetes
runfarther2 小时前
CentOS7.5下安装Docker和Docker-Compose,并支持host-gateway特性
docker·docker compose
oMcLin2 小时前
如何在 RHEL 8.4 上实现高效的 Docker 容器网络配置,优化跨容器的通信速度与可靠性?
docker·容器
人工智能训练3 小时前
UE5中如何解决角色网格体“掉下去”的问题
运维·服务器·windows·容器·ue5
DigitalOcean12 小时前
DigitalOcean容器注册表推出多注册表支持功能
容器