Centos7下Kubernetes配置使用GlusterFS

操作环境

网络拓扑



环境说明:

OS:
[html]  view plain  copy
  1. CentOS Linux release 7.4.1708   
docker:

[html]  view plain  copy
  1. Client:  
  2.  Version:         1.12.6  
  3.  API version:     1.24  
  4.  Package version: docker-1.12.6-68.gitec8512b.el7.centos.x86_64  
  5.  Go version:      go1.8.3  
  6.  Git commit:      ec8512b/1.12.6  
  7.  Built:           Mon Dec 11 16:08:42 2017  
  8.  OS/Arch:         linux/amd64  
  9.   
  10. Server:  
  11.  Version:         1.12.6  
  12.  API version:     1.24  
  13.  Package version: docker-1.12.6-68.gitec8512b.el7.centos.x86_64  
  14.  Go version:      go1.8.3  
  15.  Git commit:      ec8512b/1.12.6  
  16.  Built:           Mon Dec 11 16:08:42 2017  
  17.  OS/Arch:         linux/amd64  
kubernetes:

[html]  view plain  copy
  1. Kubernetes v1.5.2  
glusterfs:

[html]  view plain  copy
  1. glusterfs 3.12.3  

操作步骤

    Kubernetes环境已经完成部署完成,部署步骤参照  Centos7 下Kubernetes集群安装部署

部署Glusterfs

    这里有5台服务器,10.10.200.226、10.10.200.227作为GlusterFS存储服务器,10.10.200.224、

10.10.200.229、10.10.200.230调用GlusterFS,同样也需要安装Gluster。

1.分别在5台服务器上安装Gluster

[html]  view plain  copy
  1. [root@k8s-master ~]#yum -y install centos-release-gluster  
  2. [root@k8s-master ~]#yum -y install glusterfs glusterfs-fuse glusterfs-server  
2.启动Glusterfs


[html]  view plain  copy
  1. [root@k8s-master ~]#systemctl enable glusterd  
  2. [root@k8s-master ~]#systemctl start glusterd  
3.创建GlusterFS Cluster

编辑/etc/hosts文件

[html]  view plain  copy
  1. [root@k8s-master ~]# vi /etc/hosts  
  2. 127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4  
  3. ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6  
  4. 10.10.200.226   glusterfs-node1  
  5. 10.10.200.227   glusterfs-node2  
  6. 10.10.200.224   k8s-master  
  7. 10.10.200.229   k8s-node1  
  8. 10.10.200.230   k8s-node2  
选择1台服务器添加其他服务器

[html]  view plain  copy
  1. [root@gluster-node1 ~]#gluster peer probe k8s-master  
  2. [root@gluster-node1 ~]#gluster peer probe k8s-node1  
  3. [root@gluster-node1 ~]#gluster peer probe k8s-node2  
  4. [root@gluster-node1 ~]#gluster peer probe gluster-node2  
查看gluster节点状态

[html]  view plain  copy
  1. [root@k8s-master yaml]# gluster peer status  
  2. Number of Peers: 4  
  3.   
  4. Hostname: glusterfs-node2  
  5. Uuid: 3c5c5994-0c05-4be0-9ec3-a431f193a1f0  
  6. State: Peer in Cluster (Connected)  
  7.   
  8. Hostname: glusterfs-node1  
  9. Uuid: 31ac3dff-94a3-4166-991b-bf4727113539  
  10. State: Peer in Cluster (Connected)  
  11.   
  12. Hostname: 10.10.200.229  
  13. Uuid: 7c2f03fe-e2cb-412e-a91f-2baf873610ef  
  14. State: Peer in Cluster (Connected)  
  15. Other names:  
  16. 10.10.200.229  
  17.   
  18. Hostname: 10.10.200.230  
  19. Uuid: 314a7676-d1eb-4c58-9617-8c510c93961d  
  20. State: Peer in Cluster (Connected)  


3.创建volume

这里只选择10.10.200.226/10.10.200.227来进行创建volume


[html]  view plain  copy
  1. [root@gluster-node1 brick1]# gluster volume create mysql-volume replica 2 glusterfs-node2:/data/brick1/mysql-volume/ glusterfs-node1:/data/brick1/mysql-volume/  
启动mysql-volume


[html]  view plain  copy
  1. [root@gluster-node1 brick1]# gluster volume start mysql-volume  
  2. volume start: mysql-volume: success  

查看volume状态

