这几天都会学习高可用集群, 也会将其中的一些实验写出来分享给大家, 这个专题估计会写5篇左右,
这次的实验比较简单,在CentOS7使用corosync+pacemaker实现两个节点的nginx高可用。

主机 IP 功用 node1.anyisalin.com 172.16.1.2 web服务, HA节点 node2.anyisalin.com 172.16.1.3 web服务, HA节点 nfs.anyisalin.com 172.16.1.4 提供nfs服务,网站页面文件
注意: 本文实验中所有主机SElinux和iptables都是关闭的
高可用集群必须保证所有节点主机互信, 主机名解析一致, 时间同步。
配置hosts文件同步

配置互信


时间同步

在RH系6.4之后就可以通过RedHat提供的pcs来进行对集群”全生命周期”的管理, 我们这里先通过pcs安装集群并自动生成配置文件。
[root@node1 ~]# yum install pcs -y #node1安装pcs
[root@node1 ~]# ssh node2.anyisalin.com 'yum install pcs -y ' #node2安装pcs
[root@node1 ~]# echo passwd | passwd --stdin hacluster #设置hacluster用户密码
[root@node1 ~]# ssh node2.anyisalin.com "echo passwd | passwd --stdin hacluster" #为node2设置hacluster密码
[root@node1 ~]# systemctl start pcsd ; ssh node2.anyisalin.com "systemctl start pcsd" #启动pcsd
[root@node1 ~]# pcs cluster auth node1.anyisalin.com node2.anyisalin.com -u hacluster #进行认证
Password:
node1.anyisalin.com: Authorized
node2.anyisalin.com: Authorized
[root@node1 ~]# pcs cluster setup --name mycluster node1.anyisalin.com node2.anyisalin.com #安装集群
[root@node1 ~]# pcs cluster start --all #启动集群安装nginx
[root@node1 ~]# yum install nginx -y 注意: 需epel源
[root@node1 ~]# ssh node2.anyisalin.com "yum install nginx -y "
[root@node1 ~]# systemctl enable nginx #由于systemd的特性, 需将nginx设置开机自动启动
[root@node1 ~]# ssh node2.anyisalin.com "systemctl enable nginx"
配置nfs
[root@nfs ~]# mkdir /www/htdocs -pv
[root@nfs ~]# echo "<h1>This is NFS on 172.16.1.4</h1>" > /www/htdocs/index.html #创建网页文件
[root@nfs ~]# vim /etc/exports
/www/htdocs 172.16.1.0/24(ro)
[root@nfs ~]# systemctl start rpcbind.service
[root@nfs ~]# systemctl start nfs-server.service #启动nfs-server
测试nfs
[root@node1 ~]# mount -t nfs 172.16.1.4:/www/htdocs /usr/share/nginx/html/ #挂载成功
[root@node1 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
172.16.1.4:/www/htdocs 52403200 1134336 51268864 3% /usr/share/nginx/html
[root@node1 ~]# umount /usr/share/nginx/html/pcs用来安装集群还可以, 但是配置集群资源, 我用了一次再也不想用了, 实在是不好用, 个人比较喜欢crmsh来配置集群,crmsh大家可以自行去下载,安装时可能需要epel源 中的一些软件。
[root@node1 ~]# yum install crmsh-2.1.4-1.1.x86_64.rpm #安装crmsh 我们的rpm包是自行下载的
配置资源
[root@node1 ~]# crm configure
crm(live)configure# primitive webip ocf:heartbeat:IPaddr params ip=172.16.1.8
crm(live)configure# primitive nginx systemd:nginx
crm(live)configure# primitive webstore ocf:heartbeat:Filesystem params device="172.16.1.4:/www/htdocs" directory="/usr/share/nginx/html" fstype="nfs" op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=40s
定义资源组
crm(live)configure# group webservice webip webstore nginx
crm(live)configure# verify
crm(live)configure# property stonith-enabled=false
crm(live)configure# commit



本文简单演示HA集群的简单实现, 非常的简单。 HA最重要的是它的原理,我以前写过一篇HA原理的博客, 大家可以看看。
作者水平很低, 如果有错误及时指出, 如果你觉得本文写的好请点一波赞~(≧▽≦)/~ 作者: AnyISaIln QQ: 1449472454 感谢: MageEdu