1. 環境准備
1. VMware; 2. 4台CentOs7虛擬主機:192.168.122.248,192.168.122.68, 192.168.122.110, 192.168.122.167 3. 系統服務:LVS, Keepalived 4. Web服務器:nginx 5. 集群搭建:LVS DR模式
2. 軟件安裝
在四台虛擬機上,我們以如下方式搭建集群:
192.168.122.248 lvs+keepalived-1
192.168.122.68 lvs+keepalived-2
192.168.122.110 nginx-1
192.168.122.167 nginx-2
這里我們使用192.168.122.248和192.168.122.68兩台機器作為lvs+keepalived
的工作機器,也就是說這兩台機器的作用主要是進行負載均衡和故障檢測和下線的;我們使用192.168.122.110和192.168.122.167兩台機器作為應用服務器,主要是對外提供服務的。這四台服務器作為整個后端集群服務,並且對外提供的虛擬ip是192.168.122.120
。需要說明的是,這里的keepalived
所檢測的服務是兩台lvs
服務器,這兩台服務器,一台作為master服務器,一台作為backup服務器,兩者在負載均衡的配置上是完全一樣的。在正常情況下,客戶端請求虛擬ip的時候,lvs
會將該請求轉發到master服務器上,然后master服務器根據配置的負載均衡策略選擇一台應用服務器,並且將請求發送給該應用服務器進行處理。如果在某個時刻,lvs的master服務器由於故障宕機了,keepalived就會檢測到該故障,並且進行故障下線,然后將backup機器上線用於提供服務,從而實現故障轉移的功能。
2.1 lvs+keepalived安裝
在192.168.122.248和192.168.122.68上安裝ipvs和keepalived:
[root@lvskeepalived-1 ~]# yum install ipvsadm keepalived -y Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com base | 3.6 kB 00:00:00 epel | 5.3 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/2): epel/x86_64/updateinfo | 999 kB 00:00:00 (2/2): epel/x86_64/primary_db | 6.8 MB 00:00:01 Package ipvsadm-1.27-7.el7.x86_64 already installed and latest version Resolving Dependencies --> Running transaction check ---> Package keepalived.x86_64 0:1.3.5-8.el7_6.5 will be installed --> Processing Dependency: libnetsnmpmibs.so.31()(64bit) for package: keepalived-1.3.5-8.el7_6.5.x86_64 --> Processing Dependency: libnetsnmpagent.so.31()(64bit) for package: keepalived-1.3.5-8.el7_6.5.x86_64 --> Processing Dependency: libnetsnmp.so.31()(64bit) for package: keepalived-1.3.5-8.el7_6.5.x86_64 --> Running transaction check ---> Package net-snmp-agent-libs.x86_64 1:5.7.2-38.el7_6.2 will be installed ---> Package net-snmp-libs.x86_64 1:5.7.2-38.el7_6.2 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================================================================================== Installing: keepalived x86_64 1.3.5-8.el7_6.5 updates 331 k Installing for dependencies: net-snmp-agent-libs x86_64 1:5.7.2-38.el7_6.2 updates 705 k net-snmp-libs x86_64 1:5.7.2-38.el7_6.2 updates 749 k Transaction Summary =================================================================================================================================================================================================== Install 1 Package (+2 Dependent packages) Total download size: 1.7 M Installed size: 6.0 M Downloading packages: (1/3): net-snmp-libs-5.7.2-38.el7_6.2.x86_64.rpm | 749 kB 00:00:00 (2/3): keepalived-1.3.5-8.el7_6.5.x86_64.rpm | 331 kB 00:00:00 (3/3): net-snmp-agent-libs-5.7.2-38.el7_6.2.x86_64.rpm | 705 kB 00:00:01 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 1.1 MB/s | 1.7 MB 00:00:01 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 1:net-snmp-libs-5.7.2-38.el7_6.2.x86_64 1/3 Installing : 1:net-snmp-agent-libs-5.7.2-38.el7_6.2.x86_64 2/3 Installing : keepalived-1.3.5-8.el7_6.5.x86_64 3/3 Verifying : 1:net-snmp-agent-libs-5.7.2-38.el7_6.2.x86_64 1/3 Verifying : keepalived-1.3.5-8.el7_6.5.x86_64 2/3 Verifying : 1:net-snmp-libs-5.7.2-38.el7_6.2.x86_64 3/3 Installed: keepalived.x86_64 0:1.3.5-8.el7_6.5 Dependency Installed: net-snmp-agent-libs.x86_64 1:5.7.2-38.el7_6.2 net-snmp-libs.x86_64 1:5.7.2-38.el7_6.2 Complete!
在192.168.122.110和192.168.122.167上安裝nginx:
[root@nginx-1 ~]# yum install nginx -y Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com base | 3.6 kB 00:00:00 epel | 5.3 kB 00:00:00 extras | 3.4 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/2): epel/x86_64/updateinfo | 999 kB 00:00:00 (2/2): epel/x86_64/primary_db | 6.8 MB 00:00:01 Resolving Dependencies --> Running transaction check ---> Package nginx.x86_64 1:1.12.2-3.el7 will be installed --> Processing Dependency: nginx-all-modules = 1:1.12.2-3.el7 for package: 1:nginx-1.12.2-3.el7.x86_64 --> Processing Dependency: nginx-filesystem = 1:1.12.2-3.el7 for package: 1:nginx-1.12.2-3.el7.x86_64 --> Processing Dependency: nginx-filesystem for package: 1:nginx-1.12.2-3.el7.x86_64 --> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.12.2-3.el7.x86_64 --> Running transaction check ---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed ---> Package nginx-all-modules.noarch 1:1.12.2-3.el7 will be installed --> Processing Dependency: nginx-mod-http-geoip = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch --> Processing Dependency: nginx-mod-http-image-filter = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch --> Processing Dependency: nginx-mod-http-perl = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch --> Processing Dependency: nginx-mod-http-xslt-filter = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch --> Processing Dependency: nginx-mod-mail = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch --> Processing Dependency: nginx-mod-stream = 1:1.12.2-3.el7 for package: 1:nginx-all-modules-1.12.2-3.el7.noarch ---> Package nginx-filesystem.noarch 1:1.12.2-3.el7 will be installed --> Running transaction check ---> Package nginx-mod-http-geoip.x86_64 1:1.12.2-3.el7 will be installed ---> Package nginx-mod-http-image-filter.x86_64 1:1.12.2-3.el7 will be installed --> Processing Dependency: gd for package: 1:nginx-mod-http-image-filter-1.12.2-3.el7.x86_64 --> Processing Dependency: libgd.so.2()(64bit) for package: 1:nginx-mod-http-image-filter-1.12.2-3.el7.x86_64 ---> Package nginx-mod-http-perl.x86_64 1:1.12.2-3.el7 will be installed ---> Package nginx-mod-http-xslt-filter.x86_64 1:1.12.2-3.el7 will be installed --> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 --> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 --> Processing Dependency: libexslt.so.0()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 --> Processing Dependency: libxslt.so.1()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 ---> Package nginx-mod-mail.x86_64 1:1.12.2-3.el7 will be installed ---> Package nginx-mod-stream.x86_64 1:1.12.2-3.el7 will be installed --> Running transaction check ---> Package gd.x86_64 0:2.0.35-26.el7 will be installed --> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: gd-2.0.35-26.el7.x86_64 --> Processing Dependency: libjpeg.so.62()(64bit) for package: gd-2.0.35-26.el7.x86_64 --> Processing Dependency: libfontconfig.so.1()(64bit) for package: gd-2.0.35-26.el7.x86_64 --> Processing Dependency: libXpm.so.4()(64bit) for package: gd-2.0.35-26.el7.x86_64 --> Processing Dependency: libX11.so.6()(64bit) for package: gd-2.0.35-26.el7.x86_64 ---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed --> Running transaction check ---> Package fontconfig.x86_64 0:2.13.0-4.3.el7 will be installed --> Processing Dependency: fontpackages-filesystem for package: fontconfig-2.13.0-4.3.el7.x86_64 --> Processing Dependency: dejavu-sans-fonts for package: fontconfig-2.13.0-4.3.el7.x86_64 ---> Package libX11.x86_64 0:1.6.5-2.el7 will be installed --> Processing Dependency: libX11-common >= 1.6.5-2.el7 for package: libX11-1.6.5-2.el7.x86_64 --> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.6.5-2.el7.x86_64 ---> Package libXpm.x86_64 0:3.5.12-1.el7 will be installed ---> Package libjpeg-turbo.x86_64 0:1.2.90-6.el7 will be installed --> Running transaction check ---> Package dejavu-sans-fonts.noarch 0:2.33-6.el7 will be installed --> Processing Dependency: dejavu-fonts-common = 2.33-6.el7 for package: dejavu-sans-fonts-2.33-6.el7.noarch ---> Package fontpackages-filesystem.noarch 0:1.44-8.el7 will be installed ---> Package libX11-common.noarch 0:1.6.5-2.el7 will be installed ---> Package libxcb.x86_64 0:1.13-1.el7 will be installed --> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.13-1.el7.x86_64 --> Running transaction check ---> Package dejavu-fonts-common.noarch 0:2.33-6.el7 will be installed ---> Package libXau.x86_64 0:1.0.8-2.1.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =================================================================================================================================================================================================== Package Arch Version Repository Size =================================================================================================================================================================================================== Installing: nginx x86_64 1:1.12.2-3.el7 epel 531 k Installing for dependencies: dejavu-fonts-common noarch 2.33-6.el7 base 64 k dejavu-sans-fonts noarch 2.33-6.el7 base 1.4 M fontconfig x86_64 2.13.0-4.3.el7 base 254 k fontpackages-filesystem noarch 1.44-8.el7 base 9.9 k gd x86_64 2.0.35-26.el7 base 146 k gperftools-libs x86_64 2.6.1-1.el7 base 272 k libX11 x86_64 1.6.5-2.el7 base 606 k libX11-common noarch 1.6.5-2.el7 base 164 k libXau x86_64 1.0.8-2.1.el7 base 29 k libXpm x86_64 3.5.12-1.el7 base 55 k libjpeg-turbo x86_64 1.2.90-6.el7 base 134 k libxcb x86_64 1.13-1.el7 base 214 k libxslt x86_64 1.1.28-5.el7 base 242 k nginx-all-modules noarch 1:1.12.2-3.el7 epel 16 k nginx-filesystem noarch 1:1.12.2-3.el7 epel 17 k nginx-mod-http-geoip x86_64 1:1.12.2-3.el7 epel 23 k nginx-mod-http-image-filter x86_64 1:1.12.2-3.el7 epel 27 k nginx-mod-http-perl x86_64 1:1.12.2-3.el7 epel 36 k nginx-mod-http-xslt-filter x86_64 1:1.12.2-3.el7 epel 26 k nginx-mod-mail x86_64 1:1.12.2-3.el7 epel 54 k nginx-mod-stream x86_64 1:1.12.2-3.el7 epel 76 k Transaction Summary =================================================================================================================================================================================================== Install 1 Package (+21 Dependent packages) Total download size: 4.3 M Installed size: 14 M Downloading packages: (1/22): dejavu-fonts-common-2.33-6.el7.noarch.rpm | 64 kB 00:00:00 (2/22): fontconfig-2.13.0-4.3.el7.x86_64.rpm | 254 kB 00:00:00 (3/22): fontpackages-filesystem-1.44-8.el7.noarch.rpm | 9.9 kB 00:00:00 (4/22): gperftools-libs-2.6.1-1.el7.x86_64.rpm | 272 kB 00:00:00 (5/22): libX11-1.6.5-2.el7.x86_64.rpm | 606 kB 00:00:00 (6/22): gd-2.0.35-26.el7.x86_64.rpm | 146 kB 00:00:00 (7/22): libX11-common-1.6.5-2.el7.noarch.rpm | 164 kB 00:00:00 (8/22): libXau-1.0.8-2.1.el7.x86_64.rpm | 29 kB 00:00:00 (9/22): libXpm-3.5.12-1.el7.x86_64.rpm | 55 kB 00:00:00 (10/22): libjpeg-turbo-1.2.90-6.el7.x86_64.rpm | 134 kB 00:00:00 (11/22): libxslt-1.1.28-5.el7.x86_64.rpm | 242 kB 00:00:00 (12/22): dejavu-sans-fonts-2.33-6.el7.noarch.rpm | 1.4 MB 00:00:00 (13/22): libxcb-1.13-1.el7.x86_64.rpm | 214 kB 00:00:00 (14/22): nginx-all-modules-1.12.2-3.el7.noarch.rpm | 16 kB 00:00:00 (15/22): nginx-1.12.2-3.el7.x86_64.rpm | 531 kB 00:00:00 (16/22): nginx-filesystem-1.12.2-3.el7.noarch.rpm | 17 kB 00:00:00 (17/22): nginx-mod-http-geoip-1.12.2-3.el7.x86_64.rpm | 23 kB 00:00:00 (18/22): nginx-mod-http-image-filter-1.12.2-3.el7.x86_64.rpm | 27 kB 00:00:00 (19/22): nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64.rpm | 26 kB 00:00:00 (20/22): nginx-mod-mail-1.12.2-3.el7.x86_64.rpm | 54 kB 00:00:00 (21/22): nginx-mod-stream-1.12.2-3.el7.x86_64.rpm | 76 kB 00:00:00 (22/22): nginx-mod-http-perl-1.12.2-3.el7.x86_64.rpm | 36 kB 00:00:00 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Total 3.0 MB/s | 4.3 MB 00:00:01 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : fontpackages-filesystem-1.44-8.el7.noarch 1/22 Installing : dejavu-fonts-common-2.33-6.el7.noarch 2/22 Installing : dejavu-sans-fonts-2.33-6.el7.noarch 3/22 Installing : fontconfig-2.13.0-4.3.el7.x86_64 4/22 Installing : 1:nginx-filesystem-1.12.2-3.el7.noarch 5/22 Installing : libX11-common-1.6.5-2.el7.noarch 6/22 Installing : gperftools-libs-2.6.1-1.el7.x86_64 7/22 Installing : libXau-1.0.8-2.1.el7.x86_64 8/22 Installing : libxcb-1.13-1.el7.x86_64 9/22 Installing : libX11-1.6.5-2.el7.x86_64 10/22 Installing : libXpm-3.5.12-1.el7.x86_64 11/22 Installing : libxslt-1.1.28-5.el7.x86_64 12/22 Installing : libjpeg-turbo-1.2.90-6.el7.x86_64 13/22 Installing : gd-2.0.35-26.el7.x86_64 14/22 Installing : 1:nginx-mod-mail-1.12.2-3.el7.x86_64 15/22 Installing : 1:nginx-mod-stream-1.12.2-3.el7.x86_64 16/22 Installing : 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 17/22 Installing : 1:nginx-mod-http-image-filter-1.12.2-3.el7.x86_64 18/22 Installing : 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 19/22 Installing : 1:nginx-mod-http-geoip-1.12.2-3.el7.x86_64 20/22 Installing : 1:nginx-all-modules-1.12.2-3.el7.noarch 21/22 Installing : 1:nginx-1.12.2-3.el7.x86_64 22/22 Verifying : 1:nginx-all-modules-1.12.2-3.el7.noarch 1/22 Verifying : fontconfig-2.13.0-4.3.el7.x86_64 2/22 Verifying : 1:nginx-1.12.2-3.el7.x86_64 3/22 Verifying : 1:nginx-mod-mail-1.12.2-3.el7.x86_64 4/22 Verifying : 1:nginx-mod-stream-1.12.2-3.el7.x86_64 5/22 Verifying : fontpackages-filesystem-1.44-8.el7.noarch 6/22 Verifying : libjpeg-turbo-1.2.90-6.el7.x86_64 7/22 Verifying : 1:nginx-mod-http-xslt-filter-1.12.2-3.el7.x86_64 8/22 Verifying : 1:nginx-mod-http-image-filter-1.12.2-3.el7.x86_64 9/22 Verifying : dejavu-fonts-common-2.33-6.el7.noarch 10/22 Verifying : libxcb-1.13-1.el7.x86_64 11/22 Verifying : libXpm-3.5.12-1.el7.x86_64 12/22 Verifying : 1:nginx-mod-http-perl-1.12.2-3.el7.x86_64 13/22 Verifying : libxslt-1.1.28-5.el7.x86_64 14/22 Verifying : libX11-1.6.5-2.el7.x86_64 15/22 Verifying : dejavu-sans-fonts-2.33-6.el7.noarch 16/22 Verifying : gd-2.0.35-26.el7.x86_64 17/22 Verifying : 1:nginx-mod-http-geoip-1.12.2-3.el7.x86_64 18/22 Verifying : libXau-1.0.8-2.1.el7.x86_64 19/22 Verifying : gperftools-libs-2.6.1-1.el7.x86_64 20/22 Verifying : libX11-common-1.6.5-2.el7.noarch 21/22 Verifying : 1:nginx-filesystem-1.12.2-3.el7.noarch 22/22 Installed: nginx.x86_64 1:1.12.2-3.el7 Dependency Installed: dejavu-fonts-common.noarch 0:2.33-6.el7 dejavu-sans-fonts.noarch 0:2.33-6.el7 fontconfig.x86_64 0:2.13.0-4.3.el7 fontpackages-filesystem.noarch 0:1.44-8.el7 gd.x86_64 0:2.0.35-26.el7 gperftools-libs.x86_64 0:2.6.1-1.el7 libX11.x86_64 0:1.6.5-2.el7 libX11-common.noarch 0:1.6.5-2.el7 libXau.x86_64 0:1.0.8-2.1.el7 libXpm.x86_64 0:3.5.12-1.el7 libjpeg-turbo.x86_64 0:1.2.90-6.el7 libxcb.x86_64 0:1.13-1.el7 libxslt.x86_64 0:1.1.28-5.el7 nginx-all-modules.noarch 1:1.12.2-3.el7 nginx-filesystem.noarch 1:1.12.2-3.el7 nginx-mod-http-geoip.x86_64 1:1.12.2-3.el7 nginx-mod-http-image-filter.x86_64 1:1.12.2-3.el7 nginx-mod-http-perl.x86_64 1:1.12.2-3.el7 nginx-mod-http-xslt-filter.x86_64 1:1.12.2-3.el7 nginx-mod-mail.x86_64 1:1.12.2-3.el7 nginx-mod-stream.x86_64 1:1.12.2-3.el7 Complete!
需要注意的是,在兩台nginx服務器上需要將防火牆關閉,否則lvs+keepalived的兩台機器就無法將請求發送到兩台nginx服務器上來:
[root@nginx-1 ~]# systemctl disable firewalld.service
查看兩台負載均衡機器是否支持lvs:
# 如果看到如下結果,則說明是支持的 [root@lvskeepalived-1 ~]# sudo lsmod |grep ip_vs ip_vs 145497 0 nf_conntrack 137239 1 ip_vs libcrc32c 12644 3 xfs,ip_vs,nf_conntrack
如果上述命令沒有任何結果,則執行sudo ipvsadm
命令啟動ipvs之后,再通過上述命令進行查看即可。啟動ipvs之后,我們就可以在/etc/keepalived/
目錄下編輯keepalived.conf
文件,我們以192.168.122.248機器作為master機器,master節點配置如下:
# Global Configuration global_defs { lvs_id director1 # 指定lvs的id } # VRRP Configuration vrrp_instance LVS { state MASTER # 指定當前節點為master節點 interface eth0 # 這里是網卡的名稱,通過ifconfig或者ip addr可以查看 virtual_router_id 51 # 這里指定的是虛擬路由id,master節點和backup節點需要指定一樣的 priority 151 # 指定了當前節點的優先級,數值越大優先級越高,master節點要高於backup節點 advert_int 1 # 指定發送VRRP通告的間隔,單位是秒 authentication { auth_type PASS # 鑒權,默認通過 auth_pass 123456 # 鑒權訪問密碼 } virtual_ipaddress { 192.168.122.120 # 指定了虛擬ip } } # Virtual Server Configuration - for www server # 后台真實主機的配置 virtual_server 192.168.122.120 80 { delay_loop 1 # 健康檢查的時間間隔 lb_algo rr # 負載均衡策略,這里是輪詢 lb_kind DR # 調度器類型,這里是DR persistence_time 1 # 指定了持續將請求打到同一台真實主機的時間長度 protocol TCP # 指定了訪問后台真實主機的協議類型 # Real Server 1 configuration # 指定了真實主機1的ip和端口 real_server 192.168.122.110 80 { weight 1 # 指定了當前主機的權重 TCP_CHECK { connection_timeout 10 # 指定了進行心跳檢查的超時時間 nb_get_retry 3 # 指定了心跳超時之后的重復次數 delay_before_retry 3 # 指定了在嘗試之前延遲多長時間 } } # Real Server 2 Configuration real_server 192.168.122.167 80 { weight 1 # 指定了當前主機的權重 TCP_CHECK { connection_timeout 10 # 指定了進行心跳檢查的超時時間 nb_get_retry 3 # 指定了心跳超時之后的重復次數 delay_before_retry 3 # 指定了在嘗試之前延遲多長時間 } } }
上面是master節點上keepalived的配置,對於backup節點,其配置與master幾乎是一致的,只是其state和priority參數不同。如下是backup節點的完整配置:
# Global Configuration global_defs { lvs_id director2 # 指定lvs的id } # VRRP Configuration vrrp_instance LVS { state BACKUP # 指定當前節點為master節點 interface eth0 # 這里的ens33是網卡的名稱,通過ifconfig或者ip addr可以查看 virtual_router_id 51 # 這里指定的是虛擬路由id,master節點和backup節點需要指定一樣的 priority 150 # 指定了當前節點的優先級,數值越大優先級越高,master節點要高於backup節點 advert_int 1 # 指定發送VRRP通告的間隔,單位是秒 authentication { auth_type PASS # 鑒權,默認通過 auth_pass 123456 # 鑒權訪問密碼 } virtual_ipaddress { 192.168.122.120 # 指定了虛擬ip } } # Virtual Server Configuration - for www server # 后台真實主機的配置 virtual_server 192.168.122.120 80 { delay_loop 1 # 健康檢查的時間間隔 lb_algo rr # 負載均衡策略,這里是輪詢 lb_kind DR # 調度器類型,這里是DR persistence_time 1 # 指定了持續將請求打到同一台真實主機的時間長度 protocol TCP # 指定了訪問后台真實主機的協議類型 # Real Server 1 configuration # 指定了真實主機1的ip和端口 real_server 192.168.122.110 80 { weight 1 # 指定了當前主機的權重 TCP_CHECK { connection_timeout 10 # 指定了進行心跳檢查的超時時間 nb_get_retry 3 # 指定了心跳超時之后的重復次數 delay_before_retry 3 # 指定了在嘗試之前延遲多長時間 } } # Real Server 2 Configuration real_server 192.168.122.167 80 { weight 1 # 指定了當前主機的權重 TCP_CHECK { connection_timeout 10 # 指定了進行心跳檢查的超時時間 nb_get_retry 3 # 指定了心跳超時之后的重復次數 delay_before_retry 3 # 指定了在嘗試之前延遲多長時間 } } }
將master和backup配置成完全一樣的原因是,在master宕機時,可以根據backup的配置進行服務的無縫切換。
在lvs+keepalived機器配置完成之后,我們下面配置兩台應用服務器的nginx配置。這里我們是將nginx作為應用服務器,在其配置文件中配置返回狀態碼為200,並且會將當前主機的ip返回,如下是其配置:
worker_processes auto; # pid /run/nginx.pid; events { worker_connections 786; } http { server { listen 80; # 這里是直接返回200狀態碼和一段文本 location / { default_type text/html; return 200 "Hello, Nginx! Server nginx-1\n"; } } }
worker_processes auto; # pid /run/nginx.pid; events { worker_connections 786; } http { server { listen 80; # 這里是直接返回200狀態碼和一段文本 location / { default_type text/html; return 200 "Hello, Nginx! Server nginx-2\n"; } } }
可以看到,兩台機器返回的文本中主機ip是不一樣的。nginx配置完成后,可以通過如下命令進行啟動:
[root@nginx-1 nginx]# sudo nginx
在啟動nginx之后,我們需要配置虛擬ip,這是因為我們使用的lvs調度器是DR模式,前面我們講到過,這種模式下,對客戶端的響應是真實服務器直接返回給客戶端的,而真實服務器需要將響應報文中的源ip修改為虛擬ip,這里配置的虛擬ip就是起這個作用的。我們編輯/etc/init.d/lvsrs
文件,寫入如下內容:
#!/bin/bash ifconfig lo:0 192.168.122.120 netmask 255.255.255.255 broadcast 192.168.122.120 up route add -host 192.168.122.120 dev lo:0 echo "0" > /proc/sys/net/ipv4/ip_forward echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce exit 0
- lo:表示當前主機真實網卡的名稱;
- 172.16.28.120:表示虛擬ip;
編寫完成后運行該腳本文件即可。然后將兩台lvs+keepalived機器上的keepalived服務啟動起來即可:
[root@lvskeepalived-1 keepalived]# service keepalived start Redirecting to /bin/systemctl start keepalived.service [root@lvskeepalived-1 keepalived]# service keepalived status Redirecting to /bin/systemctl status keepalived.service ● keepalived.service - LVS and VRRP High Availability Monitor Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled) Active: active (running) since Tue 2019-08-27 15:01:20 CST; 10s ago Process: 1507 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS) Main PID: 1508 (keepalived) CGroup: /system.slice/keepalived.service ├─1508 /usr/sbin/keepalived -D ├─1509 /usr/sbin/keepalived -D └─1510 /usr/sbin/keepalived -D Aug 27 15:01:23 lvskeepalived-1 Keepalived_vrrp[1510]: Sending gratuitous ARP on eth0 for 192.168.122.120 Aug 27 15:01:23 lvskeepalived-1 Keepalived_vrrp[1510]: Sending gratuitous ARP on eth0 for 192.168.122.120 Aug 27 15:01:23 lvskeepalived-1 Keepalived_vrrp[1510]: Sending gratuitous ARP on eth0 for 192.168.122.120 Aug 27 15:01:23 lvskeepalived-1 Keepalived_vrrp[1510]: Sending gratuitous ARP on eth0 for 192.168.122.120 Aug 27 15:01:28 lvskeepalived-1 Keepalived_vrrp[1510]: Sending gratuitous ARP on eth0 for 192.168.122.120 Aug 27 15:01:28 lvskeepalived-1 Keepalived_vrrp[1510]: VRRP_Instance(LVS) Sending/queueing gratuitous ARPs on eth0 for 192.168.122.120 Aug 27 15:01:28 lvskeepalived-1 Keepalived_vrrp[1510]: Sending gratuitous ARP on eth0 for 192.168.122.120 Aug 27 15:01:28 lvskeepalived-1 Keepalived_vrrp[1510]: Sending gratuitous ARP on eth0 for 192.168.122.120 Aug 27 15:01:28 lvskeepalived-1 Keepalived_vrrp[1510]: Sending gratuitous ARP on eth0 for 192.168.122.120 Aug 27 15:01:28 lvskeepalived-1 Keepalived_vrrp[1510]: Sending gratuitous ARP on eth0 for 192.168.122.120 [root@lvskeepalived-1 keepalived]#
最后可以通過如下命令查看配置的lvs+keepalived的策略:
[root@lvskeepalived-1 keepalived]# ipvsadm -ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.122.120:80 rr -> 192.168.122.110:80 Route 1 0 0 -> 192.168.122.167:80 Route 1 0 0
2.2 集群測試
根據上述步驟,我們配置完成了一個lvs+keepalived+nginx的集群。在瀏覽器中,我們可以訪問http://192.168.122.120
即可看到如下響應:
Hello, Nginx! Server nginx-1
多次刷新瀏覽器之后,可以看到瀏覽器中顯示的文本切換如下,這是因為lvs的負載均衡策略產生的:
[root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 You have new mail in /var/spool/mail/root [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-2 [root@test2 ~]# curl 192.168.122.120 Hello, Nginx! Server nginx-1 [root@test2 ~]#