拓扑图
安装keepalived
[root@node1 ~]# yum install -y keepalived
修改keepalived MASTER 配置文件
[root@node1 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc router_id LVS_DEVEL} vrrp_instance VI_1 { state MASTER ##定义主节点 interface eth0 virtual_router_id 51 ##识别虚拟路由的id,相同的id则会被识别为1组 priority 100 ##优先级,优先级高的会选举为主节点 advert_int 1 authentication { auth_type PASS ##认证方式为PASS 还有AH auth_pass 1111 } virtual_ipaddress { 172.16.30.100 ##vip即虚拟IP }} virtual_server 172.16.30.100 80 { delay_loop 6 lb_algo rr ##调度模式rr,轮询 lb_kind DR ##负载均衡模型为DR 另外还有NAT persistence_timeout 0 ##超时时长,在每个节点停留的时间,此处设为0,默认为50s,则会在单一节点停留50秒在去另一节点 protocol TCP real_server 172.16.30.10 80 { ##后端rs主机 weight 1 TCP_CHECK { connect_port 80 ##连接端口 connect_timeout 3 ##连接超时时间 nb_get_retry 3 ##重试次数 delay_before_retry 3 ##每次重试之间的间隔时间 } real_server 172.16.30.11 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } }}
将配置文件复制到另一节点
[root@node1 ~]# scp /etc/keepalived/keepalived.conf172.16.30.2:/etc/keepalived/keepalived.conf
修改 state priority 如下图
启动keepalived测试vip
MASTER ip,停掉MASTER keepalived服务
BACKUP ip
VIP成功转移
配置web服务器
安装httpd
[root@node10 ~]# yum install -y httpd
提供网页文件
[root@node10 ~]# vim /var/www/html/index.html<h1>page from node10</h1>
node11同样的操作
配置rs模型
[root@node10 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore ##关闭arp转发[root@node10 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore[root@node10 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce [root@node10 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce [root@node10 ~]# ifconfig lo:0 172.16.30.100 netmask 255.255.255.255 broadcast 172.16.30.100 up ##在lo:0口设置VIP 并只对自己广播[root@node10 ~]# ifconfig eth0 Link encap:Ethernet HWaddr 08:00:27:02:A7:9A inet addr:172.16.30.10 Bcast:172.16.255.255 Mask:255.255.0.0 inet6 addr: fe80::a00:27ff:fe02:a79a/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:5319 errors:0 dropped:0 overruns:0 frame:0 TX packets:685 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:485435 (474.0 KiB) TX bytes:55938 (54.6 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) lo:0 Link encap:Local Loopback inet addr:172.16.30.100 Mask:255.255.255.255 UP LOOPBACK RUNNING MTU:65536 Metric:1
node11同样操作
分别启动httpd服务
访问测试
现在将node1上的keepalived服务停掉
再次访问还是同样的结果,我们再来看一下node2上的ip,vip已经转移过来了
这样便实现了lvs+keepalived的高可用负载均衡,lvs负责web服务器的负载均衡,而keepalived可以保证lvs不会发生单点故障。
本文出自 “LINUXU” 博客,请务必保留此出处http://linuxu.blog.51cto.com/9471357/1613488