Kubernetes使用Glusterfs做存儲持久化

GlusterFS

GlusterFS是一個開源的橫向擴展文件系統。這些示例提供有關如何允許容器使用GlusterFS卷的信息。

該示例假定您已經設置了GlusterFS服務器集羣,並且已準備好在容器中使用正在運行的GlusterFS卷。

先決條件
Kubernetes集羣已經搭建好。

Glusterfs集羣的安裝
環境介紹
OS系統:Centos 7.x
Glusterfs兩個節點:192.168.22.21,192.168.22.22

  1. 安裝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
  1. 配置 glusterfs
[root@k8s-glusterfs-01 ~]# vi /etc/hosts
192.168.22.21   k8s-glusterfs-01
192.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 status
Number of Peers: 1

Hostname: k8s-glusterfs-02
Uuid: b80f012b-cbb6-469f-b302-0722c058ad45
State: Peer in Cluster (Connected)
  1. 配置 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 force
volume create: models: success: please start the volume to access data
# 查看volume狀態
[root@k8s-glusterfs-01 ~]# gluster volume info

Volume Name: k8s-volume
Type: Distribute
Volume ID: 340d94ee-7c3d-451d-92c9-ad0e19d24b7d
Status: Created
Snapshot Count: 0
Number of Bricks: 1 x 2 = 2
Transport-type: tcp
Bricks:
Brick1: k8s-glusterfs-01:/opt/gfs_data
Brick2: k8s-glusterfs-02:/opt/gfs_data
Options Reconfigured:
transport.address-family: inet
nfs.disable: on
performance.client-io-threads: off

啓動volume

# gluster volume start k8s-volume
  1. 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-volume
k8s-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上操作!

  1. 第一步在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集羣中節點的地址。可以在port字段中提供任何有效值(從1到65535)。

##創建端點:
[root@k8s-master-01 ~]# kubectl create -f  glusterfs-endpoints.json

##驗證是否已成功創建端點
[root@k8s-master-01 ~]# kubectl get ep |grep glusterfs-cluster
glusterfs-cluster   192.168.22.21:20,192.168.22.22:20
  1. 配置 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-cluster
glusterfs-cluster   ClusterIP   10.68.114.26   <none>        20/TCP    6m
  1. 配置PersistentVolume(簡稱pv)

創建glusterfs-pv.yaml文件,指定storage容量和讀寫屬性

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv001
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  glusterfs:
    endpoints: "glusterfs-cluster"
    path: "k8s-volume"
    readOnly: false

然後執行:

[root@k8s-master-01 ~]# kubectl create -f glusterfs-pv.yaml
kubectl get pv
NAME      CAPACITY   ACCESS  MODES   RECLAIM POLICY   STATUS    CLAIM            STORAGECLASS   REASON    AGE
pv001      10Gi             RWX         Retain       Bound                                 
  1. 配置PersistentVolumeClaim(簡稱pvc)
    創建glusterfs-pvc.yaml文件,指定請求資源大小
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: pvc001
    spec:
    accessModes:
    - ReadWriteMany
    resources:
    requests:
      storage: 2Gi

執行:

[root@k8s-master-01 ~]# kubectl create -f glusterfs-pvc.yaml

[root@k8s-master-01 ~]# kubectl get pvc
NAME      STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc001    Bound     pv001     10Gi       RWX                           1h
  1. 部署應用掛載pvc
    以創建nginx,把pvc掛載到容器內的/usr/share/nginx/html文件夾爲例:

nginx_deployment.yaml文件如下

apiVersion: extensions/v1beta1 
kind: Deployment 
metadata: 
  name: nginx-dm
spec: 
  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 pods
NAME                          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-volume
192.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

至此部署完成。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章