• corosync+pacemaker+web集群


    1.  环境说明:分别在两个节点上实现部署httpd,在两个节点分别安装corosync和pacemaker用于实现web的高可用,通过pcs程序对pacemaker进行配置,当其中一个节点出现问题时用于前端访问的VIP地址将被移到另一个节点,然后启动web应用程序,以实现在两个节点上实现web高可用,本例没有使用共享存储。

    2.  环境部署:

    服务器地址主机名服务器角色
    192.168.188.223centosnode1主服务器
    192.168.188.226centosnode2备服务器

    3.  在安装corosync和pacemaker之前,要在主备节点上都做dns解析和ssh免密登录。

    1. ##dns解析
    2. vim /etc/hosts
    3. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    4. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    5. 192.168.188.223 centosnode1
    6. 192.168.188.226 centosnode2
    7. ##ssh免密登录
    8. [root@centosnode1 ~]# ssh-keygen -f ~/.ssh/id_rsa -P '' -q
    9. [root@centosnode1 ~]# ssh-copy-id centosnode1
    10. [root@centosnode1 ~]# ssh-copy-id centosnode2

    4.  主备节点上安装corosync,pacemaker,httpd,并配置web页面:

    1. ##主备节点上安装corosync,pacemaker,httpd
    2. [root@centosnode1 ~]# yum install corosync pacemaker httpd -y
    3. ##配置web页面
    4. [root@centosnode1 ~]# echo "this is page ip: 192.168.188.223" > /var/www/html/index.html
    5. [root@centosnode2 ~]# echo "this is page ip: 192.168.188.226" > /var/www/html/index.html

    5.  安装pcs并进行验证,主备节点同时操作:

    1. [root@centosnode1 ~]# yum install pcs -y
    2. [root@centosnode1 ~]# systemctl enable pcs --now
    3. ##安装组件生成的hacluster用户,用来本地启动pcs进程,因此我们需要设定密码。
    4. [root@centosnode1 ~]# echo "123456" | passwd --stdin hacluster
    5. ##节点验证,只在主节点验证
    6. [root@centosnode1 ~]# pcs cluster auto centosnode1 centosnode2

    6.  创建并启动集群:

    1. ##创建名为webcluster的集群,其中有centosnode1,centosnode2两个节点,若节点已经存在
    2. 于别的集群,可以用--force选项强制创建,会覆盖之前的操作
    3. [root@centosnode1 ~]# pcs cluster setup --name webcluster centosnode1
    4. centosnode2 --force
    5. ##启动所有集群
    6. [root@centosnode1 ~]# pcs cluster start --all
    7. ##设置集群开机自启
    8. [root@centosnode1 ~]# pcs cluster enable --all
    9. ##查看集群状态
    10. [root@centosnode1 ~]# pcs status
    11. ##查看corosync:
    12. [root@centosnode1 ~]# corosync-cfgtool -s
    13. ##查看corosync成员信息:
    14. [root@centosnode1 ~]# corosync-cmapctl | grep members
    15. ##查看corosync状态:
    16. [root@centosnode1 ~]# pcs status corosync
    17. ##查看pacemaker
    18. [root@centosnode1 ~]# ps -ef | grep pacemaker

    7.  没有Fencing设备时,禁用STONITH组件功能:

    [root@centosnode1 ~]# pcs property set stonith-enabled=false
    

    8.  查看当前配置的合法性,确保配置无误:

    [root@centosnode1 ~]# crm_verify -L -V

    9.  建立集群资源:

    1. ##配置vip资源
    2. [root@centosnode1 ~]# pcs resource create vip ocf:heartbeat:IPaddr2
    3. ip=192.168.188.100 cidr_netmask=24
    4. ##配置web资源
    5. [root@centosnode1 ~]# pcs resource create web systemd:httpd
    6. ##查找资源的用法:
    7. 1.先通过资源的关键字查找出资源的准确名字,如:
    8. [root@centosnode1 ~]# pcs resource list | grep httpd
    9. 2.查找资源的用法:
    10. [root@centosnode1 ~]# pcs resource describe systemd:httpd

    10.  查看集群状态:

    [root@centosnode1 ~]# pcs status

    由此发现,我们刚刚创建的两个资源不在同一台服务器上,因此要调整集群资源到同一台服务器上。

    11.  调整集群资源:

    1. ##目的:将资源运行在同一台节点上
    2. 方式一:配置一个资源组,组中的资源会在同一节点上运行
    3. 语法:pcs resource group add 资源组名 资源1 资源2 ...
    4. [root@centosnode1 ~]# pcs resource group add WEB web vip
    5. 方式二:配置资源的捆绑(约束)关系,这里配置的是主机约束:
    6. 语法:pcs constraint 约束规则 add 资源1 资源2 ... INFINITY
    7. [root@centosnode1 ~]# pcs constraint colocation add web vip INFINITY
    8. ##查看资源的约束规则,即一致性状态
    9. [root@centosnode2 ~]# pcs constraint
    10. Location Constraints:
    11. Ordering Constraints:
    12. Colocation Constraints:
    13. Ticket Constraints:
    14. ##定义排列约束:绑定服务资源和vip始终在同一个节点上
    15. [root@centosnode1 ~]# pcs constraint colocation add web with vip
    16. ##定义顺序约束:约束服务资源始终在vip启动之后才启动
    17. [root@centosnode1 ~]# pcs constraint order vip then web
    18. ##定义位置约束:约束vip资源首先被分配到优先级高的节点上,这里是centosnode2
    19. [root@centosnode1 ~]# pcs constraint location vip prefers centosnode2=100

    12.  再次查看集群状态:

    此时,资源就运行在同一节点上了。

    13.  停止centosnode2节点的所有集群服务,以此来模拟故障转移:

    1. [root@centosnode2 ~]# pcs cluster stop centosnode2
    2. 主节点降级,验证资源漂移
    3. [root@centosnode2 ~]# pcs cluster standby centosnode2
    4. [root@centosnode2 ~]# pcs status
    5. centosnode2上线
    6. [root@centosnode2 ~]# pcs cluster unstandby centosnode2
    7. [root@centosnode2 ~]# pcs status

     此时就会发现资源都转移到了另一个节点上。

    14.  清空集群操作:

    1. ##清空集群错误日志
    2. [root@centosnode1 ~]# pcs resource cleanup
    3. ##销毁集群配置
    4. [root@centosnode1 ~]# pcs cluster destroy --all

    实际上pcs管理程序也是可以通过web客户端来进行操作的,pcs的默认端口是2224,并且是加密的,因此要通过https访问。

     用户名就是之前的hacluster,密码是123456。

    登陆进去后就可以进行集群,资源,节点的管理。 

  • 相关阅读:
    RTC相关实验
    Pinsker 不等式证明(Proof of Pinsker‘s Inequality)
    Beautiful Soup属性和方法及文档
    Python蒙特卡洛树搜索算法实现的黑白棋AI系统
    缺流量时代,App们需要如何突围?
    【Web】ES6新特性
    2023年高级威胁预测:邮件服务器成优先攻击目标
    java spring cloud 工程企业管理软件-综合型项目管理软件-工程系统源码
    Fair原理篇Fair逻辑动态化架构设计与实现
    Promise与await 的使用
  • 原文地址:https://blog.csdn.net/NancyLCL/article/details/126781307