apiVersion: apps/v1
kind: Deployment
metadata:
  name: $APP_INSTANCE_NAME-kube-state-metrics
  labels:
    k8s-app: kube-state-metrics
    app.kubernetes.io/name: $APP_INSTANCE_NAME
    app.kubernetes.io/component: kube-state-metrics
spec:
  selector:
    matchLabels:
      k8s-app: kube-state-metrics
  replicas: 1
  template:
    metadata:
      labels:
        k8s-app: kube-state-metrics
    spec:
      serviceAccountName: kube-state-metrics
      containers:
      - name: kube-state-metrics
        # Use the `quay.io/coreos/kube-state-metrics:v1.5.0` container image
        image: quay.io/coreos/kube-state-metrics:v1.5.0
        imagePullPolicy: Always
        ports:
        # `8080` is used to scrape Kubernetes object state metrics
        - name: http-metrics
          containerPort: 8080
        # `8081` to scrape its own general process metrics
        - name: telemetry
          containerPort: 8081
        # A readiness probe is configured to hit `/healthz` at `8080`
        readinessProbe:
          httpGet:
            path: /healthz
            port: 8080
          initialDelaySeconds: 5
          timeoutSeconds: 5
      # Watch the `kube-state-metrics` Pod's resource usage, and dynamically scale the Deployment as necessary. 
      # To learn more, you can consult the `README` from https://github.com/kubernetes/autoscaler/tree/master/addon-resizer
      - name: addon-resizer
        image: k8s.gcr.io/addon-resizer:1.7
        resources:
          limits:
            cpu: 100m
            memory: 30Mi
          requests:
            cpu: 100m
            memory: 30Mi
        env:
          - name: MY_POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: MY_POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
        command:
          - /pod_nanny
          - --container=kube-state-metrics
          - --cpu=100m
          - --extra-cpu=1m
          - --memory=100Mi
          - --extra-memory=2Mi
          - --threshold=5
          - --deployment=$APP_INSTANCE_NAME-kube-state-metrics