[html]  view plain  copy
  1. [root@gluster-node1 brick1]# gluster volume status  
  2. Status of volume: mysql-volume  
  3. Gluster process                             TCP Port  RDMA Port  Online  Pid  
  4. ------------------------------------------------------------------------------  
  5. Brick glusterfs-node2:/data/brick1/mysql-vo  
  6. lume                                        49152     0          Y       2976   
  7. Brick glusterfs-node1:/data/brick1/mysql-vo  
  8. lume                                        49152     0          Y       7835   
  9. Self-heal Daemon on localhost               N/A       N/A        Y       7856   
  10. Self-heal Daemon on k8s-master              N/A       N/A        Y       2025   
  11. Self-heal Daemon on glusterfs-node2         N/A       N/A        Y       2997   
  12.    
  13. Task Status of Volume mysql-volume  
  14. ------------------------------------------------------------------------------  
  15. There are no active volume tasks  
到这里就完成了2台Glusterfs存储服务器的配置

配置Kubernetes

    可以在https://github.com/kubernetes/kubernetes/tree/master/examples/volumes/glusterfs上面找

到kuernetes配置gluster的文件glusterfs-endpoint.json/glusterfs-pod.json/glusterfs-services.json。

     这里只需要glusterfs-endpoint.json,glusterfs-service.json。

修改glusterfs-endpoint.json文件如下,主要是ip地址的修改

[html]  view plain  copy
  1. [root@k8s-master yaml]# vi glusterfs-endpoints.json   
  2. {  
  3.   "kind": "Endpoints",  
  4.   "apiVersion": "v1",  
  5.   "metadata": {  
  6.     "name": "glusterfs-cluster"  
  7.   },  
  8.   "subsets": [  
  9.     {  
  10.       "addresses": [  
  11.         {  
  12.           "ip": "10.10.200.226"  
  13.         }  
  14.       ],  
  15.       "ports": [  
  16.         {  
  17.           "port": 1  
  18.         }  
  19.       ]  
  20.     },  
  21.     {  
  22.       "addresses": [  
  23.         {  
  24.           "ip": "10.10.200.227"  
  25.         }  
  26.       ],  
  27.       "ports": [  
  28.         {  
  29.           "port": 1  
  30.         }  
  31.       ]  
  32.     }  
  33.   ]  
  34. }  
glusterfs-service.json文件如下,不需修改

[html]  view plain  copy
  1. [root@k8s-master yaml]# vi glusterfs-service.json   
  2. {  
  3.   "kind": "Service",  
  4.   "apiVersion": "v1",  
  5.   "metadata": {  
  6.     "name": "glusterfs-cluster"  
  7.   },  
  8.   "spec": {  
  9.     "ports": [  
  10.       {"port": 1}  
  11.     ]  
  12.   }  
  13. }  
启用上述两文件

[html]  view plain  copy
  1. [root@k8s-master yaml]# kubectl apply -f glusterfs-endpoints.json   
  2. endpoints "glusterfs-cluster" created  
  3. [root@k8s-master yaml]# kubectl apply -f glusterfs-service.json   
  4. service "glusterfs-cluster" created  
[html]  view plain  copy
  1. [root@k8s-master yaml]# kubectl get ep  
  2. NAME                ENDPOINTS                         AGE  
  3. glusterfs-cluster   10.10.200.226:1,10.10.200.227:1   31s  
  4. kubernetes          10.10.200.224:6443                6d  
  5. [root@k8s-master yaml]# kubectl get service  
  6. NAME                CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE  
  7. glusterfs-cluster   172.17.62.135   <none>        1/TCP     33s  
  8. kubernetes          10.254.0.1      <none>        443/TCP   6d  
创建presistent volume以及presistent volume claim yaml文件

[html]  view plain  copy
  1. [root@k8s-master yaml]# vi glusterfs-pv.yaml   
  2. apiVersion: v1  
  3. kind: PersistentVolume  
  4. metadata:  
  5.   name: gluster-dev-volume1  
  6.   labels:  
  7.     name: mysql1  
  8.   
  9. spec:  
  10.   capacity:  
  11.     storage: 10Gi  
  12.   accessModes:  
  13.     - ReadWriteMany  
  14.   glusterfs:  
  15.     endpoints: "glusterfs-cluster"  
  16.     path: "mysql-volume"  
  17.     readOnly: false  
[html]  view plain  copy
  1. [root@k8s-master yaml]# vi glusterfs-pvc.yaml   
  2. apiVersion: v1  
  3. kind: PersistentVolumeClaim  
  4. metadata:  
  5.   name: glusterfs-mysql1  
  6. spec:  
  7.   accessModes:  
  8.     - ReadWriteMany  
  9.   resources:  
  10.     requests:  
  11.       storage: 10Gi  
  12.   selector:  
  13.     matchLabels:  
  14.       name: "mysql1"  
