keepalived+lvs负载均衡两个httpd

准备4台主机172.16.71.2 172.16.71.3 172.16.71.4 172.16.71.5

前两个做后端httpd服务器。后两个做keepalived


首先配置好后端主机172.16.71.2 和172.16.71.3

# 172.16.71.2
yum install httpd
cd /var/www/html
vim index.html
server1 71.2
# 172.16.71.3
yum install httpd
cd /var/www/html
vim index.html
server2 71.3

由于lvs采用的是DR模式,所以要写脚本修改内核参数并添加路由信息,以下是keepalived双主需要的脚本,两台主机上都需要运行一次:

#!/bin/bash
#
vip=172.16.71.80
vip2=172.16.71.90
mask='255.255.255.255'
interface='lo:0'
interface2='lo:1'

case $1 in
start)
       echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
       echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
       echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
       echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
       ifconfig $interface  $vip netmask $mask broadcast $vip up
       ifconfig $interface2  $vip2 netmask $mask broadcast $vip2 up
       route add -host $vip dev $interface
       route add -host $vip2 dev $interface2
       ;;
stop)
       ifconfig $interface down
       ifconfig $interface2 down
       echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
       echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
       echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
       echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
       ;;
*)
       echo "Usage $(basename $0) start|stop"
       exit 1
       ;;
esac

执行完成后启动httpdsystemctl start httpd


然后配置前端的keepalived,这里采用的是双主模式。

配置文件如下:

# 172.16.71.4

! Configuration File for keepalived

global_defs {
  notification_email {
   root@localhost
  }  

  notification_email_from kaadmin@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id node1
  vrrp_mcast_group4 224.0.71.1
}

vrrp_instance VI_1 {
   state MASTER
   interface eno16777736
   virtual_router_id 71
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass bashrc
   }  
   virtual_ipaddress {
       172.16.71.80/16 dev eno16777736 label eno16777736:0
   }  
   track_interface {
       eno16777736
   }  
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
   state BACKUP
   interface eno16777736
   virtual_router_id 45
   priority 98
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass shell
   }
   virtual_ipaddress {
       172.16.71.90/16 dev eno16777736 label eno16777736:1
   }
   track_interface {
       eno16777736
   }
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}

virtual_server fwmark 3 {
   delay_loop 2
   lb_algo rr
   lb_kind DR
   nat_mask 255.255.0.0
   protocol TCP

   sorry_server 127.0.0.1 80

   real_server 172.16.71.2 80 {
       weight 1
       HTTP_GET {
           url {
             path /
             status_code 200
           }
           connect_timeout 2
           nb_get_retry 3
           delay_before_retry 2
       }
   }

   real_server 172.16.71.3 80 {
       weight 1
       HTTP_GET {
           url {
             path /
             status_code 200
           }
           connect_timeout 2
           nb_get_retry 3
           delay_before_retry 2
       }
   }
}
# 172.16.71.5

! Configuration File for keepalived

global_defs {
  notification_email {
   root@localhost
  }  

  notification_email_from kaadmin@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id node2
  vrrp_mcast_group4 224.0.71.1
}

vrrp_instance VI_1 {
   state BACKUP
   interface eno16777736
   virtual_router_id 71
   priority 98
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass bashrc
   }  
   virtual_ipaddress {
       172.16.71.80/16 dev eno16777736 label eno16777736:0
   }  
   track_interface {
       eno16777736
   }  
       notify_master "/etc/keepalived/notify.sh master"
       notify_backup "/etc/keepalived/notify.sh backup"
       notify_fault "/etc/keepalived/notify.sh fault"


}
vrrp_instance VI_2 {
   state MASTER
   interface eno16777736
   virtual_router_id 45
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass shell
   }
   virtual_ipaddress {
       172.16.71.90/16 dev eno16777736 label eno16777736:1
   }
   track_interface {
       eno16777736
   }
       notify_master "/etc/keepalived/notify.sh master"
       notify_backup "/etc/keepalived/notify.sh backup"
       notify_fault "/etc/keepalived/notify.sh fault"
}


