<p>I have successfully followed the documentation <a href="https://cloud.google.com/endpoints/docs/openapi/get-started-kubernetes-engine" rel="nofollow noreferrer">here</a> and <a href="https://cloud.google.com/endpoints/docs/openapi/get-started-kubernetes-engine#deploy_backend" rel="nofollow noreferrer">here</a> to deploy an API spec and GKE backend to Cloud Endpoints.</p>

<p>This has left me with a deployment.yaml that looks like this:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: esp-myproject
spec:
  ports:
  - port: 80
    targetPort: 8081
    protocol: TCP
    name: http
  selector:
    app: esp-myproject
  type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: esp-myproject
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: esp-myproject
    spec:
      containers:
      - name: esp
        image: gcr.io/endpoints-release/endpoints-runtime:1
        args: [
          "--http_port=8081",
          "--backend=127.0.0.1:8080",
          "--service=myproject1-0-0.endpoints.myproject.cloud.goog",
          "--rollout_strategy=managed",
        ]
        ports:
        - containerPort: 8081
      - name: myproject
        image: gcr.io/myproject/my-image:v0.0.1
        ports:
        - containerPort: 8080
</code></pre>

<p>This creates a single replica of the app on the backend. So far, so good...</p>

<p>I now want to update the yaml file to <strong>declaratively</strong> specify auto-scaling parameters to enable multiple replicas of the app to run alongside each other when traffic to the endpoint justifies more than one.</p>

<p>I have read around (O'Reilly book: Kubernetes Up &amp; Running, GCP docs, K8s docs), but there are two things on which I'm stumped:</p>

<ol>
<li>I've read a number of times about the HorizontalPodAutoscaler and it's not clear to me whether the deployment <strong>must</strong> make use of this in order to enjoy the benefits of autoscaling?</li>
<li>If so, I have seen examples in the docs of how to define the spec for the HorizontalPodAutoscaler in yaml as shown below - but how would I combine this with my existing deployment.yaml?</li>
</ol>

<p>HorizontalPodAutoscaler example (<a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale-walkthrough/#autoscaling-on-multiple-metrics-and-custom-metrics" rel="nofollow noreferrer">from the docs</a>):</p>

<pre><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: php-apache
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: php-apache
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
</code></pre>

<p>Thanks in advance to anyone who can shed some light on this for me.</p>

<blockquote>
  <ol>
  <li>I've read a number of times about the HorizontalPodAutoscaler and it's not clear to me whether the deployment must make use of this in order to enjoy the benefits of autoscaling?</li>
  </ol>
</blockquote>

<p>Doesn't have to, but it's recommended and it's already built in. You can build your own automation that scales up and down but the question is why since it's already supported with the HPA.</p>

<blockquote>
  <ol start="2">
  <li>If so, I have seen examples in the docs of how to define the spec for the HorizontalPodAutoscaler in yaml as shown below - but how would I combine this with my existing deployment.yaml?</li>
  </ol>
</blockquote>

<p>It should be straightforward. You basically reference your deployment in the HPA definition:</p>

<pre><code>apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: my-esp-project-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: esp-myproject  &lt;== here
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50
</code></pre>
