1、存储卷介绍
存储卷的分类
# kubectl explain pod.spec.volumes
存储卷的选择
-
文件存储,如nfs、glusterfs、cephfs等,数据共享,但是性能较差
-
块存储,如iscsi、rbd等,性能较好,不能实现数据共享(部分)
-
对象存储,如ceph对象存储,性能好,数据共享,但是使用方式特殊,支持较少。
2、本地存储卷应用
2.1 emptyDir
实现pod内容器之间数据共享,随着pod被删除,该卷也会被删除
# cat volume-emptydir.yml
apiVersion: v1
kind: Pod
metadata:
name: volume-emptydir
spec:
containers:
- name: write
image: centos:centos7
imagePullPolicy: IfNotPresent
command: ["bash", "-c", "echo haha > /data/1.txt;sleep 6000"]
volumeMounts:
- name: data
mountPath: /data
- name: read
image: centos:centos7
imagePullPolicy: IfNotPresent
command: ["bash", "-c", "cat /data/1.txt; sleep 6000"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
emptyDir: {}
# kubectl apply -f volume-emptydir.yml
# kubectl logs -f volume-emptydir write
# kubectl exec -it volume-emptydir -c write -- /bin/bash
2.2 hostPath
Pod内与集群节点主机目录映射
# cat volume-hostpath.yml
apiVersion: v1
kind: Pod
metadata:
name: volume-hostpath
spec:
containers:
- name: busybox
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c", "echo hahahah > /data/1.txt;sleep 6000"]
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
hostPath:
path: /opt
type: Directory
# kubectl apply -f volume-hostpath.yml
2.3 nfs
# cat volume-nfs.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: volume-nfs
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15-alpine
imagePullPolicy: IfNotPresent
volumeMounts:
- name: documentroot
mountPath: /usr/share/nginx/html
ports:
- containerPort: 80
volumes:
- name: documentroot
nfs:
server: 192.168.16.133
path: /data/nfs
标签:存储,持久,name,volume,nfs,k8s,data,spec
From: https://www.cnblogs.com/zbc230/p/17879606.html