Nginx負載均衡+keepalived高可用
阿新 • • 發佈:2018-01-12
_id htm ins oba web服務器 介紹 rtu netstat 相對 註:環境介紹:VMware環境下4臺虛擬機 兩臺做負載均衡服務器 兩臺做realserver (模擬物理web服務器)
1 配置keepalived實現nginx負載均衡高可用,keepalived 主要功能是是實現主備負載 均衡服務器之間的高可用,當一臺宕機另一臺會自動接管.實現熱備。
2 在主備負載均衡服務器個配置nginx反向代理服務,並在本地(你的windows電 腦)/etc/hosts 做域名解析。
1 配置keepalived實現nginx負載均衡高可用,keepalived 主要功能是是實現主備負載 均衡服務器之間的高可用,當一臺宕機另一臺會自動接管.實現熱備。
2 在主備負載均衡服務器個配置nginx反向代理服務,並在本地(你的windows電 腦)/etc/hosts 做域名解析。
[root@lb01 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id 130 } vrrp_instance VI_1 { state MASTER interface eth0 lvs_sync_daemon_inteface eth2 virtual_router_id 130 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.232.80/24 dev eth0 label eth0:1 } } virtual_server 192.168.232.80 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP } lvs01-nginx [root@lb01 ~]# cat /application/nginx/conf/nginx.conf #user nobody; worker_processes 1; error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format commonlog ‘$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; sendfile on; keepalive_timeout 65; include extra/upstream01.conf; } [root@lb01 ~]# cat /application/nginx/conf/nginx.conf #user nobody; worker_processes 1; error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format commonlog ‘$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; sendfile on; keepalive_timeout 65; include extra/upstream01.conf; } [root@lb0~]#cat /application/nginx/conf/extra/upstream01.conf #blog lb by cyt at 20180107 upstream server_pools { server 192.168.232.132:80 weight=5; server 192.168.232.133:80 weight=5; } server { listen 80; server_name www.etiantian01.org; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } lvs02: [root@lb02 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id 131 } vrrp_instance VI_1 { state BACKUP interface eth0 lvs_sync_daemon_inteface eth2 virtual_router_id 130 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.232.80/24 dev eth0 label eth0:1 } } virtual_server 192.168.232.80 80 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP } lvs02-nginx [root@lb02 ~]# cat /application/nginx/conf/nginx.conf #user nobody; worker_processes 1; error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format commonlog ‘$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; sendfile on; keepalive_timeout 65; include extra/upstream01.conf; } [root@lb02~]#cat /application/nginx/conf/extra/upstream01.conf #blog lb by cyt at 20180107 upstream server_pools { server 192.168.232.132:80 weight=5; server 192.168.232.133:80 weight=5; } server { listen 80; server_name www.etiantian01.org; location / { proxy_pass http://server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } } web01 和web02部署nginx和抑制ARP和綁定vip (後面兩部寫腳本執行) [root@web01 html]# ps -ef | grep nginx root 1342 1 0 11:20 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx nginx 1343 1342 0 11:20 ? 00:00:00 nginx: worker process root 1346 1298 0 11:29 pts/0 00:00:00 grep nginx [root@web01 html]# netstat -lnt | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN [root@web01 html]# lsof -i tcp:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 1342 root 6u IPv4 11119 0t0 TCP *:http (LISTEN) nginx 1343 nginx 6u IPv4 11119 0t0 TCP *:http (LISTEN) [root@web01 html]# 然後抑制arp和綁定vip我們寫如下腳本實現: #!/bin/bash # chkconfig: 3 52 23 #created by chenyant 11:43 2013/8/11 . /etc/init.d/functions VIP=( 192.168.232.80 ) start(){ for ((i=0;i<${#VIP[@]};i++)) do ifconfig lo:$i ${VIP[$i]} netmask 255.255.255.255 up route add -host ${VIP[$i]} dev lo done 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 } start 服務啟動後腳本執行了以後然後就檢查: 可以模擬主備負載均衡服務器宕機 然後客戶端持續訪問看下是否會有影響。 拆除單臺webserver 看下訪問是否會受影響。 此文的只是模擬環境學習使用,生產環境相對復雜些需要結合實踐情況學習。
Nginx負載均衡+keepalived高可用