HA搭建web服务器高可用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HA搭建web服务器高可用相关的知识,希望对你有一定的参考价值。
拓扑:
QJ02 主web==192.168.2.131
QJ03 从web==192.168.2.132
QJ01 NFS====192.168.2.130
1.准备工作:
(1).解析域名
#[[email protected] ~]# vim /etc/hosts
192.168.2.131 QJ02.cn
192.168.2.132 QJ03.cn
[[email protected] ~]# vim /etc/hosts
192.168.2.131 QJ02.cn
192.168.2.132 QJ03.cn
(2). 关防火墙,关掉selinux
# iptables -F
# getenforce
Permissive
2.配置QJ01为NFS服务器,提供存储资源
(1)安装NFS服务器:(3台主机均安装)
[[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]# yum -y install nfs-utils
[[email protected] ~]# mkdir /wwwdir
[[email protected] ~]# echo "Heartbeat Web test" > /wwwdir/index.html
[[email protected] ~]# vi /etc/exports
/wwwdir 192.168.2.0/24(rw)
(2)配置目录权限
[[email protected] ~]# ll -d /wwwdir/
drwxr-xr-x 2 root root 24 Sep 12 21:33 /wwwdir/
[[email protected] ~]# chmod 777 -R /wwwdir/
[[email protected] ~]# ll -d /wwwdir/
drwxrwxrwx 2 root root 24 Sep 12 21:33 /wwwdir/
(3)开启nfs服务
[[email protected] ~]# systemctl start nfs
[[email protected] ~]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
[[email protected] ~]# showmount -e
Export list for Qj01:
/wwwdir 192.168.2.0/24
3.QJ02和QJ03测试nfs 存储挂载并安装httpd web服务器:(只写QJ02一台配置,另一台QJ03配置相同)
[[email protected] ~]# yum install httpd -y
[[email protected] ~]# systemctl start nfs
[[email protected] ~]# showmount -e 192.168.2.130
Export list for 192.168.2.130:
/wwwdir 192.168.2.0/24
[[email protected] ~]# mount -t nfs 192.168.2.130:/wwwdir /var/www/html/
[[email protected] ~]# systemctl start httpd
测试:
[[email protected] ~]# yum install elinks -y
[[email protected] ~]# elinks --dump 192.168.2.131
Heartbeat Web test
卸载资源:后期这些资源通过heartbeat直接加载
[[email protected] ~]# umount /var/www/html/
[[email protected] ~]# systemctl stop httpd
[[email protected] ~]# systemctl disable httpd
4.QJ02和QJ03安装heartbeat(只写QJ02一台配置,另一台QJ03配置大致相同<不同处有备注>)
heartbeat在Ceontos7下编译步骤
下载:http://www.linux-ha.org/wiki/Downloads,从官方下载最新的HeartBeat版本:
Heartbeat 3.0.6
Cluster Glue 1.0.12
Resource Agents 3.9.6
(1).配置基础环境(配置好网络yum源)
安装依赖包[[email protected] ~]#yum install -y bzip2 bzip2-devel gcc gcc-c++ autoconf automake libtool e2fsprogs-devel glib2-devel libxml2 libxml2-devel libtool-ltdl-devel asciidoc libuuid-devel docbook
(2).编译Cluster Glue
[[email protected] ~]# tar jxf Cluster_Glue_1.0.12.tar.bz2
[[email protected] ~]#cd Reusable-Cluster-Components-glue--0a7add1d9996/
[[email protected] Reusable-Cluster-Components-glue--0a7add1d9996]# groupadd haclient 创建用户组
[[email protected] Reusable-Cluster-Components-glue--0a7add1d9996]# useradd -g haclient hacluster 创建用户
[[email protected] Reusable-Cluster-Components-glue--0a7add1d9996]# ./autogen.sh
[[email protected] Reusable-Cluster-Components-glue--0a7add1d9996]# ./configure --prefix=/usr/local/heartbeat/
[[email protected] Reusable-Cluster-Components-glue--0a7add1d9996]# make && make install
(3).编译Resource Agents
[[email protected] ~] # tar -zxf resource-agents-3.9.6.tar.gz
[[email protected] ~]# cd resource-agents-3.9.6
[[email protected] resource-agents-3.9.6]# ./autogen.sh
[[email protected] resource-agents-3.9.6]#./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS=‘/lib64/libuuid.so.1‘
[[email protected] resource-agents-3.9.6]# make
[[email protected] resource-agents-3.9.6]# make install
(4).编译Heartbeat
[[email protected] ~]# tar -jxvf Heartbeat-3.0.6.tar.bz2
[[email protected] ~]# cd Heartbeat-3-0-958e11be8686/
[[email protected] Heartbeat-3-0-958e11be8686]# ./bootstrap
[[email protected] Heartbeat-3-0-958e11be8686]# export CFLAGS="$CFLAGS -I/usr/local/heartbeat/include -L/usr/local/heartbeat/lib"
[[email protected] Heartbeat-3-0-958e11be8686]# ./configure --prefix=/usr/local/heartbeat --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS=‘/lib64/libuuid.so.1‘
[[email protected] Heartbeat-3-0-958e11be8686]# make
[[email protected] Heartbeat-3-0-958e11be8686]# make install
(5)生成配置文件:
[[email protected] /]# cd /usr/local/heartbeat/etc/ha.d/
[[email protected] ha.d]# cp /root/Heartbeat-3-0-958e11be8686/doc/{ha.cf,haresources,authkeys} .
[[email protected] ha.d]# chkconfig --add heartbeat
[[email protected] ha.d]# chkconfig heartbeat on
[[email protected] ha.d]# chmod 600 /usr/local/heartbeat/etc/ha.d/authkeys
[[email protected] ha.d]# mkdir -pv /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/
[[email protected] ha.d]# cp /usr/lib/ocf/lib/heartbeat/ocf-* /usr/local/heartbeat/usr/lib/ocf/lib/heartbeat/
[[email protected] ha.d]# ln -sv /usr/local/heartbeat/lib64/heartbeat/plugins/* /usr/local/heartbeat/lib/heartbeat/plugins/
(6)Heartbeat配置
<1>配置ha.cf----主配置文件
Heartbeat的配置主要涉及到ha.cf、haresources、authkeys这三个文件。
ha.cf: 主配置文件,
haresource: 用来配置要让Heartbeat托管的服务,
authkey: 是用来指定Heartbeat的认证方式。
[[email protected] /]# vim /usr/local/heartbeat/etc/ha.d/ha.cf vim /usr/local/heartbeat/etc/ha.d/ha.cf
24 debugfile /var/log/ha-debug ##用于记录heartbeat的调试信息
29 logfile/var/log/ha-log ##用于记录heartbeat的日志信息
34 logfacilitylocal0 ##设置heartbeat的日志,这里用的是系统日志
48 keepalive 2 ##设定心跳(监测)时间时间为2秒
56 deadtime 30 ##指定若备用节点在30秒内未收到主节点心跳信号,则接管主服务器资源
61 warntime 10 ##指定心跳延迟的时间为10秒,10秒内备节点不能接收主节点心跳信号,
即往日志写入警告日志,但不会切换服务
71 initdead 120 ##系统启动或重启后预留的忽略时间段,取值至少为deadtime的两倍
76 udpport 694 ##广播/单播通讯使用的Udp端口
91 #bcast ens32 # Linux ##使用网卡eno32发送心跳检测
113 #mcast eth0 225.0.0.1 694 1 0 ##采用网卡eth0的Udp多播来组织心跳,一般在备用节点
Bcast、ucast和mcast分别代表广播、单播和多播,是组织心跳的的方式,任选其一
121 ucast ens33 192.168.2.132 ##采用网卡eth32的udp单播来组织心跳,后面跟的IP地址为双机对方IP地址
157 auto_failback on ##定义当主节点恢复后,是否将服务自动切回
211 node QJ02.cn ##主节点名称
212 node QJ03.cn ##备用节点名称
220 ping 192.168.2.1 ##通过ping网关检测心跳是否正常,仅用来测试网络
253 respawn hacluster /usr/local/heartbeat/libexec/heartbeat/ipfail ##指定和heartbeat一起启动、关闭的进程,否则启动报错
259 apiauth ipfail gid=haclient uid=hacluster ##设置启动IPfail的用户和组
[[email protected] ha.d]# scp ha.cf 192.168.2.132:/usr/local/heartbeat/etc/ha.d/`(修改121行即可)
ucast ens33 192.168.2.131
<2>配置haresources-----资源文件
[[email protected] ha.d]# vim haresources
44 #node-name resource1 resource2 ... resourceN
45 QJ02.cn IPaddr::192.168.2.111/24ens33 Filesystem::192.168.2.130:/wwwdir::/var/www/html::nfs http [[email protected] ha.d]# vim haresources
44 #node-name resource1 resource2 ... resourceN
45 QJ02.cn IPaddr::192.168.2.111/24ens33 Filesystem::192.168.2.130:/wwwdir::/var/www/html::nfs httpd
注:QJ02.cn是主服务器的主机名, QJ03上不需要修改。这样资源默认会加一这个主机上。当QJ02坏了,QJ03会再接管。
<3>配置authkeys-----心跳密钥验证文件
[[email protected] ha.d]#vim /usr/local/heartbeat/etc/ha.d/authkeys
auth 3
3 md5 Hello!
[[email protected] ha.d]#vim /usr/local/heartbeat/etc/ha.d/authkeys
auth 3
3 md5 Hello!
<4>编写httpd启动脚本
[[email protected] ha.d]#vim /usr/local/heartbeat/etc/ha.d/resource.d/httpd
[[email protected] ha.d]#vim /usr/local/heartbeat/etc/ha.d/resource.d/httpd
#!/bin/bash
/bin/systemctl $1 httpd
[[email protected] resource.d]#chmod 755 httpd
[[email protected] resource.d]#chmod 755 httpd
<5>测试:(测试后记得关闭)
手动加载VIP 192.168.2.111到ens33上
[[email protected] ~]# cd /usr/local/heartbeat/etc/ha.d/resource.d
测试绑定VIP
[[email protected] resource.d]# ./IPaddr 192.168.2.111/24/ens33 start
INFO: Success
INFO: Success
查看 VIP:
[[email protected] ~]# ifconfig #可以看到 ens33:1 的IP为:192.168.2.111
手动加载NFS存储资源到/var/www/html (加载需要fuser支持,yum install psmisc -y)
[[email protected] resource.d]# ./Filesystem 192.168.2.130:/wwwdir /var/www/html/ nfs start
INFO: Success
INFO: Success
[[email protected] ~]# ls /var/www/html/index.html
/var/www/html/index.html
[[email protected] ~]# df -h
………
192.168.2.130:/wwwdir 18G 1.1G 17G 6% /var/www/html
手动启动httpd服务
[[email protected] ~]# systemctl start httpd
测试打开主服务页面http://192.168.2.131
启动heartbeat
# systemctl start heartbeat
# chkconfig --add heartbeat
# chkconfig heartbeat on
以上是关于HA搭建web服务器高可用的主要内容,如果未能解决你的问题,请参考以下文章
基于Nginx搭建Web服务器HA架构(实现高可用web服务)
RHEL6搭建 keepalived (HA) 任意单故障节点的高可用集群
SpringCloud系列四:Eureka 服务发现框架(定义 Eureka 服务端Eureka 服务信息Eureka 发现管理Eureka 安全配置Eureka-HA(高可用) 机制Eur(代码片段