溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)

發(fā)布時(shí)間:2021-07-06 11:44:19 來源:億速云 閱讀:524 作者:chen 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

參考文檔:

  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是一個(gè)提供RESTful API管理GlusterFS卷的框架,便于管理員對(duì)GlusterFS進(jìn)行操作:

  1. 可以用于管理GlusterFS卷的生命周期;

  2. 能夠在OpenStack,Kubernetes,Openshift等云平臺(tái)上實(shí)現(xiàn)動(dòng)態(tài)存儲(chǔ)資源供應(yīng)(動(dòng)態(tài)在GlusterFS集群內(nèi)選擇bricks構(gòu)建volume);

  3. 支持GlusterFS多集群管理。

2. 框架

怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)

  1. Heketi支持GlusterFS多集群管理;

  2. 在集群中通過zone區(qū)分故障域。

 二.環(huán)境 

1. 環(huán)境

Kubernetes與GlusterFS集群已提前部署完成,請(qǐng)參考:

  1. Kubernetes:https://www.cnblogs.com/netonline/tag/kubernetes/

  2. GlusterFS:https://www.cnblogs.com/netonline/p/9102004.html

    注意:GlusterFS只需要安裝并啟動(dòng)即可,不必組建受信存儲(chǔ)池(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. 設(shè)置iptables

# 設(shè)置iptables,heketi默認(rèn)以tcp8080端口提供RESTful API服務(wù);
[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源,默認(rèn)yum源中無相關(guān)package;
# heketi:heketi服務(wù);
# 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 
{
  # 默認(rèn)端口tcp8080
  "_port_comment": "Heketi Server Port Number",
  "port": "8080",

  # 默認(rèn)值false,不需要認(rèn)證
  "_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.",
      ">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"
],
# 默認(rèn)設(shè)置為debug,不設(shè)置時(shí)的默認(rèn)值即是warning;
# 日志信息輸出在/var/log/message
    "loglevel" : "warning"
  }
}

3. 設(shè)置heketi免密訪問GlusterFS

# 選擇ssh執(zhí)行器,heketi服務(wù)器需要免密登陸GlusterFS集群的各節(jié)點(diǎn);
# -t:秘鑰類型;
# -q:安靜模式;
# -f:指定生成秘鑰的目錄與名字,注意與heketi.json的ssh執(zhí)行器中"keyfile"值一致;
# -N:秘鑰密碼,””即為空
[root@heketi ~]# ssh-keygen -t rsa -q -f /etc/heketi/heketi_key -N ""

# heketi服務(wù)由heketi用戶啟動(dòng),heketi用戶需要有新生成key的讀賦權(quán),否則服務(wù)無法啟動(dòng)
[root@heketi ~]# chown heketi:heketi /etc/heketi/heketi_key

# 分發(fā)公鑰;
# -i:指定公鑰
[root@heketi ~]# ssh-copy-id -i /etc/heketi/heketi_key.pub root@172.30.200.81
[root@heketi ~]# ssh-copy-id -i /etc/heketi/heketi_key.pub root@172.30.200.82
[root@heketi ~]# ssh-copy-id -i /etc/heketi/heketi_key.pub root@172.30.200.83

4. 啟動(dòng)heketi

# 通過yum安裝heketi,默認(rèn)的systemd文件有1處錯(cuò)誤;
# /usr/lib/systemd/system/heketi.service文件的”-config=/etc/heketi/heketi.json”應(yīng)該修改為”--config=/etc/heketi/heketi.json”;
# 否則啟動(dòng)時(shí)報(bào)”Error: unknown shorthand flag: 'c' in -config=/etc/heketi/heketi.json“錯(cuò),導(dǎo)致服務(wù)無法啟動(dòng)
[root@heketi ~]# systemctl enable heketi
[root@heketi ~]# systemctl restart heketi
[root@heketi ~]# systemctl status heketi
# 驗(yàn)證
[root@heketi ~]# curl http://localhost:8080/hello

怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)

四.設(shè)置GlusterFS集群 

1. 創(chuàng)建topology.json文件

# 通過topology.json文件定義組建GlusterFS集群;
# topology指定了層級(jí)關(guān)系:clusters-->nodes-->node/devices-->hostnames/zone;
# node/hostnames字段的manage填寫主機(jī)ip,指管理通道,在heketi服務(wù)器不能通過hostname訪問GlusterFS節(jié)點(diǎn)時(shí)間不能填寫hostname;
# node/hostnames字段的storage填寫主機(jī)ip,指存儲(chǔ)數(shù)據(jù)通道,與manage可以不一樣;
# node/zone字段指定了node所處的故障域,heketi通過跨故障域創(chuàng)建副本,提高數(shù)據(jù)高可用性質(zhì),如可以通過rack的不同區(qū)分zone值,創(chuàng)建跨機(jī)架的故障域;
# devices字段指定GlusterFS各節(jié)點(diǎn)的盤符(可以是多塊盤),必須是未創(chuàng)建文件系統(tǒng)的裸設(shè)備

