美文网首页K8skubernetes
一文学会安装和在k8s中使用nfs

一文学会安装和在k8s中使用nfs

作者: sknfie | 来源:发表于2022-06-16 21:37 被阅读0次

一、概述

nfs是共享网络文件存储服务器,可以通过类似于磁盘的方式,挂载到客户端的目录上。
其主要作用:

  • 1.实现多台服务器之间数据共享
  • 2.实现多台服务器之间数据一致

二、安装

  1. 安装nfs
yum install -y nfs-utils

2.创建目录

mkdir /data/volumes -pv

3.配置nfs服务目录

vim /etc/exports
/data/volumes 192.168.1.0/24(rw,no_root_squash)

rw 读写权限
no_root_squash 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员

4.重启并显示

systemctl start nfs

showmount -e
Export list for storage:
/data/volumes 192.168.1.0/24

三、在k8s node节点上安装客户端工具

  1. node节点安装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/

四、创建nfs存储卷的使用清单

1.创建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服务器上创建index.html

cd /data/volumes

vim index.html
<h1> nfs storage</h1>

curl 10.244.3.7
<h1> nfs storage</h1>

五、实战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.创建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

六、实战二

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.创建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.创建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

创建pod

kubectl apply -f pod-vol-pvc.yaml

4.测试

进入到nfs目录吧

cd v3/  
echo "welcome to use pv3" > index.html

curl  10.244.2.39

相关文章

网友评论

    本文标题:一文学会安装和在k8s中使用nfs

    本文链接:https://www.haomeiwen.com/subject/rgphvrtx.html