<p>I'm trying HPA: <a href="https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/</a></p>

<p>PV:</p>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: api-orientdb-pv
  labels:
    app: api-orientdb
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: api-orientdb-{{ .Values.cluster.name | default "testing" }}
    fsType: ext4
</code></pre>

<p>PVC:</p>

<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: api-orientdb-pv-claim
  labels:
    app: api
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  selector:
    matchLabels:
      app: api-orientdb
  storageClassName: ""
</code></pre>

<p>HPA:</p>

<pre><code>Name:                           api-orientdb-deployment
Namespace:                      default
Labels:                         &lt;none&gt;
Annotations:                        &lt;none&gt;
CreationTimestamp:                  Thu, 08 Jun 2017 10:37:06 +0700
Reference:                      Deployment/api-orientdb-deployment
Metrics:                        ( current / target )
  resource cpu on pods  (as a percentage of request):   17% (8m) / 10%
Min replicas:                       1
Max replicas:                       2
Events:                         &lt;none&gt;
</code></pre>

<p>and new pod has been created:</p>

<pre><code>NAME                                       READY     STATUS    RESTARTS   AGE
api-orientdb-deployment-2506639415-n8nbt   1/1       Running   0          7h
api-orientdb-deployment-2506639415-x8nvm   1/1       Running   0          6h
</code></pre>

<p>As you can see, I'm using <code>gcePersistentDisk</code> which does not support <code>ReadWriteMany</code> access mode.</p>

<p>Newly created pod also mount the volume as <code>rw</code> mode:</p>

<pre><code>Name:        api-orientdb-deployment-2506639415-x8nvm
Containers:
    Mounts:
      /orientdb/databases from api-orientdb-persistent-storage (rw)
Volumes:
  api-orientdb-persistent-storage:
    Type:   PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  api-orientdb-pv-claim
    ReadOnly:   false
</code></pre>

<p>Question: How does it work in this case? Is there a way to config the mainly pod (<code>n8nbt</code>) to use a PV with <code>ReadWriteOnce</code> access mode, and all other scaled pod (<code>x8nvm</code>) should be <code>ReadOnlyMany</code>? How to do it automatically?</p>

<p>The only way I can think of is create another PVC mount the same disk but with different <code>accessModes</code>, but then the question becomes to: how to config the newly scaled pod to use that PVC?</p>

<hr>

<p><strong>Fri Jun  9 11:29:34 ICT 2017</strong></p>

<p>I found something: there is nothing ensure that the newly scaled pod will be run on the same node as the first pod. So, if the volume plugin does not support <code>ReadWriteMany</code> and the scaled pod is run on another node, it will failed to mount:</p>

<blockquote>
  <p>Failed to attach volume "api-orientdb-pv" on node
  "gke-testing-default-pool-7711f782-4p6f" with: googleapi: Error 400:
  The disk resource
  'projects/xx/zones/us-central1-a/disks/api-orientdb-testing' is
  already being used by
  'projects/xx/zones/us-central1-a/instances/gke-testing-default-pool-7711f782-h7xv'</p>
</blockquote>

<p><a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#access-modes" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/storage/persistent-volumes/#access-modes</a></p>

<blockquote>
  <p>Important! A volume can only be mounted using one access mode at a time, even if it supports many. For example, a GCEPersistentDisk can be mounted as ReadWriteOnce by a single node or ReadOnlyMany by many nodes, but not at the same time.</p>
</blockquote>

<p>If so, the only way to ensure that the HPA works is <code>ReadWriteMany</code> access mode must be supported by the volume plugin?</p>

<hr>

<p><strong>Fri Jun  9 14:28:30 ICT 2017</strong></p>

<blockquote>
  <p>If you want only one Pod to be able to write then create two Deployments. One with replicas: 1 and the other one that has the autoscaler attached (and has readOnly: true in it)</p>
</blockquote>

<p>OK.</p>

<blockquote>
  <p>Do note that a GCE PD can only be mounted by a single node if any of the Pods are accessing it readWrite.</p>
</blockquote>

<p>Then I have to use <a href="https://kubernetes.io/docs/concepts/configuration/assign-pod-node/" rel="nofollow noreferrer">label selectors</a> to ensure that all pods end up on the same node, right?</p>

<blockquote>
  <p>Your question is not clear to me</p>
</blockquote>

<p>Let me explain: in case of autoscaling, assuming that by using label selectors, I can ensure that newly scaled pod ends up on the same node, but since volume is mounted as <code>rw</code>, does it break the GCE PD as we have 2 pods mount a volume as <code>rw</code>?</p>

<blockquote>
  <p>First of all, generally, if you have a Deployment with replicas: 1 you won't have 2 Pod running at the same time (most of the time!!)</p>
</blockquote>

<p>I know.</p>

<blockquote>
  <p>On the other hand if a PVC specifies ReadWriteOnce then after the first Pod is scheduled any other Pods will need to be scheduled on the same node or not be scheduled at all (most common case: there aren't enough resources on the Node)</p>
</blockquote>

<p>In case of HPA, it does not. Please see my above updates for more details.</p>

<blockquote>
  <p>If for any reason you do have 2 Pods accessing the same mount readWrite then it's completely up the the application what will happen and is not kubernetes specific</p>
</blockquote>

<p>The main thing made me confused is:</p>

<blockquote>
  <p>ReadWriteOnce – the volume can be mounted as read-write by a single node</p>
</blockquote>

<p>OK, node, not pod. But in case of autoscaling, if 2 pods are running on the same node, and both mount the volume as <code>rw</code>, does GCE PD support it? If so, how does it work?</p>

<p>It's working as intended. The <em>Once</em> in <code>ReadWriteOnce</code> refers to the number of Nodes that can use the PVC and not the number of Pods (HPA or no HPA).</p>

<p>If you want only one Pod to be able to write then create two Deployments. One with <code>replicas: 1</code> and the other one that has the autoscaler attached (and has <code>readOnly: true</code> in it). Do note that a GCE PD can only be mounted by a single node if any of the Pods are accessing it readWrite.</p>
