容器云负载均衡之五:使用keepalived管理基于容器的IPVS

释放双眼,带上耳机,听听看~!

一、前言

IPVS的director上会绑定一个VIP,这个VIP是下游客户的唯一接入点,所以这个VIP需要满足高可用性的要求。另外,不同的VIP策略需要根据后端real server的部署状态进行更新。

Keepalived是一个开源软件,主要提供loadbalancing(负载均衡)和 high-availability(高可用)功能,负载均衡实现需要依赖Linux的虚拟服务内核模块(IPVS),而高可用是通过VRRP协议实现多台机器之间的故障转移服务。 

转载自https://blog.csdn.net/cloudvtech

二、配置keepalived

**2.1 测试环境配置 **


1
2
3
4
5
6
1Director node 1: 200.222.0.73
2Director node 2: 200.222.0.74
3Real Server 1: 200.222.0.87
4Real Server 2: 200.222.0.89
5VIP: 200.222.0.113
6

2.2 设置iptables允许VRRP协议交互


1
2
1iptables -t filter -A IN_public_allow -p udp -m udp --dport 112  -m conntrack --ctstate NEW -j ACCEPT
2

2.3 在CentOS容器内安装keepalived


1
2
3
4
5
6
1docker pull centos
2docker run -td --privileged --net=host --name=keepalived centos
3docker exec -it keepalived bash
4yum install -y net-tools iproute
5yum install -y keepalived ipvsadm
6

将容器打包成docker image

2.4 在两个Director node启动容器

2.5 配置Director node 1的keepalived

/etc/keepalived/keepalived.conf


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
1global_defs {
2   notification_email {
3     sysadmin@mydomain.com
4     support@mydomain.com
5   }
6   notification_email_from lb1@mydomain.com
7   smtp_server localhost
8   smtp_connect_timeout 30
9}
10
11vrrp_instance VI_1 {
12    state MASTER
13    interface ens192
14    virtual_router_id 51
15    priority 100
16    advert_int 1
17    unicast_src_ip 200.222.0.73
18    unicast_peer {
19        200.222.0.74
20    }
21    authentication {
22        auth_type PASS
23        auth_pass 1111
24    }
25    virtual_ipaddress {
26        200.222.0.113
27    }
28}
29
30virtual_server 200.222.0.113 80 {
31    delay_loop 6
32    lb_algo wlc
33    lb_kind DR
34    persistence_timeout 600
35    protocol TCP
36
37    real_server 200.222.0.87 80 {
38        weight 100
39        TCP_CHECK {
40            connect_timeout 3
41        }
42    }
43    real_server 200.222.0.89 80 {
44        weight 100
45        TCP_CHECK {
46            connect_timeout 3
47        }
48    }
49}
50

2.6 配置Director node 2的keepalived

/etc/keepalived/keepalived.conf


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
1global_defs {
2   notification_email {
3     sysadmin@mydomain.com
4     support@mydomain.com
5   }
6   notification_email_from lb1@mydomain.com
7   smtp_server localhost
8   smtp_connect_timeout 30
9}
10
11vrrp_instance VI_1 {
12    state BACKUP
13    interface ens192
14    virtual_router_id 51
15    priority 99
16    advert_int 1
17    unicast_src_ip 200.222.0.74
18    unicast_peer {
19        200.222.0.73
20    }
21    authentication {
22        auth_type PASS
23        auth_pass 1111
24    }
25    virtual_ipaddress {
26        200.222.0.113
27    }
28}
29
30virtual_server 200.222.0.113 80 {
31    delay_loop 6
32    lb_algo wlc
33    lb_kind DR
34    persistence_timeout 600
35    protocol TCP
36
37    real_server 200.222.0.87 80 {
38        weight 100
39        TCP_CHECK {
40            connect_timeout 3
41        }
42    }
43    real_server 200.222.0.89 80 {
44        weight 100
45        TCP_CHECK {
46            connect_timeout 3
47        }
48    }
49}
50

2.7 启动Director node 1/2的keepalived

可以看到keepalived仅仅在node1上绑定了VIP

2.8 查看Director node 1/2的VRRP协议交互信息

tcpdump -vvv -an -i ens192 | grep "vrid 51" 

可以看到active node持续向backup node发送VRRP协议信息

2.9 使用keepalived进行IPVS director failover

在Director node1运行pkill keepalived 

在Director node1可以看到VIP被绑定

也可以看到现在是新的active node(Director node 2)持续向node1发送VRRP协议信息

2.10 重新启动node1的keepalived,node1继续成为active的IPVS director

转载自https://blog.csdn.net/cloudvtech

给TA打赏
共{{data.count}}人
人已打赏
安全经验

英文站如何做Google Adsense

2021-10-11 16:36:11

安全经验

安全咨询服务

2022-1-12 14:11:49

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索