Nginx+Keepalived 實現反代 負載均衡 高可用(HA)配置
Nginx+Keepalived實現反代負載均衡高可用(HA)配置
OS | IP | 子網掩碼 | 路由閘道器 |
Centos6.6 nginx Keepalived | Eth0:192.168.26.210 | 255.255.252.0 | 192.168.25.3 |
VIP:192.168.27.210 | |||
Centos6.6 Nginx Keepalived | Eth0:192.168.26.211 | 255.255.252.0 | 192.168.25.3 |
VIP:192.168.27.210 | |||
Centos6.6(WEB) (nginx/apache) | Eth0:192.168.26.212 | 255.255.252.0 | 192.168.25.3 |
Centos6.6(WEB) (nginx/apache) | Eth0:192.168.26.218 | 255.255.252.0 | 192.168.25.3 |
後端:192.168.26.212和192.168.26.218伺服器配置安裝這裡略過。
192.168.26.210和192.168.26.211安裝Keepalived 和Nginx:
Yum install nginx
編輯配置檔案:vim /usr/local/nginx/conf/nginx.conf
#usernobody;
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pidlogs/nginx.pid;
events {
worker_connections1024;
}
http {
includemime.types;
default_typeapplication/octet-stream;
#log_formatmain'$remote_addr- $remote_user [$time_local] "$request" '
#'$status$body_bytes_sent "$http_referer" '
#'"$http_user_agent""$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfileon;
#tcp_nopushon;
#keepalive_timeout0;
keepalive_timeout65;
upstream nginx.jerry.com {
# ip_hash;
server 192.168.26.212:80;
server 192.168.26.218:80;
}
#gzipon;
server {
listen80;
server_namenginx.jerry.com;
#charsetkoi8-r;
#access_loglogs/host.access.logmain;
location/ {
roothtml;
indexindex.htmlindex.htm;
proxy_passhttp://nginx.jerry.com;
}
#error_page404/404.html;
#redirect server error pages to the static page /50x.html
#
error_page500502 503 504/50x.html;
location= /50x.html {
roothtml;
}
#proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location~ \.php$ {
#proxy_passhttp://127.0.0.1;
#}
#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location~ \.php$ {
#roothtml;
#fastcgi_pass127.0.0.1:9000;
#fastcgi_indexindex.php;
#fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
#includefastcgi_params;
#}
#deny access to .htaccess files, if Apache's document root
#concurs with nginx's one
#
#location~ /\.ht {
#denyall;
#}
}
#another virtual host using mix of IP-, name-, and port-based configuration
#
#server{
#listen8000;
#listensomename:8080;
#server_namesomenamealiasanother.alias;
#location/ {
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
#HTTPS server
#
#server{
#listen443ssl;
#server_namelocalhost;
#ssl_certificatecert.pem;
#ssl_certificate_keycert.key;
#ssl_session_cacheshared:SSL:1m;
#ssl_session_timeout5m;
#ssl_ciphersHIGH:!aNULL:!MD5;
#ssl_prefer_server_cipherson;
#location/ {
#roothtml;
#indexindex.htmlindex.htm;
#}
#}
}
注意:192.168.26.211安裝nginx配置完全相同這裡略過。
192.168.26.210安裝keepalived。
yum install -y keepalived
安裝成功後編輯配置檔案:vim /etc/keepalived/keepalived.conf
Keepalived配置檔案:keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from[email protected]
smtp_serversmtp.hysec.com
smtp_connect_timeout30
router_idnginx
}
vrrp_script Monitor_Nginx {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interfaceeth0
virtual_router_id51
priority 100
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
track_script{
Monitor_Nginx
}
virtual_ipaddress{
192.168.27.210
}
}
Nginx狀態檢測指令碼:vim/etc/keepalived/chk_nginx.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
echo'nginx server is died'
/etc/init.d/keepalivedstop
fi
192.168.26.211keepalived配置檔案基本相同:
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from[email protected]
smtp_serversmtp.hysec.com
smtp_connect_timeout30
router_idnginx
}
vrrp_script Monitor_Nginx {
script "/etc/keepalived/chk_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interfaceeth0
virtual_router_id51
priority 99
advert_int1
authentication{
auth_typePASS
auth_pass1111
}
track_script{
Monitor_Nginx
}
virtual_ipaddress{
192.168.27.210
}
}
Nginx狀態檢測指令碼:vim/etc/keepalived/chk_nginx.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
echo'nginx server is died'
/etc/init.d/keepalivedstop
fi
安裝完成後啟動NGINX和keepalived服務。
通過vip:192.168.27.210訪問網站:
訪問成功實現了RR負載均衡。
測試通過域名訪問網站:http://nginx.jerry.com
同樣實現了RR負載均衡。
再把192.168.26.210上的NGINX關閉,測試能否實現vip轉移(ha)
仍然可以訪問:
測試成功
基於ip_hash訪問效果(配置前端NGINX引數)
轉載於:https://blog.51cto.com/jdonghong/1883341