<p>I have a simple wordpress site defined by the <code>ReplicationController</code> and <code>Service</code> below. Once the app is deployed and running happily, I enabled autoscaling on the instance group created by Kubernetes by going to the GCE console and enabling autoscaling with the same settings (max 5, cpu 10).</p>
<p>Autoscaling the instances and the pods seem to work decent enough except that they keep going out of sync with each other. The RC autoscaling removes the pods from the CE instances but nothing happens with the instances so they start failing requests until the LB health check fails and removes them.</p>
<h2>Is there a way to make kubernetes scale the pods AS WELL as scale the instances that they run on so this doesn't happen? Or is there a way to keep them in sync?</h2>
<p>My process is as follows:</p>
<p><em>Create the cluster</em></p>
<p><code>$ gcloud container clusters create wordpress -z us-central1-c -m f1-micro</code></p>
<p><em>Create the rc</em></p>
<p><code>$ kubectl create -f rc.yml</code></p>
<p><em>Create the service</em></p>
<p><code>$ kubectl create -f service.yml</code></p>
<p><em>Autoscale the rc</em></p>
<p><code>$ kubectl autoscale rc frontend --max 5 --cpu-percent=10</code></p>
<p>Then I enabled the autoscaling in the console and gave the servers load to make them scale.</p>
<p><strong>rc.yml</strong></p>
<pre><code>apiVersion: v1
kind: ReplicationController
metadata:
  name: frontend
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      containers:
      - image: custom-wordpress-image
        name: wordpress
        ports:
          - containerPort: 80
            hostPort: 80
</code></pre>
<p><strong>service.yml</strong></p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  labels:
    name: frontend
  name: frontend
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
      protocol: TCP
  selector:
    name: wordpress
</code></pre>
<hr />
<p><strong>Update for more information</strong></p>
<p>If I don't use kubernetes autoscaler and instead set the replicas to the same number as the instance group autoscaler max instance count, I seem to get the desired result. As instances are added to the instance group, kubernetes provisions them, as they are removed kubernetes updates accordingly. At this point I wonder what the purpose of the Kubernetes autoscaler is for.</p>

<h2>TLDR;</h2>

<p>In your usecase kubernetes is only giving you overhead. You are running 1 pod (docker container) on each instance in your instance group. You could also have your Docker container be deployed to App Engine flexible (former Managed VM's) <a href="https://cloud.google.com/appengine/docs/flexible/custom-runtimes/" rel="nofollow">https://cloud.google.com/appengine/docs/flexible/custom-runtimes/</a> and let the autoscaling of your instance group handle it.</p>

<h2>Longer answer</h2>

<p>It is not possible (yet) to link the instance scaling to the pod scaling in k8s. This is because they are two separate problems. The HPA of k8s is meant to have (small) pods scale to spread load over your cluster (big machines) so they will be scaling because of increased load.</p>

<p>If you do not define any limits (1 pod per machine) you could set the max amount of pods to the max scaling of your cluster effectively setting all these pods in a <code>pending</code> state until another instance spins up.</p>

<p>If you want your pods to let your nodes scale then the best way (we found out) is to have them 'overcrowd' an instance so the instance-group scaling will kick in. We did this by setting pretty low memory/cpu requirements for our pods and high limits, effectively allowing them to burst over the total available CPU/memory of the instance.</p>

<pre><code>resources:
  requests:
    cpu: 400m
    memory: 100Mi
  limits:
    cpu: 1000m
    memory: 1000Mi
</code></pre>
