satefulsetにおけるpv,pvcの動作確認

# はじめに
statefulsetを再作成した際に、PVが変わらないことを確認する。

## GKEクラスタを作成
```
gcloud beta container --project "YOUR-PROJECT-ID" clusters create "pv-pvc-test-cluster" --zone "asia-northeast1-a" --username "admin" --cluster-version "1.11.2-gke.18" --machine-type "n1-standard-1" --image-type "COS" --disk-type "pd-standard" --disk-size "100" --scopes "https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/logging.write","https://www.googleapis.com/auth/monitoring","https://www.googleapis.com/auth/servicecontrol","https://www.googleapis.com/auth/service.management.readonly","https://www.googleapis.com/auth/trace.append" --num-nodes "1" --enable-cloud-logging --enable-cloud-monitoring --network "projects/YOUR-PROJECT-ID/global/networks/default" --addons HorizontalPodAutoscaling,HttpLoadBalancing --enable-autoupgrade --enable-autorepair
```

## nginxのstatefulsetを作成

```yml:sts.yml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      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" ]
      resources:
        requests:
          storage: 1Gi
```

`kubectl apply -f sts.yml`


## 中身の確認

```shell
$ kubectl get sts
NAME      DESIRED   CURRENT   AGE
web       2         1         38s

$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM               STORAGECLASS   REASON    AGE
pvc-3c38d1f9-e7af-11e8-b834-42010a9200af   1Gi        RWO            Delete           Bound     default/www-web-0   standard                 1m
pvc-547a769d-e7af-11e8-b834-42010a9200af   1Gi        RWO            Delete           Bound     default/www-web-1   standard                 20s

$ kubectl get pvc
NAME        STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
www-web-0   Bound     pvc-3c38d1f9-e7af-11e8-b834-42010a9200af   1Gi        RWO            standard       1m
www-web-1   Bound     pvc-547a769d-e7af-11e8-b834-42010a9200af   1Gi        RWO            standard       42s
```

## statefulsetを削除

```
$ kubectl delete sts web
statefulset.apps "web" deleted
```

## statefulsetを再作成

`kubectl apply -f sts.yml`

```
$ kubectl get sts
NAME      DESIRED   CURRENT   AGE
web       2         2         43s

$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM               STORAGECLASS   REASON    AGE
pvc-3c38d1f9-e7af-11e8-b834-42010a9200af   1Gi        RWO            Delete           Bound     default/www-web-0   standard                 4m
pvc-547a769d-e7af-11e8-b834-42010a9200af   1Gi        RWO            Delete           Bound     default/www-web-1   standard                 3m

$ kubectl get pvc
NAME        STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
www-web-0   Bound     pvc-3c38d1f9-e7af-11e8-b834-42010a9200af   1Gi        RWO            standard       6m
www-web-1   Bound     pvc-547a769d-e7af-11e8-b834-42010a9200af   1Gi        RWO            standard       5m
```

同じディスクがアタッチされていることが分かる。

## PVCのディスクサイズを変えてみるテスト

### statesulsetを削除

`$ kubectl delete sts web`

###削除後の状態を確認

```
$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM               STORAGECLASS   REASON    AGE
pvc-3c38d1f9-e7af-11e8-b834-42010a9200af   1Gi        RWO            Delete           Bound     default/www-web-0   standard                 15m
pvc-547a769d-e7af-11e8-b834-42010a9200af   1Gi        RWO            Delete           Bound     default/www-web-1   standard                 15m

$ kubectl get pvc
NAME        STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
www-web-0   Bound     pvc-3c38d1f9-e7af-11e8-b834-42010a9200af   1Gi        RWO            standard       15m
www-web-1   Bound     pvc-547a769d-e7af-11e8-b834-42010a9200af   1Gi        RWO            standard       15m

```

```yml:sts.yml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      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" ]
      resources:
        requests:
          storage: 2Gi #←1Giを2Giに変更
```

### 再度statefulsetを作成

`$ kubectl apply -f sts.yml`

### pv,pvcを確認しても(CAPACITY含めて)何も変わらない。

```
$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM               STORAGECLASS   REASON    AGE
pvc-3c38d1f9-e7af-11e8-b834-42010a9200af   1Gi        RWO            Delete           Bound     default/www-web-0   standard                 17m
pvc-547a769d-e7af-11e8-b834-42010a9200af   1Gi        RWO            Delete           Bound     default/www-web-1   standard                 16m

$ kubectl get pvc
NAME        STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
www-web-0   Bound     pvc-3c38d1f9-e7af-11e8-b834-42010a9200af   1Gi        RWO            standard       18m
www-web-1   Bound     pvc-547a769d-e7af-11e8-b834-42010a9200af   1Gi        RWO            standard       17m
```