经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Kubernetes » 查看文章
通过Heketi管理GlusterFS为K8S集群提供持久化存储
来源:cnblogs  作者:Netonline  时间:2019/1/21 9:27:35  对本文有异议

参考文档:

  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/

 一.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只需要安装并启动即可,不必组建受信存储池(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

  1. # 设置iptables,heketi默认以tcp8080端口提供RESTful API服务;
  2. [root@heketi ~]# vim /etc/sysconfig/iptables
  3. -A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT
  4. [root@heketi ~]# service iptables restart

三.部署heketi

1. 安装heketi

  1. # 添加gluster yum源,默认yum源中无相关package;
  2. # heketi:heketi服务;
  3. # heketi-client:heketi客户端/命令行工具
  4. [root@heketi ~]# yum install -y centos-release-gluster
  5. [root@heketi ~]# yum install -y heketi heketi-client

2. 配置heketi.json

  1. # 注意红色字体是修改部分
  2. [root@heketi ~]# vim /etc/heketi/heketi.json
  3. {
  4. # 默认端口tcp8080
  5. "_port_comment": "Heketi Server Port Number",
  6. "port": "8080",
  7. # 默认值false,不需要认证
  8. "_use_auth": "Enable JWT authorization. Please enable for deployment",
  9. "use_auth": true,
  10. "_jwt": "Private keys for access",
  11. "jwt": {
  12. "_admin": "Admin has access to all APIs",
  13. "admin": {
  14. "key": "admin@123"
  15. },
  16. "_user": "User only has access to /volumes endpoint",
  17. "user": {
  18. "key": "user@123"
  19. }
  20. },
  21. "_glusterfs_comment": "GlusterFS Configuration",
  22. "glusterfs": {
  23. "_executor_comment": [
  24. "Execute plugin. Possible choices: mock, ssh",
  25. "mock: This setting is used for testing and development.",
  26. " It will not send commands to any node.",
  27. "ssh: This setting will notify Heketi to ssh to the nodes.",
  28. " It will need the values in sshexec to be configured.",
  29. "kubernetes: Communicate with GlusterFS containers over",
  30. " Kubernetes exec api."
  31. ],
  32. # mock:测试环境下创建的volume无法挂载;
  33. # kubernetes:在GlusterFS由kubernetes创建时采用
  34. "executor": "ssh",
  35. "_sshexec_comment": "SSH username and private key file information",
  36. "sshexec": {
  37. "keyfile": "/etc/heketi/heketi_key",
  38. "user": "root",
  39. "port": "22",
  40. "fstab": "/etc/fstab"
  41. },
  42. "_kubeexec_comment": "Kubernetes configuration",
  43. "kubeexec": {
  44. "host" :"https://kubernetes.host:8443",
  45. "cert" : "/path/to/crt.file",
  46. "insecure": false,
  47. "user": "kubernetes username",
  48. "password": "password for kubernetes user",
  49. "namespace": "OpenShift project or Kubernetes namespace",
  50. "fstab": "Optional: Specify fstab file on node. Default is /etc/fstab"
  51. },
  52. "_db_comment": "Database file name",
  53. "db": "/var/lib/heketi/heketi.db",
  54. "_loglevel_comment": [
  55. "Set log level. Choices are:",
  56. " none, critical, error, warning, info, debug",
  57. "Default is warning"
  58. ],
  59. # 默认设置为debug,不设置时的默认值即是warning;
  60. # 日志信息输出在/var/log/message
  61. "loglevel" : "warning"
  62. }
  63. }

3. 设置heketi免密访问GlusterFS

  1. # 选择ssh执行器,heketi服务器需要免密登陆GlusterFS集群的各节点;
  2. # -t:秘钥类型;
  3. # -q:安静模式;
  4. # -f:指定生成秘钥的目录与名字,注意与heketi.json的ssh执行器中"keyfile"值一致
  5. # -N:秘钥密码,””即为空
  6. [root@heketi ~]# ssh-keygen -t rsa -q -f /etc/heketi/heketi_key -N ""
  7.  
  8. # heketi服务由heketi用户启动,heketi用户需要有新生成key的读赋权,否则服务无法启动
  9. [root@heketi ~]# chown heketi:heketi /etc/heketi/heketi_key
  10.  
  11. # 分发公钥;
  12. # -i:指定公钥
  13. [root@heketi ~]# ssh-copy-id -i /etc/heketi/heketi_key.pub root@172.30.200.81
  14. [root@heketi ~]# ssh-copy-id -i /etc/heketi/heketi_key.pub root@172.30.200.82
  15. [root@heketi ~]# ssh-copy-id -i /etc/heketi/heketi_key.pub root@172.30.200.83

4. 启动heketi

  1. # 通过yum安装heketi,默认的systemd文件有1处错误;
  2. # /usr/lib/systemd/system/heketi.service文件的”-config=/etc/heketi/heketi.json”应该修改为”--config=/etc/heketi/heketi.json”;
  3. # 否则启动时报”Error: unknown shorthand flag: 'c' in -config=/etc/heketi/heketi.json“错,导致服务无法启动
  4. [root@heketi ~]# systemctl enable heketi
  5. [root@heketi ~]# systemctl restart heketi
  6. [root@heketi ~]# systemctl status heketi

  1. # 验证
  2. [root@heketi ~]# curl http://localhost:8080/hello

四.设置GlusterFS集群 

1. 创建topology.json文件

  1. # 通过topology.json文件定义组建GlusterFS集群;
  2. # topology指定了层级关系:clusters-->nodes-->node/devices-->hostnames/zone;
  3. # node/hostnames字段的manage填写主机ip,指管理通道,在heketi服务器不能通过hostname访问GlusterFS节点时间不能填写hostname
  4. # node/hostnames字段的storage填写主机ip,指存储数据通道,与manage可以不一样;
  5. # node/zone字段指定了node所处的故障域,heketi通过跨故障域创建副本,提高数据高可用性质,如可以通过rack的不同区分zone值,创建跨机架的故障域;
  6. # devices字段指定GlusterFS各节点的盘符(可以是多块盘),必须是未创建文件系统的裸设备
  7. [root@heketi ~]# vim /etc/heketi/topology.json
  8. {
  9. "clusters": [
  10. {
  11. "nodes": [
  12. {
  13. "node": {
  14. "hostnames": {
  15. "manage": [
  16. "172.30.200.81"
  17. ],
  18. "storage": [
  19. "172.30.200.81"
  20. ]
  21. },
  22. "zone": 1
  23. },
  24. "devices": [
  25. "/dev/sdb"
  26. ]
  27. },
  28. {
  29. "node": {
  30. "hostnames": {
  31. "manage": [
  32. "172.30.200.82"
  33. ],
  34. "storage": [
  35. "172.30.200.82"
  36. ]
  37. },
  38. "zone": 2
  39. },
  40. "devices": [
  41. "/dev/sdb"
  42. ]
  43. },
  44. {
  45. "node": {
  46. "hostnames": {
  47. "manage": [
  48. "172.30.200.83"
  49. ],
  50. "storage": [
  51. "172.30.200.83"
  52. ]
  53. },
  54. "zone": 3
  55. },
  56. "devices": [
  57. "/dev/sdb"
  58. ]
  59. }
  60. ]
  61. }
  62. ]
  63. }

2. 通过topology.json组建GlusterFS集群

  1. # GlusterFS集群各节点的glusterd服务已正常启动,但不必组建受信存储池;
  2. # heketi-cli命令行也可手动逐层添加cluster,node,device,volume等;
  3. # “--server http://localhost:8080”:localhost执行heketi-cli时,可不指定;
  4. # ”--user admin --secret admin@123 “:heketi.json中设置了认证,执行heketi-cli时需要带上认证信息,否则报”Error: Invalid JWT token: Unknown user”错
  5. [root@heketi ~]# heketi-cli --server http://localhost:8080 --user admin --secret admin@123 topology load --json=/etc/heketi/topology.json

  1. # 查看heketi topology信息,此时volume与brick等未创建;
  2. # 通过”heketi-cli cluster info“可以查看集群相关信息;
  3. # 通过”heketi-cli node info“可以查看节点相关信息;
  4. # 通过”heketi-cli device info“可以查看device相关信息
  5. [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

  1. # provisioner:表示存储分配器,需要根据后端存储的不同而变更;
  2. # reclaimPolicy: 默认即”Delete”,删除pvc后,相应的pv及后端的volume,brick(lvm)等一起删除;设置为”Retain”时则保留数据,需要手工处理
  3. # resturl:heketi API服务提供的url;
  4. # restauthenabled:可选参数,默认值为”false”,heketi服务开启认证时必须设置为”true”;
  5. # restuser:可选参数,开启认证时设置相应用户名;
  6. # secretNamespace:可选参数,开启认证时可以设置为使用持久化存储的namespace;
  7. # secretName:可选参数,开启认证时,需要将heketi服务的认证密码保存在secret资源中;
  8. # clusterid:可选参数,指定集群id,也可以是1个clusterid列表,格式为”id1,id2”;
  9. # volumetype:可选参数,设置卷类型及其参数,如果未分配卷类型,则有分配器决定卷类型;如”volumetype: replicate:3”表示3副本的replicate卷,”volumetype: disperse:4:2”表示disperse卷,其中‘4’是数据,’2’是冗余校验,”volumetype: none”表示distribute卷#
  10. [root@kubenode1 ~]# mkdir -p heketi
  11. [root@kubenode1 ~]# cd heketi/
  12. [root@kubenode1 heketi]# vim gluster-heketi-storageclass.yaml
  13. apiVersion: storage.k8s.io/v1
  14. kind: StorageClass
  15. metadata:
  16. name: gluster-heketi-storageclass
  17. provisioner: kubernetes.io/glusterfs
  18. reclaimPolicy: Delete
  19. parameters:
  20. resturl: "http://172.30.200.80:8080"
  21. restauthenabled: "true"
  22. restuser: "admin"
  23. secretNamespace: "default"
  24. secretName: "heketi-secret"
  25. volumetype: "replicate:2"
  26.  
  27. # 生成secret资源,其中”key”值需要转换为base64编码格式
  28. [root@kubenode1 heketi]# echo -n "admin@123" | base64
  29.  
  30. # 注意name/namespace与storageclass资源中定义一致;
  31. # 密码必须有“kubernetes.io/glusterfs” type
  32. [root@kubenode1 heketi]# cat heketi-secret.yaml
  33. apiVersion: v1
  34. kind: Secret
  35. metadata:
  36. name: heketi-secret
  37. namespace: default
  38. data:
  39. # base64 encoded password. E.g.: echo -n "mypassword" | base64
  40. key: YWRtaW5AMTIz
  41. type: kubernetes.io/glusterfs

  1. # 创建secret资源
  2. [root@kubenode1 heketi]# kubectl create -f heketi-secret.yaml
  3.  
  4. # 创建storageclass资源;
  5. # 注意:storageclass资源创建后不可变更,如修改只能删除后重建
  6. [root@kubenode1 heketi]# kubectl create -f gluster-heketi-storageclass.yaml

  1. # 查看storageclass资源
  2. [root@kubenode1 heketi]# kubectl describe storageclass gluster-heketi-storageclass

3. 定义PVC

1)定义PVC

  1. # 注意“storageClassName”的对应关系
  2. [root@kubenode1 heketi]# vim gluster-heketi-pvc.yaml
  3. kind: PersistentVolumeClaim
  4. apiVersion: v1
  5. metadata:
  6. name: gluster-heketi-pvc
  7. spec:
  8. storageClassName: gluster-heketi-storageclass
  9. # ReadWriteOnce:简写RWO,读写权限,且只能被单个node挂载;
  10. # ReadOnlyMany:简写ROX,只读权限,允许被多个node挂载;
  11. # ReadWriteMany:简写RWX,读写权限,允许被多个node挂载;
  12. accessModes:
  13. - ReadWriteOnce
  14. resources:
  15. requests:
  16. # 注意格式,不能写“GB”
  17. storage: 1Gi
  18. # 创建pvc资源
  19. [root@kubenode1 heketi]# kubectl create -f gluster-heketi-pvc.yaml

2)查看k8s资源

  1. # 查看PVC,状态为”Bound”;
  2. # “Capacity”为2G,是因为同步创建meta数据
  3. [root@kubenode1 heketi]# kubectl describe pvc gluster-heketi-pvc

  1. # 查看PV详细信息,除容量,引用storageclass信息,状态,回收策略等外,同时给出GlusterFS的Endpoint与path;
  2. [root@kubenode1 heketi]# kubectl get pv
  3. [root@kubenode1 heketi]# kubectl describe pv pvc-532cb8c3-cfc6-11e8-8fde-005056bfa8ba

  1. # 查看endpoints资源,可以从pv信息中获取,固定格式:glusterfs-dynamic-PVC_NAME
  2. # endpoints资源中指定了挂载存储时的具体地址
  3. [root@kubenode1 heketi]# kubectl describe endpoints glusterfs-dynamic-gluster-heketi-pvc

