今回はwordpressをおうちk8sに乗せてみようと思って試してみた記事になります。
「helmでinstallするだけやろ」と高を括っていましたが、アイキャッチ画像からわかるように、波乱になって時間がかかりました。
やはり、k8s初心者には案の定k8sが上手くいかないというわけです。
そこそこボリュームがあるので、記事は2部構成で、本記事はpvをboundするまでになります。
今回の作業は全てmaster nodeにて行います。
master nodeにてhelm install
まずは、helm installしてみます。
$ helm install my-wordpress bitnami/wordpress
しかし、起動させたpodを見てみると、Pendingになっており起動ができていませんでした。
$ kubectl get pods | grep wordpress
my-wordpress-7d7f6d45fc-k2mt2 0/1 Pending 0 37s
my-wordpress-mariadb-0 0/1 Pending 0 36s
podの詳細を見てみます。
$ kubectl describe pod my-wordpress-7d7f6d45fc-k2mt2
・・・【略】・・・
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 84s default-scheduler 0/2 nodes are available: pod has unbound immediate PersistentVolumeClaims. preemption: 0/2 nodes are available: 2 Preemption is not helpful for scheduling.
Warning FailedScheduling 83s default-scheduler 0/2 nodes are available: pod has unbound immediate PersistentVolumeClaims. preemption: 0/2 nodes are available: 2 Preemption is not helpful for scheduling.
以下のメッセージが出ていました。
0/2 nodes are available: pod has unbound immediate PersistentVolumeClaims. preemption: 0/2 nodes are available: 2 Preemption is not helpful for scheduling.
PersistentVolumeClaims(PVC)とは永続化領域の要求を行うリソースのことです。
永続化領域はPersistentVolume(PV)というVolumeとして確保されます。
今回のメッセージはpvcを使用しているけど、そのpvcはどのpvにもバインドされていないことで発生しています。
pvcを見てみると、Pendingになっています。
$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
data-my-wordpress-mariadb-0 Pending <unset> 24h
my-wordpress Pending <unset> 24h
このPendingがBoundになっていることが今回の記事のゴールです。
ちなみに、最初の状態だとpvはありませんでした。
ということでまずはpvを用意する必要があります。
今回用意したファイルは以下です。
- my-wordpress-mariadb-pv.yaml
- mariadbのpv用ファイル
- my-wordpress-pv.yaml
- wordpressのpv用ファイル
- values.yaml
- helmの設定を更新するためのvalueファイル
values.yamlは以下のコマンドで出力します。
helm show values bitnami/wordpress > values.yaml
それぞれのファイルの中身は以下のようになっています。
mariadbのpv(my-wordpress-mariadb-pv.yaml)
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-data-my-wordpress-mariadb-0
spec:
capacity:
storage: 8Gi # PVCが要求する容量と一致させる
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: strage-my-wordpress-mariadb # PVCが要求するストレージクラスと一致させる
hostPath:
path: "/mnt/data-my-wordpress-mariadb-0"
wordpressのpv(my-wordpress-pv.yaml)
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-data-my-wordpress
spec:
capacity:
storage: 10Gi # PVCが要求する容量と一致させる
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: strage-my-wordpress # PVCが要求するストレージクラスと一致させる
hostPath:
path: "/mnt/data-my-wordpress"
values.yaml
USER-SUPPLIED VALUES:
persistence:
accessMode: ReadWriteOnce
enabled: true
size: 10Gi
storageClass: strage-my-wordpress
mariadb:
persistence:
enabled: true
existingClaim: data-my-wordpress-mariadb-0
「UPGRADE FAILED: cannot patch “xxxxx” with kind PersistentVolumeClaim」が発生した
ここで以下のようなエラーが発生した場合、
$ helm upgrade my-wordpress bitnami/wordpress -f values.yaml
Error: UPGRADE FAILED: cannot patch "my-wordpress" with kind PersistentVolumeClaim: PersistentVolumeClaim "my-wordpress" is invalid: [spec: Forbidden: spec is immutable after creation except resources.requests and volumeAttributesClassName for bound claims
core.PersistentVolumeClaimSpec{
AccessModes: {"ReadWriteOnce"},
Selector: nil,
Resources: core.VolumeResourceRequirements{
Limits: nil,
- Requests: core.ResourceList{
- s"storage": {i: resource.int64Amount{value: 10737418240}, s: "10Gi", Format: "BinarySI"},
- },
+ Requests: core.ResourceList{
+ s"storage": {i: resource.int64Amount{value: 5368709120}, s: "5Gi", Format: "BinarySI"},
+ },
},
VolumeName: "",
StorageClassName: nil,
... // 4 identical fields
}
, spec.resources.requests.storage: Forbidden: field can not be less than previous value]
values.yamlファイルの以下の場所を変更すること。
USER-SUPPLIED VALUES:
persistence:
accessMode: ReadWriteOnce
enabled: true
size: 10Gi # ★変更
storageClass: strage-my-wordpress
ちなみに、storageClassの値は以下のコマンドで取得できます。
$ kubectl get pvc my-wordpress -o=jsonpath='{.spec.storageClassName}'
strage-my-wordpress
ここまで出来たら払い出していきます。
pvを2つ払い出します。
$ kubectl apply -f my-wordpress-mariadb-pv.yaml
$ kubectl apply -f my-wordpress-pv.yaml
helmを更新します。
$ helm upgrade my-wordpress bitnami/wordpress -f values.yaml
これでSTATUSがBoundになります。
$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
pv-data-my-wordpress 10Gi RWO Retain Bound default/my-wordpress strage-my-wordpress <unset> 6d16h
pv-data-my-wordpress-mariadb-0 8Gi RWO Retain Bound default/data-my-wordpress-mariadb-0 strage-my-wordpress-mariadb <unset> 6d16h
で、上手くいったと思いきや、まだpodがちゃんと立ち上がっていません。
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
my-wordpress-7d7f6d45fc-d97ml 0/1 CreateContainerConfigError 0 24m
my-wordpress-mariadb-0 0/1 CrashLoopBackOff 5 (21s ago) 24m
案の定ではありますが、k8sうまくいきませんね。。
これは次回にしたいと思います。
今回はここまでにします。