1. 程式人生 > 實用技巧 >通過Heketi管理GlusterFS為K8S叢集提供持久化儲存

通過Heketi管理GlusterFS為K8S叢集提供持久化儲存

參考文件:

  1. Github project:https://github.com/heketi/heketi
  2. MANAGING VOLUMES USING HEKETI:https://access.redhat.com/documentation/en-us/red_hat_gluster_storage/3.3/html/administration_guide/ch05s02
  3. StorageClass:https://kubernetes.io/docs/concepts/storage/storage-classes/
  4. StorageClass(中文):https://k8smeetup.github.io/docs/concepts/storage/storage-classes/
  5. Dynamic Volume Provisioning:https://kubernetes.io/docs/concepts/storage/dynamic-provisioning/
  6. nfs- provisioner:https://github.com/kubernetes-incubator/external-storage/tree/master/nfs

一.Heketi簡介

1.簡介

Heketi是一個提供RESTful API管理GlusterFS卷的框架,便於管理員對GlusterFS進行操作:

  1. 可以用於管理GlusterFS卷的生命週期;
  2. 能夠在OpenStack,Kubernetes,Openshift等雲平臺上實現動態儲存資源供應(動態在GlusterFS叢集內選擇bricks構建volume);
  3. 支援GlusterFS多叢集管理。

2.框架

  1. Heketi支援GlusterFS多叢集管理;
  2. 在叢集中通過zone區分故障域。

二.環境

1.環境

Kubernetes與GlusterFS叢集已提前部署完成,請參考:

  1. Kubernetes:https://www.cnblogs.com/netonline/tag/kubernetes/
  2. GlusterFS:https://www.cnblogs.com/netonline/p/9102004.html

    注意:GlusterFS只需要安裝並啟動即可,不必組建受信儲存池(trusted storage pools)

Hostname

IP

Remark

kubenode1

172.30.200.21

kubenode2

172.30.200.22

kubenode3

172.30.200.23

heketi

172.30.200.80

selinux disabled

glusterfs01

172.30.200.81

glusterfs02

172.30.200.82

glusterfs03

172.30.200.83

2.設定iptables

# 設定iptables,heketi預設以tcp8080埠提供RESTful API服務;
[root@heketi ~]# vim /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT

[root@heketi ~]# service iptables restart

三.部署heketi

1.安裝heketi

# 新增gluster yum源,預設yum源中無相關package;
# heketi:heketi服務
# heketi-client:heketi客戶端/命令列工具
[root@heketi ~]# yum install -y centos-release-gluster
[root@heketi ~]# yum install -y heketi heketi-client

2.配置heketi.json

# 注意紅色字型是修改部分
[root@heketi ~]# vim /etc/heketi/heketi.json 
{
  # 預設埠tcp8080
  "_port_comment": "Heketi Server Port Number",
  "port": "8080",

  # 預設值false,不需要認證
  "_use_auth": "Enable JWT authorization. Please enable for deployment",
  "use_auth": true,

  "_jwt": "Private keys for access",
  "jwt": {
    "_admin": "Admin has access to all APIs",
    "admin": {
      "key": "admin@123"
    },
    "_user": "User only has access to /volumes endpoint",
    "user": {
      "key": "user@123"
    }
  },

  "_glusterfs_comment": "GlusterFS Configuration",
  "glusterfs": {
    "_executor_comment": [
      "Execute plugin. Possible choices: mock, ssh",
      "mock: This setting is used for testing and development.",
      "      It will not send commands to any node.",
      "ssh:  This setting will notify Heketi to ssh to the nodes.",
      "      It will need the values in sshexec to be configured.",
      "kubernetes: Communicate with GlusterFS containers over",
      "            Kubernetes exec api."
],
# mock:測試環境下建立的volume無法掛載;
# kubernetes:在GlusterFS由kubernetes建立時採用
    "executor": "ssh",

    "_sshexec_comment": "SSH username and private key file information",
    "sshexec": {
      "keyfile": "/etc/heketi/heketi_key",
      "user": "root",
      "port": "22",
      "fstab": "/etc/fstab"
    },

    "_kubeexec_comment": "Kubernetes configuration",
    "kubeexec": {
      "host" :"https://kubernetes.host:8443",
      "cert" : "/path/to/crt.file",
      "insecure": false,
      "user": "kubernetes username",
      "password": "password for kubernetes user",
      "namespace": "OpenShift project or Kubernetes namespace",
      "fstab": "Optional: Specify fstab file on node.  Default is /etc/fstab"
    },

    "_db_comment": "Database file name",
    "db": "/var/lib/heketi/heketi.db",

    "_loglevel_comment": [
      "Set log level. Choices are:",
      "  none, critical, error, warning, info, debug",
      "Default is warning"
],
# 預設設定為debug,不設定時的預設值即是warning;
# 日誌資訊輸出在/var/log/message
    "loglevel" : "warning"
  }
}

