Running Stateful Sets on Non Preemptible nodes?

9/12/2018

I'm trying to run a StatefulSet 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.

As mentioned in this post, We can do it with Deployments / Pods like this:

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

But How am I supposed to do this on statefulsets?

-- Daksh Miglani
google-cloud-platform
google-kubernetes-engine
kubernetes

1 Answer

9/12/2018

You can use it in the spec definition just like in deployments:

Example:

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
-- Rico
Source: StackOverflow