<p>I'm trying to run a <code>StatefulSet</code> on my Kubernetes Cluster which has preemptible nodes in it, but I don't want to run StatefulSets on preemptible nodes as they are available for 24hrs at max.</p>

<p>As mentioned in this <a href="https://medium.com/google-cloud/using-preemptible-vms-to-cut-kubernetes-engine-bills-in-half-de2481b8e814" rel="nofollow noreferrer">post</a>, We can do it with Deployments / Pods like this:</p>

<pre><code>affinity:
  nodeAffinity:
    requiredDuringSchedulingIgnoredDuringExecution:
      nodeSelectorTerms:
      - matchExpressions:
        - key: cloud.google.com/gke-preemptible
          operator: DoesNotExist 
</code></pre>

<p>But How am I supposed to do this on statefulsets?</p>

<p>You can use it in the spec definition just like in deployments:</p>

<p>Example:</p>

<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
  serviceName: "nginx"
  replicas: 3 # by default is 1
  template:
    metadata:
      labels:
        app: nginx # has to match .spec.selector.matchLabels
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
           - matchExpressions:
              - key: cloud.google.com/gke-preemptible
                operator: DoesNotExist
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "my-storage-class"
      resources:
        requests:
          storage: 1Gi
</code></pre>
