Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

hostPathによるPodへのデータ共有(kubernetes)

LinuCエヴァンジェリスト・Open Source Summit Japanボランティアリーダー鯨井貴博@opensourcetechです。


はじめに
Kubernetes volume機能のhostPathを使ったデータ共有に関するメモです。


やってみる
早速、やってみます。

root@rke2-1:~# cat pod10.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: testweb
spec:
  containers:
  - name: testweb
    image: nginx
    volumeMounts:
    - mountPath: /tmp/aaa
      name: mydir
    - mountPath: /tmp/aaa/1.txt
      name: myfile
  volumes:
  - name: mydir
    hostPath:
      path: /tmp/aaa
      type: DirectoryOrCreate
  - name: myfile
    hostPath:
      path: /tmp/aaa/1.txt
      type: FileOrCreate

root@rke2-1:~# kubectl create -f pod10.yaml 
pod/testweb created

root@rke2-1:~# kubectl get pod
NAME      READY   STATUS    RESTARTS   AGE
testweb   1/1     Running   0          4s


では、実際に/tmp/aaaというディレクトリと1.txtというファイルが出来ているか確認。

root@rke2-1:~# kubectl exec -it testweb -- sh -c /bin/bash

root@testweb:/# cd /tmp

root@testweb:/tmp# ls   
aaa

root@testweb:/tmp# cd aaa

root@testweb:/tmp/aaa# ls
1.txt

root@testweb:/tmp/aaa# exit
exit

ありますね!


おまけ:hostPathに関する情報

root@rke2-1:~# kubectl explain pods.spec.volumes.hostPath
KIND:       Pod
VERSION:    v1

FIELD: hostPath <HostPathVolumeSource>

DESCRIPTION:
    hostPath represents a pre-existing file or directory on the host machine
    that is directly exposed to the container. This is generally used for system
    agents or other privileged things that are allowed to see the host machine.
    Most containers will NOT need this. More info:
    https://kubernetes.io/docs/concepts/storage/volumes#hostpath
    Represents a host path mapped into a pod. Host path volumes do not support
    ownership management or SELinux relabeling.
    
FIELDS:
  path  <string> -required-
    path of the directory on the host. If the path is a symlink, it will follow
    the link to the real path. More info:
    https://kubernetes.io/docs/concepts/storage/volumes#hostpath

  type  <string>
    type for HostPath Volume Defaults to "" More info:
    https://kubernetes.io/docs/concepts/storage/volumes#hostpath
    
    Possible enum values:
     - `""` For backwards compatible, leave it empty if unset
     - `"BlockDevice"` A block device must exist at the given path
     - `"CharDevice"` A character device must exist at the given path
     - `"Directory"` A directory must exist at the given path
     - `"DirectoryOrCreate"` If nothing exists at the given path, an empty
    directory will be created there as needed with file mode 0755, having the
    same group and ownership with Kubelet.
     - `"File"` A file must exist at the given path
     - `"FileOrCreate"` If nothing exists at the given path, an empty file will
    be created there as needed with file mode 0644, having the same group and
    ownership with Kubelet.
     - `"Socket"` A UNIX socket must exist at the given path


参照本家サイト
https://kubernetes.io/docs/concepts/storage/volumes/

Opensourcetech by Takahiro Kujirai