k8s中部署有状态应用等需要持久化数据的应用,必不可少得用存储,k8s支持很多中存储方案,我司目前使用的存储有glusterfs(分为容器化和裸机方式)、nfs供应用选用,本次就简单实战下glusterfs配合k8s做数据存储。

介绍

GlusterFS系统是一个可扩展的网络文件系统,相比其他分布式文件系统,GlusterFS具有高扩展性、高可用性、高性能、可横向扩展等特点,并且其没有元数据服务器的设计,让整个服务没有单点故障的隐患。

规划

  • 192.168.209.130 master-01
  • 192.168.209.131 master-02
  • 192.168.209.132 master-03

以上三台作为主控并作为GlusterFS集群节点。

添加hosts

[root@master-01 gluster]# cat /etc/hosts
192.168.209.130 master-01
192.168.209.131 master-02
192.168.209.132 master-03

安装GlusterFS

GlusterFS默认配置文件在/etc/glusterfs/glusterd.vol

工作目录默认在/var/lib/glusterfsd

[root@master-01 ~]#yum install -y centos-release-gluster glusterfs glusterfs-server glusterfs-fuse
[root@master-01 ~]#systemctl start glusterd
[root@master-01 ~]# systemctl status glusterd
● glusterd.service - GlusterFS, a clustered file-system server
Loaded: loaded (/usr/lib/systemd/system/glusterd.service; disabled; vendor preset: disabled)
Active: active (running) since 日 2019-04-28 13:20:08 CST; 2min 57s ago
Docs: man:glusterd(8)

集群配置

[root@master-01 ~]# gluster peer probe master-01
peer probe: success. Probe on localhost not needed
[root@master-01 ~]# gluster peer probe master-02
peer probe: success.
[root@master-01 ~]# gluster peer probe master-03
peer probe: success.

创建卷并启动

[root@master-01 ~]# gluster volume create gv0 replica 3 master-01:/opt/data/gv0 master-02:/opt/data/gv0 master-03:/opt/data/gv0 force
volume create: gv0: success: please start the volume to access data
[root@master-01 ~]# gluster volume start gv0
volume start: gv0: success
  • 默认不支持使用/分区作为存储路径,我这没有数据盘,所以使用force强制创建了。
  • /opt/data/gv0目录得先在三台机器上都创建。

查看节点和卷信息

[root@master-01 ~]# gluster peer status
Number of Peers: 2
Hostname: master-02
Uuid: 6f5a823c-4044-4dd9-95bc-129018b2eec0
State: Peer in Cluster (Connected)
Hostname: master-03
Uuid: dfeb6d81-c90b-4628-82f2-e379ed0ceded
State: Peer in Cluster (Connected)
[root@master-01 ~]# gluster volume info
Volume Name: gv0
Type: Replicate
Volume ID: 1891c9f8-8acb-48c2-94d9-29e2a625f9a0
Status: Started
Snapshot Count: 0
Number of Bricks: 1 x 3 = 3
Transport-type: tcp
Bricks:
Brick1: master-01:/opt/data/gv0
Brick2: master-02:/opt/data/gv0
Brick3: master-03:/opt/data/gv0
Options Reconfigured:
transport.address-family: inet
nfs.disable: on
performance.client-io-threads: off

查看端口监听

[root@master-01 ~]#  netstat -tunlp|grep glusterd
tcp 0 0 0.0.0.0:24007 0.0.0.0:* LISTEN 19533/glusterd

测试挂载

想要正常的在kubernetes集群中使用或者挂载glusterfs,集群中的对应节点都需要安装 glusterfs-fuse

[root@harbor-01 harbor]# yum install -y  centos-release-gluster  glusterfs-fuse
[root@master-01 ~]# mount -t glusterfs master-01:/gv0 /mnt
[root@master-01 ~]# df -h
...
master-01:/gv0 38G 13G 25G 34% /mnt

常用命令

