Kubernets存储卷

存储卷在POD之外。

常用存储卷

  • EmptyDir 临时存储,POD删除,也会被删除。基于内存创建的,速度很快

  • gitRepo  自动到git仓库拉取项目后挂载为EmptyDir,供POD使用,POD删除时跟着删除

  • HostPath 宿主机路径存储卷,POD删除时不会被删除。

  • 分布式存储  glusterfs,rdb,cephfs

  • 云存储  EBS,Azure Disk

查看Kubernets支持的存储

kubectl explain pods.spec.volumes

持久存储卷申请 persistentVolumeClaim (PVC)

  • PV 关联到后端存储服务。PV存储了配置好的存储资源,如NFS,Glusterfs,iSCSI都配置在上边。

  • PVC  需要向PV建立联系,向PV申请容量资源,如写POD配置清单时,Volumes 处写需要10G空间容量,PVC会与PV申请10G的资源,POD从而绑定这个PV。

PV存储类级别分类

  • Bronze Storage Class   慢

  • Silver Storage Class   中

  • Gold Storage Class   快

POD中的容器要使用EmptyDir存储卷的步骤

  1. 定义Volume

  2. 容器中挂载(kubectl explain pods.spec.containers.volumeMounts

例1:

apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
      magedu.com/created-by: "cluster admin"
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    imagePullPolicy: IfNotPresent
    ports:
    - name: http
      containerPort: 80
    - name: https
      containerPort: 443
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html/
  - name: busybox
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: html
      mountPath: /data/
    command:
    - "/bin/sh"
    args: ["-c","while true; do echo $(date) >> /data/index.html; sleep 2; done"]                                                                                                                                 
  volumes:
  - name: html
    emptyDir: {}

POD中的容器要使用HostPath存储卷的步骤

  1. 定义Volume

  2. 容器中挂载(kubectl explain pods.spec.volumes.hostPath

  3. 节点宿主机上 创建目录 (mkdir -p /data/pod/volume2

  4. 节点宿主机上 创建文件(echo $(date) >/data/pod/volume2/index.html)

例2:

apiVersion: v1
kind: Pod
metadata:
  name: pod-vol-hostpath
  namespace: default
  labels:
    app: myapp
spec:
  containers:
  - name: myapp 
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    - name: https
      containerPort: 443
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html/                                                                                                                                               
  volumes:
  - name: html
    hostPath:
      path: /data/pod/volume2
      type: DirectoryOrCreate

POD中的容器要使用NFS存储卷的步骤

  1. 定义Volume

  2. 确保节点能挂载NFS (mount

  3. 容器中挂载(kubectl explain pods.spec.volumes.nfs

搭建NFS存储:

1.安装

yum -y install nfs-utils

2.创建共享目录

 mkdir -pv /data/volumes

3.配置NFS

vim /etc/exports

/data/volumes  172.20.0.0/16(rw,no_root_squash)

systmectl start nfs

ss -lnl   :2039

4.节点上挂载

yum install nfs-utils -y
mount -t nfs 192.168.1.10:/data/volumes /mnt

例3:

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/pod/volume2/
      server: 192.168.1.10                                                                                                                                                            
      readOnly: false