美文网首页
K8s -- 持久卷

K8s -- 持久卷

作者: 沉沦2014 | 来源:发表于2018-11-17 15:55 被阅读65次
    1. emptyDIR 临时目录

    2. hostPath :使用主机的路径

    3. 网络存储:

    • 传统的设备存储:NAS,SAN
    • 分布式存储:glusterfs,rbd,cephfs
    • 云存储:EBS,Azure,阿里云的

    一、emptyDir 的类型:一个pod里面2个容器,挂载同一个目录,

    注意:emptyDir的生命周期同pod周期,简单来说,pod删除了,emptyDir也随之删除

    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-demo
      namespace: default
      labels:
        app: myapp
        tier: frontend
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - name: http
          containerPort: 80
        volumeMounts:  #容器挂载
        - name: html
          mountPath: /data/web/html/
      - name: busybox
        image: busybox:latest
        command: ["/bin/sh" ,"-c","sleep 3600"]
        volumeMounts:   #容器挂载
        - name: html
          mountPath: /data/
      volumes:     #定义存储
      - name: html
        emptyDir: {}
    

    二、hostPath 类型存储

    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-volume
      namespace: default
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html/
      volumes:
      - name: html
        hostPath:
          path: /data/nginx/v1/
          type: DirectoryOrCreate
    

    三、nfs 类型存储

    首先要准备好nfs,我在172.17.1.36的机器上安装nfs,到处的目录为/nfs/data

    yum install nfs-utils

    vim vim /etc/exports

    /nfs/data/ 172.17.0.0/16(rw,no_root_squash)

    systemctl start nfs

    nfs准备好了,注意,在pod的节点机器要一定确定可以挂载nfs类型的,否则创建pod会出错。执行yum install nfs-utils 就可以挂载了

    nfs pod 的yaml文件:

    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-volume-nfs
      namespace: default
    spec:
      containers:
      - name: nginx-nfs
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html/
      volumes:
      - name: html
        nfs:
          path: /nfs/data/
          server: 172.17.1.36
    

    四、pv,pvc的使用

    对于pv和pvc,首先要准备存储的,我用的是nfs,在172.17.1.36的节点上划分了5个路径作为存储的设备。

    mkdir /data/{v1,v2,v3,v4,v5} -pv

    vim /etc/exports

    添加如下的内容:

    /data/v1 172.17.0.0/16(rw,no_root_squash)
    /data/v2 172.17.0.0/16(rw,no_root_squash)
    /data/v3 172.17.0.0/16(rw,no_root_squash)
    /data/v4 172.17.0.0/16(rw,no_root_squash)
    /data/v5 172.17.0.0/16(rw,no_root_squash)

    保存,导出下。

    exportfs -avr

    image

    查看下是否正常。

    nfs的存储的好了。下一步要先创建pv,yaml文件如下

    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv01
      labels:
        name: pv01
    spec:
      nfs:
        path: /data/v1/
        server: 172.17.1.36
      accessModes: ["ReadWriteOnce","ReadWriteMany"]
      capacity:
        storage: 5Gi
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv02
      labels:
        name: pv02
    spec:
      nfs:
        path: /data/v2/
        server: 172.17.1.36
      accessModes: ["ReadWriteOnce","ReadWriteMany"]
      capacity:
        storage: 3Gi
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv03
      labels:
        name: pv03
    spec:
      nfs:
        path: /data/v3/
        server: 172.17.1.36
      accessModes: ["ReadWriteOnce","ReadWriteMany"]
      capacity:
        storage: 5Gi
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv04
      labels:
        name: pv04
    spec:
      nfs:
        path: /data/v4/
        server: 172.17.1.36
      accessModes: ["ReadWriteOnce","ReadWriteMany"]
      capacity:
        storage: 10Gi
    ---
    apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: pv05
      labels:
        name: pv05
    spec:
      nfs:
        path: /data/v5/
        server: 172.17.1.36
      accessModes: ["ReadWriteOnce","ReadWriteMany"]
      capacity:
        storage: 10Gi
    

    kubectl apply -f pv-nfs.yaml

    image

    显示,pv创建完成

    接下来创建pvc 和pod,yaml文件如下

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mypvc
      namespace: default
    spec:
      accessModes: ["ReadWriteMany"]
      resources:
        requests:
          storage: 5Gi               #到这里是创建pvc的
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: nginx-volume-pvc
      namespace: default
    spec:
      containers:
      - name: nginx-pvc
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html/
      volumes:               #这里是选择volume的类型
      - name: html
        persistentVolumeClaim:
          claimName: mypvc
    
    image

    显示pvc创建好了,同时pvc绑定了一个pv,到此pv和pvc的安装部署完成。

    ps:pv和pvc是一对一绑定的。但是多个pod可以挂载同一个pvc

    通常使用的流程是,首先创建存储,在创建pv,接着创建pvc,pod挂载到相应的pvc。

    相关文章

      网友评论

          本文标题:K8s -- 持久卷

          本文链接:https://www.haomeiwen.com/subject/igfifqtx.html