Linux系统 CentOS 7怎么搭建集群? 搭建高可用集群的步骤
CentOS 7下怎么搭建高可用集群。高可用集群是指以减少服务中断时间为目的的服务器集群技术。它通过保护用户的业务程序对外不间断提供的服务,把因软件/硬件/人为造成的故障对业务的影响降低到最小程度。那么新的centos下怎么来搭建高可用集群。
环境:本文以两台机器实现双集热备高可用集群,主机名node1的IP为192.168.122.168 ,主机名node2的IP为192.168.122.169 。
一、安装集群软件必须软件pcs,pacemaker,corosync,fence-agents-all,如果需要配置相关服务,也要安装对应的软件
二、配置防火墙
1、禁止防火墙和selinux

修改/etc/sysconfig/selinux确保SELINUX=disabled,然后执行setenforce 0或者reboot服务器以生效
2、设置防火墙规则

三、各节点之间主机名互相解析分别修改2台主机名分别为node1和node2,在centos 7中直接修改/etc/hostname加入本机主机名和主机表,然后重启网络服务即可。

配置2台主机的主机表,在/etc/hosts中加入

四、各节点之间时间同步在node1和node2分别进行时间同步,可以使用ntp实现。

五、各节点之间配置ssh的无密码密钥访问。下面的操作需要在各个节点上操作。

两台主机都要互相可以通信,所以两台主机都得互相生成密钥和复制公钥,相互的节点上的hosts文件是都要解析对方的主机名, 192.168.122.168 node1 192.168.122.169 node2

六、通过pacemaker来管理高可用集群
1、创建集群用户
为了有利于各节点之间通信和配置集群,在每个节点上创建一个hacluster的用户,各个节点上的密码必须是同一个。

2、设置pcsd开机自启动

3、集群各节点之间进行认证

4、创建并启动集群

5、设置集群自启动

6、查看集群状态信息

7、设置fence设备
这个可以参考<Red Hat Enterprise Linux 7 High Availability Add-On Reference>
corosync默认启用了stonith,而当前集群并没有相应的stonith设备,因此此默认配置目前尚不可用,这可以通过如下命令验证:

可以通过如下面命令禁用stonith:

8、配置存储
高可用集群既可以使用本地磁盘来构建纯软件的镜像型集群系统,也可以使用专门的共享磁盘装置来构建大规模的共享磁盘型集群系统,充分满足客户的不同需求。
共享磁盘主要有iscsi或DBRD。本文并没有使用共享磁盘。
9、配置浮点IP
不管集群服务在哪运行,我们要一个固定的地址来提供服务。在这里我选择192.168.122.101作为浮动IP,给它取一个好记的名字ClusterIP 并且告诉集群 每30秒检查它一次。

10、配置apache服务
在node1和node2上安装httpd ,确认httpd开机被禁用

配置httpd监控页面(貌似不配置也可以通过systemd监控),分别在node1和node2上执行

首先我们为Apache创建一个主页。在centos上面默认的Apache docroot是/var/www/html,所以我们在这个目录下面建立一个主页。
node1节点修改如下:

node2节点修改如下:

下面语句是将httpd作为资源添加到集群中:

11、创建group
将VIP和WEB resource捆绑到这个group中,使之作为一个整体在集群中切换。(此配置为可选)

12、配置服务启动顺序
以避免出现资源冲突,语法:(pcs resource group add的时候也可以根据加的顺序依次启动,此配置为可选)

13、指定优先的 Location (此配置为可选)
Pacemaker 并不要求你机器的硬件配置是相同的,可能某些机器比另外的机器配置要好。这种状况下我们会希望设置:当某个节点可用时,资源就要跑在上面之类的规则。为了达到这个效果我们创建location约束。同样的,我们给他取一个描述性的名字(prefer-node1),指明我们想在上面跑WEB 这个服务,多想在上面跑(我们现在指定分值为50,但是在双节点的集群状态下,任何大于0的值都可以达到想要的效果),以及目标节点的名字:

这里指定分值越大,代表越想在对应的节点上运行。
14、资源粘性(此配置为可选)
一些环境中会要求尽量避免资源在节点之间迁移。迁移资源通常意味着一段时间内无法提供服务,某些复杂的服务,比如Oracle数据库,这个时间可能会很长。
为了达到这个效果,Pacemaker 有一个叫做“资源粘性值”的概念,它能够控制一个服务(资源)有多想呆在它正在运行的节点上。
Pacemaker为了达到最优分布各个资源的目的,默认设置这个值为0。我们可以为每个资源定义不同的粘性值,但一般来说,更改默认粘性值就够了。资源粘性表示资源是否倾向于留在当前节点,如果为正整数,表示倾向,负数则会离开,-inf表示负无穷,inf表示正无穷。

常用命令汇总:查看集群状态:#pcs status
查看集群当前配置:#pcs config
开机后集群自启动:#pcs cluster enable –all
启动集群:#pcs cluster start –all
查看集群资源状态:#pcs resource show
验证集群配置情况:#crm_verify -L -V
测试资源配置:#pcs resource debug-start resource
设置节点为备用状态:#pcs cluster standby node1
这就是小编给大家带来的CentOS 7下怎么搭建高可用集群步骤,是不是很详细。谢谢阅读,希望能帮到大家,请继续关注,我们会努力分享更多优秀的文章。
下一篇:16个 Linux系统方面的在线工具类网站