5、haproxy+keepalived实现负载均衡与高可用
haproxy+keepalived实现负载均衡与高可用
环境:
| 主机名 | ip地址 | 规格 | 系统 | 说明 |
|---|---|---|---|---|
| k8s-haproxy-01 | 172.31.3.144 | 2C4G | Ubuntu Server 20.04 | master 节点 |
| k8s-haproxy-01 | 172.31.3.145 | 2C4G | Ubuntu Server 20.04 | backup 节点 |
下载keepalived和haproxy
root@k8s-haproxy-01:~# apt install -y keepalived haproxy
配置keepalived
直接在模板文件基础上修改配置
master节点配置文件
root@k8s-haproxy-01:~# cp /usr/share/doc/keepalived/samples/keepalived.conf.vrrp /etc/keepalived/keepalived.conf
root@k8s-haproxy-01:~# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_10
}
vrrp_instance VI_1 {
state MASTER
interface ens33
garp_master_delay 10
smtp_alert
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.31.3.150 dev ens33 label ens33:0
172.31.3.151 dev ens33 label ens33:1
172.31.3.152 dev ens33 label ens33:2
172.31.3.153 dev ens33 label ens33:3
172.31.3.154 dev ens33 label ens33:4
}
}
backup节点配置文件
root@k8s-haproxy-02:~# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_11
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
garp_master_delay 10
smtp_alert
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.31.3.150 dev ens33 label ens33:0
172.31.3.151 dev ens33 label ens33:1
172.31.3.152 dev ens33 label ens33:2
172.31.3.153 dev ens33 label ens33:3
172.31.3.154 dev ens33 label ens33:4
}
}
切换测试

配置haproxy
测试haproxy是否可以正常使用,本次连接一个nginx。
#两个节点添加配置
root@k8s-haproxy-01:~# vim /etc/haproxy/haproxy.cfg
listen haproxy-80
bind 172.31.3.120:80
mode tcp
server server1 172.31.3.150:80 check inter 3s fall 3 rise 3

浙公网安备 33010602011771号