本文共 3617 字,大约阅读时间需要 12 分钟。
环境:
主keepalived服务器:192.168.80.100从keepalived服务器: 192.168.80.101Web服务器:192.168.80.102
192.168.80.103windows7 :192.168.80.20
主从keepalived服务器上:
modprobe ip_vsyum install ipvsadm -y
yum -y install gcc gcc-c++ make popt-devel kernel-devel openssl-devel
tar xf keepalived-1.4.2.tar.gz -C /opt
cd keepalived-1.4.2/
./configure --prefix=/
make && make install
cp keepalived/etc/init.d/keepalived /etc/init.d/ //加入系统管理服务
systemctl enable keepalived
在主服务器上
vi /etc/keepalived/keepalived.conf! Configuration File for keepalived
global_defs { router_id LVS_01 //本服务器的名称}vrrp_instance VI_1 { //定义VRRP热备实例state MASTER //热备状态,MASTER表示主服务器,BACKUP表示从服务器interface ens32 //承载VIP地址的物理接口virtual_router_id 51 //虚拟路由器的ID号,每个热备组保持一致priority 110 //优先级,数值越大优先级越高advert_int 1 //通告间隔秒数(心跳频率)authentication { //热备认证信息,每个热备组保持一致auth_type PASS //认证类型auth_pass 6666 //密码字符串}virtual_ipaddress { //指定漂移地址(VIP),可以有多个192.168.80.188}}virtual_server 192.168.80.188 80 { //虚拟服务器地址(VIP)、端口delay_loop 6 //健康检查的间隔时间(秒)lb_algo rr //轮询(rr)调度算法lb_kind DR //直接路由(DR)群集工作模式persistence_timeout 60 //连接保持时间(秒)protocol TCP //应用服务器采用的是TCP协议real_server 192.168.80.102 80 { //第一个Web服务器节点的地址、端口weight 1 //节点的权重TCP_CHECK { //健康检查方式connect_port 80 //检查的目标端口connect_timeout 3 //连接超时(秒)nb_get_retry 3 //重试次数delay_before_retry 3 //重试间隔}}real_server 192.168.80.103 80 { //第二个Web服务器节点的地址、端口weight 1TCP_CHECK { connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}}service keepalived start
ip addr show dev ens32ipvsadm –Ln //查看LVS虚拟服务器
cat /var/log/messages
配置从keepalived服务器:
vi /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs { router_id LVS_02}vrrp_instance VI_1 { state BACKUPinterface ens32virtual_router_id 51priority 105advert_int 1authentication { auth_type PASSauth_pass 6666}virtual_ipaddress { 192.168.80.188}}virtual_server 192.168.80.188 80 { delay_loop 6lb_algo rrlb_kind DRpersistence_timeout 60protocol TCPreal_server 192.168.80.102 80 { weight 1TCP_CHECK { connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.80.103 80 { weight 1TCP_CHECK { connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}}service keepalived start
ipvsadm –Ln验证热备结果
配置web节点服务器
第一台web节点:yum install -y httpdvi /etc/httpd/conf/httpd.confcd /var/www/html/
echo "<h1>SERVER AA</h1>" > index.htmlservice httpd startvi web.sh
#!/bin/bash#hahaifconfig lo:0 192.168.80.188 broadcast 192.168.80.188 netmask 255.255.255.255 uproute add -host 192.168.80.188 dev lo:0echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announcesysctl -p &>/dev/nullsh web.sh
ifconfig第二台web节点:
yum install -y httpdvi /etc/httpd/conf/httpd.confcd /var/www/html/
echo "<h1>SERVER BB</h1>" > index.htmlservice httpd startvi web.sh
#!/bin/bash#hahaifconfig lo:0 192.168.80.188 broadcast 192.168.80.188 netmask 255.255.255.255 uproute add -host 192.168.80.188 dev lo:0echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/lo/arp_announceecho "1" >/proc/sys/net/ipv4/conf/all/arp_ignoreecho "2" >/proc/sys/net/ipv4/conf/all/arp_announcesysctl -p &>/dev/nullsh web.sh
ifconfig测试LVS+Keepalived高可用群集
在客户端的浏览器中,能够通过LVS+Keepalived群集的漂移地址(192.168.80.100)正常访问web页面内容,则验证群集构建成功。验证两台Web服务器轮询工作
Win7访问http://192.168.80.1883、模拟Web服务器aa故障
LVS+Keepalived已成功搭建并测试完成
转载于:https://blog.51cto.com/14150862/2339418