数据卷 (Data Volumes )是一个可供容器使用的特殊目录,它将主机操作系统目录直接映射进容器,类似于 Linux 中的 mount 行为。

containerd创建的容器里的数据存储在下面的目录中
[root@master01 httpbin]#ls /run/containerd/io.containerd.runtime.v2.task/k8s.io/{podid}/rootfs/
所以我们可以这样思考一下
让我们先重温一下,k8s如何将volumes挂载到容器内部,下面是一段nginx,静态文件挂载到node上的yaml配置。
spec: #期望Pod实现的功能(即在pod中部署)containers: #生成container,与docker中的container是同一种- name: ssx-nginx-cimage: nginx:latest #使用镜像nginx: 创建container,该container默认80端口可访问ports:- containerPort: 80 # 开启本容器的80端口可访问volumeMounts: #挂载持久存储卷- name: volume #挂载设备的名字,与volumes[*].name 需要对应 mountPath: /usr/share/nginx/html #挂载到容器的某个路径下 volumes:- name: volume #和上面保持一致 这是本地的文件路径,上面是容器内部的路径hostPath:path: /opt/web/dist #此路径需要实现创建
对于存储来说,我们需要确定使用那些挂载类型可以满足当前容器的需求,所以我们需要对挂载类型进行一一列举分析。
下面只列举几个常用的类型,至于全部支持的类型请查看源码staging/src/k8s.io/api/core/v1/types.go:VolumeSource{}
直接挂载到node节点上的目录中
容器删除时,挂载的内容不会消失
type HostPathVolumeSource struct {// node路径Path string `json:"path" protobuf:"bytes,1,opt,name=path"`// 挂载类型Type *HostPathType `json:"type,omitempty" protobuf:"bytes,2,opt,name=type"`
}
// +enum
type HostPathType string
const (//对于向后兼容,如果未设置,则将其留空HostPathUnset HostPathType=“”//如果给定路径上不存在任何内容,将在那里创建一个空目录//如文件模式0755所需,具有与Kubelet相同的组和所有权。HostPathDirectoryOrCreate HostPathType=“DirectoryOr创建”//给定路径上必须存在目录HostPathDirectory HostPathType=“Directory”//如果给定路径上不存在任何内容,将在那里创建一个空文件//如文件模式0644所需,具有与Kubelet相同的组和所有权。HostPathFileOrCreate HostPathType=“FileOrCreate”//文件必须存在于给定路径HostPathFile HostPathType=“File”//给定路径上必须存在UNIX套接字HostPathSocket HostPathType=“Socket”//给定路径上必须存在字符设备HostPathCharDev HostPathType=“CharDevice”//给定路径上必须存在块设备HostPathBlockDev HostPathType=“BlockDevice”
)
使用此类卷时要小心,因为:
spec:volumes:- name: secret-volumesecret:secretName: test-db-secretcontainers:- name: db-client-containerimage: myClientImagevolumeMounts:- name: secret-volumereadOnly: truemountPath: "/etc/secret-volume"
secret是指定目录,而里面的key就是文件名,value是文件内容,configmap也一致。
spec:containers:- name: testimage: busybox:1.28volumeMounts:- name: config-volmountPath: /etc/configvolumes:- name: config-volconfigMap:name: log-configitems:- key: log_levelpath: log_level
apiVersion: v1
kind: Pod
metadata:name: cephfs
spec:containers:- name: cephfs-rwimage: kubernetes/pausevolumeMounts:- mountPath: "/mnt/cephfs"name: cephfsvolumes:- name: cephfscephfs:monitors:- 10.16.154.78:6789- 10.16.154.82:6789- 10.16.154.83:6789# by default the path is /, but you can override and mount a specific path of the filesystem by using the path attribute# path: /some/path/in/side/cephfsuser: adminsecretFile: "/etc/ceph/admin.secret"readOnly: true
apiVersion: v1
kind: Pod
metadata:name: test-pd
spec:containers:- image: registry.k8s.io/test-webservername: test-containervolumeMounts:- mountPath: /my-nfs-dataname: test-volumevolumes:- name: test-volumenfs:server: my-nfs-server.example.compath: /my-nfs-volumereadOnly: true
apiVersion: v1
kind: Pod
metadata:name: mypod
spec:containers:- name: myfrontendimage: nginxvolumeMounts:- mountPath: "/var/www/html"name: mypdvolumes:- name: mypdpersistentVolumeClaim:claimName: myclaimreadOnly: true
https://kubernetes.io/docs/concepts/storage/volumes/ (讲解volumes每个类型的用途)
https://github.com/kubernetes/examples/blob/master/volumes/cephfs/cephfs.yaml (ceph例子)
https://blog.csdn.net/hwruirui/article/details/119566635(pid命名空间讲解)
上一篇:描写华清池的优美句子
下一篇:形容酱干口味的词语