目前常見的web集羣調度器分爲軟件和硬件,軟件一般使用開源的LVS,Haproxy,Nginx,硬件通常使用比較多的是F5,也有不少人使用國內的一些產品,如梭子魚,綠盟等php
- LVS不支持正則處理,不能實現動靜分離
- 對於大型網站,LVS的實施配置複雜,維護成本相對較高
- 特別適用於負載特別大的web站點
- 運行在當前的硬件上可支持數以萬計的併發鏈接鏈接請求
修改Haproxy配置文件中關於日誌配置的選項,加入配置:html
log /devlog local0 info log /dev/log local0 notice
Haproxy參數優化前端
maxconn:最大鏈接數,根據應用實際狀況進行調整,推薦使用10240 daemon:守護進程模式,Haproxy可使用非守護進程模式啓動,建議使用守護進程模式啓動 nbproc:負載均衡的併發進程數,建議與當前服務器CPU核數相等或爲其2倍 retries:重試次數,主要用於對集羣節點的檢查,若是節點多,且併發量大,設置爲2次或3次 option http-server-close:主動關閉http請求選項,建議在生產環境中使用此選項 timeout http-keep-alive:長鏈接超時時間,設置長鏈接超時時間能夠設置爲10s timeout http-request:http請求超時時間,建議將此時間設置爲5~10s,增長http鏈接釋放速度 timeout client:客戶端超時時間,若是訪問量過大,節點響應慢能夠將此時間設置短一些,建議設置爲1min左右就能夠了
Haporxy服務器:192.168.100.210 Nginx服務器1:192.168.100.201 Nginx服務器2:192.168.100.202 client測試機:192.168.100.100
Nginx服務器1: [root@localhost ~]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make Nginx服務器2: [root@localhost ~]# yum install -y pcre-devel zlib-devel gcc gcc-c++ make Haporxy服務器: [root@localhost ~]# yum install -y pcre-devel bzip2-devel gcc gcc-c++ make
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static #dhcp改成static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=8ecd53ce-afdb-46f8-b7ff-b2f428a3bc8f DEVICE=ens33 ONBOOT=yes IPADDR=192.168.100.201 #IP地址 NETMASK=255.255.255.0 #子網掩碼 GATEWAY=192.168.100.1 #網關 [root@localhost ~]# systemctl restart network #重啓網絡服務 [root@localhost ~]# ifconfig #查看網卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.201 netmask 255.255.255.0 broadcast 192.168.100.255 #IP地址配置成功 inet6 fe80::c776:9d00:618:88f2 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:eb:34:07 txqueuelen 1000 (Ethernet) RX packets 51789 bytes 75688349 (72.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7987 bytes 543001 (530.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# mkdir /mnt/tools #建立掛載點 [root@localhost ~]# mount.cifs //192.168.100.100/tools /mnt/tools/ #掛載 Password for root@//192.168.100.100/tools: [root@localhost ~]# cd /mnt/tools/LNMP/ [root@localhost LNMP]# ls Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz php-7.1.20.tar.gz mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2 [root@localhost LNMP]# tar zxf nginx-1.12.2.tar.gz -C /opt/ #解壓源碼包 [root@localhost LNMP]# cd /opt/ [root@localhost opt]# ls nginx-1.12.2 rh [root@localhost opt]#
[root@localhost opt]# cd nginx-1.12.2/ [root@localhost nginx-1.12.2]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx #建立nginx用戶 [root@localhost nginx-1.12.2]# ./configure \ #配置Nginx服務 > --prefix=/usr/local/nginx \ #安裝路徑 > --user=nginx \ #屬主 > --group=nginx \ #屬組 > --with-http_stub_status_module #啓用ngx_http_stub_status_module支持(獲取nginx自上次啓動以來的工做狀態) [root@localhost nginx-1.12.2]# make && make install #編譯安裝Nginx服務
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/html/ [root@localhost html]# echo "this is abc web" > index.html #建立站點首頁 [root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #創建軟連接 [root@localhost html]# nginx -t #檢查配置文件語法 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost html]# nginx #開啓服務 [root@localhost html]# netstat -ntap | grep 80 #查看端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18683/nginx: master [root@localhost html]# systemctl stop firewalld.service #關閉防火牆 [root@localhost html]# setenforce 0 #關閉加強型安全功能 [root@localhost html]#
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static #dhcp改成static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=09073596-a7cf-404c-a098-28c3ff58c44b DEVICE=ens33 ONBOOT=yes IPADDR=192.168.100.202 #IP地址 NETMASK=255.255.255.0 #子網掩碼 GATEWAY=192.168.100.1 #網關 [root@localhost ~]# systemctl restart network #重啓網絡服務 [root@localhost ~]# ifconfig #查看網卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.202 netmask 255.255.255.0 broadcast 192.168.100.255 #IP地址配置成功 inet6 fe80::5134:22f5:842b:5201 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:0d:f1:75 txqueuelen 1000 (Ethernet) RX packets 50790 bytes 74736905 (71.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7366 bytes 466513 (455.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# mkdir /mnt/tools #建立掛載點 [root@localhost ~]# mount.cifs //192.168.100.100/tools /mnt/tools/ #掛載 Password for root@//192.168.100.100/tools: [root@localhost ~]# cd /mnt/tools/LNMP/ [root@localhost LNMP]# ls Discuz_X3.4_SC_UTF8.zip nginx-1.12.2.tar.gz php-7.1.20.tar.gz mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2 [root@localhost LNMP]# tar zxf nginx-1.12.2.tar.gz -C /opt/ #解壓源碼包 [root@localhost LNMP]# cd /opt/ [root@localhost opt]# ls nginx-1.12.2 rh [root@localhost opt]#
[root@localhost opt]# cd nginx-1.12.2/ [root@localhost nginx-1.12.2]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src [root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx #建立nginx用戶 [root@localhost nginx-1.12.2]# ./configure \ #配置Nginx服務 > --prefix=/usr/local/nginx \ #安裝路徑 > --user=nginx \ #屬主 > --group=nginx \ #屬組 > --with-http_stub_status_module #啓用ngx_http_stub_status_module支持(獲取nginx自上次啓動以來的工做狀態) [root@localhost nginx-1.12.2]# make && make install #編譯安裝Nginx服務
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/html/ [root@localhost html]# echo "this is xyz web" > index.html #建立站點首頁 [root@localhost html]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #創建軟連接 [root@localhost html]# nginx -t #檢查配置文件語法 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost html]# nginx #開啓服務 [root@localhost html]# netstat -ntap | grep 80 #查看端口 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18683/nginx: master [root@localhost html]# systemctl stop firewalld.service #關閉防火牆 [root@localhost html]# setenforce 0 #關閉加強型安全功能 [root@localhost html]#
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static #dhcp改成static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=3ceed540-b04c-48d6-a4f7-79951f09ea1d DEVICE=ens33 ONBOOT=yes IPADDR=192.168.100.210 #IP地址 NETMASK=255.255.255.0 #子網掩碼 GATEWAY=192.168.100.1 #網關 [root@localhost ~]# systemctl restart network #重啓網絡服務 [root@localhost ~]# ifconfig #查看網卡信息 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.100.210 netmask 255.255.255.0 broadcast 192.168.100.255 #IP地址配置成功 inet6 fe80::3e1d:31ba:f66a:6f80 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:95:9b:1b txqueuelen 1000 (Ethernet) RX packets 51747 bytes 75871654 (72.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 7407 bytes 470471 (459.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@localhost ~]# mkdir /mnt/tools #建立掛載點 [root@localhost ~]# mount.cifs //192.168.100.100/tools /mnt/tools/ #掛載 Password for root@//192.168.100.100/tools: [root@localhost ~]# cd /mnt/tools/MySQL/ [root@localhost MySQL]# ls amoeba-mysql-binary-2.2.0.tar.gz haproxy-1.5.19.tar.gz mysql-5.5.24.tar.gz boost_1_59_0.tar.gz jdk-6u14-linux-x64.bin mysql-5.7.17.tar.gz [root@localhost MySQL]# tar zxf haproxy-1.5.19.tar.gz -C /opt/ #解壓haproxy源碼包 [root@localhost MySQL]# cd /opt/ [root@localhost opt]# ls haproxy-1.5.19 rh [root@localhost opt]# [root@localhost opt]# cd haproxy-1.5.19/ [root@localhost haproxy-1.5.19]# make TARGET=linux26 #編譯haproxy服務 [root@localhost haproxy-1.5.19]# make install #安裝haproxy服務
[root@localhost haproxy-1.5.19]# mkdir /etc/haproxy #建立配置文件目錄 [root@localhost haproxy-1.5.19]# cp examples/haproxy.cfg /etc/haproxy/ #模板複製到配置目錄下 [root@localhost haproxy-1.5.19]# vim /etc/haproxy/haproxy.cfg #編輯配置文件 chroot /usr/share/haproxy ##註釋掉 redispatch ##註釋掉 # this config needs haproxy-1.1.28 or haproxy-1.2.1 #爲全局配置 global log 127.0.0.1 local0 #配置日誌記,local0爲日誌設備,默認存放到系統日誌 log 127.0.0.1 local1 notice #notice爲 日誌級別,一般有24個級別 #log loghost local0 info maxconn 4096 #最大鏈接數 uid 99 #用戶uid gid 99 #用戶gid daemon #debug #quiet #defaults配置項配置默認參數,通常會被應用組件繼承,若是在應用組件中沒有特別聲明,將安裝默認配置參數設置 defaults log global #定義日誌爲global配置中的日誌定義 mode http #模式爲http option httplog #採用http日誌格式記錄日誌 option dontlognull #日誌不爲空 retries 3 #檢查節點服務器失敗連續達到三次則認爲節點不可用 maxconn 2000 #最大鏈接數 contimeout 5000 #鏈接超時時間 clitimeout 50000 #客戶端超時時間 srvtimeout 50000 #服務器超時時間 listen webcluster 0.0.0.0:80 option httpchk GET /index.html #檢查服務器的index.html文件 balance roundrobin #負載均衡調度算法使用輪詢算法 server inst1 192.168.100.201:80 check inter 2000 fall 3 #定義備份節點,健康檢查請求三次 server inst2 192.168.100.202:80 check inter 2000 fall 3 [root@localhost haproxy-1.5.19]# cp /opt/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy #啓動文件 [root@localhost haproxy-1.5.19]# chmod +x /etc/init.d/haproxy #添加執行權限 [root@localhost haproxy-1.5.19]# chkconfig --add haproxy #添加到service [root@localhost haproxy-1.5.19]# ln -s /usr/local/sbin/haproxy /usr/sbin/ #便於系統識別 [root@localhost haproxy-1.5.19]# [root@localhost haproxy-1.5.19]# service haproxy start #開啓服務 Starting haproxy (via systemctl): [ 肯定 ] [root@localhost haproxy-1.5.19]# systemctl stop firewalld.service #關閉防火牆 [root@localhost haproxy-1.5.19]# setenforce 0 #關閉加強型安全功能 [root@localhost haproxy-1.5.19]#
[root@localhost haproxy-1.5.19]# cd /etc/haproxy/ [root@localhost haproxy]# vim haproxy.cfg log /dev/log local0 info #添加 log /dev/log local0 notice #添加 #log 127.0.0.1 local0 #註釋 #log 127.0.0.1 local1 notice #註釋 [root@haproxy haproxy]# service haproxy restart ##重啓服務 [root@haproxy haproxy]# touch /etc/rsyslog.d/haproxy.conf ##建立系統日誌haproxy配置文件 [root@haproxy haproxy]# vim /etc/rsyslog.d/haproxy.conf if ($programname == 'haproxy' and $syslogseverity-text == 'info') ##根據級別建立不一樣的日誌文件 then -/var/log/haproxy/haproxy-info.log &~ if ($programname == 'haproxy' and $syslogseverity-text == 'notice') then -/var/log/haproxy/haproxy-notice.log &~ [root@haproxy haproxy]# systemctl restart rsyslog.service ##重啓系統日誌服務 [root@haproxy haproxy]# cd /var/log/ ##此時是沒有haproxy日誌 ##從新訪問網頁 [root@haproxy haproxy]# cd /var/log/haproxy/ [root@haproxy haproxy]# ls ##此時就生成了info級別的日誌文件 haproxy-info.log