heartbeat + drbd + nginx
一、环境介绍
1、系统版本:
[root@www-slave ha.d]# uname -r 3.10.0-862.9.1.el7.x86_64
2、主机
主机 | ip |
www-master | 10.10.10.109 |
www-slave | 10.10.10.108 |
3、基础准备
systemctl stop firewalld.service #关闭防火墙 sed -i 's/Enforcing/disabled/g' /etc/sysconfig/selinux #关闭selinux ntpdate 0.cn.pool.ntp.org #同步时间
4、host解析
10.10.10.109 www-master 10.10.10.108 www-slave
二、安装DRBD
1、添加附加库(两台都操作)
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
2、安装drbd(两台都操作)
yum install drbd84 kmod-drbd84 -y
3、磁盘分区(两台都操作)
Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p
不做磁盘初始化
4、开始配置drbd
modprobe drbd
提示:centos7没有这个模块,需要升级内核,yum install kenel* -y
5、修改配置文件/etc/drbd.d/global_common.conf(两台相同配置)
global { usage-count yes; } common { protocol C; disk { on-io-error detach; } syncer { rate 100M; #100M传输速度 } } resource nginx { on www-master{ device /dev/drbd0; disk /dev/sdb; address 10.10.10.109:7788; meta-disk internal; } on www-slave{ device /dev/drbd0; disk /dev/sdb; address 10.10.10.108:7788; meta-disk internal; } }
6、激活前面配置的DRBD资源nginx(两个节点都要执行)
drbdadm create-md nginx
7、启动drbd(两个节点都要执行)
systemctl start drbd
按提示输入 yes
8、一下几个步骤都在主上执行
drbdadm primary nginx #设置主 drbdadm -- --overwrite-data-of-peer primary nginx
watch -n1 'cat /proc/drbd'
version: 8.4.11-1 (api:1/proto:86-101)
GIT-hash: 66145a308421e9c124ec391a7848ac20203bb03c build by mockbuild@, 2018-04-26 12:10:42
0: cs:SyncTarget ro:Secondary/Primary ds:Inconsistent/UpToDate C r-----
ns:0 nr:258403840 dw:258403840 dr:0 al:8 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:912557636
[===>................] sync'ed: 22.1% (891168/1143516)M
finish: 6:13:23 speed: 40,732 (40,112) want: 0 K/sec
等状态同步完成之后,在主上执行一下
mkfs.ext4 /dev/drbd1
三、Heartbeat环境搭建
1、安装heartbeat依赖包
yum install -y bzip2 autoconf automake libtool glib2-devel libxml2-devel bzip2-devel libtool-ltdl-devel asciidoc libuuid-devel psmisc
2、安装glue
wget http://hg.linux-ha.org/glue/archive/0a7add1d9996.tar.bz2 tar jxvf 0a7add1d9996.tar.bz2 cd Reusable-Cluster-Components-glue--0a7add1d9996/ groupadd haclient useradd -g haclient hacluster ./autogen.sh ./configure --prefix=/usr/local/heartbeat/ make make install
3、安装Resource Agents
wget https://github.com/ClusterLabs/resource-agents/archive/v3.9.6.tar.gz tar zxvf v3.9.6.tar.gz cd resource-agents-3.9.6/ ./autogen.sh export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib" ./configure --prefix=/usr/local/heartbeat/ vi /etc/ld.so.conf.d/heartbeat.conf #计入下边内容
/usr/local/heartbeat/lib
ldconfig make make install
4、安装HeartBeat
wget http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/958e11be8686.tar.bz2 tar jxvf 958e11be8686.tar.bz2 cd Heartbeat-3-0-958e11be8686 ./bootstrap export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib" ./configure --prefix=/usr/local/heartbeat/ vi /usr/local/heartbeat/include/heartbeat/glue_config.h /*define HA_HBCONF_DIR “/usr/local/heartbeat/etc/ha.d/”*/ (注意这行用/**/注释掉) make make install
2、复制配置文件
cp /usr/local/heartbeat/share/doc/heartbeat/ha.cf /usr/local/heartbeat/etc/ha.d cp /usr/local/heartbeat/share/doc/heartbeat/authkeys /usr/local/heartbeat/etc/ha.d cp /usr/local/heartbeat/share/doc/heartbeat/haresources /usr/local/heartbeat/etc/ha.d
3、设置ha.cf配置文件(两台机器都操作)
debugfile /var/log/ha-debug #日志 logfile /var/log/ha-log keepalive 2 #设定检查时间间隔 warntime 10 #设定警告时间 deadtime 30 #设定在时间内没有心跳就立即切换服务 initdead 60 #初始化时间 udpport 1112 #设定集群节点间的通信协议及端口为udp1112 bcast em1 ucast em1 10.10.10.109 #设置心跳方式使用单播方式,并且在em1网卡上进行单播 ,ip地址为对方的IP #baud 19200 auto_failback off #当主节点恢复后,是否自动切回,一般都设为off node www-master #指定两个节点 node www-slave ping 10.10.10.1 #两个IP的网关 respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail #使用这个脚本去侦听对方是否还活着(使用的是ICMP报文检测)
4、编辑双机互联验证文件authkeys,添加以下内容:(node1,node2)
# vi /usr/local/heartbeat/etc/ha.d/authkeys auth 1 1 crc
5、给验证文件600权限
# chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys
6、编辑集群资源/usr/local/heartbeat/etc/ha.d/haresources (在文件的最后一行加入以下内容)(主服务器)
从服务器配置相同 唯一的不同 是吧主机名改成自己的主机。
7、heartbeat关联服务
heartbeat启动自动启动项关联的服务是通过调用脚本来实现的,咱们这次做的是nginx,所以要把 nginx的脚本放在/usr/local/heartbeat/etc/ha.d/resource.d/下,我们在编译安装的时候,官网是提供给我们很多脚本的,具体目录在/usr/local/src/resource-agents-3.9.6/heartbeat/。
[root@www-master etc]# ls /usr/local/src/resource-agents-3.9.6/heartbeat/ anything dhcpd galera IPv6addr_utils.c Makefile.am ocf-binaries.in portblock sapdb-nosha.sh shellfuncs.in vmware AoEtarget dnsupdate http-mon.sh IPv6addr_utils.o Makefile.in ocf-directories postfix sapdb.sh slapd vsftpd apache docker ICP iscsi ManageRAID ocf-directories.in pound SAPInstance SphinxSearchDaemon WAS apache-conf.sh Dummy ids iSCSILogicalUnit ManageVE ocf-rarun proftpd scsi2reservation Squid WAS6 asterisk eDir88 iface-bridge iSCSITarget mysql ocf-returncodes Pure-FTPd SendArp Stateful WinPopup AudibleAlarm ethmonitor iface-vlan jboss mysql-common.sh ocf-shellfuncs ra-api-1.dtd send_ua symlink Xen ClusterMon Evmsd IPaddr kamailio mysql-proxy ocf-shellfuncs.in Raid1 send_ua.c SysInfo Xinetd clvm EvmsSCC IPaddr2 LinuxSCSI named oracle README send_ua.o syslog-ng zabbixserver conntrackd exportfs IPsrcaddr LVM nfsnotify ora-common.sh Route ServeRAID tomcat CTDB Filesystem IPv6addr lxc nfsserver oralsnr rsyncd sfex varnish db2 findif.sh IPv6addr.c MailTo nginx pgsql rsyslog sg_persist VIPArip Delay fio IPv6addr.o Makefile ocf-binaries pingd SAPDatabase shellfuncs VirtualDomain
把nginx脚本copy到heartbeat服务下
cp -a /usr/local/src/resource-agents-3.9.6/heartbeat/nginx /usr/local/heartbeat/etc/ha.d/resource.d/
到这里 我们的heartbeat就配置完了,下边就让我们测试一下吧 。
四、切换测试。
1、 查看主服务器的磁盘挂载状况。(主)
[root@www-master etc]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda2 50G 2.8G 48G 6% / devtmpfs 16G 0 16G 0% /dev tmpfs 16G 0 16G 0% /dev/shm tmpfs 16G 25M 16G 1% /run tmpfs 16G 0 16G 0% /sys/fs/cgroup /dev/sda1 197M 152M 46M 77% /boot /dev/mapper/cl-data 492G 33M 492G 1% /application tmpfs 3.2G 0 3.2G 0% /run/user/0
查看主服务器的磁盘挂载状况。(从)
[root@www-slave ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/cl-root 50G 2.6G 48G 6% / devtmpfs 16G 0 16G 0% /dev tmpfs 16G 0 16G 0% /dev/shm tmpfs 16G 17M 16G 1% /run tmpfs 16G 0 16G 0% /sys/fs/cgroup /dev/sda1 1014M 158M 857M 16% /boot /dev/mapper/cl-home 492G 33M 492G 1% /home tmpfs 3.2G 0 3.2G 0% /run/user/0
以上可以看出 /data 目录没有被挂载
2、查看主的服务启动状况(主和从)
root@www-master etc]# netstat -lntup (主) Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 683/rpcbind tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 15715/vsftpd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 998/sshd tcp6 0 0 :::111 :::* LISTEN 683/rpcbind tcp6 0 0 :::22 :::* LISTEN 998/sshd udp 0 0 127.0.0.1:323 0.0.0.0:* 691/chronyd udp 0 0 0.0.0.0:694 0.0.0.0:* 20632/heartbeat: wr udp 0 0 0.0.0.0:694 0.0.0.0:* 20630/heartbeat: wr udp 0 0 0.0.0.0:851 0.0.0.0:* 683/rpcbind udp 0 0 0.0.0.0:44076 0.0.0.0:* 20630/heartbeat: wr udp 0 0 0.0.0.0:46102 0.0.0.0:* 20632/heartbeat: wr udp 0 0 0.0.0.0:111 0.0.0.0:* 683/rpcbind udp6 0 0 ::1:323 :::* 691/chronyd udp6 0 0 :::851 :::* 683/rpcbind udp6 0 0 :::111 :::* 683/rpcbind
[root@www-slave ~]# netstat -lntup (从) Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 791/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1142/sshd tcp6 0 0 :::111 :::* LISTEN 791/rpcbind tcp6 0 0 :::22 :::* LISTEN 1142/sshd udp 0 0 127.0.0.1:323 0.0.0.0:* 796/chronyd udp 0 0 0.0.0.0:961 0.0.0.0:* 791/rpcbind udp 0 0 0.0.0.0:111 0.0.0.0:* 791/rpcbind udp6 0 0 ::1:323 :::* 796/chronyd udp6 0 0 :::961 :::* 791/rpcbind udp6 0 0 :::111 :::* 791/rpcbind
3、主从服务器按顺序先后执行一下命令。
systemctl start heartbeat
4、启动命令后
[root@www-master etc]# netstat -lntup|grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 21331/nginx: master
5、关闭主服务器的heartbeat
一下是从服务器的debug日志,看下从库变成主的过程。
到此服务配置完成。