Nginx負(fù)載均衡器的優(yōu)點(diǎn):
實(shí)現(xiàn)看彈性化操作的架構(gòu),壓力增大的時候可以臨時添加后端Web服務(wù)器;
upstream具有負(fù)載均衡能力(默認(rèn)使用輪詢),可以自動判斷下面的機(jī)器,并且自動踢出不能正常提供服務(wù)的機(jī)器;
Keepalvied加Nginx監(jiān)測腳本可保證單個nginx負(fù)載均衡器的有效性,避免單點(diǎn)故障
系統(tǒng)
兩臺Nginx:
CentOS6.7 x86_64
兩臺Web:
Ubuntu15.04 desktop
IP地址
nginx(主LB):192.168.15.132
nginx(備LB):192.168.15.133
VIP地址:192.168.15.135
Real1的IP:192.168.15.128
Real2的IP:192.168.15.130
部署整個環(huán)境用到的軟件為:
nginx-1.6.3.tar.gz
prce-8.38.tar.gz
zlib-1.2.8.tar.gz
①2臺Web主機(jī)(Ubuntu)上部署Nginx+PHP-FPM+MySQL,此處省略。
②分別在二臺Nginx負(fù)載均衡器上安裝Nginx,配置
安裝GCC編譯器等工具:
yum install -y gcc gcc-c++ autoconf automake libtool make openssl openssl-devel
安裝Nginx:
wget http://exim.mirror.fr/pcre/pcre-8.38.tar.gz
tar -zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure
make && make install
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make && make install
wget http://nginx.org/download/nginx-1.6.3.tar.gz
tar -zxvf nginx-1.6.3.tar.gz
cd nginx-1.6.3/
./configure --prefix=/usr/local/nginx
--sbin-path=/usr/local/nginx/sbin/nginx
--conf-path=/usr/local/nginx/conf/nginx.conf
--pid-path=/usr/local/nginx/logs/nginx.pid \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
make && make install
注:查詢"./configure --help"相關(guān)模塊,按需求指定啟用
Nginx.conf配置文件,二個nginx負(fù)載均衡器的文件一樣
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | user www-data www-data; worker_processes 1; error_log /usr/local/nginx/logs/error .log notice; pid /usr/local/nginx/logs/nginx .pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } http { include mime.types; default_type application /octet-stream ; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' ; access_log logs /access .log main; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; tcp_nopush on; server_tokens off; keepalive_timeout 60; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; gzip on; upstream backend { server 192.168.15.128; server 192.168.15.130; } server { listen 80; server_name 192.168.15.135; location / { root html; index index.php index.html index.htm; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; #后端的Web服務(wù)器可以通過X-Forwarded-For獲取用戶真實(shí)IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http: //backend ; } error_page 500 502 503 504 /50x .html; location = /50x .html { root html; } location /nginx_status { stub_status on; auth_basic "NginxStatus" ; auth_basic_user_file /usr/local/nginx/htpasswd ; #allow 127.0.0.1; #deny all; } location ~* \.(ini|docx|txt|doc|pdf)$ { #禁止訪問文檔性文件 root /usr/share/nginx/html ; deny all; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|html|htm|css)$ { root /home/image ; proxy_store on; proxy_store_access user:rw group:rw all:rw; proxy_temp_path /home/image ; if ( !-e $request_filename) { proxy_pass http: //backend ; } } } } |
③在二臺Nginx上安裝及配置keepalived:
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
tar -zxvf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./configure --sysconf=/etc/ --with-kernel-dir=/usr/src/kernels/2.6.32-573.8.1.el6.x86_64
make && make install
ln -s /usr/local/sbin/keepalived /sbin/
#這一步很重要,不執(zhí)行l(wèi)n -s會報錯“Starting keepalived: /bin/bash: keepalived: command not found”
service keepalived start
二臺Nginx上keepalived.conf配置文件如下,配置完成后分別service keepalived start啟動檢驗(yàn)keepalived配置是否成功
主:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | global_defs { notification_email { test @163.com } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_MASTER } vrrp_script chk_http_port { script "/usr/local/src/check_nginx_pid.sh" interval 2 #(檢測腳本執(zhí)行的間隔) weight 2 } vrrp_instance VI_1 { state MASTER interface bond0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port #(調(diào)用檢測腳本) } virtual_ipaddress { 192.168.15.135 } } |
備:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | global_defs { notification_email { test @163.com } notification_email_from keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_BACKUP } vrrp_script chk_http_port { script "/usr/local/src/check_nginx_pid.sh" interval 2 #(檢測腳本執(zhí)行的間隔) weight 2 } vrrp_instance VI_1 { state BACKUP interface bond0 virtual_router_id 51 priority 66 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port #(調(diào)用檢測腳本) } virtual_ipaddress { 192.168.15.135 } } |
以下是針對nginx狀態(tài)進(jìn)行檢測的腳本,第一次nginx服務(wù)死掉時,會重新啟動,如果Nginx服務(wù)無法正常啟動,則殺掉keepalived進(jìn)程
vim /usr/local/src/check_nginx_pid.sh
Ok,開始nginx負(fù)載均衡測試,停掉其中一臺的任何服務(wù),不影響整個系統(tǒng)的運(yùn)作
注:兩臺LBServer也可分別添加一個VIP①②(Keepalived心跳監(jiān)控,服務(wù)不可用或者宕機(jī),VIP①被備LBServer接管),外部使用智能DNS輪詢兩個VIP①②,提高硬件資源利用率。
聯(lián)系客服