apiVersion: apps/v1
kind: Deployment
metadata:
  name: customer-service
  namespace: erp
  labels:
    app: customer-service
    version: v1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: customer-service
  template:
    metadata:
      labels:
        app: customer-service
        version: v1
    spec:
      containers:
      - name: customer-service
        image: erp-customer-service:latest
        ports:
        - containerPort: 8086
          name: http
          protocol: TCP
        env:
        - name: SPRING_PROFILES_ACTIVE
          value: prod
        - name: NACOS_SERVER_ADDR
          value: "nacos.erp.svc.cluster.local:8848"
        - name: NACOS_NAMESPACE
          value: "prod"
        - name: DB_HOST
          value: "mysql.erp.svc.cluster.local"
        - name: DB_NAME
          value: "erp_java"
        - name: DB_USERNAME
          valueFrom:
            secretKeyRef:
              name: erp-db-secret
              key: username
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: erp-db-secret
              key: password
        - name: REDIS_HOST
          value: "redis.erp.svc.cluster.local"
        - name: REDIS_PASSWORD
          valueFrom:
            secretKeyRef:
              name: erp-redis-secret
              key: password
        resources:
          requests:
            memory: "512Mi"
            cpu: "250m"
          limits:
            memory: "1Gi"
            cpu: "1000m"
        livenessProbe:
          httpGet:
            path: /customer/actuator/health
            port: 8086
          initialDelaySeconds: 60
          periodSeconds: 30
          timeoutSeconds: 10
          failureThreshold: 3
        readinessProbe:
          httpGet:
            path: /customer/actuator/health
            port: 8086
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 3
        volumeMounts:
        - name: logs
          mountPath: /app/logs
      volumes:
      - name: logs
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  name: customer-service
  namespace: erp
  labels:
    app: customer-service
spec:
  type: ClusterIP
  ports:
  - port: 8086
    targetPort: 8086
    protocol: TCP
    name: http
  selector:
    app: customer-service
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: customer-service-hpa
  namespace: erp
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: customer-service
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70
  - type: Resource
    resource:
      name: memory
      target:
        type: Utilization
        averageUtilization: 80
