apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: local-test-affinity
spec:
  serviceName: "local-service"
  replicas: 3
  template:
    metadata:
      labels:
        app: local-test-affinity
    spec:
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - local-test-affinity
            topologyKey: kubernetes.io/hostname
      containers:
      - name: test-container
        image: gcr.io/google_containers/busybox:1.24
        command: 
        - "/bin/sh"
        args:
        - "-c"
        - "count=0; count_file=\"/usr/test-pod/count\"; test_file=\"/usr/test-pod/test_file\"; if [ -e $count_file ]; then count=$(cat $count_file); fi; echo $((count+1)) > $count_file; while [ 1 ]; do date >> $test_file; echo \"This is $MY_POD_NAME, count=$(cat $count_file)\" >> $test_file; sleep 10; done"
        volumeMounts:
        - name: local-vol
          mountPath: /usr/test-pod
        env:
        - name: MY_POD_NAME
          valueFrom: 
            fieldRef:
              fieldPath: metadata.name
      securityContext:
        fsGroup: 1234
  volumeClaimTemplates:
  - metadata:
      name: local-vol
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "local-storage"
      resources:
        requests:
          storage: 1Gi
