k8s挂载nfs,实现持久化存储
配置nfs共享目录:yum install nfs-common nfs-utils -y# cat /etc/exports/data/sharedir10.0.0.0/8(rw,sync,no_root_squash)systemctl restart rpcbindsystemctl restart nfscat nfs-pv-pvc.yaml---apiVersion: v1kind: P
配置nfs共享目录:
yum install nfs-common nfs-utils -y
# cat /etc/exports
/data/sharedir 10.0.0.0/8(rw,sync,no_root_squash)
systemctl restart rpcbind
systemctl restart nfs
第一种方式:通过做pv,pvc进行挂载
cat nfs-pv-pvc.yaml
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-malico # pv 名字
namespace: sharenfs
labels:
app: nfs-malico # 定义 labels ap: nfs-malico
spec:
capacity:
storage: 20Gi # 定义容量
accessModes:
- ReadWriteMany # 访问模式RWX
persistentVolumeReclaimPolicy: Retain # 回收策略 保留数据,需要管理员手工清理数据
storageClassName: nfs # 定义 storageClassName 只有相同名字的才能绑定在一起
nfs:
path: /data/sharedir/ngpvc
server: 10.4.11.11
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nfs-malico # pvc 名字
namespace: sharenfs
spec:
storageClassName: nfs # Name of Storage Class Name, use same class name defined in pv
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Gi # amout of Storage request
selector:
matchLabels:
app: nfs-malico # 指定 pv 的标签 nfs-malico
cat nginx-pvc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-pvc
namespace: sharenfs
spec:
replicas: 1
selector:
matchLabels:
app: nginx-pvc
template:
metadata:
labels:
app: nginx-pvc
spec:
containers:
- name: nginx
image: nginx:1.20
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: web
volumeMounts:
- name: nfs-malico
mountPath: /usr/share/nginx/html
volumes:
- name: nfs-malico
persistentVolumeClaim:
claimName: nfs-malico
验证:
共享主机:echo AAAA > /data/sharedir/test.log
nginx-pvc容器:ls -l /usr/share/nginx/html && cat /usr/share/nginx/html/test.log
第二种方式:直接挂载nfs,不做pv,pvc
cat nginx-nfs.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-nfs
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: nginx-nfs
template:
metadata:
labels:
app: nginx-nfs
spec:
containers:
- name: nginx
image: nginx:1.20
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: web
volumeMounts:
- name: nfs-malico
mountPath: /usr/share/nginx/html
volumes:
- name: nfs-malico
nfs:
server: 10.4.11.11 #nfs服务主机IP或主机名
path: /data/sharedir/ngnfs #挂载的目录
readOnly: false
更多推荐
所有评论(0)