virtual_server fwmark 3 {
   delay_loop 2
   lb_algo rr
   lb_kind DR
   nat_mask 255.255.0.0
   protocol TCP

   sorry_server 127.0.0.1 80

   real_server 172.16.71.2 80 {
       weight 1
       HTTP_GET {
           url {
             path /
             status_code 200
               }
           connect_timeout 2
           nb_get_retry 3
           delay_before_retry 2
       }
   }

   real_server 172.16.71.3 80  {
       weight 1
       HTTP_GET {
           url {
             path /
             status_code 200
           }
           connect_timeout 2
           nb_get_retry 3
           delay_before_retry 2
       }
   }
}

# 告警脚本

#!/bin/bash
#
contact='root@localhost'
notify() {
   mailsubject="$(hostname) to be $1, vip floating"
   mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
   echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
   notify master
   ;;
backup)
   notify backup
   ;;
fault)
   notify fault
   ;;
*)
   echo "Usage: $(basename $0) {master|backup|fault}"
   exit 1
   ;;
esac            

在两个主机上添加iptables

iptables -t mangle -A PREROUTING -d 172.16.71.80 -p tcp --dport 80 -j MARK --set-mark 3
iptables -t mangle -A PREROUTING -d 172.16.71.90 -p tcp --dport 80 -j MARK --set-mark 3

在两台主机上安装httpd并添加sorry page,启动httpd和keepalived

# 172.16.71.4
yum install httpd
cd /var/www/html
vim index.html
sorry page 1
systemctl start httpd keepalived
# 172.16.71.5
yum install httpd
cd /var/www/html
vim index.html
sorry page 2
systemctl start httpd keepalived

测试

访问172.16.71.80或者172.16.71.90时,会显示server1 71.2 或server2 71.3页面。

断掉后端两个httpd服务器,再次访问会显示sorry page 1或者sorry page 2

断掉不同的keepalived会显示不同的sorry page


下面是keepalived单主模式的配置:

# /etc/keepalived/keepalived.conf

# 172.16.71.4

! Configuration File for keepalived

global_defs {
  notification_email {
   root@localhost
  }  

  notification_email_from kaadmin@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id node1
  vrrp_mcast_group4 224.0.71.1
}

vrrp_instance VI_1 {
   state MASTER
   interface eno16777736
   virtual_router_id 71
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass bashrc
   }  
   virtual_ipaddress {
       172.16.71.80/16 dev eno16777736 label eno16777736:0
   }  
   track_interface {
       eno16777736
   }  
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 { # 这段可以注释掉,不用。
   state BACKUP
   interface eno16777736
   virtual_router_id 45
   priority 98
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass shell
   }
   virtual_ipaddress {
       172.16.71.90/16 dev eno16777736 label eno16777736:1
   }
   track_interface {
       eno16777736
   }
   notify_master "/etc/keepalived/notify.sh master"
   notify_backup "/etc/keepalived/notify.sh backup"
   notify_fault "/etc/keepalived/notify.sh fault"
}

virtual_server 172.16.71.80 80 {
   delay_loop 2
   lb_algo rr
   lb_kind DR
   nat_mask 255.255.0.0
   protocol TCP

   sorry_server 127.0.0.1 80

   real_server 172.16.71.2 80 {
       weight 1
       HTTP_GET {
           url {
             path /
             status_code 200
           }
           connect_timeout 2
           nb_get_retry 3
           delay_before_retry 2
       }
   }

   real_server 172.16.71.3 80 {
       weight 1
       HTTP_GET {
           url {
             path /
             status_code 200
           }
           connect_timeout 2
           nb_get_retry 3
           delay_before_retry 2
       }
   }
}

# /etc/keepalived/keepalived.conf

# 172.16.71.5

! Configuration File for keepalived

global_defs {
  notification_email {
   root@localhost
  }  

  notification_email_from kaadmin@localhost
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id node2
  vrrp_mcast_group4 224.0.71.1
}

