apiVersion: apps/v1
kind: Deployment
metadata:
  name: product-service
  namespace: erp-system
  labels:
    app: product-service
    version: v1
spec:
  replicas: 3
  selector:
    matchLabels:
      app: product-service
  template:
    metadata:
      labels:
        app: product-service
        version: v1
    spec:
      containers:
        - name: product-service
          image: erp-product-service:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8083
              name: http
              protocol: TCP
          env:
            - name: SPRING_PROFILES_ACTIVE
              value: "prod"
            - name: DB_HOST
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: db.host
            - name: DB_PORT
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: db.port
            - name: DB_NAME
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: db.name
            - name: DB_USERNAME
              valueFrom:
                secretKeyRef:
                  name: erp-secrets
                  key: db.username
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: erp-secrets
                  key: db.password
            - name: REDIS_HOST
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: redis.host
            - name: REDIS_PORT
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: redis.port
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: erp-secrets
                  key: redis.password
            - name: NACOS_SERVER
              valueFrom:
                configMapKeyRef:
                  name: erp-config
                  key: nacos.server
            - name: JAVA_OPTS
              value: "-Xms512m -Xmx1024m -XX:+UseG1GC -Duser.timezone=Asia/Shanghai"
          resources:
            requests:
              memory: "512Mi"
              cpu: "250m"
            limits:
              memory: "1024Mi"
              cpu: "1000m"
          livenessProbe:
            httpGet:
              path: /product/api/goods/health
              port: 8083
            initialDelaySeconds: 60
            periodSeconds: 30
            timeoutSeconds: 10
            successThreshold: 1
            failureThreshold: 3
          readinessProbe:
            httpGet:
              path: /product/api/goods/health
              port: 8083
            initialDelaySeconds: 30
            periodSeconds: 10
            timeoutSeconds: 5
            successThreshold: 1
            failureThreshold: 3
          volumeMounts:
            - name: log-volume
              mountPath: /var/log/product-service
      volumes:
        - name: log-volume
          persistentVolumeClaim:
            claimName: product-service-logs
---
apiVersion: v1
kind: Service
metadata:
  name: product-service
  namespace: erp-system
  labels:
    app: product-service
spec:
  type: ClusterIP
  ports:
    - port: 8083
      targetPort: 8083
      protocol: TCP
      name: http
  selector:
    app: product-service
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: product-service-hpa
  namespace: erp-system
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: product-service
  minReplicas: 3
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 80
