长枪不倒 发表于 2018-12-30 08:27:26

keepalived实现lvs的高可用

  搭建环境:
      两台director,两台RS

      director1:ip(172.16.125.5),安装好keepalived;

      director2:ip(172.16.125.6),安装好keepalived;

      RS1:ip(172.16.125.7),安装好httpd;
      RS2:ip(172.16.125.8),安装好httpd;
      vip(1):172.16.125.100,vip(2):172.16.125.110。

      在此处keepalived实现lvs的高可用,使用了lvs的dr模型。关闭iptables和selinux,且实现时间同步。

  

  使用主从模式配置过程:
      (1)首先配置RS,在每一台的RS上安装好httpd,同时为了实现lvs调度算法的效果,在此故意使得每一台RS上提供的网页内容不一样。

      (2)首先设置内核参数,保证请求vip的报文必须经过director进行调度,才能到达后端RS上。
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      (3)进行每一台RS上lo接口的vip的绑定,同时设置路由。
~]# ifconfig lo:0 172.16.125.100/32 broadcast 172.16.125.100 up
~]# route add -host 172.16.125.100 dev lo:0        通过以上设置,完成了后端RS的设置。

      (4)进行两台director上keepalived服务的配置。首先设置一个虚拟路由器组,其虚拟ip为172.16.125.100(也就是vip),在虚拟主机内部配置两台RS,即后端的两台RS,具体配置文件如下所示。

      配置文件一(在主keepalived服务器上的配置):
global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 125110
    }
    virtual_ipaddress {
      172.16.125.100/16 dev eno16777736 label eno16777736:0
    }
}
virtual_server 172.16.125.100 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    protocol TCP
    real_server 172.16.125.7 80 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
    real_server 172.16.125.8 80 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
    }
    }   
}     在从keepalived所在的主机上的配置,不同的地方有两条:一,state设置为BACKUP,表明该主机为从服务器;二,priority(优先级)要低于主keepalived服务器上的优先级。

      在两台调度器上同时启动keepalived服务,进行查看集群服务的相关信息。
        一、使用ipvsadm查看负载均衡集群服务信息:
        http://s3.运维网.com/wyfs02/M01/74/EA/wKioL1YuTC2TKbnjAAEMttIGF_I797.jpg
        二、查看vip是否已经加在主keepalived服务器上了:
        http://s3.运维网.com/wyfs02/M02/74/ED/wKiom1YuTBGBwCjlAAG_-RHvsXs604.jpg
        三、在客户端访问vip,看是否能够实现负载均衡调度,此处使用轮询(rr)算法。
              http://s3.运维网.com/wyfs02/M02/74/EA/wKioL1YuTFqxlYX2AABTWGJHdks631.jpg
        四、在主keepalived服务器上关闭keepalived服务,看高可用资源是否会发生转移,同时进行vip的访问,是否能够继续进行负载均衡调度。
http://s3.运维网.com/wyfs02/M00/74/ED/wKiom1YuTDbT9HjFAAH3tIC2YKs855.jpg
  

  使用双主模型实现keepalived+lvs:
      (1)在后端RS上设置内核参数,保证请求vip的报文必须经过director进行调度,才能到达后端RS上。设置第一个vip(172.16.125.100),同时设置路由,详细配置步骤如主从模式的RS的vip的设置,在此处不再赘述。
        在后端RS上lo端口上配置第二个vip(172.16.125.110),同时设置路由。
~]# ifconfig lo:1 172.16.125.110/32 broadcast 172.16.125.110 up
~]# route add -host 172.16.125.110 dev lo:1      (2)在keepalived的服务器上修改配置文件,增加一个新的虚拟路由器,详细配置如下配置文件所示:
        主keepalived服务器1上的配置:
global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface eno16777736
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 125110
    }
    virtual_ipaddress {
    172.16.125.100/16 dev eno16777736 label eno16777736:0
    }
}
virtual_server 172.16.125.100 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    protocol TCP
    real_server 172.16.125.7 80 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
    real_server 172.16.125.8 80 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
}
    }   
}
vrrp_instance VI_2 {
    state BACKUP
    interface eno16777736
    virtual_router_id 110
    priority 90
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 123456789
    }
    virtual_ipaddress {
    172.16.125.110/16 dev eno16777736 label eno16777736:1
    }
}
virtual_server 172.16.125.110 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    protocol TCP
    real_server 172.16.125.7 80 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
    real_server 172.16.125.8 80 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
}
    }   
}      主keepalived服务器2上的配置:
global_defs {
   notification_email {
   root@localhost
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 125110
    }
    virtual_ipaddress {
    172.16.125.100/16 dev eno16777736 label eno16777736:0
    }
}
virtual_server 172.16.125.100 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    protocol TCP
    real_server 172.16.125.7 80 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
    real_server 172.16.125.8 80 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
}
    }   
}
vrrp_instance VI_2 {
    state MASTER
    interface eno16777736
    virtual_router_id 110
    priority 100
    advert_int 1
    authentication {
      auth_type PASS
      auth_pass 123456789
    }
    virtual_ipaddress {
    172.16.125.110/16 dev eno16777736 label eno16777736:1
    }
}
virtual_server 172.16.125.110 80 {
    delay_loop 3
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.0.0
    protocol TCP
    real_server 172.16.125.7 80 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
      }
    }
    real_server 172.16.125.8 80 {
      weight 1
      HTTP_GET {
            url {
            path /
            status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
}
    }   
}      (3)在两台高可用服务器上启动keepalived服务,查看vip是否分别加载到高可用服务器上。
              http://s3.运维网.com/wyfs02/M02/74/ED/wKiom1YuTWOym4qJAAIgu4l9HI4303.jpg
              http://s3.运维网.com/wyfs02/M02/74/EA/wKioL1YuTbHz1A8eAAIaHTlcApk794.jpg
        在客户端进行测试,分别访问vip(172.16.125.100)和vip(172.16.125.110)。
              http://s3.运维网.com/wyfs02/M00/74/ED/wKiom1YuTZyD7sEcAADL0rEbASg764.jpg
         可以看到,访问两个vip都可以正常访问,并且实现了rr(轮询)调度算法。
  




页: [1]
查看完整版本: keepalived实现lvs的高可用