keepalived+lvs+apche实现高可用负载均衡

Keepalived简介

Keepalived起初是为LVS设计的,专门用来监控集群系统中各个服务节点的状态,它根据TCP/IP参考模型的第三、第四层、第五层交换机制检测每个服务节点的状态,如果某个服务器节点出现异常,或者工作出现故障,Keepalived将检测到,并将出现的故障的服务器节点从集群系统中剔除,这些工作全部是自动完成的,不需要人工干涉,需要人工完成的只是修复出现故障的服务节点。

本次实验的拓扑结构:

在这里插入图片描述

实验步骤:

同步集群中机器的时间,关闭防火墙和selinux

ntpdate cn.pool.ntp.org
systemctl stop firewalld.service
setenforce 0

对lvs1/2的操作

  1. 下载必要工具

    	yum install gcc gcc-c++ openssl-devel.x86_64 -y  
    
  2. 下载keepalived和LVS工具
    网址:https://keepalived.org

      wget https://keepalived.org/software/keepalived-2.0.10.tar.gz
      yum install ipvsadm.x86_64 -y  
    
  3. 安装编译keepalived

    	tar -xzf keepalived-2.0.10.tar.gz -C /usr/local/src/
    	cd /usr/local/src/keepalived-2.0.10/
    	mkdir /data
        yum install libnl-devel.x86_64 -y
    	./configure --prefix=/data/kepalived
    

    在这里插入图片描述

    	[root@master1 keepalived-2.0.10]# make && make install
    
  4. .配置启动项

     	cp /data/kepalived/etc/init.d/keepalived /etc/init.d/
       cp /data/kepalived/etc/sysconfig/keepalived /etc/sysconfig/
       mkdir /etc/keepalived
       cp /data/kepalived/etc/keepalived/keepalived.conf /etc/keepalived/
    
  5. 修改配置文件

    	vim /etc/keepalived/keepalived.conf
    
    #主节点的配置文件
    ! Configuration File for keepalived
    
    global_defs {
    notification_email {
    	 acassen@firewall.loc
      	}
      	notification_email_from Alexandre.Cassen@firewall.loc
      	smtp_server 127.0.0.1
      	smtp_connect_timeout 30
      	router_id LVS_1
    }
    
    vrrp_instance VI_1 {
    	 state MASTER  
     	interface eth0
     	lvs_sync_daemon_interface eth0
     	virtual_router_id 51
     	priority 150    
    	 advert_int 1
    	 authentication {
        	 auth_type PASS        
         	auth_pass 1111    
    	}    	
    	virtual_ipaddress {        
        	 192.168.192.123
    	}
    }
    
    virtual_server 192.168.192.123 80 {    
    	 delay_loop 6    
    	lb_algo wrr     
    	lb_kind DR    
    	nat_mask 255.255.255.255
    	persistence_timeout 300   
    	protocol TCP
    
    	real_server 192.168.192.139 80 {
        	weight 1
       	 	TCP_CHECK {
        	    connect_timeout 8        
            	nb_get_retry 3        
            	delay_before_retry 3        
            	connect_port 80
            	}    
       	 }
    
    	real_server 192.168.192.140 80 {        
        	weight 1        
       	 TCP_CHECK {        
       	     	connect_timeout 8        
       	     	nb_get_retry 3        
        	    delay_before_retry 3        
            	conect_port 80        
        	}    
     }
    }
    
    #从节点的配置文件
    ! Configuration File for keepalived
    
    global_defs {
    notification_email {
    	 acassen@firewall.loc
    	  }
      	notification_email_from Alexandre.Cassen@firewall.loc
      	smtp_server 127.0.0.1
     	 smtp_connect_timeout 30
      	router_id LVS_1
    }	
    
    vrrp_instance VI_1 {
       state BACKUP
       interface eth0
       lvs_sync_daemon_interface eth0
       virtual_router_id 51
     	priority 100
     	advert_int 1
     	authentication {
        	 auth_type PASS        
         	auth_pass 1111    
    	}    
    	virtual_ipaddress {
        	 192.168.192.123
     }
    }
    
    virtual_server 192.168.192.123 80 {    
     delay_loop 6    
     lb_algo wrr     
     lb_kind DR    
     nat_mask 255.255.255.255
     persistence_timeout 300   
     protocol TCP
     real_server 192.168.192.140 80 {
    	    weight 1
        	TCP_CHECK {
            	connect_timeout 8        
           		nb_get_retry 3        
            	delay_before_retry 3        
            	connect_port 80
           	}    
        }
    
     real_server 192.168.192.139 80 {        
    	    weight 1        
        	TCP_CHECK {        
            	connect_timeout 8        
            	nb_get_retry 3        
            	delay_before_retry 3        
            	conect_port 80        
        	}    
    	}
    }
    
  6. 开启服务并查看LVS

    systemctl start keepalived
    

    在这里插入图片描述

对 RS1/2节点操作

  1. 禁止响应对VIP的ARP请求
    由于是基于LVS的DR模型,我们需要设置本机VIP不能响应外部主机的arp请求

    echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
    echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
    echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
    
  2. 设置vip

    ifconfig lo:0 192.168.192.123 netmask 255.255.255.255
    
  3. 配置httpd的页面

    yum install httpd.x86_64 -y
    
    #192.168.192.139
    echo web 139 > /var/www/html/index.html
    
    #192.168.192.140
    echo web 140 > /var/www/html/index.html
    

测试

查看LVS的配置情况

ipvsadm -Ln

在这里插入图片描述
在clien进行测试
在这里插入图片描述
keepalived的配置文件文件默认在 /etc/keepalived/keepalived.conf ,不是随意存放的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值