546324 发表于 2015-10-27 08:37:30

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上。

1
2
3
4
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的绑定,同时设置路由。

1
2
~]# 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服务器上的配置):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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查看负载均衡集群服务信息:
      
      二、查看vip是否已经加在主keepalived服务器上了:
      
      三、在客户端访问vip,看是否能够实现负载均衡调度,此处使用轮询(rr)算法。
            
      四、在主keepalived服务器上关闭keepalived服务,看高可用资源是否会发生转移,同时进行vip的访问,是否能够继续进行负载均衡调度。
   

使用双主模型实现keepalived+lvs:
    (1)在后端RS上设置内核参数,保证请求vip的报文必须经过director进行调度,才能到达后端RS上。设置第一个vip(172.16.125.100),同时设置路由,详细配置步骤如主从模式的RS的vip的设置,在此处不再赘述。
      在后端RS上lo端口上配置第二个vip(172.16.125.110),同时设置路由。

1
2
~]# 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上的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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上的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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是否分别加载到高可用服务器上。
            
            
      在客户端进行测试,分别访问vip(172.16.125.100)和vip(172.16.125.110)。
            
       可以看到,访问两个vip都可以正常访问,并且实现了rr(轮询)调度算法。

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