nginx負載均衡+keepalived高可用完全配置小結
nginx做負載均衡(無高可用)
大致步驟。
1. 前端
nginx安裝,pcre安裝,具體步驟不解釋。
2. 負載配置
A. 預設輪循
在nginx.conf 里加入一行
include upstream.conf,然後所有的負載均衡的配置直接在upstream.conf裡配置。
[[email protected] conf]# cat upstream.conf
upstream httpservers {
server 192.168.137.10:80 weight=5;
server 192.168.137.20:80 weight=5;
}
server {
listen 80;
server_name 192.168.137.100;
location / {
proxy_pass http://httpservers;
}
}
A. ip_hash每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題
[[email protected] conf]# cat upstream.conf
upstream httpservers {
ip_hash;
server 192.168.137.10:80 weight=5;
server 192.168.137.20:80 weight=5;
}
server {
listen 80;
server_name 192.168.137.100;
location / {
proxy_pass http://httpservers;
}
}
nginx做負載均衡(keepalived高可用)
拓撲圖
1. nginx配置(在主備伺服器上配置)
upstream httpservers {
ip_hash;
server 192.168.137.10:80 weight=5;
server 192.168.137.20:80 weight=5;
}
server {
listen 80;
server_name 192.168.137.201;
location / {
proxy_pass http://httpservers;
}
}
2. keepalived安裝及配置(在主備伺服器上配置)
下載:wget http://www.keepalived.org/software/keepalived-1.2.8.tar.gz
解壓:tar zxvf keepalived-1.2.8.tar.gz
編譯安裝: ./configure --prefix=/usr/local/keepalived
make
make install
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d
cp /usr/local/keepalived/sbin/keepalived /sbin/
mkdir -p /etc/keepalived/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ (配置檔案一定要在這裡,讓我費了好大的勁)
修改/etc/keepalived/keepalived.conf , 以下分別列出master和backup的配置
+++++++++++++++++++++++++++++++++++++master+++++++++++++++++++++++++++++++++++++++++++
! Configuration File for keepalived
global_defs {
router_id nginx-proxy-ha
}
vrrp_script chk_http_port {
script "/usr/bin/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
track_interface {
eth0
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.137.201
}
}
++++++++++++++++++++++++backup++++++++++++++++++++++++++++++++++++++++++++++++++
! Configuration File for keepalived
global_defs {
router_id nginx-proxy-ha
}
vrrp_script chk_http_port {
script "/usr/bin/check_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 180
advert_int 1
authentication {
auth_type PASS
auth_pass 1234
}
track_interface {
eth0
}
track_script {
chk_http_port
}
virtual_ipaddress {
192.168.137.201
}
}
以上配置完成之後 啟動nginx和keepalived,一定要先啟動nginx,然後在啟動keepalived。
測試效果:master和backup都啟動keepalived和nginx後,預設的虛擬ip是在master上的,在master上kill nginx後,浮動ip會繫結在backup上。
各配置解釋:
global_defs {
notification_email {
[email protected] (這裡可以定義多個報警郵箱)
}
notification_email_from [email protected] (報警人)
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/opt/tools/bin/check_ng.sh"
interval 2 (檢測指令碼執行的間隔)
weight 2
}
vrrp_instance VI_1 {
state BACKUP (顯示定義為從伺服器)
interface eth1 (繫結的網口,該網口即上面提到的兩個IP的介面)
virtual_router_id 51 (定義的ID,官方的是 51,主從伺服器必須一直)
mcast_src_ip 211.151.138.3 (從伺服器的IP)
priority 50 (優先順序,任意定義,但是一定要比主伺服器低)
advert_int 1
authentication {
auth_type PASS
auth_pass 1111 (預設即可)
}
track_script {
chk_http_port (呼叫檢測指令碼)
}
virtual_ipaddress {
211.151.137.5 (繫結的虛IP)
}
}
++++++++++++++++++++++check_nginx.sh+++++++++++++++++++++++++++++++++++++++++++++++++
該指令碼用來檢測nginx狀態,一旦nginx退出,則關閉master的 keepalived,此時的backup收到訊息會自動切換為master並繫結虛擬ip。該指令碼放在/usr/bin下
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall -9 keepalived
fi
轉載於:https://blog.51cto.com/flyingdreams/1535155