Kubernetes使用Glusterfs做存儲持久化
GlusterFS是一個開源的橫向擴展文件系統。這些示例提供有關如何允許容器使用GlusterFS卷的信息。
該示例假定您已經設置了GlusterFS服務器集群,并且已準備好在容器中使用正在運行的GlusterFS卷。
先決條件
Kubernetes集群已經搭建好。
Glusterfs集群的安裝
環境介紹
OS系統:Centos 7.x
Glusterfs兩個節點:192.168.22.21,192.168.22.22
安裝glusterfs
我們直接在物理機上使用yum安裝,如果你選擇在kubernetes上安裝,請參考:
https://github.com/gluster/gluster-kubernetes/blob/master/docs/setup-guide.md
#?先安裝?gluster?源[root@k8s-glusterfs-01?~]#?yum?install?centos-release-gluster?-y#?安裝?glusterfs?組件[root@k8s-glusterfs-01?~]#?yum?install?-y?glusterfs?glusterfs-server?glusterfs-fuse?glusterfs-rdma?glusterfs-geo-replication?glusterfs-devel##創建?glusterfs?目錄[root@k8s-glusterfs-01?~]#?mkdir?/opt/glusterd#?修改?glusterd?目錄[root@k8s-glusterfs-01?~]#?sed?-i?'s/var\/lib/opt/g'?/etc/glusterfs/glusterd.vol#?啟動?glusterfs[root@k8s-glusterfs-01?~]#?systemctl?start?glusterd.service#?設置開機啟動[root@k8s-glusterfs-01?~]#?systemctl?enable?glusterd.service#查看狀態[root@k8s-glusterfs-01?~]#?systemctl?status?glusterd.service
配置 glusterfs
[root@k8s-glusterfs-01?~]#?vi?/etc/hosts192.168.22.21???k8s-glusterfs-01192.168.22.22???k8s-glusterfs-02 ?如果開啟了防火墻則?開放端口 [root@k8s-glusterfs-01?~]#?iptables?-I?INPUT?-p?tcp?--dport?24007?-j?ACCEPT ?創建存儲目錄 [root@k8s-glusterfs-01?~]#?mkdir?/opt/gfs_data ?添加節點到?集群?執行操作的本機不需要probe?本機 [root@k8s-master-01?~]#?gluster?peer?probe?k8s-glusterfs-02查看集群狀態 [root@k8s-glusterfs-01?~]#?gluster?peer?statusNumber?of?Peers:?1Hostname:?k8s-glusterfs-02Uuid:?b80f012b-cbb6-469f-b302-0722c058ad45 State:?Peer?in?Cluster?(Connected)
配置 volume
GlusterFS 幾種volume 模式說明
1)、默認模式,既DHT, 也叫 分布卷: 將文件已hash算法隨機分布到 一臺服務器節點中存儲。
命令格式:gluster volume create test-volume server1:/exp1 server2:/exp2
2)、復制模式,既AFR, 創建volume 時帶 replica x 數量: 將文件復制到 replica x 個節點中。
命令格式:gluster volume create test-volume replica 2 transport tcp server1:/exp1 server2:/exp2
3)、條帶模式,既Striped, 創建volume 時帶 stripe x 數量: 將文件切割成數據塊,分別存儲到 stripe x 個節點中 ( 類似raid 0 )。
命令格式:gluster volume create test-volume stripe 2 transport tcp server1:/exp1 server2:/exp2
4)、分布式條帶模式(組合型),最少需要4臺服務器才能創建。 創建volume 時 stripe 2 server = 4 個節點: 是DHT 與 Striped 的組合型。
命令格式:gluster volume create test-volume stripe 2 transport tcp server1:/exp1 server2:/exp2 server3:/exp3 server4:/exp4
5)、分布式復制模式(組合型), 最少需要4臺服務器才能創建。 創建volume 時 replica 2 server = 4 個節點:是DHT 與 AFR 的組合型。
命令格式:gluster volume create test-volume replica 2 transport tcp server1:/exp1 server2:/exp2 server3:/exp3 server4:/exp4
6)、條帶復制卷模式(組合型), 最少需要4臺服務器才能創建。 創建volume 時 stripe 2 replica 2 server = 4 個節點: 是 Striped 與 AFR 的組合型。
命令格式:gluster volume create test-volume stripe 2 replica 2 transport tcp server1:/exp1 server2:/exp2 server3:/exp3 server4:/exp4
7)、三種模式混合, 至少需要8臺 服務器才能創建。 stripe 2 replica 2 , 每4個節點 組成一個 組。
命令格式:gluster volume create test-volume stripe 2 replica 2 transport tcp server1:/exp1 server2:/exp2 server3:/exp3 server4:/exp4 server5:/exp5 server6:/exp6 server7:/exp7 server8:/exp8
#創建GlusterFS磁盤:?[root@k8s-glusterfs-01?~]#?gluster?volume?create?models?replica?2?k8s-glusterfs-01:/opt/gfs_data??k8s-glusterfs-02:/opt/gfs_data?forcevolume?create:?models:?success:?please?start?the?volume?to?access?data
#?查看volume狀態[root@k8s-glusterfs-01?~]#?gluster?volume?infoVolume?Name:?k8s-volumeType:?DistributeVolume?ID:?340d94ee-7c3d-451d-92c9-ad0e19d24b7dStatus:?CreatedSnapshot?Count:?0 Number?of?Bricks:?1?x?2?=?2Transport-type:?tcpBricks:Brick1:?k8s-glusterfs-01:/opt/gfs_dataBrick2:?k8s-glusterfs-02:/opt/gfs_dataOptions?Reconfigured:transport.address-family:?inetnfs.disable:?onperformance.client-io-threads:?off
Glusterfs調優
開啟?指定?volume?的配額 gluster?volume?quota?k8s-volume?enable 限制?指定?volume?的配額 gluster?volume?quota?k8s-volume?limit-usage?/?1TB 設置?cache?大小,?默認32MB gluster?volume?set?k8s-volume?performance.cache-size?4GB 設置?io?線程,?太大會導致進程崩潰 gluster?volume?set?k8s-volume?performance.io-thread-count?16設置?網絡檢測時間,?默認42s gluster?volume?set?k8s-volume?network.ping-timeout?10設置?寫緩沖區的大小,?默認1M gluster?volume?set?k8s-volume?performance.write-behind-window-size?1024MB
客戶端使用Glusterfs
物理機上使用Gluster的volume
[root@k8s-master-01?~]#?yum?install?-y?glusterfs?glusterfs-fuse[root@k8s-master-01?~]#?mkdir?-p?/opt/gfsmnt[root@k8s-master-01?~]#?mount?-t?glusterfs?k8s-glusterfs-01:k8s-volume?/opt/gfsmnt/##df查看掛載狀態:[root@k8s-master-01?~]#?df?-h?|grep?k8s-volumek8s-glusterfs-01:k8s-volume???46G??1.6G???44G????4%?/opt/gfsmnt
Kubernetes配置使用glusterfs:
官方文檔對配置過程進行了介紹:https://github.com/kubernetes/examples/blob/master/staging/volumes/glusterfs/README.md
注:以下操作在kubernetes集群中任意一個可以執行kubectl的master上操作!
第一步在Kubernetes中創建GlusterFS端點定義
這是glusterfs-endpoints.json的片段:
"{ "kind":?"Endpoints", "apiVersion":?"v1", "metadata":?{ "name":?"glusterfs-cluster" }, "subsets":?[ { ??"addresses":?[ ????{ ??????"ip":?"192.168.22.21" ????} ??], ??"ports":?[ ????{ ??????"port":?20 ????} ??] }, { ??"addresses":?[ ????{ ??????"ip":?"192.168.22.22" ????} ??], ??"ports":?[ ????{ ??????"port":?20 ????} ??] } ] }
備:該subsets字段應填充GlusterFS集群中節點的地址??梢栽趐ort字段中提供任何有效值(從1到65535)。
##創建端點:[root@k8s-master-01?~]#?kubectl?create?-f??glusterfs-endpoints.json##驗證是否已成功創建端點[root@k8s-master-01?~]#?kubectl?get?ep?|grep?glusterfs-clusterglusterfs-cluster???192.168.22.21:20,192.168.22.22:20
配置 service
我們還需要為這些端點創建服務,以便它們能夠持久存在。我們將在沒有選擇器的情況下添加此服務,以告知Kubernetes我們想要手動添加其端點
[root@k8s-master-01?]#?cat?glusterfs-service.json{"kind":?"Service","apiVersion":?"v1","metadata":?{"name":?"glusterfs-cluster"},"spec":?{"ports":?[ ??{"port":?20} ] } }
##創建服務
[root@k8s-master-01?]#?kubectl?create?-f??glusterfs-service.json##查看service[root@k8s-master-01?]#?kubectl?get?service?|?grep?glusterfs-clusterglusterfs-cluster???ClusterIP???10.68.114.26???
配置PersistentVolume(簡稱pv)
創建glusterfs-pv.yaml文件,指定storage容量和讀寫屬性
apiVersion:?v1kind:?PersistentVolumemetadata: ??name:?pv001spec: ??capacity: ????storage:?10Gi ??accessModes: ????-?ReadWriteMany ??glusterfs: ????endpoints:?"glusterfs-cluster" ????path:?"k8s-volume" ????readOnly:?false
然后執行:
[root@k8s-master-01?~]#?kubectl?create?-f?glusterfs-pv.yamlkubectl?get?pv NAME??????CAPACITY???ACCESS??MODES???RECLAIM?POLICY???STATUS????CLAIM????????????STORAGECLASS???REASON????AGE pv001??????10Gi?????????????RWX?????????Retain???????Bound
配置PersistentVolumeClaim(簡稱pvc)
創建glusterfs-pvc.yaml文件,指定請求資源大小
apiVersion:?v1kind:?PersistentVolumeClaimmetadata:name:?pvc001spec:accessModes:-?ReadWriteManyresources:requests: ??storage:?2Gi
執行:
[root@k8s-master-01?~]#?kubectl?create?-f?glusterfs-pvc.yaml[root@k8s-master-01?~]#?kubectl?get?pvcNAME??????STATUS????VOLUME????CAPACITY???ACCESS?MODES???STORAGECLASS???AGE pvc001????Bound?????pv001?????10Gi???????RWX???????????????????????????1h
部署應用掛載pvc
以創建nginx,把pvc掛載到容器內的/usr/share/nginx/html文件夾為例:
nginx_deployment.yaml文件如下
apiVersion:?extensions/v1beta1?kind:?Deployment?metadata:? ??name:?nginx-dmspec:? ??replicas:?2 ??template:? ????metadata:? ??????labels:? ????????name:?nginx? ????spec:? ??????containers:? ????????-?name:?nginx? ??????????image:?nginx ??????????ports:? ????????????-?containerPort:?80 ??????????volumeMounts: ????????????-?name:?storage001 ??????????????mountPath:?"/usr/share/nginx/html" ??????volumes: ??????-?name:?storage001 ????????persistentVolumeClaim: ??????????claimName:?pvc001
執行:
[root@k8s-master-01?~]#?kubectl?create?-f?nginx_deployment.yaml查看nginx是否部署成功? [root@k8s-master-01?~]#?kubectl?get?podsNAME??????????????????????????READY?????STATUS????RESTARTS???AGE nginx-dm-5fbdb54795-77f7v?????1/1???????Running???0??????????1h nginx-dm-5fbdb54795-rnqwd?????1/1???????Running???0??????????1h 查看掛載: [root@k8s-master-01?~]#?kubectl?exec?-it?nginx-dm-5fbdb54795-77f7v??--?df?-h?|grep?k8s-volume192.168.22.21:k8s-volume???46G??1.6G???44G???4%?/usr/share/nginx/html創建文件: [root@k8s-master-01?~]#?kubectl?exec?-it?nginx-dm-5fbdb54795-77f7v?--?touch?/usr/share/nginx/html/123.txt查看文件屬性: [root@k8s-master-01?~]#?kubectl?exec?-it?nginx-dm-5fbdb54795-77f7v?--?ls?-lt??/usr/share/nginx/html/123.txt?-rw-r--r--?1?root?root?0?Jul??9?06:25?/usr/share/nginx/html/123.txt
再回到glusterfs的服務器的數據目錄/opt/gfs_data查看是否有123.txt文件
##192.168.22.21上查看:[root@k8s-glusterfs-01?~]#?ls?-lt?/opt/gfs_data/總用量?0-rw-r--r--?2?root?root?0?7月???9?14:25?123.txt##192.168.22.22上查看:[root@k8s-glusterfs-02?~]#?ls?-lt?/opt/gfs_data/總用量?0-rw-r--r--?2?root?root?0?7月???9?14:25?123.txt
至此部署完成。
Kubernetes
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。