apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: default
  name: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
  serviceName: "wordpress"
  replicas: 1
  template:
    metadata:
      labels:
        app: wordpress
    spec:
      terminationGracePeriodSeconds: 30
      serviceAccountName: sql-wp-ksa-wi
      containers:
      - name: wordpress
        resources: 
          limits:
            cpu: "200m"
            memory: "100Mi"
        image: wordpress:5.2.2-apache
        imagePullPolicy: IfNotPresent
        env:
        - name: WORDPRESS_DB_HOST
          value: 127.0.0.1:3306
        - name: WORDPRESS_DB_USER
          valueFrom:
            secretKeyRef:
              name: wordpress-cloudsql-db-credentials
              key: username
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: wordpress-cloudsql-db-credentials
              key: password
        ports:
        - containerPort: 80
        volumeMounts:
        - name: wordpress-volume
          mountPath: /var/www/html
        readinessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 180
          periodSeconds: 10
          timeoutSeconds: 10
          failureThreshold: 10
          successThreshold: 1
        livenessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 10
          failureThreshold: 20
          successThreshold: 1
      - name: cloudsql-proxy
        resources:
          limits:
            cpu: "200m"
            memory: "100Mi"
        image: gcr.io/cloudsql-docker/gce-proxy:1.11
        env:
          - name: CONNECTION_NAME
            valueFrom:
              secretKeyRef:
                name: wordpress-cloudsql-db-credentials
                key: connectionName
          - name: PROJECT_ID
            valueFrom:
              secretKeyRef:
                name: wordpress-cloudsql-db-credentials
                key: projectId
        command: ["/cloud_sql_proxy",
                  "-instances=$(PROJECT_ID):$(CONNECTION_NAME)=tcp:3306"]
  volumeClaimTemplates:
    - metadata:
        name: wordpress-volume
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 10Gi