[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集群各節(jié)點(diǎn)的glusterd服務(wù)已正常啟動(dòng),但不必組建受信存儲(chǔ)池;
# heketi-cli命令行也可手動(dòng)逐層添加cluster,node,device,volume等;
# “--server http://localhost:8080”:localhost執(zhí)行heketi-cli時(shí),可不指定;
# ”--user admin --secret admin@123 “:heketi.json中設(shè)置了認(rèn)證,執(zhí)行heketi-cli時(shí)需要帶上認(rèn)證信息,否則報(bào)”Error: Invalid JWT token: Unknown user”錯(cuò)
[root@heketi ~]# heketi-cli --server http://localhost:8080 --user admin --secret admin@123 topology load --json=/etc/heketi/topology.json

# 查看heketi topology信息,此時(shí)volume與brick等未創(chuàng)建;
# 通過”heketi-cli cluster info“可以查看集群相關(guān)信息;
# 通過”heketi-cli node info“可以查看節(jié)點(diǎn)相關(guān)信息;
# 通過”heketi-cli device info“可以查看device相關(guān)信息
[root@heketi ~]# heketi-cli --user admin --secret admin@123 topology info

怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)

五.K8S集群動(dòng)態(tài)掛載GlusterFS存儲(chǔ) 

1. 基于StorageClass的動(dòng)態(tài)存儲(chǔ)流程

kubernetes共享存儲(chǔ)供應(yīng)模式:

  1. 靜態(tài)模式(Static):集群管理員手工創(chuàng)建PV,在定義PV時(shí)需設(shè)置后端存儲(chǔ)的特性;

  2. 動(dòng)態(tài)模式(Dynamic):集群管理員不需要手工創(chuàng)建PV,而是通過StorageClass的設(shè)置對(duì)后端存儲(chǔ)進(jìn)行描述,標(biāo)記為某種"類型(Class)";此時(shí)要求PVC對(duì)存儲(chǔ)的類型進(jìn)行說明,系統(tǒng)將自動(dòng)完成PV的創(chuàng)建及與PVC的綁定;PVC可以聲明Class為"",說明PVC禁止使用動(dòng)態(tài)模式。

 基于StorageClass的動(dòng)態(tài)存儲(chǔ)供應(yīng)整體過程如下圖所示:

  1. 集群管理員預(yù)先創(chuàng)建存儲(chǔ)類(StorageClass);

  2. 用戶創(chuàng)建使用存儲(chǔ)類的持久化存儲(chǔ)聲明(PVC:PersistentVolumeClaim);

  3. 存儲(chǔ)持久化聲明通知系統(tǒng),它需要一個(gè)持久化存儲(chǔ)(PV: PersistentVolume);

  4. 系統(tǒng)讀取存儲(chǔ)類的信息;

  5. 系統(tǒng)基于存儲(chǔ)類的信息,在后臺(tái)自動(dòng)創(chuàng)建PVC需要的PV;

  6. 用戶創(chuàng)建一個(gè)使用PVC的Pod;

  7. Pod中的應(yīng)用通過PVC進(jìn)行數(shù)據(jù)的持久化;

  8. 而PVC使用PV進(jìn)行數(shù)據(jù)的最終持久化處理。

2. 定義StorageClass

# provisioner:表示存儲(chǔ)分配器,需要根據(jù)后端存儲(chǔ)的不同而變更;
# reclaimPolicy: 默認(rèn)即”Delete”,刪除pvc后,相應(yīng)的pv及后端的volume,brick(lvm)等一起刪除;設(shè)置為”Retain”時(shí)則保留數(shù)據(jù),需要手工處理
# resturl:heketi API服務(wù)提供的url;
# restauthenabled:可選參數(shù),默認(rèn)值為”false”,heketi服務(wù)開啟認(rèn)證時(shí)必須設(shè)置為”true”;
# restuser:可選參數(shù),開啟認(rèn)證時(shí)設(shè)置相應(yīng)用戶名;
# secretNamespace:可選參數(shù),開啟認(rèn)證時(shí)可以設(shè)置為使用持久化存儲(chǔ)的namespace;
# secretName:可選參數(shù),開啟認(rèn)證時(shí),需要將heketi服務(wù)的認(rèn)證密碼保存在secret資源中;
# clusterid:可選參數(shù),指定集群id,也可以是1個(gè)clusterid列表,格式為”id1,id2”;
# volumetype:可選參數(shù),設(shè)置卷類型及其參數(shù),如果未分配卷類型,則有分配器決定卷類型;如”volumetype: replicate:3”表示3副本的replicate卷,”volumetype: disperse:4:2”表示disperse卷,其中‘4’是數(shù)據(jù),’2’是冗余校驗(yàn),”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”值需要轉(zhuǎn)換為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

怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)

# 創(chuàng)建secret資源
[root@kubenode1 heketi]# kubectl create -f heketi-secret.yaml 

# 創(chuàng)建storageclass資源;
# 注意:storageclass資源創(chuàng)建后不可變更,如修改只能刪除后重建
[root@kubenode1 heketi]# kubectl create -f gluster-heketi-storageclass.yaml

# 查看storageclass資源
[root@kubenode1 heketi]# kubectl describe storageclass gluster-heketi-storageclass

怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)

3. 定義PVC

1)定義PVC

# 注意“storageClassName”的對(duì)應(yīng)關(guān)系
[root@kubenode1 heketi]# vim gluster-heketi-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: gluster-heketi-pvc
spec:
  storageClassName: gluster-heketi-storageclass
  # ReadWriteOnce:簡寫RWO,讀寫權(quán)限,且只能被單個(gè)node掛載;
  # ReadOnlyMany:簡寫ROX,只讀權(quán)限,允許被多個(gè)node掛載;
  # ReadWriteMany:簡寫RWX,讀寫權(quán)限,允許被多個(gè)node掛載;
  accessModes:
    - ReadWriteOnce
  resources:
requests:
  # 注意格式,不能寫“GB”
      storage: 1Gi

# 創(chuàng)建pvc資源
[root@kubenode1 heketi]# kubectl create -f gluster-heketi-pvc.yaml

2)查看k8s資源

# 查看PVC,狀態(tài)為”Bound”;
# “Capacity”為2G,是因?yàn)橥絼?chuàng)建meta數(shù)據(jù)
[root@kubenode1 heketi]# kubectl describe pvc gluster-heketi-pvc

# 查看PV詳細(xì)信息,除容量,引用storageclass信息,狀態(tài),回收策略等外,同時(shí)給出GlusterFS的Endpoint與path;
[root@kubenode1 heketi]# kubectl get pv
[root@kubenode1 heketi]# kubectl describe pv pvc-532cb8c3-cfc6-11e8-8fde-005056bfa8ba

怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)

# 查看endpoints資源,可以從pv信息中獲取,固定格式:glusterfs-dynamic-PVC_NAME;
# endpoints資源中指定了掛載存儲(chǔ)時(shí)的具體地址
[root@kubenode1 heketi]# kubectl describe endpoints glusterfs-dynamic-gluster-heketi-pvc

3)查看heketi

# volume與brick已經(jīng)創(chuàng)建;
# 主掛載點(diǎn)(通信)在glusterfs01節(jié)點(diǎn),其余兩個(gè)節(jié)點(diǎn)備選;
# 兩副本的情況下,glusterfs03節(jié)點(diǎn)并未創(chuàng)建brick
[root@heketi ~]# heketi-cli --user admin --secret admin@123 topology info

怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)

4)查看GlusterFS節(jié)點(diǎn)

# 以glusterfs01節(jié)點(diǎn)為例
[root@glusterfs01 ~]# lsblk

怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)

[root@glusterfs01 ~]# df -Th

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

怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)

4. Pod掛載存儲(chǔ)資源

# 設(shè)置1個(gè)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

# 創(chuàng)建pod
[root@kubenode1 heketi]# kubectl create -f gluster-heketi-pod.yaml

5. 驗(yàn)證

# 在容器的掛載目錄中創(chuàng)建文件
[root@kubenode1 heketi]# kubectl exec -it gluster-heketi-pod /bin/sh
/ # cd /pv-data
/pv-data # echo "This is a file!"># 在GlusterFS節(jié)點(diǎn)對(duì)應(yīng)掛載目錄查看創(chuàng)建的文件;
# 掛載目錄通過”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. 驗(yàn)證StorageClass的ReclaimPolicy# 刪除Pod應(yīng)用后,再刪除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節(jié)點(diǎn)
[root@glusterfs01 ~]# lsblk
[root@glusterfs01 ~]# df -Th
[root@glusterfs01 ~]# gluster volume list

“怎么通過Heketi管理GlusterFS為K8S集群提供持久化存儲(chǔ)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI