pvc作为pod的volume

1.基本用法

kind: Pod
apiVersion: v1
metadata:
  name: mypod
spec:
  containers:
    - name: myfrontend
      image: dockerfile/nginx
      volumeMounts:
      - mountPath: "/var/www/html"
        name: mypd
  volumes:
    - name: mypd
      persistentVolumeClaim:    # 使用PVC
        claimName: myclaim

那么需要创建一个pvc

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: myclaim
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: 8Gi
  storageClassName: slow
  selector:
    matchLabels:
      release: "stable"
    matchExpressions:
      - {key: environment, operator: In, values: [dev]}

2.注意区分persistentVolumeClaim和statefulset中的 volumeClaimTemplates

volumeClaimTempaltes:存储卷申请模板,创建 pvc并指定 pvc 名称大小,自动创建 pvc,且 pvc 由存储类供应用法如下

apiVersion: v1
kind: Service
metadata:
  name: pvc-svc
  namespace: default
  labels:
    app: nginx
spec:
  clusterIP: None
  ports:
  - name: web
    port: 80
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  serviceName: "pvc-svc"
  template:
    metadata:
      labels:
        app: nginx
    spec:
      #dnsPolicy: ClusterFirstWithHostNet #使pod使用k8s的dns而不是宿主机dns
      containers:
      - name: nginx
        image: nginx:stable
        imagePullPolicy: IfNotPresent
        ports:
        - name: web
          containerPort: 80
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
      annotations:
        volume.beta.kubernetes.io/storage-class: "nfs"   # nfs为我们创建的storage-class名称
    spec:
      accessModes: [ "ReadWriteMany" ]    #指定pv的读写策略
     # storageClassName: "nfs"    #指定pv的存储类型
      resources:
        requests:
          storage: 1Gi    #指定pv的存储大小
posted @ 2023-08-24 09:38  坚强的小蚂蚁  阅读(25)  评论(0编辑  收藏  举报