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

相关推荐

  • Linux破解root口令

    Centos5、6版本破解口令方法 1 启动系统 2 在操作系统选择菜单界面选中要启动的内核 — 按a键 3 在行尾输入 1(或s或S或single)— 按回车键 4 使用passwd命令修改口令即可   Centos7版本破解口令方法 方法1 1 启动系统 2 在操作系统选择菜单界面选中要启动的内核 — 按e键…

    Linux干货 2018-01-08
  • 人志建,则无敌—磁盘、LVM2和简单脚本练习

    马哥网络班21期-第七周博客 1、创建一个10G分区,并格式为ext4文件系统;  disk /dev/sdb         Command (m for help): n    &nbs…

    Linux干货 2016-08-19
  • 基于heartbeat v2 crm实现基于nfs的mysql高可用集群

    前言 因heartbeat v1内置的资源管理器haresource功能比较简单,且不支持图形化管理,所以heartbeat v2不再支持haresource,转而使用更加强大的资源管理器crm进行集群管理。本文将讲解如何基于heartbeat v2 crm实现基于nfs的mysql高可用集群。 高可用实现 实验拓扑 实验环境 node1:172.16.10…

    Linux干货 2015-06-11
  • 随笔

    GREP正则表达式: 复习: glob文件通配符:     *:任意长度字符:     ?:任意单个字符:     []:括号内的任意单个字符:     [^]:括号内字符除外:  &nbs…

    Linux干货 2016-07-16
  • python agent应用

    BaseHTTPServer 模块说明 class BaseHTTPServer.HTTPServer(server_address, RequestHandlerClass) server_address : 是一个服务器 (ip, port)元组。 RequestHandlerClas…

    Linux干货 2016-09-19
  • 文本三剑客 grep sed awk

    模式,选项。常用用法。

    2017-12-03