kubernetes實踐之二十六:GlusterFS

百聯達發表於2018-04-27
一:前言
GlusterFS是Scale-Out儲存解決方案Gluster的核心,它是一個開源的分散式檔案系統,具有強大的橫向擴充套件能力,透過擴充套件能夠支援數PB儲存容量和處理數千客戶端。GlusterFS藉助TCP/IP或InfiniBand RDMA網路將物理分佈的儲存資源聚集在一起,使用單一全域性名稱空間來管理資料。GlusterFS基於可堆疊的使用者空間設計,可為各種不同的資料負載提供優異的效能。

伺服器:
10.116.137.196          k8s_master
10.116.82.28             k8s_node1
10.116.36.57             k8s_node2

二:安裝glusterfs
我們直接在物理機上使用yum安裝,在三臺伺服器上分別執行以下安裝命令。

1.先安裝 gluster 源
yum install centos-release-gluster -y

2.安裝 glusterfs 元件
yum install -y glusterfs glusterfs-server glusterfs-fuse glusterfs-rdma glusterfs-geo-replication glusterfs-devel

3.建立 glusterfs 目錄
mkdir /mnt/glusterd

4.修改 glusterd 目錄

點選(此處)摺疊或開啟

  1. volume management
  2.     type mgmt/glusterd
  3.     option working-directory /mnt/glusterd
  4.     option transport-type socket,rdma
  5.     option transport.socket.keepalive-time 10
  6.     option transport.socket.keepalive-interval 2
  7.     option transport.socket.read-fail-log off
  8.     option ping-timeout 0
  9.     option event-threads 1
  10. # option lock-timer 180
  11. # option transport.address-family inet6
  12. # option base-port 49152
  13. end-volume
5.啟動glusterfs

systemctl enable glusterd.service
systemctl start glusterd.service
systemctl status glusterd.service


三:
配置 glusterfs

1.開放埠
iptables -I INPUT -p tcp --dport 24007 -j ACCEPT

2.建立儲存目錄
mkdir /opt/gfs_data

3.新增節點到叢集
10.116.137.196上執行以下操作:
gluster peer probe 10.116.82.28
gluster peer probe 10.116.36.57

4.檢視叢集狀態
gluster peer status

點選(此處)摺疊或開啟

  1. Number of Peers: 2

  2. Hostname: 10.116.82.28
  3. Uuid: f73138ca-e32e-4d87-a99d-cf842fc29447
  4. State: Peer in Cluster (Connected)

  5. Hostname: 10.116.36.57
  6. Uuid: 18e22d2c-049b-4b0c-8cc7-2560319e6c05
  7. State: Peer in Cluster (Connected)
  8. [root@iZwz95trb3stk6afg8oozuZ kubernetes]# clear
  9. [root@iZwz95trb3stk6afg8oozuZ kubernetes]# gluster volume create k8s-volume transport tcp 10.116.137.196:/mnt/gfs_data 10.116.82.28:/mnt/gfs_data 10.116.36.57:/mnt/gfs_data force
  10. volume create: k8s-volume: success: please start the volume to access data
  11. [root@iZwz95trb3stk6afg8oozuZ kubernetes]# gluster volume info
  12.  
  13. Volume Name: k8s-volume
  14. Type: Distribute
  15. Volume ID: 62900029-02c9-4870-951c-38fafd5f5d9b
  16. Status: Created
  17. Snapshot Count: 0
  18. Number of Bricks: 3
  19. Transport-type: tcp
  20. Bricks:
  21. Brick1: 10.116.137.196:/mnt/gfs_data
  22. Brick2: 10.116.82.28:/mnt/gfs_data
  23. Brick3: 10.116.36.57:/mnt/gfs_data
  24. Options Reconfigured:
  25. transport.address-family: inet
  26. nfs.disable: on

四:
配置 volume