启动上述两文件

[html]  view plain  copy
  1. [root@k8s-master yaml]# kubectl apply -f glusterfs-pv.yaml   
  2. persistentvolume "gluster-dev-volume1" created  
  3. [root@k8s-master yaml]# kubectl apply -f glusterfs-pvc.yaml   
  4. persistentvolumeclaim "glusterfs-mysql1" created  
  5. [root@k8s-master yaml]# kubectl get pv  
  6. NAME                  CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS    CLAIM                      REASON    AGE  
  7. gluster-dev-volume1   10Gi       RWX           Retain          Bound     default/glusterfs-mysql1             8s  
  8. [root@k8s-master yaml]# kubectl get pvc  
  9. NAME               STATUS    VOLUME                CAPACITY   ACCESSMODES   AGE  

glusterfs-mysql1 Bound gluster-dev-volume1 10Gi RWX 6s

以上就完成了kubernetes配置glusterfs的全过程,下面我们通过运行mysql测试一下,编写mysql deployment yaml如下

[html]  view plain  copy
  1. [root@k8s-master yaml]# vi mysql-deployment.yaml   
  2. apiVersion: v1  
  3. kind: Service  
  4. metadata:  
  5.   name: mysql  
  6.   labels:  
  7.     name: mysql  
  8. spec:  
  9.   type: NodePort  
  10.   ports:  
  11.   - name: mysqlport  
  12.     port: 3306  
  13.     nodePort: 32006  
  14.   selector:  
  15.     name: mysql  
  16. ---  
  17.   
  18. apiVersion: extensions/v1beta1  
  19. kind: Deployment  
  20. metadata:  
  21.   name: mysql  
  22. spec:  
  23.   replicas: 1  
  24.   template:  
  25.     metadata:  
  26.       labels:  
  27.         name: mysql  
  28.     spec:  
  29.       containers:  
  30.         - name: mysqlcontainer  
  31.           image: docker.io/mysql  
  32.           imagePullPolicy: IfNotPresent  
  33.           env:  
  34.           - name: MYSQL_ROOT_PASSWORD  
  35.             value: root123456  
  36.           ports:  
  37.             - containerPort : 3306  
  38.           volumeMounts:  
  39.             - name: gluster-mysql-data  
  40.               mountPath: "/var/lib/mysql"  
  41.       volumes:  
  42.         - name: gluster-mysql-data  
  43.           persistentVolumeClaim:  
  44.             claimName: glusterfs-mysql1  

启动该文件

[html]  view plain  copy
  1. [root@k8s-master yaml]# kubectl apply -f mysql-deployment.yaml   
  2. service "mysql" created  
  3. deployment "mysql" configured  

查看状态

[html]  view plain  copy
  1. [root@k8s-master yaml]# kubectl get service  
  2. NAME                CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE  
  3. glusterfs-cluster   172.17.62.135    <none>        1/TCP            13m  
  4. kubernetes          10.254.0.1       <none>        443/TCP          6d  
  5. mysql               172.17.231.221   <nodes>       3306:32006/TCP   1m  
  6. [root@k8s-master yaml]# kubectl get pods  
  7. NAME                     READY     STATUS    RESTARTS   AGE  
  8. mysql-3736381796-5j11t   1/1       Running   0          1m  
  9. nginx-controller-1hxkz   1/1       Running   3          3d  
  10. nginx-controller-3xcl8   1/1       Running   3          3d  
  11. [root@k8s-master yaml]# kubectl get pods -o wide  
  12. NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE  
  13. mysql-3736381796-5j11t   1/1       Running   0          1m        172.17.99.3   10.10.200.230  
  14. nginx-controller-1hxkz   1/1       Running   3          3d        172.17.99.2   10.10.200.230  
  15. nginx-controller-3xcl8   1/1       Running   3          3d        172.17.17.2   10.10.200.229  

登录mysql

[html]  view plain  copy
  1. [root@k8s-node2 ~]# mysql -h 10.10.200.230 -P 32006 -uroot -proot123456  
  2. Welcome to the MariaDB monitor.  Commands end with ; or \g.  
  3. Your MySQL connection id is 3  
  4. Server version: 5.7.20 MySQL Community Server (GPL)  
  5.   
  6. Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.  
  7.   
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.  
  9.   
  10. MySQL [(none)]>   











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值