vrrp_instance VI_1 {
   state BACKUP
   interface eno16777736
   virtual_router_id 71
   priority 98
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass bashrc
   }  
   virtual_ipaddress {
       172.16.71.80/16 dev eno16777736 label eno16777736:0
   }  
   track_interface {
       eno16777736
   }  
       notify_master "/etc/keepalived/notify.sh master"
       notify_backup "/etc/keepalived/notify.sh backup"
       notify_fault "/etc/keepalived/notify.sh fault"


}
vrrp_instance VI_2 { # 这段可以注释掉,不用。
   state MASTER
   interface eno16777736
   virtual_router_id 45
   priority 100
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass shell
   }
   virtual_ipaddress {
       172.16.71.90/16 dev eno16777736 label eno16777736:1
   }
   track_interface {
       eno16777736
   }
       notify_master "/etc/keepalived/notify.sh master"
       notify_backup "/etc/keepalived/notify.sh backup"
       notify_fault "/etc/keepalived/notify.sh fault"
}


virtual_server 172.16.71.80 80 {
   delay_loop 2
   lb_algo rr
   lb_kind DR
   nat_mask 255.255.0.0
   protocol TCP

   sorry_server 127.0.0.1 80

   real_server 172.16.71.2 80 {
       weight 1
       HTTP_GET {
           url {
             path /
             status_code 200
               }
           connect_timeout 2
           nb_get_retry 3
           delay_before_retry 2
       }
   }

   real_server 172.16.71.3 80  {
       weight 1
       HTTP_GET {
           url {
             path /
             status_code 200
           }
           connect_timeout 2
           nb_get_retry 3
           delay_before_retry 2
       }
   }
}

单主模式不需要手动添加iptables。


原创文章,作者:wangshuai,如若转载,请注明出处:http://www.178linux.com/68329

(0)
wangshuaiwangshuai
上一篇 2017-02-13
下一篇 2017-02-13

相关推荐

  • 0809行编辑器sed

    sed :Stream EDitor, 行编辑器  sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出或者使用-i选…

    Linux干货 2016-08-10
  • lvm 逻辑卷 和 网络管理

    lvm 逻辑卷 和 网络管理 逻辑卷管理器(LVM) LVM:      Logical Volume Manager, Version: 2  dm: device mapper:     将一个或多个底层块设备组织成一个 逻辑设备的模块   设备名:     /dev/d…

    Linux干货 2016-09-01
  • 本周学习总结

    这周我们学习了很多比较深的命令,有点烧脑,这次我总结一下sed命令。      sed 是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(patternspace ),接着用sed 命令处理缓冲区中的内容,处理完成后,把缓冲区的容送往屏幕。然后读入下行,执行下一个循环。如果…

    2017-08-28
  • linux系统从windows xp上同步时间

    最近公司客户的调度系统GPS天文时钟出现故障,导致整个调度系统失去时间源,时间积累误差达十几分钟。去现场更换时钟很不方便,所有就想用现场的一台win xp(可上网)的远程机器做NTPserver,然后给linux系统对时。上网查了好多资料,终于找到了一个有效的解决方案,具体内容和步骤如下:首先下载一个Windows下的NTP服务软件。这个软件可以搭建wind…

    Linux干货 2015-03-27
  • rsyslog记录日志于mysql

    rsyslog记录日志于mysql:     前提:准备好msql server或mariadb server;     (1) 安装rsyslog连接至mysql server的驱动模块;        &n…

    Linux干货 2016-12-05
  • 推荐-Corosync + Pacemaker 搭建高可用Httpd服务

    Corosync + Pacemaker 搭建高可用Httpd服务 实验描述 1.两个测试节点,分别为node5.redhat.com和node6.redhat.com地址分别为172.16.100.5和172.16.100.62.集群服务为httpd,利用nfs做共享存储,NFS地址为172.16.0.254,NFS已经共享出了一个/www/htdocs目…

    系统运维 2016-04-05