apiVersion: apps/v1
kind: Deployment
metadata:
  name: email-api
  namespace: juwan
  labels:
    app: email-api
spec:
  replicas: 3
  revisionHistoryLimit: 5
  selector:
    matchLabels:
      app: email-api
  template:
    metadata:
      labels:
        app: email-api
    spec:
      serviceAccountName: find-endpoints
      containers:
        - name: email-api
#          image: 103.236.53.208:4418/library/email-api@sha256:fe5c66f5bcb1a39652620df42351de3e48227920a34be3110a45eb13db327020
          image: email-api:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8888
            - containerPort: 4001
          env:
            - name: KAFKA_BROKER
              value: "my-cluster-kafka-bootstrap.kafka:9092"
            - name: REDIS_M_HOST
              value: "user-redis-master.juwan:6379"
            - name: REDIS_S_HOST
              value: "user-redis-replica.juwan:6379"
            - name: REDIS_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: user-redis
                  key: password
          readinessProbe:
            tcpSocket:
              port: 8888
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            tcpSocket:
              port: 8888
            initialDelaySeconds: 15
            periodSeconds: 20
          resources:
            requests:
              cpu: 500m
              memory: 512Mi
            limits:
              cpu: 1000m
              memory: 1024Mi
          volumeMounts:
            - name: timezone
              mountPath: /etc/localtime
      volumes:
        - name: timezone
          hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai

---
apiVersion: v1
kind: Service
metadata:
  name: email-api-svc
  namespace: juwan
  annotations:
    prometheus.io/scrape: "true"
    prometheus.io/port: "4001"
    prometheus.io/path: "/metrics"
spec:
  ports:
    - name: http
      port: 8888
      targetPort: 8888
    - name: metrics
      port: 4001
      targetPort: 4001
  selector:
    app: email-api

---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: email-api-hpa-c
  namespace: juwan
  labels:
    app: email-api-hpa-c
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: email-api
  minReplicas: 3
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 80

---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: email-api-hpa-m
  namespace: juwan
  labels:
    app: email-api-hpa-m
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: email-api
  minReplicas: 3
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: memory
        target:
          type: Utilization
          averageUtilization: 80