#删除卷
gluster volume stop gv0
gluster volume delete gv0
#将机器移出集群
gluster peer detach master-01
#只允许172.28.0.0的网络访问glusterfs
gluster volume set gv0 auth.allow 172.28.26.*
gluster volume set gv0 auth.allow 192.168.222.1,192.168.*.*
#加入新的机器并添加到卷里(由于副本数设置为2,至少要添加2(4、6、8..)台机器)
gluster peer probe master-02
gluster peer probe master-03
#新加卷
gluster volume add-brick gv0 repl 2 master-02:/data/gluster master-03:/data/gluster force
#删除卷
gluster volume remove-brick gv0 repl 2 master-02:/opt/data/gv0 master-03:/opt/data/gv0 start
gluster volume remove-brick gv0 repl 2 master-02:/opt/data/gv0 master-03:/opt/data/gv0 status
gluster volume remove-brick gv0 repl 2 master-02:/opt/data/gv0 master-03:/opt/data/gv0 commit
注意:扩展或收缩卷时,也要按照卷的类型,加入或减少的brick个数必须满足相应的要求。
#当对卷进行了扩展或收缩后,需要对卷的数据进行重新均衡。
gluster volume rebalance mamm-volume start|stop|status
###########################################################
迁移卷---主要完成数据在卷之间的在线迁移
#启动迁移过程
gluster volume replace-brick gv0 master-02:/opt/data/gv0 master-02:/opt/test start force
#查看迁移状态
gluster volume replace-brick gv0 master-02:/opt/data/gv0 master-02:/opt/test status
#迁移完成后提交完成
gluster volume replace-brick gv0 master-02:/opt/data/gv0 master-02:/opt/test commit
#机器出现故障,执行强制提交
gluster volume replace-brick gv0 master-02:/opt/data/gv0 master-02:/opt/test commit force
###########################################################
触发副本自愈
gluster volume heal mamm-volume #只修复有问题的文件
gluster volume heal mamm-volume full #修复所有文件
gluster volume heal mamm-volume info #查看自愈详情
#####################################################
data-self-heal, metadata-self-heal and entry-self-heal
启用或禁用文件内容、文件元数据和目录项的自我修复功能,默认情况下三个全部是“on”。
#将其中的一个设置为off的范例:
gluster volume set gv0 entry-self-heal off

到这一步GlusterFS就部署完成了,接下来就在k8s使用它。

创建Endpoints

# ep 编排
[root@master-01 gluster]# cat gluster-ep.yaml
apiVersion: v1
kind: Endpoints
metadata:
name: glusterfs-cluster
subsets:
- addresses:
- ip: 192.168.209.130
- ip: 192.168.209.131
- ip: 192.168.209.132
ports:
- port: 1990
protocol: TCP
---
kind: Service
apiVersion: v1
metadata:
name: glusterfs-cluster
spec:
ports:
- port: 1990
# 开始创建
[root@master-01 gluster]# kubectl apply -f gluster-ep.yaml
endpoints/glusterfs-cluster created
service/glusterfs-cluster created
# 查看状态结果
[root@master-01 gluster]# kubectl get ep,svc|grep glusterfs
endpoints/glusterfs-cluster 192.168.209.130:1990,192.168.209.131:1990,192.168.209.132:1990 2m4s
service/glusterfs-cluster ClusterIP 10.254.120.33 <none> 1990/TCP 2m4s

创建Pv/Pvc

