# 网站架构优化 **Repository Path**: lingding0521/keepalived ## Basic Information - **Project Name**: 网站架构优化 - **Description**: 利用lvs+keepalived分发流量,保持次集群的可用性 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-02-11 - **Last Updated**: 2025-02-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 网站架构优化 #### 介绍 利用lvs+keepalived保障集群的负载均衡和高可用性 #### 软件架构 lvs+keepalived+lamp或lnmp #### 安装教程 1.环境 192.168.221.134 master 192.168.221.163 backup 192.168.221.166 web1 192.168.221.167 web2 2.在master和backup上安装配置Keepalived: ``` yum -y install keepalived ipvsadm ``` 3.在master上修改配置文件 ``` ! Configuration File for keepalived global_defs { router_id Director1 } vrrp_instance VI_1 { state MASTER interface ens33 virtual_router_id 51 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.221.20/24 dev ens33 } } virtual_server 192.168.221.20 80 { delay_loop 3 lb_algo rr lb_kind DR protocol TCP real_server 192.168.221.166 80 { weight 1 TCP_CHECK { connect_timeout 3 } } real_server 192.168.221.167 80 { weight 1 TCP_CHECK { connect_timeout 3 } } } ``` 4.在backup上修改配置文件,修改router_id Director2,state BACKUP,priority 100 ``` ! Configuration File for keepalived global_defs { router_id Director2 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.221.20/24 dev ens33 } } virtual_server 192.168.221.20 80 { delay_loop 3 lb_algo rr lb_kind DR protocol TCP real_server 192.168.221.166 80 { weight 1 TCP_CHECK { connect_timeout 3 } } real_server 192.168.221.167 80 { weight 1 TCP_CHECK { connect_timeout 3 } } } ``` 5.在master和backup上启动keepalived ``` systemctl start keepalived systemctl enable keepalived reboot ``` 6.web服务器配置,在web1和web2上安装http ``` yum install -y httpd && systemctl start httpd && systemctl enable httpd ``` 7.自定义web主页,以便观察负载均衡结果 web1 ``` echo "1111111111" > /var/www/html/index.html ``` web2 ``` echo "2222222222" > /var/www/html/index.html ``` 8.配置虚拟地址 复制虚拟网卡配置文件 ``` cp /etc/sysconfig/network-scripts/{ifcfg-lo,ifcfg-lo:0} vim /etc/sysconfig/network-scripts/ifcfg-lo:0 ``` 修改配置文件 ``` DEVICE=lo:0 IPADDR=192.168.221.20 NETMASK=255.255.255.255 ONBOOT=yes ``` 9.配置路由 ``` vim /etc/rc.local ``` ``` /sbin/route add -host 192.168.221.20 dev lo:0 ``` 10.配置ARP ``` vim /etc/sysctl.conf ``` 忽略arp请求可以回复 ``` net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.default.arp_ignore = 1 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 ``` 重启 ``` reboot ``` 11.测试负载均衡 ![输入图片说明](%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202025-02-11%20094642.png) 12.关闭master的keepalived服务,测试高可用,发现虚拟ip:192.168.221.20从master转移到backup上 ``` systemctl stop keepalived ``` ![输入图片说明](%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202025-02-11%20095400.png) ![输入图片说明](%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202025-02-11%20095450.png) 再次测试负载均衡 ![输入图片说明](%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202025-02-11%20095853.png) -------------到此keepalived+lvs负载均衡部署结束,后续可以在两个web服务器上部署web项目-------------