Kubernetes Persistent Volume 介紹 (Micro Service)

栏目: 数据库 · Mysql · 发布时间: 7年前

内容简介:Kubernetes Persistent Volume (簡稱 pv) 是一個讓 Pod 可以保留儲存資料的元件,提供給 Container 將 Volume 中的資料永久地儲存下來,不會因為 Pod 被銷毀而不見。在 K8S 架構中 Persistent Volume 算是底層的元件,目前已經有很多不同的實做,這些實做稱為 Provisioner,可以參考官方提供的但是嚴格說起來,Persistent Volume 並不關心誰來用他,對於 K8S 的管理者來說,就只是在 K8S Cluster 建立一個

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 如下:

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

Kubernetes Persistent Volume 介紹 (Micro Service)

上述的設定檔中我們建立了兩個 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 如下:

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

執行畫面如下:

Kubernetes Persistent Volume 介紹 (Micro Service)

要刪除 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 服務設定檔如下:

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

執行畫面如下:

Kubernetes Persistent Volume 介紹 (Micro Service)

開啟瀏覽器連線「http://192.168.99.100:30080/adminer.php」並且重新整理,確認 adminer 可以正確運作:

Kubernetes Persistent Volume 介紹 (Micro Service)

整個 Kubernetes PV/PVC 的故事就先演到這裡,今天其實玩了很多 K8s Object,包含 Pod, Service. Deployment, Job, PersistentVolumeClaim, PersistentVolume 這些元件,也算是一個比較完整的應用,今天所有的 K8s Micro Service 設定檔放在 GitHub 。有興趣的朋友可以深入研究 yaml 檔案的相關設定,下台一鞠躬。掰~


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

数据结构与算法

数据结构与算法

卓滋德克 / 陈曙晖 / 清华大学出版社 / 2003-4-1 / 69.00

本书是一本介绍数据结构与算法的优秀书籍。 本书系统介绍了C++面向对象程序设计、算法复杂度、链表、栈、队列、递归、树、图、排序和查找算法、散列技术、数据压缩算法、内存管理等内容;尤其对递归算法进行了深入剖析。在附录中详细介绍了大O符号与标准模板库:在大多数章中提供了相应的实例分析和程序设计作业。 本书适合作为计算机软件专业或其他相关专业的教科书。对于需要参加计算机考试,......一起来看看 《数据结构与算法》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试