LoginSignup
0
1

More than 5 years have passed since last update.

[小ネタ]PersistentVolume の Reclaim Policy を変える

Last updated at Posted at 2018-07-14

小ネタ。
以下のドキュメントの通りですが少しハマったのでメモ
基本的には以下のようにやれば良い

Change the Reclaim Policy of a PersistentVolume

kubectl patch pv -p ‘{“spec”:{“persistentVolumeReclaimPolicy”:“Retain”}}’

ただ上記はそのままコピペするとエラーになった

# JSON のパースでエラーとなる模様
$kubectl patch pv pvc-3fdc5920-8595-11e8-a565-06fc7dc53c2c -p{“spec”:{“persistentVolumeReclaimPolicy”:“Delete”}}’
Error from server (BadRequest): json: cannot unmarshal string into Go value of type map[string]interface {}

# コピペのJSONはダメ
$echo{“spec”:{“persistentVolumeReclaimPolicy”:“Delete”}}’ | jq .
parse error: Invalid numeric literal at line 1, column 4

# 少し変えて JSON がパースできることを確認
$echo '{"spec": {"persistentVolumeReclaimPolicy":"Delete"}}' | jq .
{
  "spec": {
    "persistentVolumeReclaimPolicy": "Delete"
  }
}

# 一応指定する JSON が合っているかも確認したが良さそう
$kubectl get pv pvc-3fdc5920-8595-11e8-a565-06fc7dc53c2c --output json |less

(一部略)
  "spec": {
        "accessModes": [
            "ReadWriteOnce"
        ],
        "awsElasticBlockStore": {
            "fsType": "ext4",
            "volumeID": "aws://us-west-2c/vol-03c4886d59252eb54"
        },
        "capacity": {
            "storage": "20Gi"
        },
        "claimRef": {
            "apiVersion": "v1",
            "kind": "PersistentVolumeClaim",
            "name": "mysql-pv-claim-6",
            "namespace": "default",
            "resourceVersion": "4636186",
            "uid": "04449e69-8558-11e8-a565-06fc7dc53c2c"
        },
        "mountOptions": [
            "debug"
        ],
        "persistentVolumeReclaimPolicy": "Retain",
        "storageClassName": "gp2"
    },
    "status": {
        "phase": "Released"
    }
}

これで準備OK
やってみる

# patch が無事成功
$kubectl patch pv pvc-3fdc5920-8595-11e8-a565-06fc7dc53c2c -p '{"spec": {"persistentVolumeReclaimPolicy":"Delete"}}'
persistentvolume "pvc-3fdc5920-8595-11e8-a565-06fc7dc53c2c" patched

# policy が Retain から Delete に変わっていることを確認
$kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                      STORAGECLASS   REASON    AGE
pvc-3fdc5920-8595-11e8-a565-06fc7dc53c2c   20Gi       RWO            Delete           Bound     default/mysql-pv-claim-3   gp2-2                    2d
pvc-41fbc5b0-8595-11e8-971c-022ca775d168   20Gi       RWO            Delete           Bound     default/mysql-pv-claim-4   gp2-2                    2d
pvc-440b8688-8595-11e8-a565-06fc7dc53c2c   20Gi       RWO            Delete           Bound     default/mysql-pv-claim-5   gp2-2                    2d

OK.

なお、PullRequest しようかと思ったが Github 上のコードだと抜けてしまっている「your-pv-name」というのもあり「'{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'」もJSONパース出来て問題ない。
Hugoにする時の問題?
整理できたらIssue切るか。。。。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1