1.volume的模式
a.分佈卷(預設模式):即DHT, 也叫 分佈卷: 將檔案已hash演算法隨機分佈到 一臺伺服器節點中儲存。
b.複製模式:即AFR, 建立volume 時帶 replica x 數量: 將檔案複製到 replica x 個節點中。
c.條帶模式:即Striped, 建立volume 時帶 stripe x 數量: 將檔案切割成資料塊,分別儲存到 stripe x 個節點中 ( 類似raid 0 )。
d.分散式條帶模式:最少需要4臺伺服器才能建立。 建立volume 時 stripe 2 server = 4 個節點: 是DHT 與 Striped 的組合型。
e.分散式複製模式:最少需要4臺伺服器才能建立。 建立volume 時 replica 2 server = 4 個節點:是DHT 與 AFR 的組合型。
f.條帶複製卷模式:最少需要4臺伺服器才能建立。 建立volume 時 stripe 2 replica 2 server = 4 個節點: 是 Striped 與 AFR 的組合型。
g.三種模式混合: 至少需要8臺 伺服器才能建立。 stripe 2 replica 2 , 每4個節點 組成一個 組。

2.建立分佈卷
gluster volume create k8s-volume transport tcp 10.116.137.196:/mnt/gfs_data 10.116.82.28:/mnt/gfs_data 10.116.36.57:/mnt/gfs_data force

3.
檢視volume狀態
gluster volume info

點選(此處)摺疊或開啟

  1. Volume Name: k8s-volume
  2. Type: Distribute
  3. Volume ID: 62900029-02c9-4870-951c-38fafd5f5d9b
  4. Status: Created
  5. Snapshot Count: 0
  6. Number of Bricks: 3
  7. Transport-type: tcp
  8. Bricks:
  9. Brick1: 10.116.137.196:/mnt/gfs_data
  10. Brick2: 10.116.82.28:/mnt/gfs_data
  11. Brick3: 10.116.36.57:/mnt/gfs_data
  12. Options Reconfigured:
  13. transport.address-family: inet
  14. nfs.disable: on
4.啟動分佈卷
gluster volume start k8s-volume

五:
Glusterfs調優

1.開啟指定volume的配額
gluster volume quota k8s-volume enable
2.限制指定volume的配額
gluster volume quota k8s-volume limit-usage / 5GB
3.設定cache大小, 預設32MB
gluster volume set k8s-volume performance.cache-size 64MB
4.設定io執行緒, 太大會導致程式崩潰

gluster volume set k8s-volume performance.io-thread-count 16
5.設定網路檢測時間, 預設42s
gluster volume set k8s-volume network.ping-timeout 10
6.設定寫緩衝區的大小, 預設1M
gluster volume set k8s-volume performance.write-behind-window-size 512MB

六:
Kubernetes中配置glusterfs

1.
配置 endpoints(glusterfs-endpoints.json)

點選(此處)摺疊或開啟

  1. {
  2.   "kind": "Endpoints",
  3.   "apiVersion": "v1",
  4.   "metadata": {
  5.     "name": "glusterfs-cluster"
  6.   },
  7.   "subsets": [
  8.     {
  9.       "addresses": [
  10.         {
  11.           "ip": "10.116.137.196"
  12.         }
  13.       ],
  14.       "ports": [
  15.         {
  16.           "port": 1990
  17.         }
  18.       ]
  19.     }
  20.   ]
  21. }
kubectl apply -f glusterfs-endpoints.json

2.
配置 service(glusterfs-service.json)

點選(此處)摺疊或開啟

  1. {
  2.   "kind": "Service",
  3.   "apiVersion": "v1",
  4.   "metadata": {
  5.     "name": "glusterfs-cluster"
  6.   },
  7.   "spec": {
  8.     "ports": [
  9.       {"port": 1990}
  10.     ]
  11.   }
  12. }
kubectl apply -f glusterfs-service.json

3.
建立測試 pod

點選(此處)摺疊或開啟

  1. {
  2.     "apiVersion": "v1",
  3.     "kind": "Pod",
  4.     "metadata": {
  5.         "name": "glusterfs"
  6.     },
  7.     "spec": {
  8.         "containers": [
  9.             {
  10.                 "name": "glusterfs",
  11.                 "image": "registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0",
  12.                 "volumeMounts": [
  13.                     {
  14.                         "mountPath": "/mnt/glusterfs",
  15.                         "name": "glusterfsvol"
  16.                     }
  17.                 ]
  18.             }
  19.         ],
  20.         "volumes": [
  21.             {
  22.                 "name": "glusterfsvol",
  23.                 "glusterfs": {
  24.                     "endpoints": "glusterfs-cluster",
  25.                     "path": "k8s-volume",
  26.                     "readOnly": true
  27.                 }
  28.             }
  29.         ]
  30.     }
  31. }
