keepalived +lvs应用部署

一、keepalived概述:
概述:keepalived起初为Lvs设计的一款强大的辅助工具,保证Lvs负载调度器的故障切换以及web节点的健康状态检查,后续被应用到很多需要容错的场景下,keepalived自身基于VRRP协议–虚拟路由冗余协议,思科公有协议;

案例环境:
主服务器 192.168.100.101
从服务器 192.168.100.102
安装node1节点上的httpd的服务;
安装node2节点上的httpd的服务;
在两台node节点上安装keepalived软件程序(两台安装步骤一致,在此只列出一台);
配置node1上master主节点;
配置node2上backup从节点;
客户端访问测试双机热备的效果;
扩展内容:上述步骤实现了keepalived在real server节点池中进行了四层健康检测(传输层,基于协议或者端口),下边展示keepalived如何对节点池中进行七层健康检测(应用层);

安装node1节点上的httpd的服务;

[root@node1 ~]# yum -y install httpd
[root@node1 ~]# cat <>/var/www/html/index.html
192.168.100.202
END
[root@node1 ~]# systemctl start httpd
[root@node1 ~]# systemctl enable httpd
[root@node1 ~]# netstat -utpln |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      13891/httpd

安装node2节点上的httpd的服务;

[root@node2 ~]# yum -y install httpd
[root@node2 ~]# cat <>/var/www/html/index.html
192.168.100.203
END
[root@node2 ~]# systemctl start httpd
[root@node2 ~]# systemctl enable httpd
[root@node2 ~]# netstat -utpln |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      13891/httpd

在两台node节点上安装keepalived软件程序(两台安装步骤一致,在此只列出一台);
[root@node1 ~]# yum -y install kernel-devel openssl-devel popt-devel ##安装内核开发包,popt支持库等工具;

[root@node1 ~]# tar -zxvf keepalived-1.2.13.tar.gz -C /usr/src/
[root@node1 ~]# cd /usr/src/keepalived-1.2.13/
[root@node1 keepalived-1.2.13]# ./configure --prefix=/usr/local/keepalived 
[root@node1 keepalived-1.2.13]# make &&make install
[root@node1 keepalived-1.2.13]# cd
[root@node1 ~]# mkdir -p /etc/keepalived						##程序的主配置目录
[root@node1 ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/	##复制主配置文件
[root@node1 ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/		##复制启动时需要加载的配置文件
[root@node1 ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/		##复制服务的控制脚本
[root@node1 ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/			##复制keepalived的命令
[root@node1 ~]# chmod 755 /etc/init.d/keepalived	
		##为控制脚本指定权限

配置node1上master主节点;

[root@node1 ~]# vi /etc/keepalived/keepalived.conf
global_defs {
    router_id HA_TEST_R1		##本服务器的名称,若环境中有多个keepalived时,此名称不能一致
}
vrrp_instance VI_1 {			##定义VRRP热备实例,每一个keep组都不同
    state MASTER				##MASTER表示主服务器
    interface eth0				##承载VIP地址的物理接口
    virtual_router_id 1			##虚拟路由器的ID号,每一个keep组都不同
    priority 100				##优先级,数值越大优先级越高
    advert_int 1				##通告检查间隔秒数(心跳频率)
    authentication {			##认证信息
        auth_type PASS		##认证类型
        auth_pass 123456		##密码字串
    }
    virtual_ipaddress {
  192.168.100.95				##指定漂移地址(VIP)
    }
}
virtual_server 192.168.100.95 80 { 	 #vip配置
    delay_loop 2  				 #每隔2秒检查一次real_server状态
    lb_algo wrr					##指定lvs的调度算法
    lb_kind DR					##lvs集群模式(如若不结合LVS请删掉此三个配置)
    persistence_timeout 60 		##会话保持时间
    protocol TCP					##选择协议
    real_server 192.168.100.101 80 { 		 ##本机地址
        weight :3				##服务器的权重
        notify_down /etc/keepalived/check.sh		##指定节点失效后,采用的脚本,notify_up表示节点正常后,采用的脚本
		##健康检查方式一共有HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK这些
        TCP_CHECK {
            connect_timeout 10   	##连接超时时间
            nb_get_retry 3    	##重连次数
            delay_before_retry 3  	##重连间隔时间
			connect_port 80   	##健康检查端口
        }
    }
   }
:wq
[root@node1 ~]# vi /etc/keepalived/check.sh
#!/bin/bash
/etc/init.d/keepalived stop
echo -e "$(ip a |grep eth0 |grep inet |awk '{print $2}'|awk -F'/' '{print $1}') (httpd) is down on $(date +%F-%T)" >>/root/check_httpd.log
:wq
[root@node1 ~]# chmod 777 /etc/keepalived/check.sh
[root@node1 ~]# /etc/init.d/keepalived start
Starting keepalived (via systemctl):                       [  确定  ]
[root@node1 ~]# ip a |grep 192.168.100.95
    inet 192.168.100.95/32 scope global eth0

配置node2上backup从节点

[root@node2 ~]# vi /etc/keepalived/keepalived.conf
global_defs {
    router_id HA_TEST_R2			##本服务器的名称
}
vrrp_instance VI_1 {
    state BACKUP					##BACKUP表示从服务器
    interface eth0
    virtual_router_id 1
    priority 99					##优先级,低于主服务器
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 123456
    }
    virtual_ipaddress {
        192.168.100.95
    }
}
virtual_server 192.168.100.95 80 { 	 ##vip配置
    delay_loop 2 					 ##每隔2秒检查一次real_server状态
    lb_algo wrr
    lb_kind DR					##如若不结合LVS,需要去掉此三个配置项
    persistence_timeout 60  		 ##会话保持时间
    protocol TCP
    real_server 192.168.100.102 80 {  	##本机地址
        weight :3
        notify_down /etc/keepalived/check.sh
        TCP_CHECK {
            connect_timeout 10   		##连接超时时间
            nb_get_retry 3    		##重连次数
            delay_before_retry 3  		##重连间隔时间
        	connect_port 80   			##健康检查端口
        }
    }
   }
[root@node2 ~]# vi /etc/keepalived/check.sh
#!/bin/bash
/etc/init.d/keepalived stop
echo -e "$(ip a |grep eth0 |grep inet |awk '{print $2}'|awk -F'/' '{print $1}') (httpd) is down on $(date +%F-%T)" >>/root/check_httpd.log
:wq
[root@node2 ~]# chmod 777 /etc/keepalived/check.sh
[root@node2 ~]# /etc/init.d/keepalived start
Starting keepalived (via systemctl):                       [  确定  ]

[root@node2~]# ip a |grep 192.168.100.95

客户端访问测试双机热备的效果;
本实验虚拟地址为: 192.168.100.95
直接访问就可以 ;
两台服务器的web访问页面上 写点不一样的
关闭一台 能访问到另一台就成功实现负载均衡 和 lvs 了

你可能感兴趣的:(keepalived)