前言

k8s中镜像内部数据与外部需要建立管理,对于k8s集群,每个pod节点需要共享文件目录,因此,nfs文件系统主要用于文件同步,通过nfs实现数据的同步。
PV持久卷(Persistent Volume),将应用需要持久化的数据保存到指定位置,主要用于创建pv池供pvc绑定。
PVC:持久卷申明(Persistent Volume Claim),申明需要使用的持久卷规格。


一、搭建nfs

1、安装

#所有机器安装
yum install -y nfs-utils

配置主节点

#nfs主节点
echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports
mkdir -p /nfs/data
systemctl enable rpcbind --now
systemctl enable nfs-server --now
#配置生效
exportfs -r

配置从节点

showmount -e 14.10.10.101
#执行以下命令挂载 nfs 服务器上的共享目录到本机路径 /root/nfsmount
mkdir -p /nfs/data
mount -t nfs 14.10.10.101:/nfs/data /nfs/data

2、原生数据挂载案例

将nginx镜像内部的/usr/share/nginx/html挂载到nfs文件系统的/nfs/data/nginx-pv目录,本案例创建2个nginx的pod,两个pod挂载到共享的html目录

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-pv-demo
  name: nginx-pv-demo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx-pv-demo
  template:
    metadata:
      labels:
        app: nginx-pv-demo
    spec:
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html
      volumes:
        - name: html
          nfs:
            server: 14.10.10.101
            path: /nfs/data/nginx-pv

二、PV与PVC

1、创建PV池

创建目录

#nfs主节点
mkdir -p /nfs/data/01
mkdir -p /nfs/data/02
mkdir -p /nfs/data/03

创建pv

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv01-10m
spec:
  capacity:
    storage: 10M
  accessModes:
    - ReadWriteMany
  storageClassName: nfs
  nfs:
    path: /nfs/data/01
    server: 14.10.10.101
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv02-1gi
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  storageClassName: nfs
  nfs:
    path: /nfs/data/02
    server: 14.10.10.101
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv03-3gi
spec:
  capacity:
    storage: 3Gi
  accessModes:
    - ReadWriteMany
  storageClassName: nfs
  nfs:
    path: /nfs/data/03
    server: 14.10.10.101

2、PVC与PV绑定

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nginx-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 200Mi
  storageClassName: nfs

3、Pod与PVC绑定

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-deploy-pvc
  name: nginx-deploy-pvc
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx-deploy-pvc
  template:
    metadata:
      labels:
        app: nginx-deploy-pvc
    spec:
      containers:
      - image: nginx
        name: nginx
        volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html
      volumes:
        - name: html
          persistentVolumeClaim:
            claimName: nginx-pvc

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