<p>our server running using Kubernetes for auto-scaling and we use newRelic for observability
but we face some issues</p>
<p>1- we need to restart pods when memory usage reaches 1G it automatically restarts when it reaches 1.2G but everything goes slowly.</p>
<p>2- terminate pods when there no requests to the server</p>
<p>my configuration</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
  labels:
    app: {{ .Release.Name }}
spec:
  revisionHistoryLimit: 2
  replicas: {{ .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Release.Name }}
          image: &quot;{{ .Values.imageRepository }}:{{ .Values.tag }}&quot;
          env:
            {{- include &quot;api.env&quot; . | nindent 12 }}
          resources:
            limits:
              memory: {{ .Values.memoryLimit }}
              cpu: {{ .Values.cpuLimit }}
            requests:
              memory: {{ .Values.memoryRequest }}
              cpu: {{ .Values.cpuRequest }}
      imagePullSecrets:
        - name: {{ .Values.imagePullSecret }}      
      {{- if .Values.tolerations }}
      tolerations:
{{ toYaml .Values.tolerations | indent 8 }}
      {{- end }}
      {{- if .Values.nodeSelector }}
      nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
      {{- end }}
</code></pre>
<p>my values file</p>
<pre><code>memoryLimit: &quot;2Gi&quot;
cpuLimit: &quot;1.0&quot;
memoryRequest: &quot;1.0Gi&quot;
cpuRequest: &quot;0.75&quot;
</code></pre>
<p>thats what I am trying to approach</p>
<p><a href="https://i.stack.imgur.com/kcoV1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kcoV1.png" alt="enter image description here" /></a></p>

<p>If you want to be sure your pod/deployment won't consume more than <code>1.0Gi</code> of memory then setting that <code>MemoryLimit</code> will do job just fine.</p>
<p>Once you set that limits and your container exceed it it becomes a potential candidate for termination. If it continues to consume memory beyond its limit, the Container will be terminated. If a terminated Container can be restarted, kubelet restarts it, as with any other type of runtime container failure.</p>
<p>For more readying please visit section <a href="https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/#exceed-a-container-s-memory-limit" rel="nofollow noreferrer">exceeding a container's memory limit</a></p>
<p>Moving on if you wish to scale your deployment based on requests you would require to have custom metrics to be provided by external adapter such as <a href="https://github.com/kubernetes-sigs/prometheus-adapter" rel="nofollow noreferrer">prometheus</a>. Horizontal pod autoascaler natively provides you scaling based only on CPU and Memory (based on the metrics from metrics server).</p>
<p>The adapter documents provides you <a href="https://github.com/kubernetes-sigs/prometheus-adapter/blob/master/docs/walkthrough.md" rel="nofollow noreferrer">walkthrough</a> how to configure it with Kubernetes API and HPA.  The list of other adapters can be found <a href="https://github.com/kubernetes/metrics/blob/master/IMPLEMENTATIONS.md" rel="nofollow noreferrer">here</a>.</p>
<p>Then you can scale your deployment based on the <code>http_requests</code> metric as showed <a href="https://github.com/stefanprodan/k8s-prom-hpa#auto-scaling-based-on-custom-metrics" rel="nofollow noreferrer">here</a> or <code>request-per-seconds</code> as described <a href="https://www.weave.works/blog/kubernetes-horizontal-pod-autoscaler-and-prometheus" rel="nofollow noreferrer">here</a>.</p>
