Keepalived+LVS负载均衡双机备热

版权声明:转载请通知 https://blog.csdn.net/qq_41674452/article/details/84890948

实验环境

两台Keepalived服务器,三台提供http服务的节点服务器
主调度器(192.168.3.4)
从调度器(192.168.3.5)
HTTP-1(192.168.3.1)
HTTP-2(192.168.3.2)
HTTP-3(192.168.3.3)
  1. 主调度器:
	[root@Master ~]# yum -y install keepalived ipvsadm
   ###如果交换机外网有独立的IP可以不用指定虚拟IP###
   [root@Master ~]# cd /etc/sysconfig/network-scripts/
   [root@Master ~]# cp ifcfg-ens32 ifcfg-ens32:0
   [root@Master ~]# vim ifcfg-ens32:0			#配置虚拟IP地址
   NAME=ens32:0
   DEVICE=ens32:0
   ONBOOT=yes
   IPADDR=192.168.3.10
   NETMASK=255.255.255.0
   :wq!
   [root@Master ~]# ifup ens32:0				#开启虚拟IP
   [root@Master ~]# ifconfig ens32:0			#查看
   [root@Master ~]# vim /etc/sysctl.conf			#调整/proc响应参数
   ...
   	net.ipv4.conf.all.send_redirects = 0
   	net.ipv4.conf.default.send_redirects = 0
   	net.ipv4.conf.ens32.send_redirects = 0
   	:wq!
   [root@Master ~]# cd /etc/keepalived
   [root@Master ~]# cp keepalived.conf keepalived.conf.bak
   [root@Master ~]# systemctl enable keepalived.service 
   [root@Master ~]# vim /etc/keepalived/keeplived.conf
   global_defs {
      router_id LVS_DR_1		#路由器名称
   }

   vrrp_instance VI_1 {
       state MASTER
       interface ens32			#网卡
       virtual_router_id 1
       priority 100			#优先级
       advert_int 1
       authentication {
           auth_type PASS
           auth_pass 123456		#匹配密码
       }
       virtual_ipaddress {
           192.168.3.10			#VIP地址
       }
   }
   
   virtual_server 192.168.3.10 80 {
       delay_loop 6
       lb_algo rr
       lb_kind DR
       persistence_timeout 50
       protocol TCP
   
       real_server 192.168.3.1 80 {			#真实服务器
           weight 1
           TCP_CHECK {
                   connect_port 80
                   connect_timeout 3
                   nb_get_retry 3
                   delay_before_retry 4
                   }
           }
       real_server 192.168.3.2 80 {
           weight 1
           TCP_CHECK {
                   connect_port 80
                   connect_timeout 3
                   nb_get_retry 3
                   delay_before_retry 4
                   }
           }
       real_server 192.168.3.3 80 {
           weight 1
           TCP_CHECK {
                   connect_port 80
                   connect_timeout 3
                   nb_get_retry 3
                   delay_before_retry 4
                   }
           }
   }
   :wq!
   [root@MASTER ~]# systemctl restart keepalived.service
   [root@MASTER ~]# ip a
  1. 从调度器:
  [root@BACKUP ~]# yum -y install keeplived ipvsadm
  [root@BACKUP ~]# systemctl enable keepalived.service
  [root@BACKUP ~]# cd /etc/keepalived
  [root@BACKUP ~]# cp keepalived.conf keepalived.conf.bak
  [root@BACKUP ~]# vim /etc/keepalived/keeplived.conf
  global_defs { 
     router_id LVS_DR_2		#路由器名称
  }
  
  vrrp_instance VI_1 {
      state BACKUP
      interface ens32			#网卡
  	virtual_router_id 1
  	priority 99			#优先级
  	advert_int 1
  	authentication {
  	    auth_type PASS
  	    auth_pass 123456		#匹配密码
  	}
  	virtual_ipaddress {
  	    192.168.3.10		#漂移地址
  	}
  }
  
  virtual_server 192.168.3.10 80 {		#真实服务器
      delay_loop 6
      lb_algo rr
      lb_kind DR
      persistence_timeout 50
      protocol TCP
  
      real_server 192.168.3.1 80 {
          weight 1
          TCP_CHECK {
                  connect_port 80
                  connect_timeout 3
                  nb_get_retry 3
                  delay_before_retry 4
                  }
          }
      real_server 192.168.3.2 80 {
          weight 1
          TCP_CHECK {
                  connect_port 80
                  connect_timeout 3
                  nb_get_retry 3
                  delay_before_retry 4
                  }
          }
      real_server 192.168.3.3 80 {
          weight 1
          TCP_CHECK {
                  connect_port 80
                  connect_timeout 3
                  nb_get_retry 3
                  delay_before_retry 4
                  }
          }
  }
  :wq!
  	[root@MASTER ~]# systemctl restart keepalived
  1. 节点服务器
 [root@HTTP ~]# cd /etc/sysconfig/network-scripts/
 [root@HTTP ~]#  cp ifcfg-lo ifcfg-lo:0
 [root@HTTP ~]# vim ifcfg-lo:0			#配置虚拟IP,要和LVS虚拟IP一致,子网掩码全为1
 	DEVICE=lo:0
 	IPADDR=192.168.3.5
 	NETMASK=255.255.255.255 
 	:wq!
 [root@HTTP ~]# ifup lo:0
 [root@HTTP ~]# ifconfig lo:0
 [root@HTTP ~]# vim /etc/rc.local 		#添加VIP本地访问路由	
 	...
 	/sbin/route add -host 192.168.3.10 dev lo:0
 	:wq!
 [root@HTTP ~]# route add -host 192.168.3.10 dev lo:0	
 [root@HTTP ~]# vim /etc/sysctl.conf			#调整/proc响应参数
 	...
 	net.ipv4.conf.all.arp_ignore = 1
 	net.ipv4.conf.all.arp_announce = 2
 	net.ipv4.conf.default.arp_ignore = 1
 	net.ipv4.conf.default.arp_announce = 2
 	net.ipv4.conf.lo.arp_ignore = 1
 	net.ipv4.conf.lo.arp_announce = 2
 	:wq!
 [root@HTTP ~]# sysctl -p
  1. 测试:
    客户端访问“http://192.168.3.10
    关闭主调度器,客户端任然能够正常访问节点服务器
    关闭一台节点服务器,客户端任然能正常访问其他节点服务器

猜你喜欢

转载自blog.csdn.net/qq_41674452/article/details/84890948