3.設定heketi免密訪問GlusterFS

# 選擇ssh執行器,heketi伺服器需要免密登陸GlusterFS叢集的各節點;
# -t:祕鑰型別;
# -q:安靜模式;
# -f:指定生成祕鑰的目錄與名字,注意與heketi.json的ssh執行器中"keyfile"值一致;
# -N:祕鑰密碼,””即為空
[root@heketi ~]# ssh-keygen -t rsa -q -f /etc/heketi/heketi_key -N ""

# heketi服務由heketi使用者啟動,heketi使用者需要有新生成key的讀賦權,否則服務無法啟動
[root@heketi ~]# chown heketi:heketi /etc/heketi/heketi_key

# 分發公鑰;
# -i:指定公鑰
[root@heketi ~]# ssh-copy-id -i /etc/heketi/heketi_key.pub [email protected]
[root@heketi ~]# ssh-copy-id -i /etc/heketi/heketi_key.pub [email protected]
[root@heketi ~]# ssh-copy-id -i /etc/heketi/heketi_key.pub [email protected]

4.啟動heketi

# 通過yum安裝heketi,預設的systemd檔案有1處錯誤;
# /usr/lib/systemd/system/heketi.service檔案的”-config=/etc/heketi/heketi.json”應該修改為”--config=/etc/heketi/heketi.json”;
# 否則啟動時報”Error: unknown shorthand flag: 'c' in -config=/etc/heketi/heketi.json“錯,導致服務無法啟動
[root@heketi ~]# systemctl enable heketi
[root@heketi ~]# systemctl restart heketi
[root@heketi ~]# systemctl status heketi

# 驗證
[root@heketi ~]# curl http://localhost:8080/hello

四.設定GlusterFS叢集

1.建立topology.json檔案

# 通過topology.json檔案定義組建GlusterFS叢集;
# topology指定了層級關係:clusters-->nodes-->node/devices-->hostnames/zone;
# node/hostnames欄位的manage填寫主機ip,指管理通道,在heketi伺服器不能通過hostname訪問GlusterFS節點時不能填寫hostname;
# node/hostnames欄位的storage填寫主機ip,指儲存資料通道,與manage可以不一樣;
# node/zone欄位指定了node所處的故障域,heketi通過跨故障域建立副本,提高資料高可用性質,如可以通過rack的不同區分zone值,建立跨機架的故障域;
# devices欄位指定GlusterFS各節點的碟符(可以是多塊盤),必須是未建立檔案系統的裸裝置
[root@heketi ~]# vim /etc/heketi/topology.json
{
    "clusters": [
        {
            "nodes": [
                {
                    "node": {
                        "hostnames": {
                            "manage": [
                                "172.30.200.81"
                            ],
                            "storage": [
                                "172.30.200.81"
                            ]
                        },
                        "zone": 1
                    },
                    "devices": [
                        "/dev/sdb"
                    ]
                },
                {
                    "node": {
                        "hostnames": {
                            "manage": [
                                "172.30.200.82"
                            ],
                            "storage": [
                                "172.30.200.82"
                            ]
                        },
                        "zone": 2
                    },
                    "devices": [
                        "/dev/sdb"
                    ]
                },
                {
                    "node": {
                        "hostnames": {
                            "manage": [
                                "172.30.200.83"
                            ],
                            "storage": [
                                "172.30.200.83"
                            ]
                        },
                        "zone": 3
                    },
                    "devices": [
                        "/dev/sdb"
                    ]
                }
            ]
        }
    ]
}

