基于keepalived高可用代理轮询后端服务器组

1、节点1配置

[root@keepalive21 ~] vim /etc/hosts#设置解析
192.168.1.21 keepalive21#主节点
192.168.1.22 keepalive22#备节点
[root@keepalive21 ~] ip link set multicast on dev ens33#开启多播
[root@keepalive21 ~]# yum -y install httpd ipvsadm keepalived
[root@keepalive21 ~]# echo "sorry server 1" > /var/www/html/index.html #设置主页内容
[root@keepalive21 ~]# systemctl start httpd#启动httpd
[root@keepalive21 ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {#全局配置
   notification_email {#邮件通知地址 ,这里是本机收件地址
    root@localhost
 }
   notification_email_from keepalived@localhost   #发件地址
   smtp_server 127.0.0.1 #邮件服务地址,端口是25
   smtp_connect_timeout 30  #连接邮件服务器超时时间30秒
   router_id keepalive21 #可以使用主机名
   vrrp_mcast_group4 224.1.101.33 #设置多播地址,其他主机也一样
}

vrrp_instance VI_1 { #定义一个虚拟路由器,第一个
    state MASTER  #当前状态,主的
    interface ens33  #当前的vrp应用,绑定到那个网卡设备上
    virtual_router_id 33#这个虚拟ip,与其他主机要保持一至
    priority 100 #优先级,高于其他主机
    advert_int 1
    authentication {
        auth_type PASS  #密码认证
        auth_pass a1b2c3d4   #密码为8位,不能用默认的密码
    }
    virtual_ipaddress {#虚拟ip地址
        192.168.1.99/24 dev ens33
    }
}
vrrp_instance VI_2{ #定义一个虚拟路由器,第二个
    state BACKUP  #当前状态,从的
    interface ens33  #当前的vrp应用,绑定到那个网卡设备上
    virtual_router_id 34#这个虚拟ip,与其他主机要保持一至
    priority 96 #优先级,低于master主机
    advert_int 1
    authentication {
        auth_type PASS  #密码认证
        auth_pass a6b7c8d9   #密码为8位,不能用默认的密码,这里修改一下
    }
    virtual_ipaddress {#虚拟ip地址
        192.168.1.99/24 dev ens33 
    }
}
virtual_server 192.168.1.99 80 {
          delay_loop 1
          lb_algo wrr
          lb_kind DR
          protocol TCP
          sorry_server 127.0.0.1 80#故障页所在地址,本机所以要启动http
real_server 192.168.1.11 80 {
          weight 1 #权重
          HTTP_GET {
               url {
                  path /index.html
                 status_code 200
               }
               nb_get_retry 3
               delay_before_retry 2
               connect_timeout 3
      }
    }
}
[root@keepalive21 ~]# systemctl start keepalived#启动keepalived
[root@keepalive21 ~]# ipvsadm -ln        #查询ipvadm状态
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.99:80 wrr
  -> 192.168.1.11:80              Route   1      0          0         
  -> 192.168.1.12:80              Route   1      0          0     

1、节点2配置

[root@keepalive21 ~] vim /etc/hosts#设置解析
192.168.1.21 keepalive21#主节点
192.168.1.22 keepalive22#备节点
[root@keepalive21 ~] ip link set multicast on dev ens33#开启多播
[root@keepalive22 ~]# yum -y install httpd ipvsadm keepalived
[root@keepalive22 ~]# echo "sorry server 2" > /var/www/html/index.html #设置主页内容
[root@keepalive22 ~]# systemctl start httpd#启动httpd
[root@keepalive22 ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {#全局配置
   notification_email {#邮件通知地址 ,这里是本机收件地址
    root@localhost
 }
   notification_email_from keepalived@localhost   #发件地址
   smtp_server 127.0.0.1 #邮件服务地址,端口是25
   smtp_connect_timeout 30  #连接邮件服务器超时时间30秒
   router_id keepalive21 #可以使用主机名
   vrrp_mcast_group4 224.1.101.33 #设置多播地址,其他主机也一样
}

vrrp_instance VI_1 { #定义一个虚拟路由器,第一个
    state BACKUP  #当前状态,从的
    interface ens33  #当前的vrp应用,绑定到那个网卡设备上
    virtual_router_id 33#这个虚拟ip,与其他主机要保持一至
    priority 96 #优先级,低于master主机
    advert_int 1
    authentication {
        auth_type PASS  #密码认证
        auth_pass a1b2c3d4   #密码为8位,不能用默认的密码
    }
    virtual_ipaddress {#虚拟ip地址
        192.168.1.99/24 dev ens33
    }
}
vrrp_instance VI_2{ #定义一个虚拟路由器,第二个
    state  MASTER #当前状态,主的
    interface ens33  #当前的vrp应用,绑定到那个网卡设备上
    virtual_router_id 34#这个虚拟ip,与其他主机要保持一至
    priority 100 #优先级,高于其他主机
    advert_int 1
    authentication {
        auth_type PASS  #密码认证
        auth_pass a6b7c8d9   #密码为8位,不能用默认的密码,这里修改一下
    }
    virtual_ipaddress {#虚拟ip地址
        192.168.1.99/24 dev ens33 
    }
}
virtual_server 192.168.1.99 80 {
          delay_loop 1
          lb_algo wrr
          lb_kind DR
          protocol TCP
          sorry_server 127.0.0.1 80#故障页所在地址,本机所以要启动http
real_server 192.168.1.11 80 {
          weight 1 #权重
          HTTP_GET {
               url {
                  path /index.html
                 status_code 200
               }
               nb_get_retry 3
               delay_before_retry 2
               connect_timeout 3
      }
    }
}
[root@keepalive22 ~]# systemctl start keepalived#启动keepalived
[root@keepalive22 ~]# ipvsadm -ln        #查询ipvadm状态
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.1.99:80 wrr
  -> 192.168.1.11:80              Route   1      0          0         
  -> 192.168.1.12:80              Route   1      0          0     

后端主机rs1设置

[root@rs1 ~]# yum -y install httpd
[root@rs1 ~]# vim /var/www/html/index.html #编辑首页

RS Server1

[root@rs1 ~]# vim setparam.sh #!/bin/bash # vip=192.168.1.99 mask='255.255.255.255' 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 lo:0 $vip netmask $mask broadcast $vip up route add -host $vip dev lo:0 #设置本地地址广播 ;; stop)#停止还原成0值 ifconfig lo:0 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 [root@rs1 ~]# bash -n setparam.sh #检测语法 [root@rs1 ~]# bash -x setparam.sh start#执行bash [root@rs1 ~]# ifconfig ..... lo:0: flags=73 mtu 65536#添加成功 inet 192.168.1.99 netmask 255.255.255.255 loop txqueuelen 1 (Local Loopback) [root@rs1 ~]# systemctl start httpd#启动httpd服务

后端主机rs2设置

[root@rs2 ~]# yum -y install httpd
[root@rs2 ~]# vim /var/www/html/index.html #编辑首页

RS Server2

[root@rs2 ~]# vim setparam.sh #!/bin/bash # vip=192.168.1.99 mask='255.255.255.255' 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 lo:0 $vip netmask $mask broadcast $vip up route add -host $vip dev lo:0 #设置本地地址广播 ;; stop)#停止还原成0值 ifconfig lo:0 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 [root@rs2 ~]# bash -n setparam.sh #检测语法 [root@rs2 ~]# bash -x setparam.sh start#执行bash [root@rs2 ~]# ifconfig ..... lo:0: flags=73 mtu 65536#添加成功 inet 192.168.1.99 netmask 255.255.255.255 loop txqueuelen 1 (Local Loopback) [root@rs2 ~]# systemctl start httpd#启动httpd服务

客户主机测试:

  • rs1、rs2全部启动http服务,节点1、节点2其中一个分别关闭keepalived服务:
[root@vs ~]# curl http://192.168.1.99#节点1和2分别停止服务不影响调度

RS Server2

[root@vs ~]# curl http://192.168.1.99

RS Server1

[root@vs ~]# curl http://192.168.1.99

RS Server2

[root@vs ~]# curl http://192.168.1.99

RS Server1

  • 节点1、节点2全部启动keepalived服务,rs1、rs2其中一个分别停止http服务:
rs1停止http服务:
[root@vs ~]# curl http://192.168.1.99

RS Server2

[root@vs ~]# curl http://192.168.1.99

RS Server2

rs2停止http服务: [root@vs ~]# curl http://192.168.1.99

RS Server1

[root@vs ~]# curl http://192.168.1.99

RS Server1

  • rs1、rs2全部停止http服务,节点1、节点2其中一个分别关闭keepalived服务:
  • 分别返回故障页面信息
节点1关闭keepalived服务:
[root@vs ~]# curl http://192.168.1.99
sorry server 2
[root@vs ~]# curl http://192.168.1.99
sorry server 2
节点2关闭keepalived服务:
[root@vs ~]# curl http://192.168.1.99
sorry server 1
[root@vs ~]# curl http://192.168.1.99
sorry server 1

你可能感兴趣的:(基于keepalived高可用代理轮询后端服务器组)