3C科技 娛樂遊戲 美食旅遊 時尚美妝 親子育兒 生活休閒 金融理財 健康運動 寰宇綜合

Zi 字媒體

2017-07-25T20:27:27+00:00
加入好友
Persistent Volume 持久性雲硬碟 Kubernetes Persistent Volume (簡稱 pv) 是一個讓 Pod 可以保留儲存資料的元件,提供給 Container 將 Volume 中的資料永久地儲存下來,不會因為 Pod 被銷毀而不見。在 K8S 架構中 Persistent Volume 算是底層的元件,目前已經有很多不同的實做,這些實做稱為 Provisioner,可以參考官方提供的 Storage Provisioner 列表。 但是嚴格說起來,Persistent Volume 並不關心誰來用他,對於 K8S 的管理者來說,就只是在 K8S Cluster 建立一個 Persistent Volume,像是裝上一顆硬碟的概念。目前 Container 真正的掛載使用需要透過 Persistent Volume Claim 機制的幫忙。 K8s 創建 Persistent Volume 以下操作需要 K8s 環境,可以參考之前的文章「minikube 安裝教學」建立 minikube。我們可以透過以下範例在 K8S 中建立一個持久性儲存空間,建立一個 pv.yml 如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 apiVersion: v1 kind: PersistentVolume metadata:   name: local-storage-www   labels:     type: local spec:   storageClassName: local-storage-www   persistentVolumeReclaimPolicy: Retain   capacity:     storage: 20Gi   accessModes:     - ReadWriteMany   hostPath:     path: "/mnt/local-storage-www"   ---   apiVersion: v1 kind: PersistentVolume metadata:   name: local-storage-mysql   labels:     type: local spec:   storageClassName: local-storage-mysql   persistentVolumeReclaimPolicy: Retain   capacity:     storage: 20Gi   accessModes:     - ReadWriteOnce   hostPath:     path: "/mnt/local-storage-mysql" 透過以下命令建立: kubectl create -f pv.yml 查看 pv 狀態: kubectl get pv 上述的設定檔中我們建立了兩個 PV,分別用在沒有分散能力的 MySQL 單一 Pod 環境,與預計有多個 Apache Micro Service 同時提供服務的 Pod 環境。其中的 accessModes: ReadWriteMany 表示這一個 Persistent Volume 可以同時分配給不同的 Persistent Volume Claim,如果想要設定被某一個 Persistent Volume Claim 獨占,那麼將設定改成 ReadWriteOnce 即可。 如果要刪除 PV 可以用 kubectl delete pv [PersistentVolumeName] 可以刪除 Persistent Volume。 K8s 創建 Persistent Volume Claim 建立好 PV 之後要來建立 PVC,建立一個 pvc.yml 如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 kind: PersistentVolumeClaim apiVersion: v1 metadata:   name: pvc-apache-www spec:   storageClassName: local-storage-www   accessModes:     - ReadWriteMany   resources:     requests:       storage: 1Gi   ---   kind: PersistentVolumeClaim apiVersion: v1 metadata:   name: pvc-mysql-data spec:   storageClassName: local-storage-mysql   accessModes:     - ReadWriteOnce   resources:     requests:       storage: 2Gi 透過以下命令建立: kubectl create -f pvc.yml 查看 PV 與 PVC kubectl get pvc 執行畫面如下: 要刪除 PVC 一樣用 kubectl delete pvc [PersistentVolumeClaimName] 可以刪除 PersistentVolumeClaim。 佈署 Micro Service 測試 Persistent Volume 掛載 最後為了要測試 PV 與 PVC 實際的應用,我們規劃了一個簡單的 Micro Service 服務架構,並且實現了 HA 與 Load Balance,如下: MySql Pod:單一節點,資料透過 ReadWriteOnce 模式掛載 Volume,Pod 本身沒有複製分散能力。 PHP Apache Pod:兩個節點,透過 Service 分散流量,兩個 Pod 資料透過 ReadWriteMany 掛載掛載同一個 Volume 好讓 Document Root 的資料同步,可以創建多個副本來提供服務。 Web Service:將兩個 PHP Apache Pod 組成 HA 與 Load Balance 的 Web 服務。 Adminer Job:純粹下載 adminer.php 放到共用的 volume 中,好讓每一個 PHP Apache Pod 可以存取。 這個假的 LAMP 服務設定檔如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 apiVersion: apps/v1 kind: Deployment metadata:   name: php-apache spec:   replicas: 2   selector:     matchLabels:       app: php-apache   template:     metadata:       labels:         app: php-apache     spec:       containers:       - image: php:7.2-apache         name: php-apache         ports:         - containerPort: 80           name: php-apache         volumeMounts:           - mountPath: "/var/www/html"             name: apache-www       volumes:       - name: apache-www         persistentVolumeClaim:           claimName: pvc-apache-www   ---   apiVersion: apps/v1 kind: Deployment metadata:   name: mysql spec:   replicas: 1   selector:     matchLabels:       app: mysql   template:     metadata:       labels:         app: mysql     spec:       containers:       - image: mysql:5.6         name: mysql         env:         - name: MYSQL_ROOT_PASSWORD           value: 1qaz2wsx         ports:         - containerPort: 3306           name: mysql         volumeMounts:         - mountPath: /var/lib/mysql           name: mysql-data       volumes:       - name: mysql-data         persistentVolumeClaim:           claimName: pvc-mysql-data   ---   apiVersion: batch/v1 kind: Job metadata:   name: install-adminer spec:   template:     spec:       containers:       - image: php:7.2-apache         name: php-apache         volumeMounts:           - mountPath: "/var/www/html"             name: apache-www         command: ["bash", "-c", "curl -L 'https://github.com/vrana/adminer/releases/download/v4.7.0/adminer-4.7.0.php' -o adminer.php"]       volumes:       - name: apache-www         persistentVolumeClaim:           claimName: pvc-apache-www       restartPolicy: Never   backoffLimit: 4   ---   apiVersion: v1 kind: Service metadata:   name: web spec:   type: NodePort   ports:   - name: http     port: 80     protocol: TCP     nodePort: 30080     targetPort: 80   - name: https     port: 443     protocol: TCP     nodePort: 30443     targetPort: 443   selector:     app: php-apache 透過 kubectl 佈署: kubectl create -f lamp.yml 顯示所有 k8s object: kubectl get all 執行畫面如下: 開啟瀏覽器連線「http://192.168.99.100:30080/adminer.php」並且重新整理,確認 adminer 可以正確運作: 整個 Kubernetes PV/PVC 的故事就先演到這裡,今天其實玩了很多 K8s Object,包含 Pod, Service. Deployment, Job, PersistentVolumeClaim, PersistentVolume 這些元件,也算是一個比較完整的應用,今天所有的 K8s Micro Service 設定檔放在 GitHub。有興趣的朋友可以深入研究 yaml 檔案的相關設定,下台一鞠躬。掰~ 分享到 Twitter(在新視窗中開啟) 按一下以分享至 Facebook(在新視窗中開啟) 分享到 LinkedIn(在新視窗中開啟) 點這裡寄給朋友(在新視窗中開啟) 按一下即可分享至 Skype(在新視窗中開啟) 分享到 Reddit(在新視窗中開啟) 分享到 Tumblr(在新視窗中開啟) 按一下以分享到 Telegram(在新視窗中開啟)

本文由toright提供 原文連結

寫了 5860316篇文章,獲得 23313次喜歡
精彩推薦