はじめに
microk8sでローカルのディレクトリをhostPath
でマウントしようとしたとき2時間ぐらい詰まったので、解決方法をメモとして残します
結論から言うと、pathに以下を指定すると上手く行くみたいです
- /data
- /var/lib/minikube
- /var/lib/docker
- /tmp/hostpath_pv
- /tmp/hostpath-provisioner
環境
- Ubuntu 18.0.4.3 LTS
- microk8s 1.14
背景
以下のようなカレントディレクトリの構造で
.
├── data
│ └── storage
└── nginx.yml
ローカルの./data/storage
をnginxのコンテナ上の/home/storage
にマウントしようと思っていました
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- image: nginx:1.17.8-alpine
name: nginx
volumeMounts:
- name: storage
mountPath: /home/storage
volumes:
- name: storage
hostPath:
path: ./data/storage
type: DirectoryOrCreate
しかし、いつまでたってもPodのステータスがRunning
にならない!?
終いにはCrashLoopBackOff
になってる。。。
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 0/1 CrashLoopBackOff 7 14m
ログの確認
describe
オプションで詳細を確認したところ、下から3行目にエラーが書いてありました
$ kubectl describe pod nginx
Name: nginx
Namespace: default
Priority: 0
PriorityClassName: <none>
〜〜〜〜〜〜〜
<<中略>>
〜〜〜〜〜〜〜
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 41s default-scheduler Successfully assigned default/nginx to taro
Normal Created 24s (x3 over 40s) kubelet, taro Created container nginx
Warning Failed 24s (x3 over 39s) kubelet, taro Error: failed to create containerd task: OCI runtime create failed: container_linux.go:345: starting container process caused "process_linux.go:424: container init caused \"rootfs_linux.go:58: mounting \\\"/var/snap/microk8s/common/run/containerd/io.containerd.runtime.v1.linux/k8s.io/nginx/data/storage\\\" to rootfs \\\"/var/snap/microk8s/common/run/containerd/io.containerd.runtime.v1.linux/k8s.io/nginx/rootfs\\\" at \\\"/home/storage\\\" caused \\\"stat /var/snap/microk8s/common/run/containerd/io.containerd.runtime.v1.linux/k8s.io/nginx/data/storage: no such file or directory\\\"\"": unknown
Warning BackOff 13s (x4 over 38s) kubelet, taro Back-off restarting failed container
Normal Pulled 0s (x4 over 41s) kubelet, taro Container image "nginx:1.17.8-alpine" already present on machine
no such file or directory!?
いやいや、ローカルに./data/storage
あるけども!?
解決策
2時間ぐらい試行錯誤した結果、以下の記事を見つけ解決しました。
A note on mounts, persistence, and minikube hosts
minikube is configured to persist files stored under the following directories, which are made in the Minikube VM (or on your localhost if running on bare metal). You may lose data from other directories on reboots.
/data
/var/lib/minikube
/var/lib/docker
/tmp/hostpath_pv
/tmp/hostpath-provisioner
引用:https://minikube.sigs.k8s.io/docs/reference/persistent_volumes/
どうやら、minikubeでは上記のパスが対応しているようです。
これは、microk8sでも同様で、nginx.yml
のhostPath.pathを以下のように書き換えるとエラーが解消しました。
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- image: nginx:1.17.8-alpine
name: nginx
volumeMounts:
- name: storage
mountPath: /home/storage
volumes:
- name: storage
hostPath:
path: /data/storage # カレントではなく、ルートのディレクトリを見るように修正
type: DirectoryOrCreate
PodのステータスもRunning
!!🎉🎉
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 2m