2.通過topology.json組建GlusterFS叢集

# GlusterFS叢集各節點的glusterd服務已正常啟動,但不必組建受信儲存池;
# heketi-cli命令列也可手動逐層新增cluster,node,device,volume等;
# “--server http://localhost:8080”:localhost執行heketi-cli時,可不指定;
# ”--user admin --secret admin@123 “:heketi.json中設定了認證,執行heketi-cli時需要帶上認證資訊,否則報”Error: Invalid JWT token: Unknown user”錯
[root@heketi ~]# heketi-cli --server http://localhost:8080 --user admin --secret admin@123 topology load --json=/etc/heketi/topology.json

# 檢視heketi topology資訊,此時volume與brick等未建立;
# 通過”heketi-cli cluster info“可以檢視叢集相關資訊;
# 通過”heketi-cli node info“可以檢視節點相關資訊;
# 通過”heketi-cli device info“可以檢視device相關資訊
[root@heketi ~]# heketi-cli --user admin --secret admin@123 topology info

五.K8S叢集動態掛載GlusterFS儲存

1.基於StorageClass的動態儲存流程

kubernetes共享儲存供應模式:

  1. 靜態模式(Static):叢集管理員手工建立PV,在定義PV時需設定後端儲存的特性;
  2. 動態模式(Dynamic):叢集管理員不需要手工建立PV,而是通過StorageClass的設定對後端儲存進行描述,標記為某種"型別(Class)";此時要求PVC對儲存的型別進行說明,系統將自動完成PV的建立及與PVC的繫結;PVC可以宣告Class為"",說明PVC禁止使用動態模式。

基於StorageClass的動態儲存供應整體過程如下圖所示:

  1. 叢集管理員預先建立儲存類(StorageClass);
  2. 使用者建立使用儲存類的持久化儲存宣告(PVC:PersistentVolumeClaim);
  3. 儲存持久化宣告通知系統,它需要一個持久化儲存(PV: PersistentVolume);
  4. 系統讀取儲存類的資訊;
  5. 系統基於儲存類的資訊,在後臺自動建立PVC需要的PV;
  6. 使用者建立一個使用PVC的Pod;
  7. Pod中的應用通過PVC進行資料的持久化;
  8. 而PVC使用PV進行資料的最終持久化處理。

2.定義StorageClass

# provisioner:表示儲存分配器,需要根據後端儲存的不同而變更;
# reclaimPolicy: 預設即”Delete”,刪除pvc後,相應的pv及後端的volume,brick(lvm)等一起刪除;設定為”Retain”時則保留資料,需要手工處理
# resturl:heketi API服務提供的url;
# restauthenabled:可選引數,預設值為”false”,heketi服務開啟認證時必須設定為”true”;
# restuser:可選引數,開啟認證時設定相應使用者名稱;
# secretNamespace:可選引數,開啟認證時可以設定為使用持久化儲存的namespace;
# secretName:可選引數,開啟認證時,需要將heketi服務的認證密碼儲存在secret資源中;
# clusterid:可選引數,指定叢集id,也可以是1個clusterid列表,格式為”id1,id2”;
# volumetype:可選引數,設定卷型別及其引數,如果未分配卷型別,則有分配器決定卷型別;如”volumetype: replicate:3”表示3副本的replicate卷,”volumetype: disperse:4:2”表示disperse卷,其中‘4’是資料,’2’是冗餘校驗,”volumetype: none”表示distribute卷# 
[root@kubenode1 ~]# mkdir -p heketi
[root@kubenode1 ~]# cd heketi/
[root@kubenode1 heketi]# vim gluster-heketi-storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gluster-heketi-storageclass
provisioner: kubernetes.io/glusterfs
reclaimPolicy: Delete
parameters:
  resturl: "http://172.30.200.80:8080"
  restauthenabled: "true"
  restuser: "admin"
  secretNamespace: "default"
  secretName: "heketi-secret"
  volumetype: "replicate:2"