# 编排
[root@master-01 gluster]# cat gluster-pvc.yaml
---
apiVersion: v1
kind: PersistentVolume # pv
metadata:
name: test-pv
namespace: default
labels:
alicloud-pvname: test-pv
spec: # 定义pv属性
capacity: # 容量
storage: 2Gi # 存储容量
accessModes: # 访问模式,支持ReadWriteOnce、ReadOnlyMany和ReadWriteMany
- ReadWriteMany
glusterfs:
endpoints: 'glusterfs-cluster'
path: 'gv0'
readOnly: false
persistentVolumeReclaimPolicy: Recycle
---
kind: PersistentVolumeClaim # pvc
apiVersion: v1
metadata:
name: test-pvc
namespace: default
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
selector:
matchLabels:
alicloud-pvname: test-pv
# 开始创建
[root@master-01 gluster]# kubectl apply -f gluster-pvc.yaml
persistentvolume/dt-pv created
persistentvolumeclaim/dt-pvc created
# 查看结果状态
[root@master-01 gluster]# kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/test-pv 2Gi RWX Recycle Bound default/test-pvc 50s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/test-pvc Bound test-pv 2Gi RWX 50s

创建pod

# 编排
[root@master-01 gluster]# cat ng-deploy.yaml
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
labels:
run: nginx01
name: nginx01
spec:
replicas: 1
selector:
matchLabels:
app: nginx-pod1
template:
metadata:
labels:
app: nginx-pod1
spec:
containers:
- name: nginx11
image: nginx
imagePullPolicy: Always
volumeMounts:
- mountPath: /usr/share/nginx/html # 挂载到容器的路径
name: glu
restartPolicy: Always
volumes:
- name: glu 定义卷
persistentVolumeClaim:
claimName: test-pvc 使用pvc
---
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx1
name: nginx1
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 30003
selector:
app: nginx-pod1
type: NodePort # 创建
[root@master-01 gluster]# kubectl apply -f ng-deploy.yaml
deployment.apps/nginx01 created
service/nginx1 created

查看状态

[root@master-01 gluster]# kubectl  get pod,svc|grep nginx
pod/nginx01-7df547bf8c-cxvht 1/1 Running 0 10m
service/nginx1 NodePort 10.254.55.187 <none> 80:30003/TCP 79m [root@master-01 gluster]# kubectl describe po nginx01-7df547bf8c-cxvht
Name: nginx01-7df547bf8c-cxvht
......
Volumes:
glu:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: test-pvc
ReadOnly: false
default-token-tb5bg:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-tb5bg

测试

在glusterfs存储卷中创建index.html,并写入内容

[root@master-01 gluster]# vi  /opt/data/gv0/index.html
[root@master-01 gluster]# kubectl exec -ti nginx01-7df547bf8c-cxvht bash
root@nginx01-7df547bf8c-cxvht:/# ls /usr/share/nginx/html/
index.html
root@nginx01-7df547bf8c-cxvht:/# cat /usr/share/nginx/html/index.html
<h1>hello glusterfs</h1>

浏览器访问测试

现在就算pod 挂了等情况,数据也不会丢的,需要注意的是,上GlusterFS 一定得有人技术兜底,不然出了问题很麻烦的。

Kubernetes使用GlusterFS实现数据持久化的更多相关文章

  1. kubernetes资源使用glusterfs卷进行数据持久化

    1.GlusterFS部署 安装GlusterFS集群的主要目的是为k8s集群提供分布式持久化存储. GlusterFS部署使用2台服务器,服务名称与IP如下: 1 db-storagea 10.1. ...

  2. kubernetes的应用数据持久化

    1.无状态应用与有状态应用 应用的有状态和无状态是根据应用是否有持久化保存数据的需求而言的,即持久化保存数据的应用为有状态的应用,反之则为无状态的应用.常见的系统往往是有状态的应用,比如对于微博和微信 ...

  3. openshift 容器云从入门到崩溃之七《数据持久化》

    数据持久化常用的有两种: hostPath 挂载容器宿主机的本地文件夹,直接修改pod的配置 volumes: - hostPath: path: /data/logging-es type: '' ...

  4. Kubernetes 系列(六):持久化存储 PV与PVC

    在使用容器之后,我们需要考虑的另外一个问题就是持久化存储,怎么保证容器内的数据存储到我们的服务器硬盘上.这样容器在重建后,依然可以使用之前的数据.但是显然存储资源和 CPU 资源以及内存资源有很大不同 ...

  5. k8s集群,使用pvc方式实现数据持久化存储

    环境: 系统 华为openEulerOS(CentOS7) k8s版本 1.17.3 master 192.168.1.244 node1 192.168.1.245 介绍: 在Kubernetes中 ...

  6. k8s部署mysql数据持久化

    在这里我部署mysql的目的是为了后面将上一篇博客docker打包的el-admin镜像部署到k8s上,所以本文主要是部署mysql并实现持久化. 1.将我们的应用都部署到 el-admin 这个命名 ...

  7. Redis学习总结(1)——数据持久化

    以前研究Redis的时候,很多东西都不太明白,理解得也不太深,现在有时间重新拾起来看看,将一些心得记录下来,希望和大家一起探讨. 一.简介 Redis是一个单线程高可用的Key-Value存储系统,和 ...

  8. iOS之数据持久化方案

    概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) ...

  9. Docker数据持久化与容器迁移

    上节讲到当容器运行期间产生的数据是不会在写镜像里面的,重新用此镜像启动新的容器就会初始化镜像,会加一个全新的读写入层来保存数据.如果想做到数据持久化,Docker提供数据卷(Data volume)或 ...

