k8s中的存储方案-emptydir/hostPath/nfs/pv/pvc

本地存储

1、emptydir

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: write
    image: centos
    command: ["sh","-c","for i in {1..100};do echo $i >> /data/hello;sleep 1;done"]
    volumeMounts:
      - name: data
        mountPath: /data

  - name: read
    image: centos
    command: ["sh","-c","tail -f /data/hello"]
    volumeMounts:
      - name: data
        mountPath: /data
  volumes:
  - name: data
    emptyDir: {}

注意command,bash、sh的用法。
一个pod中有两个contrainer,一个读一个写,实现两个容器之间的数据共享。
在这里插入图片描述

kubectl logs my-pod -c write
kubectl logs my-pod -c read

hostPath

挂载node文件系统上的文件或者目录到pod中的容器
假如pod是一个监控,需要采集宿主机上的应用,就需要将宿主机上的文件或目录挂载到pod中的容器中
应用场景:Pod中容器需要访问宿主机文件

apiVersion: v1
kind: Pod
metadata:
  name: my-pod2
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 36000
    volumeMounts:
    - name: data
      mountPath: /data
  volumes:
  - name: data
    hostPath:
      path: /tmp
      type: Directory

k8s中的存储方案-emptydir/hostPath/nfs/pv/pvc_第1张图片
查看挂载的node节点
在这里插入图片描述
在node节点10.0.3.248上查看挂载点与pod中的文件
node248节点:
在这里插入图片描述
pod内部:
k8s中的存储方案-emptydir/hostPath/nfs/pv/pvc_第2张图片

网络存储

在非集群服务器中,部署nfs
如在104上部署

yum install -y nfs-utils
# 暴露启动挂载的路径
vi /etc/exports
# ------------
/data/nfs *(rw,no_root_squash)
# ------------
systemctl start nfs
ps -ef |grep nfs

k8s中的存储方案-emptydir/hostPath/nfs/pv/pvc_第3张图片
并在每个客户端安装nfs-utils

yum install -y nfs-utils

在master运行nfs.yaml文件:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: wwwroot
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80
      volumes:
      - name: wwwroot
        nfs:
          server: 10.0.3.104
          path: /data/nfs

note:数据卷挂载:volumes挂载nfs服务器端,
PV 对存储资源创建和使用的抽象,使得存储作为集群中的资源管理。
PVC 消耗资源。用户不需要关心具体的volume实现细节。

静态存储

设置一个pv存储资源,以便应用进行绑定。

# vi pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: /data/nfs/wwwroot
    server: 10.0.3.104

展示在网页中存储的使用

动态供给(重要)

动态供给Dynamic Provisioning机制工作的核心在于StorageClass的API对象。
StorageClass声明存储插件,用于自动创建pv

# 镜像拉取需要进行认证
kubectl apply -f registry-pull-secret1.yaml
kubectl apply -f rbac.yaml
kubectl apply -f storageclass-nfs.yaml
kubectl apply -f pvc.yaml

在这里插入图片描述
在这里插入图片描述

# vi storageclass-nfs.yaml
apiVersion: storage.k8s.io/v1beta1
kind: StorageClass
metadata:
  name: managed-nfs-storage
provisioner: fuseim.pri/ifs
# vi deployment-nfs.yaml 静态pv
apiVersion: apps/v1beta1 
kind: Deployment
metadata:
  name: nfs-client-provisioner
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      imagePullSecrets:
        - name: registry-pull-secret
      serviceAccount: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: test.com/project/nfs-client-provisioner:v2.0.0
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: fuseim.pri/ifs
            - name: NFS_SERVER
              value: 10.0.3.104
            - name: NFS_PATH
              value: /data/nfs
      volumes:
        - name: nfs-client-root
          nfs:
            server: 10.0.3.104 
            path: /data/nfs
# vi pvc.yaml 动态pvc
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment2
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: wwwroot
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80
      volumes:
      - name: wwwroot
        persistentVolumeClaim:
          claimName: my-pvc2

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc2
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: managed-nfs-storage
  resources:
    requests:
      storage: 5Gi

你可能感兴趣的:(k8s应用)