# 生成secret資源,其中”key”值需要轉換為base64編碼格式
[root@kubenode1 heketi]# echo -n "admin@123" | base64

# 注意name/namespace與storageclass資源中定義一致;
# 密碼必須有“kubernetes.io/glusterfs” type
[root@kubenode1 heketi]# cat heketi-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: heketi-secret
  namespace: default
data:
  # base64 encoded password. E.g.: echo -n "mypassword" | base64
  key: YWRtaW5AMTIz
type: kubernetes.io/glusterfs

# 建立secret資源
[root@kubenode1 heketi]# kubectl create -f heketi-secret.yaml 

# 建立storageclass資源;
# 注意:storageclass資源建立後不可變更,如修改只能刪除後重建
[root@kubenode1 heketi]# kubectl create -f gluster-heketi-storageclass.yaml

# 檢視storageclass資源
[root@kubenode1 heketi]# kubectl describe storageclass gluster-heketi-storageclass

3.定義PVC

1)定義PVC

# 注意“storageClassName”的對應關係
[root@kubenode1 heketi]# vim gluster-heketi-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: gluster-heketi-pvc
spec:
  storageClassName: gluster-heketi-storageclass
  # ReadWriteOnce:簡寫RWO,讀寫許可權,且只能被單個node掛載;
  # ReadOnlyMany:簡寫ROX,只讀許可權,允許被多個node掛載;
  # ReadWriteMany:簡寫RWX,讀寫許可權,允許被多個node掛載;
  accessModes:
    - ReadWriteOnce
  resources:
requests:
  # 注意格式,不能寫“GB”
      storage: 1Gi

# 建立pvc資源
[root@kubenode1 heketi]# kubectl create -f gluster-heketi-pvc.yaml

2)檢視k8s資源

# 檢視PVC,狀態為”Bound”;
# “Capacity”為2G,是因為同步建立meta資料
[root@kubenode1 heketi]# kubectl describe pvc gluster-heketi-pvc

# 檢視PV詳細資訊,除容量,引用storageclass資訊,狀態,回收策略等外,同時給出GlusterFS的Endpoint與path;
[root@kubenode1 heketi]# kubectl get pv
[root@kubenode1 heketi]# kubectl describe pv pvc-532cb8c3-cfc6-11e8-8fde-005056bfa8ba

# 檢視endpoints資源,可以從pv資訊中獲取,固定格式:glusterfs-dynamic-PVC_NAME;
# endpoints資源中指定了掛載儲存時的具體地址
[root@kubenode1 heketi]# kubectl describe endpoints glusterfs-dynamic-gluster-heketi-pvc

3)檢視heketi

# volume與brick已經建立;
# 主掛載點(通訊)在glusterfs01節點,其餘兩個節點備選;
# 兩副本的情況下,glusterfs03節點並未建立brick
[root@heketi ~]# heketi-cli --user admin --secret admin@123 topology info

4)檢視GlusterFS節點

# 以glusterfs01節點為例
[root@glusterfs01 ~]# lsblk

[root@glusterfs01 ~]# df -Th

# 檢視volume的具體資訊:2副本的replicate卷;
# 另有”vgscan”,”vgdisplay”也可檢視邏輯卷組資訊等
[root@glusterfs01 ~]# gluster volume list
[root@glusterfs01 ~]# gluster volume info vol_308342f1ffff3aea7ec6cc72f6d13cd7

4.Pod掛載儲存資源

# 設定1個volume被pod引用,volume的型別為”persistentVolumeClaim”
[root@kubenode1 heketi]# vim gluster-heketi-pod.yaml
kind: Pod
apiVersion: v1
metadata:
  name: gluster-heketi-pod
spec:
  containers:
  - name: gluster-heketi-container
    image: busybox
    command:
    - sleep
    - "3600"
    volumeMounts:
    - name: gluster-heketi-volume
      mountPath: "/pv-data"
      readOnly: false
  volumes:
  - name: gluster-heketi-volume
    persistentVolumeClaim:
      claimName: gluster-heketi-pvc

