<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] 參考連接:[https://feisky.gitbooks.io/kubernetes/content/concepts/persistent-volume.html](https://feisky.gitbooks.io/kubernetes/content/concepts/persistent-volume.html) ## 緣由 在看statefluset[基礎教程]([https://kubernetes.io/zh/docs/tutorials/stateful-application/basic-stateful-set/](https://kubernetes.io/zh/docs/tutorials/stateful-application/basic-stateful-set/))的時候遇到pod狀態為pending ``` kubectl describe pod web-0 ``` 日志內容是 ``` pod has unbound immediate PersistentVolumeClaims ``` 于是就對PV和PVC進行研究。 具體內容查看[statefulset詳解](statefulset%E8%AF%A6%E8%A7%A3.md),有給出完整的 web.yaml。但是我建議先把PV和PVC的概念弄懂了,自然就知曉怎么處理pod的狀態為pending的問題了。 ## PV PersistentVolume(PV)是集群之中的一塊網絡存儲。跟 Node 一樣,也是集群的資源。PV 跟 Volume (卷) 類似,不過會有獨立于 Pod 的生命周期。比如一個 NFS 的 PV 可以定義為 ``` apiVersion: v1 kind: PersistentVolume metadata: name: "www-data-pv" labels: name: www-data-pv release: stable spec: capacity: storage: 5Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Recycle nfs: path: /nfs/www/data server: 192.168.84.75 ``` PV 的訪問模式(accessModes)有三種: * ReadWriteOnce(RWO):是最基本的方式,可讀可寫,但只支持被單個節點掛載。 * ReadOnlyMany(ROX):可以以只讀的方式被多個節點掛載。 * ReadWriteMany(RWX):這種存儲可以以讀寫的方式被多個節點共享。不是每一種存儲都支持這三種方式,像共享方式,目前支持的還比較少,比較常用的是 NFS。在 PVC 綁定 PV 時通常根據兩個條件來綁定,一個是存儲的大小,另一個就是訪問模式。 PV 的回收策略(persistentVolumeReclaimPolicy,即 PVC 釋放卷的時候 PV 該如何操作)也有三種 * Retain,不清理, 保留 Volume(需要手動清理) * Recycle,刪除數據,即`rm -rf /thevolume/*`(只有 NFS 和 HostPath 支持) * Delete,刪除存儲資源,比如刪除 AWS EBS 卷(只有 AWS EBS, GCE PD, Azure Disk 和 Cinder 支持) ## PVC PV 是存儲資源,而 PersistentVolumeClaim (PVC) 是對 PV 的請求。PVC 跟 Pod 類似:Pod 消費 Node 資源,而 PVC 消費 PV 資源;Pod 能夠請求 CPU 和內存資源,而 PVC 請求特定大小和訪問模式的數據卷。 ``` apiVersion: v1 kind: PersistentVolumeClaim metadata: name: www-data-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 5Gi selector: matchLabels: name: www-data-pv release: stable ``` PVC 可以直接掛載到 Pod/StatefulSet 中: ``` apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: serviceName: "nginx" replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80 name: web volumeMounts: - name: www mountPath: /usr/share/nginx/html volumes: - name: www persistentVolumeClaim: claimName: www-data-pvc ``` ## 完整示例 ``` apiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx spec: ports: - port: 80 name: web clusterIP: None selector: app: nginx --- apiVersion: v1 kind: PersistentVolume metadata: name: "www-data-pv" labels: name: www-data-pv release: stable spec: capacity: storage: 5Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Recycle nfs: path: /nfs/www/data server: 192.168.84.75 --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: www-data-pvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 5Gi selector: matchLabels: name: www-data-pv release: stable --- apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: serviceName: "nginx" replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80 name: web volumeMounts: - name: www mountPath: /usr/share/nginx/html volumes: - name: www persistentVolumeClaim: claimName: www-data-pvc ``` nfs是需要另外安裝的 ``` sudo apt install nfs-kernel-server ``` 需要手動創建一個NFS Volume,具體細節看這里:[nfs的安裝和使用](nfs%E7%9A%84%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8.md) ## StorageClass 上面通過手動的方式創建了一個 NFS Volume,這在管理很多 Volume 的時候不太方便。Kubernetes 還提供了[StorageClass](https://kubernetes.io/docs/user-guide/persistent-volumes/#storageclasses)來動態創建 PV,不僅節省了管理員的時間,還可以封裝不同類型的存儲供 PVC 選用。 StorageClass 包括四個部分 * provisioner:指定 Volume 插件的類型,包括內置插件(如`kubernetes.io/glusterfs`)和外部插件(如[external-storage](https://github.com/kubernetes-incubator/external-storage/tree/master/ceph/cephfs)提供的`ceph.com/cephfs`)。 * mountOptions:指定掛載選項,當 PV 不支持指定的選項時會直接失敗。比如 NFS 支持`hard`和`nfsvers=4.1`等選項。 * parameters:指定 provisioner 的選項,比如`kubernetes.io/aws-ebs`支持`type`、`zone`、`iopsPerGB`等參數。 * reclaimPolicy:指定回收策略,同 PV 的回收策略。 在使用 PVC 時,可以通過`DefaultStorageClass`準入控制設置默認 StorageClass, 即給未設置 storageClassName 的 PVC 自動添加默認的 StorageClass。而默認的 StorageClass 帶有 annotation`storageclass.kubernetes.io/is-default-class=true`。 ## 技巧:Kuberntes 中無法刪除 PV 的解決方法 系統內有一個已經不再使用的 PV ,已經刪除了與其關聯的 Pod 及 PVC ,并對其執行了刪除命令,但是無法正常刪除,一直出于如下狀態: ``` $ kubectl get pv NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE pv-nfs-gysl 1Gi RWO Recycle Terminating default/www-vct-statefulset-pvc-gysl-0 managed-nfs-storage 22h ``` 解決方法 ``` $ kubectl patch pv pv-nfs-gysl -p '{"metadata":{"finalizers":null}}' persistentvolume/pv-nfs-gysl patched $ kubectl get pv No resources found. ``` 通過系統幫助信息,我們可以獲取patch的簡要使用說明: ``` patch: 使用 strategic merge patch 更新一個資源的 field(s) ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看