kubectl apply -f glusterfs-pod.json   在Pod所在的Node節點上執行 df -h 可以檢視k8s-volume掛載目錄

4.
配置PersistentVolume(glusterfs-pv.yaml)

點選(此處)摺疊或開啟

  1. apiVersion: v1
  2. kind: PersistentVolume
  3. metadata:
  4.   name: gluster-dev-volume
  5. spec:
  6.   capacity:
  7.     storage: 1Gi
  8.   accessModes:
  9.     - ReadWriteMany
  10.   glusterfs:
  11.     endpoints: "glusterfs-cluster"
  12.     path: "k8s-volume"
  13.     readOnly: false
kubectl apply -f glusterfs-pv.yaml

5.
配置PVC(glusterfs-pvc.yaml)

點選(此處)摺疊或開啟

  1. kind: PersistentVolumeClaim
  2. apiVersion: v1
  3. metadata:
  4.   name: glusterfs-nginx
  5. spec:
  6.   accessModes:
  7.     - ReadWriteMany
  8.   resources:
  9.     requests:
  10.       storage: 1Gi
kubectl apply -f glusterfs-pvc.yaml

6.
建立nginx deployment 掛載 volume(nginx-deployment.yaml)

點選(此處)摺疊或開啟

  1. apiVersion: extensions/v1beta1
  2. kind: Deployment
  3. metadata:
  4.   name: nginx-dm
  5. spec:
  6.   replicas: 2
  7.   template:
  8.     metadata:
  9.       labels:
  10.         name: nginx
  11.     spec:
  12.       containers:
  13.         - name: nginx
  14.           image: nginx:latest
  15.           imagePullPolicy: IfNotPresent
  16.           ports:
  17.             - containerPort: 80
  18.           volumeMounts:
  19.             - name: gluster-dev-volume
  20.               mountPath: "/usr/share/nginx/html"
  21.       volumes:
  22.       - name: gluster-dev-volume
  23.         persistentVolumeClaim:
  24.           claimName: glusterfs-nginx
kubectl apply -f nginx-deployment.yaml

a.檢視 deployment
kubectl get pods |grep nginx-dm

點選(此處)摺疊或開啟

  1. nginx-dm-64dcbb8d55-pp9vn 1/1 Running 0 1h
  2. nginx-dm-64dcbb8d55-sclj6 1/1 Running 0 1h
b.檢視掛載
kubectl exec -it nginx-dm-64dcbb8d55-pp9vn -- df -h

點選(此處)摺疊或開啟

  1. Filesystem Size Used Avail Use% Mounted on
  2. overlay 40G 23G 16G 60% /
  3. tmpfs 3.7G 0 3.7G 0% /dev
  4. tmpfs 3.7G 0 3.7G 0% /sys/fs/cgroup
  5. /dev/xvda1 40G 23G 16G 60% /etc/hosts
  6. shm 64M 0 64M 0% /dev/shm
  7. 10.116.137.196:k8s-volume 5.0G 0 5.0G 0% /usr/share/nginx/html
  8. tmpfs 3.7G 12K 3.7G 1% /run/secrets/kubernetes.io/serviceaccount
c.建立檔案測試
kubectl exec -it nginx-dm-64dcbb8d55-pp9vn -- touch /usr/share/nginx/html/index.html
kubectl exec -it nginx-dm-64dcbb8d55-sclj6  -- ls -lt /usr/share/nginx/html/index.html

d.驗證 glusterfs
因為我們使用分佈卷,所以可以看到某個節點中有檔案
ls /mnt/gfs_data/
index.html


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28624388/viewspace-2153444/,如需轉載,請註明出處,否則將追究法律責任。

相關文章