当前位置:   article > 正文

LVS + KeepAlived实现高可用负载均衡

LVS + KeepAlived实现高可用负载均衡

上文已经介绍了lvs和keepalived的基本概念和用法,下面直接做lvs和keepalived来实现高可用负载均衡

配置:

主机名ip系统用途
client172.16.147.1mac客户端
lvs-keepalived-master172.16.147.154centos7.5分发器
lvs-keepalived-slave172.16.147.155centos7.5分发器备
test-nginx1172.16.147.153centos7.5web1
test-nginx2172.16.147.156centos7.5web2
vip

1. 主备调度器安装软件

  1. [root@lvs-keepalived-master ~]# yum -y install ipvsadm keepalived
  2. [root@lvs-keepalived-slave ~]# yum -y install ipvsadm keepalived
  3. 2. Keepalived
  4. lvs-master
  5. [root@lvs-keepalived-master ~]# vim /etc/keepalived/keepalived.conf
  6. ! Configuration File for keepalived
  7. global_defs {
  8. router_id lvs-keepalived-master #辅助改为lvs-backup
  9. }
  10. vrrp_instance VI_1 {
  11. state MASTER
  12. interface ens33 #VIP绑定接口
  13. virtual_router_id 80 #VRID 同一组集群,主备一致
  14. priority 100 #本节点优先级,辅助改为50
  15. advert_int 1 #检查间隔,默认为1s
  16. authentication {
  17. auth_type PASS
  18. auth_pass 1111
  19. }
  20. virtual_ipaddress {
  21. 172.16.147.101/24 # 可以写多个vip
  22. }
  23. }
  24. virtual_server 172.16.147.101 80 { #LVS配置
  25. delay_loop 3
  26. lb_algo rr #LVS调度算法
  27. lb_kind DR #LVS集群模式(路由模式)
  28. net_mask 255.255.255.0
  29. protocol TCP #健康检查使用的协议
  30. real_server 172.16.147.153 80 {
  31. weight 1
  32. inhibit_on_failure #当该节点失败时,把权重设置为0,而不是从IPVS中删除
  33. TCP_CHECK { #健康检查
  34. connect_port 80 #检查的端口
  35. connect_timeout 3 #连接超时的时间
  36. }
  37. }
  38. real_server 172.16.147.156 80 {
  39. weight 1
  40. inhibit_on_failure
  41. TCP_CHECK {
  42. connect_timeout 3
  43. connect_port 80
  44. }
  45. }
  46. }

2. 备用机安装和配置

  1. [root@lvs-keepalived-slave ~]# vim /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. router_id lvs-keepalived-slave
  5. }
  6. vrrp_instance VI_1 {
  7. state BACKUP
  8. interface ens33
  9. nopreempt #不抢占资源
  10. virtual_router_id 80
  11. priority 50
  12. advert_int 1
  13. authentication {
  14. auth_type PASS
  15. auth_pass 1111
  16. }
  17. virtual_ipaddress {
  18. 172.16.147.101/24
  19. }
  20. }
  21. virtual_server 172.16.147.101 80 {
  22. delay_loop 3
  23. lb_algo rr
  24. lb_kind DR
  25. net_mask 255.255.255.0
  26. protocol TCP
  27. real_server 172.16.147.153 80 {
  28. weight 1
  29. inhibit_on_failure
  30. TCP_CHECK {
  31. connect_port 80
  32. connect_timeout 3
  33. }
  34. }
  35. real_server 172.16.147.156 80 {
  36. weight 1
  37. inhibit_on_failure
  38. TCP_CHECK {
  39. connect_timeout 3
  40. connect_port 80
  41. }
  42. }
  43. }

3. 启动KeepAlived

  1. [root@lvs-keepalived-master ~]# systemctl start keepalived
  2. [root@lvs-keepalived-master ~]# systemctl enable keepalived
  3. [root@lvs-keepalived-master ~]# ipvsadm -Ln
  4. IP Virtual Server version 1.2.1 (size=4096)
  5. Prot LocalAddress:Port Scheduler Flags
  6. -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  7. TCP 172.16.147.101:80 rr persistent 20
  8. -> 172.16.147.153:80 Route 1 0 0
  9. -> 172.16.147.156:80 Route 0 0 0

4. 所有RS配置(nginx1,nginx2)

  1. [root@test-nginx1 ~]# yum install -y nginx
  2. [root@test-nginx2 ~]# yum install -y nginx
  3. [root@test-nginx1 ~]# echo "ip addr add dev lo 172.16.147.101/32" >> /etc/rc.local
  4. [root@test-nginx1 ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf
  5. [root@test-nginx1 ~]# echo "net.ipv4.conf.all.arp_announce = 2" >> /etc/sysctl.conf
  6. [root@test-nginx1 ~]# sysctl -p
  7. [root@test-nginx1 ~]# echo "web1..." >> /usr/share/nginx/html/index.html
  8. [root@test-nginx1 ~]# systemctl start nginx
  9. [root@test-nginx1 ~]# chmod +x /etc/rc.local

上述步骤实现了两台机器的高可用并进行172.16.147.156和172.16.147.153的负载均衡

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/492396
推荐阅读
相关标签
  

闽ICP备14008679号