3)查看heketi

  1. # volume与brick已经创建;
  2. # 主挂载点(通信)在glusterfs01节点,其余两个节点备选;
  3. # 两副本的情况下,glusterfs03节点并未创建brick
  4. [root@heketi ~]# heketi-cli --user admin --secret admin@123 topology info

4)查看GlusterFS节点

  1. # 以glusterfs01节点为例
  2. [root@glusterfs01 ~]# lsblk

  1. [root@glusterfs01 ~]# df -Th

  1. # 查看volume的具体信息:2副本的replicate卷;
  2. # 另有”vgscan”,”vgdisplay”也可查看逻辑卷组信息等
  3. [root@glusterfs01 ~]# gluster volume list
  4. [root@glusterfs01 ~]# gluster volume info vol_308342f1ffff3aea7ec6cc72f6d13cd7

4. Pod挂载存储资源

  1. # 设置1个volume被pod引用,volume的类型为”persistentVolumeClaim”
  2. [root@kubenode1 heketi]# vim gluster-heketi-pod.yaml
  3. kind: Pod
  4. apiVersion: v1
  5. metadata:
  6. name: gluster-heketi-pod
  7. spec:
  8. containers:
  9. - name: gluster-heketi-container
  10. image: busybox
  11. command:
  12. - sleep
  13. - "3600"
  14. volumeMounts:
  15. - name: gluster-heketi-volume
  16. mountPath: "/pv-data"
  17. readOnly: false
  18. volumes:
  19. - name: gluster-heketi-volume
  20. persistentVolumeClaim:
  21. claimName: gluster-heketi-pvc
  22. # 创建pod
  23. [root@kubenode1 heketi]# kubectl create -f gluster-heketi-pod.yaml

