# A headless service to create DNS records
apiVersion: v1
kind: Service
metadata:
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  # *.nginx.default.svc.cluster.local
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1alpha1
kind: PetSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        pod.alpha.kubernetes.io/initialized: "true"
        pod.beta.kubernetes.io/init-containers: '[
            {
                "name": "peerfinder",
                "image": "gcr.io/google_containers/peer-finder:0.1",
                "args": [
                    "-on-start=\"\"echo `
                    readarray PEERS;
                    echo $(hostname) > /usr/share/nginx/html/index.html;
                    if [ 1 = ${#PEERS[@]} ]; then
                        echo \"events{} http { server{ } }\";
                    else
                        echo \"events{} http { server{ location / { proxy_pass http://${PEERS[0]}; } } }\";
                    fi;` > /conf/nginx.conf\"\"",
                    "-service=nginx"
                ],
                "env": [
                  {
                      "name": "POD_NAMESPACE",
                      "valueFrom": {
                          "fieldRef": {
                              "apiVersion": "v1",
                              "fieldPath": "metadata.namespace"
                          }
                      }
                   }
                ],
                "volumeMounts": [
                    {
                        "name": "www",
                        "mountPath": "/usr/share/nginx/html"
                    },
                    {
                        "name": "conf",
                        "mountPath": "/conf"
                    }
                ]
            }
        ]'
    spec:
      terminationGracePeriodSeconds: 0
      containers:
      - name: nginx
        image: gcr.io/google_containers/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        command:
        - nginx
        args:
        - -g
        - "daemon off;"
        - -c
        - "/conf/nginx.conf"
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
        - name: conf
          mountPath: /conf
      volumes:
      - name: conf
        emptyDir: {}
  volumeClaimTemplates:
  - metadata:
      name: www
      annotations:
        volume.alpha.kubernetes.io/storage-class: anything
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi

