一叫挟、概述
nfs是共享網(wǎng)絡(luò)文件存儲服務(wù)器艰匙,可以通過類似于磁盤的方式,掛載到客戶端的目錄上抹恳。
其主要作用:
- 1.實現(xiàn)多臺服務(wù)器之間數(shù)據(jù)共享
- 2.實現(xiàn)多臺服務(wù)器之間數(shù)據(jù)一致
二员凝、安裝
- 安裝nfs
yum install -y nfs-utils
2.創(chuàng)建目錄
mkdir /data/volumes -pv
3.配置nfs服務(wù)目錄
vim /etc/exports
/data/volumes 192.168.1.0/24(rw,no_root_squash)
rw 讀寫權(quán)限
no_root_squash 當NFS客戶端以root管理員訪問時,映射為NFS服務(wù)器的root管理員
4.重啟并顯示
systemctl start nfs
showmount -e
Export list for storage:
/data/volumes 192.168.1.0/24
三奋献、在k8s node節(jié)點上安裝客戶端工具
- node節(jié)點安裝nfs-utils
yum install -y nfs-utils
2.掛載nfs存儲并展示
mount -t nfs storage:/data/volumes /mnt
mount
storage:/data/volumes on /mnt type nfs4 (rw,relatime,vers=4.1,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=192.168.1.13,local_lock=none,addr=192.168.1.14)
取消掛載:
umount /mnt/
四健霹、創(chuàng)建nfs存儲卷的使用清單
1.創(chuàng)建pod并掛載nfs存儲
vim pod-nfs-vol.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-nfs
namespace: default
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
nfs:
path: /data/volumes
server: storage
啟動pod
kubectl apply -f pod-nfs-vol.yaml
2.在nfs服務(wù)器上創(chuàng)建index.html
cd /data/volumes
vim index.html
<h1> nfs storage</h1>
curl 10.244.3.7
<h1> nfs storage</h1>
五、實戰(zhàn)1
1. 配置pv
apiVersion: v1
kind: PersistentVolume
metadata:
finalizers:
- kubernetes.io/pv-protection
labels:
name: nfs-learn
name: nas-learn
spec:
accessModes:
- ReadWriteMany
capacity:
storage: 1Gi
mountOptions:
- vers=4.0
- noresvport
nfs:
path: /data/volumes
server: storage
persistentVolumeReclaimPolicy: Retain
2.配置pvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
finalizers:
- kubernetes.io/pvc-protection
name: nasclaim-nas-learn
namespace: learn
spec:
accessModes:
- ReadWriteMany
dataSource: null
resources:
requests:
storage: 1Pi
selector:
matchLabels:
name: nfs-learn
volumeName: nas-learn
3.創(chuàng)建pod掛載pvc及pv
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: pv-learn
name: pv-learn
namespace: learn
spec:
replicas: 1
selector:
matchLabels:
run: pv-learn
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
labels:
run: pv-learn
spec:
containers:
- env:
image: registry.yunlearn.org:5000/release/go-dingding:test
imagePullPolicy: Always
name: pv-learn
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /mnt/
name: alinas
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- name: alinas
persistentVolumeClaim:
claimName: nasclaim-nas-learn
六瓶蚂、實戰(zhàn)二
1.配置nfs存儲
vim /etc/exports
mkdir v{1,2,3,4,5}
/data/volumes/v1 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v2 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v3 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v4 192.168.1.0/24(rw,no_root_squash)
/data/volumes/v5 192.168.1.0/24(rw,no_root_squash)
exportfs -arv
showmount -e
2.創(chuàng)建pv
vim pv-demo.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv001
labels:
name: pv001
spec:
nfs:
path: /data/volumes/v1
server: storage
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 1Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv002
labels:
name: pv002
spec:
nfs:
path: /data/volumes/v2
server: storage
accessModes: ["ReadWriteOnce"]
capacity:
storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv003
labels:
name: pv003
spec:
nfs:
path: /data/volumes/v3
server: storage
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv004
labels:
name: pv004
spec:
nfs:
path: /data/volumes/v4
server: storage
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 4Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv005
labels:
name: pv005
spec:
nfs:
path: /data/volumes/v5
server: storage
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 5Gi
3.創(chuàng)建pvc并掛到pod
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc
namespace: default
spec:
accessModes: ["ReadWriteMany"]
resources:
requests:
storage: 2Gi
---
apiVersion: v1
kind: Pod
metadata:
name: pod-vol-pvc
namespace: default
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
persistentVolumeClaim:
claimName: mypvc
創(chuàng)建pod
kubectl apply -f pod-vol-pvc.yaml
4.測試
進入到nfs目錄吧
cd v3/
echo "welcome to use pv3" > index.html
curl 10.244.2.39