RWO  PV 使用時の RollingUpdate strategy について

# 目的

レプリカ 1個であれば、RWO の PVでも、`spec.updateStrategy.type` = `RollingUpdate` の指定が使える事を確認する。
OpenShift 環境なので、`kubectl` の代わりに `oc` を使用。

# 準備 

`updateStrategy` に `RollingUpdate` を指定した Deploymentを用意。`replicas` は `1` に指定。

```yaml: deploy-rolling.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rolling-update-test
spec:
  selector:
    matchLabels:
      app: sample-container
  replicas: 1
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: sample-container
    spec:
      containers:
      - name: test-container
        image: gcr.io/google-samples/hello-app:1.0
        volumeMounts:
        - name: test-pvc
          mountPath: /data
      volumes:
      - name:  test-pvc
        persistentVolumeClaim:
            claimName: test-claim
```

RWO PV の Dynamic Provisioning 用の PVC

```yaml:test-pvc.yaml 
apiVersion: "v1"
kind: "PersistentVolumeClaim"
metadata:
  name: "test-claim"
spec:
  accessModes:
    - "ReadWriteOnce"
  resources:
    requests:
      storage: "1Gi"
  storageClassName: thin
```

Pod と PVC の作成

```
[root@bastion openshift]# oc apply -f deploy-rolling.yaml 
deployment.apps/rolling-update-test created
[root@bastion openshift]# oc get pods
NAME                                   READY   STATUS    RESTARTS   AGE
rolling-update-test-5d87f68687-vvwv5   0/1     Pending   0          5s
[root@bastion openshift]# oc apply -f test-pvc.yaml 
[root@bastion openshift]# oc get pods
NAME                                   READY   STATUS    RESTARTS   AGE
rolling-update-test-5d87f68687-vvwv5   1/1     Running   0          2m22s
[root@bastion openshift]#
```


PVC内に データ (test.txt) の作成

```
[root@bastion openshift]# oc exec -it rolling-update-test-5d87f68687-vvwv5 -- touch /data/test.txt
[root@bastion openshift]# oc exec -it rolling-update-test-5d87f68687-vvwv5 -- ls /data
lost+found  test.txt
[root@bastion openshift]#
```

# 実験

## Pod を削除しても同じ PVC を使って復活する事を確認

Pod の削除

```
[root@bastion openshift]# kubectl delete pods rolling-update-test-5d87f68687-vvwv5
pod "rolling-update-test-5d87f68687-vvwv5" deleted
[root@bastion openshift]#
```

Deployment が生きているので自動で復活する

```
[root@bastion openshift]# oc get pods
NAME                                   READY   STATUS              RESTARTS   AGE
rolling-update-test-5d87f68687-fxjmw   0/1     ContainerCreating   0          5s
[root@bastion openshift]# oc get pods
NAME                                   READY   STATUS    RESTARTS   AGE
rolling-update-test-5d87f68687-fxjmw   1/1     Running   0          28s
[root@bastion openshift]#
```

以前の PVCの内容が残っているか確認。

```
[root@bastion openshift]# oc exec -it rolling-update-test-5d87f68687-fxjmw -- ls /data
lost+found  test.txt
[root@bastion openshift]# 
```

`test.txt` が、ちゃんと残っている。

## replicas = 2 にすると 2つ目の Pod が起動しない事を確認

`replicas` = `2` に変更

```
[root@bastion openshift]# cat deploy-rolling.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rolling-update-test
spec:
  selector:
    matchLabels:
      app: sample-container
  replicas: 2
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: sample-container
    spec:
      containers:
      - name: test-container
        image: gcr.io/google-samples/hello-app:1.0
        volumeMounts:
        - name: test-pvc
          mountPath: /data
      volumes:
      - name:  test-pvc
        persistentVolumeClaim:
            claimName: test-claim

[root@bastion openshift]#
[root@bastion openshift]# oc apply -f deploy-rolling.yaml 
deployment.apps/rolling-update-test configured
[root@bastion openshift]# 
```

2つ目の Pod がスタック中。

```
[root@bastion openshift]# oc get pods
NAME                                   READY   STATUS              RESTARTS   AGE
rolling-update-test-5d87f68687-fxjmw   1/1     Running             0          19m
rolling-update-test-5d87f68687-sjjjh   0/1     ContainerCreating   0          4m45s
[root@bastion openshift]# 
```

スタックしている Pod の event を確認

```
[root@bastion openshift]# oc describe pods rolling-update-test-5d87f68687-sjjjh
<省略>
Events:
  Type     Reason              Age                  From                     Message
  ----     ------              ----                 ----                     -------
  Normal   Scheduled           5m21s                default-scheduler        Successfully assigned rolling-update-test/rolling-update-test-5d87f68687-sjjjh to ocp48-6vldl-infra-94vjm
  Warning  FailedAttachVolume  5m21s                attachdetach-controller  Multi-Attach error for volume "pvc-015d41c7-e2ca-4393-a984-98d9a691fe6f" Volume is already used by pod(s) rolling-update-test-5d87f68687-fxjmw
  Warning  FailedMount         74s (x2 over 3m18s)  kubelet                  Unable to attach or mount volumes: unmounted volumes=[test-pvc], unattached volumes=[test-pvc kube-api-access-r92tk]: timed out waiting for the condition
```

理由は ` Volume is already used by pod(s) rolling-update-test-5d87f68687-fxjmw` なので予想通りの動き。