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

相关推荐

  • VimTutor(上)

    柚子翻译,如有理解错误或翻译错误,请指正! VimTutor Vim是一款强大的文本编辑器,拥有多命令的特性,tutor解释了其中大多数命令。 tutor是为了描述足够多的Vim命令帮助你轻松使用Vim而被制作的,使Vim能成为一个可完成你所有工作意图的编辑器。 完成tutor的内容大概需要25-30分钟,所完成时间取决于你通过时间积累的经验。 注意: 1.…

    Linux干货 2015-10-18
  • 文件的查找作业

    1、查找/var目录下属主为root,且属组为mail的所有文件 2、查找/var目录下不属于root、lp、gdm的、所有文件 3、查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件 4、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件 5、查找/etc目录下大于1M且类型为普通文件的所有文件 6、查找/…

    Linux干货 2016-08-15
  • GOPS2017全球运维大会 • 深圳站将在深圳召开!

    第六届GOPS2017全球运维大会(本次)将于2017年4月21日-22日在深圳举行,历届金牌讲师精选亮相,各种精彩等您发掘。活动家为GOPS2017全球运维大会提供在线报名!在线报名地址:https://www.huodongjia.com/event-231365274.html 大会亮点 众多国外重量级嘉宾在路上 ► 目前正在和各位大咖商榷行程中,主会…

    2017-04-10
  • for,while,until简介

    Shell(以Bash为例)中的循环语句一般有for、while、until这几种,偶尔还有写错语法的时候,这里结合实例来自己总结一下。也为今后使用提供一个快捷的资料获取渠道。 一、for循环语句 实例1.1 最基本的for循环: (传统的形式,for var in …) 代码如下: #!/bin/bashfor x in one two three fou…

    Linux干货 2016-08-22
  • 高可用keepalived结合haproxy代理WordPress(动静分离)

    (1)A  B两台服务器做keepalived高可用,同时作为haproxy动静分离后端代理。         (keepalived时主备模型,haproxy轮询调度) (2)C 服务器搭建apache作为动态资源服务器 (3)D 服务器搭建nginx作为静态资源服务器 (4)VIP:172.18.0.42…

    2017-05-20
  • 开班第一天

    我的一篇小日记

    Linux干货 2018-03-26