准备

  • 所有k8s节点需要安装nfs服务
  • 一台nfs服务器提供存储
  • 根据需求创建数据挂载目录

例如我的:
在这里插入图片描述

创建PV

// vi pv-nfs.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name:  web-pv
  labels:
    app: web-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  nfs:
    path: /k8s-data/web
    server: 192.168.50.190
kubectl apply -f pv-nfs.yaml
  • 查看pv
kubectl get pv

在这里插入图片描述

创建PVC

// vi pvc-nfs.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: web-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
  selector:
    matchLabels:
      app: web-pv
kubectl apply -f pvc-nfs.yaml
  • 查看PVC
kubectl get pvc

在这里插入图片描述

使用PVC挂载

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 2
  template:
    metadata:
      labels:
        app: web
    spec:
      terminationGracePeriodSeconds: 3
      containers:
      - name: web
        image: nginx
        imagePullPolicy: Never
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: index
      volumes:
      - name: index
        persistentVolumeClaim:
          claimName: web-pvc
  • 创建service
apiVersion: v1
kind: Service
metadata:
  name: web-svc
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    nodePort: 30991
  selector:
    app: web

把主页文件放到之前准本的nfs目录下,访问ip+30991即可

Logo

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

更多推荐