Pacemaker高可用环境实践
配置两台虚拟机来模拟安装整个过程,通过corosync定义两个资源ipaddr和httpd来说明在crm命令行里如何定义资源。
host1.zyg.com 192.168.5.101 host2.zyg.com 192.168.5.102 提供web服务的虚拟IP Ipaddr: 192.168.5.10 先关闭selinux和防火墙并进行时间同步
两台都需要做的:
[root@host1 ~]# yum install corosync(负责心跳) pacemaker(资源管理) pcs resource-agents [root@host1 ~]# systemctl start pcsd [root@host1 ~]# passwd hacluster [root@host1 ~]# pcs cluster auth host1.zyg.com Username: hacluster Password: 123 host1.zyg.com: Authorized [root@host1 ~]# pcs cluster auth host2.zyg.com Username: hacluster Password: 123 host2.zyg.com: Authorized
在host1.zyg.com上操作;将两个虚拟机加入到集群中;
[root@host1 ~]# pcs cluster setup --start --name mycluster host1.zyg.com host2.zyg.com --transport udpu
禁用爆头功能和仲裁功能;
[root@host1 ~]# pcs property set stonith-enabled=false [root@host1 ~]# pcs property set no-quorum-policy=ignore
查看集群状态
[root@host1 ~]# pcs status
这职位开机自启动;
[root@host1 ~]# systemctl enable pcsd [root@host1 ~]# systemctl enable corosync [root@host1 ~]# systemctl enable pacemaker
查看可用资源;
[root@host1 ~]# pcs resource list
Pacemaker 的资源主要有两类,即 LSB 和 OCF(Open Cluster Framework)。其中 LSB 即 linux 标准服务,通常就是 /etc/init.d 目录下那些脚本。Pacemaker 可以用这些脚本来启停服务。另一类 OCF 实际上是对 LSB 服务的扩展,增加了一些高可用集群管理的功能如故障监控等和更多的元信息。要让 pacemaker 可以很好的对服务进行高可用保障就得实现一个 OCF 资源。
添加IP资源;
[root@host1 ~]# pcs resource create VirtualIP ocf:heartbeat:IPaddr2 ip=192.168.5.10 cidr_netmask=32 nic=eth0 op monitor interval=30s
添加webserver资源;
[root@host1 ~]# pcs resource create WebServer systemd:httpd op monitor interval=20s timeout=40s
这里会出现资源放在两台虚拟机上的情况;
可以通过这条命令建立约束;
[root@host1 ~]# pcs constraint colocation add WebServer with VirtualIP INFINITY
测试:
[root@sxb-1 ~]# ip addr s 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:e2:62:04 brd ff:ff:ff:ff:ff:ff inet 192.168.88.105/24 brd 192.168.88.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet 192.168.88.200/32 brd 192.168.88.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fee2:6204/64 scope link valid_lft forever preferred_lft forever [root@sxb-1 ~]# systemctl status httpd ● httpd.service - Cluster Controlled httpd Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Drop-In: /run/systemd/system/httpd.service.d └─50-pacemaker.conf Active: active (running) since Thu 2019-08-01 07:46:44 EDT; 2min 1s ago Docs: man:httpd(8) man:apachectl(8) Main PID: 4301 (httpd) Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─4301 /usr/sbin/httpd -DFOREGROUND ├─4302 /usr/sbin/httpd -DFOREGROUND ├─4303 /usr/sbin/httpd -DFOREGROUND ├─4304 /usr/sbin/httpd -DFOREGROUND ├─4305 /usr/sbin/httpd -DFOREGROUND └─4306 /usr/sbin/httpd -DFOREGROUND Aug 01 07:46:44 105.com systemd[1]: Starting Cluster Controlled httpd... Aug 01 07:46:44 105.com systemd[1]: Started Cluster Controlled httpd.
我们停掉host1的httpd服务;
[root@sxb-2 ~]# ip addr s 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 00:0c:29:f1:e4:7b brd ff:ff:ff:ff:ff:ff inet 192.168.88.106/24 brd 192.168.88.255 scope global noprefixroute ens33 valid_lft forever preferred_lft forever inet 192.168.88.200/32 brd 192.168.88.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fef1:e47b/64 scope link valid_lft forever preferred_lft forever