keepalived基礎知識總結(2)
阿新 • • 發佈:2022-03-27
keepalived基礎知識總結(2)
上一篇部落格已經對keepalived原理,應用,配置以及和nginx做了簡單的搭配,這一篇主要對keepalived+lvs做個實驗和總結。
1、拓撲圖
2、安裝配置
[root@init-04 ~]# yum install keepalived ipvsadm -y [root@init-05 ~]# yum install keepalived ipvsadm -y # 修改配置檔案 [root@init-04 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LB1 # 兩邊不一樣。 } vrrp_instance VI_2 { state MASTER # 另外一臺機器是BACKUP interface ens33 # 心跳網絡卡 virtual_router_id 51 # 虛擬路由編號,主備要一致 priority 150 # 優先順序 advert_int 1 # 檢查間隔,單位秒 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.253.120/24 dev ens33 #VIP和工作介面 } } virtual_server 192.168.253.120 80 { # LVS 配置,VIP delay_loop 3 # 服務論詢的時間間隔,#每隔3秒檢查一次real_server狀態 lb_algo rr # LVS 排程演算法 lb_kind DR # LVS 叢集模式 protocol TCP real_server 192.168.253.133 80 { weight 1 TCP_CHECK { connect_timeout 3 # 健康檢查方式,連線超時時間 } } real_server 192.168.253.134 80 { weight 1 TCP_CHECK { connect_timeout 3 } } } [root@init-05 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id LB2 # 兩邊不一樣。 } vrrp_instance VI_2 { state BACKUP # 另外一臺機器是BACKUP interface ens33 # 心跳網絡卡 virtual_router_id 51 # 虛擬路由編號,主備要一致 priority 100 # 優先順序 advert_int 1 # 檢查間隔,單位秒 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.253.120/24 dev ens33 #VIP和工作介面 } } virtual_server 192.168.253.120 80 { # LVS 配置,VIP delay_loop 3 # 服務論詢的時間間隔,#每隔3秒檢查一次real_server狀態 lb_algo rr # LVS 排程演算法 lb_kind DR # LVS 叢集模式 protocol TCP real_server 192.168.253.133 80 { weight 1 TCP_CHECK { connect_timeout 3 # 健康檢查方式,連線超時時間 } } real_server 192.168.253.134 80 { weight 1 TCP_CHECK { connect_timeout 3 } } } [root@init-04 ~]# systemctl enable keepalived && reboot [root@init-05 ~]# systemctl enable keepalived && reboot [root@init-06 ~]# yum install -y httpd && systemctl start httpd && systemctl enable httpd [root@init-07 ~]# yum install -y httpd && systemctl start httpd && systemctl enable httpd [root@init-06 ~]# netstat -antp | grep httpd tcp6 0 0 :::80 :::* LISTEN 6820/httpd [root@init-07 ~]# netstat -antp | grep httpd tcp6 0 0 :::80 :::* LISTEN 7184/httpd [root@init-06 ~]# echo init-06 > /var/www/html/index.html [root@init-07 ~]# echo init-07 > /var/www/html/index.html [root@init-06 ~]# cp /etc/sysconfig/network-scripts/{ifcfg-lo,ifcfg-lo:0} [root@init-06 ~]# vim /etc/sysconfig/network-scripts/ifcfg-lo:0 DEVICE=lo:0 IPADDR=192.168.253.120 NETMASK=255.255.255.255 #NETWORK=127.0.0.0 # If you're having problems with gated making 127.0.0.0/8 a martian, # you can change this to something else (255.255.255.255, for example) #BROADCAST=127.255.255.255 ONBOOT=yes #NAME=loopback [root@init-06 ~]# scp /etc/sysconfig/network-scripts/ifcfg-lo:0 192.168.253.134:/etc/sysconfig/network-scripts/ [root@init-06 ~]# vim /etc/rc.local #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local /sbin/route add -host 192.168.253.120 dev lo:0 [root@init-07 ~]# vim /etc/rc.local #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local /sbin/route add -host 192.168.253.120 dev lo:0 [root@init-06 ~]# vim /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). 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 [root@init-07 ~]# vim /etc/sysctl.conf # sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/. # # Vendors settings live in /usr/lib/sysctl.d/. # To override a whole file, create a new file with the same in # /etc/sysctl.d/ and put new settings there. To override # only specific settings, add a file with a lexically later # name in /etc/sysctl.d/ and put new settings there. # # For more information, see sysctl.conf(5) and sysctl.d(5). 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 [root@init-06 ~]# reboot [root@init-07 ~]# reboot [root@init-04 ~]# ipvsadm -L IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP init-05:http rr -> 192.168.253.133:http Route 1 0 0 -> 192.168.253.134:http Route 1 0 0
3、應用測試
# 對兩個web進行訪問測試。
# 根據vip進行訪問。
# 測試1,將master宕掉,檢視vip和路由情況,並對頁面進行訪問。 # 測試2,將web-init06宕掉,檢視頁面訪問情況。 # 總結:本次實驗答題思路是通過lvs+keepalived的方式,對來自虛擬ip的流量進行分發,通過路由轉發至lo:0,再將請求轉到後端真實的伺服器上。所以大體思路就可以進行劃分為配置keepalived並配置lvs四層負載均衡,配置後端伺服器應用與迴環介面lo:0,而keepalived.conf分為兩部分,一部分為keepalived相關配置檔案,一部分為lvs的配置。四層負載均衡是通過ip+埠的方式進行流量分發,所以在keepalive.conf檔案中採用ip加埠的形式,原理和流程更加熟悉和理解,更方便我們進行環境部署、應用配置和故障排查。