0%

kubernetes NFS实践

NFS实践

安装NFS

1
2
3
4
5
6
7
8
9
# 下载nfs相关软件(全成员)
yum install -y nfs-common nfs-utils rpcbind
# 创建NFS共享文件夹,以及授权(需要root)
mkdir /nfs && chmod 766 /nfs && chown nfsnobody /nfs/
# 声明共享文件权限(NFS主服务器)
echo "/nfs *(rw,no_root_squash,no_all_squash,sync)" >> /etc/exports
#
exportfs -r
systemctl restart rpcbind && systemctl restart nfs && systemctl status rpcbind && systemctl status nfs

验证

IP: 主机地址

1
2
3
4
5
6
7
8
9
# 查看共享目录
showmount -e IP
mkdir /test
# 将本机目录(test)挂载至目标目录(nfs)
mount -t nfs IP:/nfs /test
cd /test
echo "asdsadsa" >> a.txt
cd /
umount /test && rm -rf /test

部署PV

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfspv-master
spec:
capacity:
storage: 10Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs
nfs:
path: /nfs
server: 192.168.0.27 # 节点ip

PVC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- port: 80
name: web
clusterIP: None

---

apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: wangyanglinux/myapp:v2
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "nfs"
resources:
requests:
storage: 1Gi

验证

1
2
kubectl get pv
kubectl desc

image-20210903233505574

1
2
3
# 192.168.0.27 /nfs
echo "asds" > index.html
kubectl get pod -o wid

image-20210903234426686

1
2
curl 10.244.1.53
# asds