随机推荐

  1. 2018.10.19 bzoj1584: Cleaning Up 打扫卫生(线性dp)

    传送门 dp妙题. 考虑到每个位置分一组才花费nnn的贡献. 因此某一段不同的数的个数不能超过sqrt(n)sqrt(n)sqrt(n),于是对于当前的位置iii我们记pos[j]pos[j]pos[ ...

  2. 数塔问题-hdu-2084(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2084 思路:要求从顶到底的最大值,可以反过来考虑,从底部向上. 只有下面一行的最大值确定,这一行的最大 ...

  3. linux中nfs启动报rpcbind.socket failed to listen on sockets: Address family not supported by protocol

    1.systemctl start rpcbind.service 报错: [root@autodeploy ~]# journalctl -xe -- Support: http://lists.f ...

  4. =delete(c++11)

    1.为什么要阻止类对象的拷贝? 1)有些类,不需要拷贝和赋值运算符,如:IO类,以避免多个拷贝对象写入或读取相同的IO缓冲 2.如何阻止? 1)不定义拷贝构造函数和拷贝赋值运算符时,好心的编译器也会及 ...

  5. Initialization of bean failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'dataSource' of bean class [com.liuyang.jdbc.PersonDao]: No property 'dataSource

    这个错误是说我的启动失败了.这类问题要从配置文件中开始找原因,我用的是spring框架,所以我从application.中找的原因 然后对比路径,对比文件的命名和id,都没有问题,为什么会在启动的时候 ...

  6. HDU 2161 Primes (素数筛选法)

    题意:输入一个数判断是不是素数,并规定2不是素数. 析:一看就很简单吧,用素数筛选法,注意的是结束条件是n<0,一开始被坑了... 不说了,直接上代码: #include <iostrea ...

  7. HDU 1050 Moving Tables (贪心)

    题意:在一个走廊两边都有对称分布的连续房间,现在有n张桌子需要从a移动到b房间.每次移动需要10分钟, 但是如果两次移动中需要经过相同的走廊位置,则不能同时进行,需要分开移动.最后求最少需要多长时间移 ...

  8. 使用vbs给PPT(包括公式)去背景

    在 视图—>宏 内新建宏 '终极版 Sub ReColor() Dim sld As Slide Dim sh As Shape For Each sld In ActivePresentati ...

  9. python编码(六)

    1. 字符编码简介 1.1. ASCII ASCII(American Standard Code for Information Interchange),是一种单字节的编码.计算机世界里一开始只有 ...

  10. 安装hue-3.11.0

    1) 配置HDFS HttpFS和WebHDFS 如果HDFS是HA方式部署的,则只能使用HttpFS,而不能用WebHDFS. 2) 安装依赖: apr-iconv-1.2.1 confuse-3. ...