Kubernetes本地共享,网络共享,pv,pvc卷挂载示例

版本:v1.18.0

本地共享类型:emptyDir,hostpath

网络共享类型:nfs,pv、pvc卷

其他:ConfigMap,Secret

一、emptyDir示例:

apiVersion: v1
kind: Pod
metadata:
  name: testemp
  labels:
    app: testemp
spec:
  restartPolicy: Always
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: myweb
      mountPath: /usr/share/nginx/html
    resources:
      requests:
        cpu: 200m
        memory: 64Mi
      limits:
        cpu: 300m
        memory: 128Mi
  volumes:
  - name: myweb
    emptyDir: {}

二、hostPath示例:

apiVersion: v1
kind: Pod
metadata:
  name: testhost
  labels:
    app: testhost
spec:
  restartPolicy: Always
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: myweb
      mountPath: /usr/share/nginx/html
    resources:
      requests:
        cpu: 200m
        memory: 64Mi
      limits:
        cpu: 300m
        memory: 128Mi
  volumes:
  - name: myweb
    hostPath:
      path: /tmp/myweb
      type: Directory

三、nfs示例:

apiVersion: v1
kind: Pod
metadata:
  name: testnfs
  labels:
    app: testnfs
spec:
  restartPolicy: Always
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: myweb
      mountPath: /usr/share/nginx/html
    resources:
      requests:
        cpu: 200m
        memory: 64Mi
      limits:
        cpu: 300m
        memory: 128Mi
  volumes:
  - name: myweb
    nfs:
      server: 192.168.199.120
      path: /data

四、利用网络共享存储创建pv卷,手动创建pvc卷应用于挂载

pv卷创建

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  nfs:
    path: /mypv/pv0001
    server: 192.168.199.120
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0002
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  nfs:
    path: /mypv/pv0002
    server: 192.168.199.120
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0003
spec:
  capacity:
    storage: 8Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  nfs:
    path: /mypv/pv0003
    server: 192.168.199.120

pvc卷创建

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: fornginx
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: 4Gi
  storageClassName: slow

pod应用pvc卷

apiVersion: v1
kind: Pod
metadata:
  name: testpvc
  labels:
    app: testpvc
spec:
  restartPolicy: Always
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: myweb
      mountPath: /usr/share/nginx/html
    resources:
      requests:
        cpu: 200m
        memory: 64Mi
      limits:
        cpu: 300m
        memory: 128Mi
  volumes:
  - name: myweb
    persistentVolumeClaim:
        claimName: fornginx

五、安装nfs-client插件,实现创建pv后,自动创建pvc。

github上下载   nfs-client  插件,里面饱含了三个文件,一个是rbac,一个是deploy,一个是class,分别是授权,部署nfs-client的pod和定义class的。

直接创建pvc

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: autopvc
spec:
  accessModes:
    - ReadWriteOnce
  volumeMode: Filesystem
  resources:
    requests:
      storage: 8Gi
  storageClassName: "managed-nfs-storage"

直接创建应用pod,使用该pvc

apiVersion: v1
kind: Pod
metadata:
  name: testautopvc
  labels:
    app: testautopvc
spec:
  restartPolicy: Always
  containers:
  - name: nginx
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: myweb
      mountPath: /usr/share/nginx/html
    resources:
      requests:
        cpu: 200m
        memory: 64Mi
      limits:
        cpu: 300m
        memory: 128Mi
  volumes:
  - name: myweb
    persistentVolumeClaim:
        claimName: autopvc

 

上一篇:Kubernetes实践之深入掌握Pod——在容器内获取Pod信息


下一篇:k8s无脑系列(八)- 部署Jenkins