Kubernetes(七) - Volume

Kubernetes(七) - Volume

Docker是無狀態的無論被銷燬多少次都會恢復到最初的狀態,可是這就意味着在程序過程當中產生的配置也好文件也好會丟失,對於Docker咱們常常會使用磁盤掛載的方式來保存一些重要的內容,好比運行在Docker下的數據庫的源數據,好比程序的日誌文件等,在K8S中也提供一樣的配置方式php

PS: 磁盤使用中1.8 和 1.9存在差別,1.8須要建立PersistentVolume在建立以後才能建立PersistentVolumeClaim,1.9以後只須要建立PersistentVolumeClaim就能夠了mysql

Kubernetes官方文檔:https://kubernetes.io/docs/reference/git

Kubernetes官方Git地址:https://github.com/kubernetes/kubernetesgithub

PS:本系列中使用 KubernetesV1.8 RancherV1.6.14redis

1.本地磁盤

> vim local-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv-1
  labels:
    type: local
spec:
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /tmp/data/pv-1
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-pv-claim
  labels:
    app: redis
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

> kubectl create -f local-pv.yaml
persistentvolume "local-pv-1" created
persistentvolumeclaim "mysql-pv-claim" created

而後咱們就能夠對對進行進行掛載了sql

> vim volume-local.yaml
apiVersion: v1
kind: Pod
metadata:
  name: volume-local-pod
spec:
  containers:
  - name: mypod
    image: redis
    volumeMounts:                         # 磁盤掛載
      - name: redis-pv-claim
        mountPath: "/etc/redis"
  volumes:                                  # 磁盤掛載別稱定義
  - name: redis-pv-claim
    persistentVolumeClaim:
      claimName: redis-pv-claim
> kubectl create -f volume-local.yaml
pod "volume-local-pod" created

這個時候容器的節點在K8S-S1上咱們看一下是否保存到了K8S-S1的磁盤上了嗎數據庫

2.NAS網絡盤

可是這樣作有一個很大的弊端,若是這個Pod重啓可能會被調度到其餘的節點上,那麼對應掛載盤的就會狀況,這裏有兩種方式解決,第一種就是固定Pod運行的節點,在就是使用共享磁盤(首先你須要建立一個NAS盤)vim

通常用的比較頻繁的就是NAS盤做爲掛載盤,用法以下api

> vim nfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: xxxxxx.cn-hangzhou.nas.aliyuncs.com   # nfs的地址
    path: "/"                                     # nfs的掛載目錄(必定須要有這個文件目錄)
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pv
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: ""
  resources:
    requests:
      storage: 5Gi

> kubectl create -f nfs-pv.yaml
persistentvolume "nfs-pv" created
persistentvolumeclaim "nfs-pv" created

咱們建立兩個Pod共享一個NAS盤網絡

> vim volume-nfs.yaml
apiVersion: extensions/v1beta1 
kind: Deployment
metadata:
  name: volume-nfs
spec:
  replicas: 2  
  template:
    metadata:
      labels:                                   # 容器的標籤 可和service關聯
        app: volume-nfs
    spec:
      containers:
      - name: mypod
        image: redis
        volumeMounts:                         # 磁盤掛載
          - name: nfs-pv
            mountPath: "/etc/redis"
      volumes:                                  # 磁盤掛載別稱定義
      - name: nfs-pv
        persistentVolumeClaim:
          claimName: php-general-test
> kubectl create -f volume-nfs.yaml
deployment "volume-nfs" created

兩個Pod分別在不一樣的節點中

3. 其餘Volume支持類型

具體使用明細能夠參考官方文檔: Volumes | Kubernetes

  • awsElasticBlockStore
  • azureDisk
  • azureFile
  • cephfs
  • downwardAPI
  • emptyDir
  • fc (光纖通道)
  • flocker
  • gcePersistentDisk
  • gitRepo
  • glusterfs
  • hostPath
  • iscsi
  • local
  • nfs
  • persistentVolumeClaim
  • projected
  • portworxVolume
  • quobyte
  • rbd
  • scaleIO
  • secret
  • storageos
  • vsphereVolume
相關文章
相關標籤/搜索