# 建立pod
[root@kubenode1 heketi]# kubectl create -f gluster-heketi-pod.yaml

5.驗證

# 在容器的掛載目錄中建立檔案
[root@kubenode1 heketi]# kubectl exec -it gluster-heketi-pod /bin/sh
/ # cd /pv-data
/pv-data # echo "This is a file!" >> a.txt
/pv-data # echo "This is b file!" >> b.txt
/pv-data # ls

# 在GlusterFS節點對應掛載目錄檢視建立的檔案;
# 掛載目錄通過”df -Th”或”lsblk”獲取
[root@glusterfs01 ~]# df -Th
[root@glusterfs01 ~]# cd /var/lib/heketi/mounts/vg_af339b60319a63a77b05ddbec1b21bbe/brick_d712f1543476c4198d3869c682cdaa9a/brick/
[root@glusterfs01 brick]# ls
[root@glusterfs01 brick]# cat a.txt 
[root@glusterfs01 brick]# cat b.txt

6.驗證StorageClass的ReclaimPolicy

# 刪除Pod應用後,再刪除pvc
[root@kubenode1 heketi]# kubectl delete -f gluster-heketi-pod.yaml 
[root@kubenode1 heketi]# kubectl delete -f gluster-heketi-pvc.yaml

# k8s資源
[root@kubenode1 heketi]# kubectl get pvc
[root@kubenode1 heketi]# kubectl get pv
[root@kubenode1 heketi]# kubectl get endpoints

# heketi
[root@heketi ~]# heketi-cli --user admin --secret admin@123 topology info

# GlusterFS節點
[root@glusterfs01 ~]# lsblk
[root@glusterfs01 ~]# df -Th
[root@glusterfs01 ~]# gluster volume list

分類:Docker/Kubernetes/Mesos,GlusterFS/Ceph/vSAN 標籤:heketi,glusterfs,kubernetes 好文要頂關注我收藏該文 Netonline
關注 - 4
粉絲 - 56 +加關注 2 0 «上一篇:CDH 5.16.1 離線部署 & 通過 CDH 部署 Hadoop 服務
»下一篇:CEPH LIO iSCSI Gateway posted @2019-01-18 16:12Netonline 閱讀(6636) 評論(4)編輯收藏

評論列表 #1樓2019-08-22 10:438年扛槍夢 按照你的文件部署下來了,向你學習。 支援(0)反對(0) #2樓2019-11-16 13:59木二 借用部分了 支援(0)反對(0) #3樓2020-06-19 10:50小胖子方法

你好,在碟符中,devices 可以寫某個分割槽嗎或者資料夾路徑

支援(0)反對(0) #4樓[樓主]2020-07-05 10:29Netonline

@小胖子方法
你好,文中有一條說明:必須是未建立檔案系統的裸裝置,所以資料夾路徑應該是不行的;裸裝置說明應該也不能用分割槽,但太久沒有看了,手裡現在也沒有驗證環境,分割槽如果沒有建立檔案系統的話倒是可以試一試,有訊息的話盼回覆。

支援(0)反對(0) 重新整理評論重新整理頁面返回頂部 註冊使用者登入後才能發表評論,請登入註冊訪問網站首頁。 【推薦】超50萬行VC++原始碼: 大型組態工控、電力模擬CAD與GIS原始碼庫
【推薦】7天蛻變!阿里雲專家免費授課,名額有限!
【推薦】828企業上雲節,億元上雲補貼,華為雲更懂企業
【推薦】未知數的距離,毫秒間的傳遞,聲網與你實時互動
【推薦】了不起的開發者,擋不住的華為,園子裡的品牌專區
【推薦】96秒100億!哪些“黑科技”支撐全球最大流量洪峰? 相關博文:
·GlusterFS分散式儲存叢集-2.使用
·獨立部署GlusterFS+Heketi實現Kubernetes共享儲存
·k8s的持久化儲存PV&&PVC
·[k8s]k8s配置nfs做後端儲存&配置多nginx共享儲存&&statefulset配置
·分散式檔案系統---GlusterFS
»更多推薦... 【推薦】電子簽名認準大家籤,上海CA權威認證