5. 验证

  1. # 在容器的挂载目录中创建文件
  2. [root@kubenode1 heketi]# kubectl exec -it gluster-heketi-pod /bin/sh
  3. / # cd /pv-data
  4. /pv-data # echo "This is a file!" >> a.txt
  5. /pv-data # echo "This is b file!" >> b.txt
  6. /pv-data # ls

  1. # 在GlusterFS节点对应挂载目录查看创建的文件;
  2. # 挂载目录通过”df -Th”或”lsblk”获取
  3. [root@glusterfs01 ~]# df -Th
  4. [root@glusterfs01 ~]# cd /var/lib/heketi/mounts/vg_af339b60319a63a77b05ddbec1b21bbe/brick_d712f1543476c4198d3869c682cdaa9a/brick/
  5. [root@glusterfs01 brick]# ls
  6. [root@glusterfs01 brick]# cat a.txt
  7. [root@glusterfs01 brick]# cat b.txt

6. 验证StorageClass的ReclaimPolicy

  1. # 删除Pod应用后,再删除pvc
  2. [root@kubenode1 heketi]# kubectl delete -f gluster-heketi-pod.yaml
  3. [root@kubenode1 heketi]# kubectl delete -f gluster-heketi-pvc.yaml
  4.  
  5. # k8s资源
  6. [root@kubenode1 heketi]# kubectl get pvc
  7. [root@kubenode1 heketi]# kubectl get pv
  8. [root@kubenode1 heketi]# kubectl get endpoints

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

  1. # GlusterFS节点
  2. [root@glusterfs01 ~]# lsblk
  3. [root@glusterfs01 ~]# df -Th
  4. [root@glusterfs01 ~]# gluster volume list

原文链接:http://www.cnblogs.com/netonline/p/10288219.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号