nginx+keepalived双master负载均衡配置
部署了一下nginx+keepalived 负载均衡总结如下主负载均衡器负责转发,而备份负载均衡器则处于等待状态,只有主负载均衡器失效,备份负载均衡器才承担用户请求转发任务。在多负载均衡应用场景下,将两个负载均衡器都转发用户请求,其主要用意在于提高资源的利用率。nginx互为主备。
环境如下:
nginx master 1:192.168.137.160
nginx master 2:192.168.137.161
VIP1:192.168.137.164
VIP2:192.168.137.165
1.在nginx master上安装nginx和keepalived,安装方法不再复述
安装完成后将Keepalived安装成Linux服务。依次执行下列命令
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
chkconfig --level 2345 keepalived on
2.master1,master2的nginx配置文件简略如下
user www www;
worker_processes 8;
error_log /services/web/log/nginx_error.log crit;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
.................
upstream backend
{
#ip_hash;
server 192.168.137.179:80;
server 192.168.137.180:80;
}
server
{
listen 80;
server_name localhost;
location /
{
root /services/web/htdocs;
index index.html index.php index.htm;
proxy_redirect off;
proxy_set_header Host $Host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for;
proxy_pass http://backend;
#include fastcgi.conf;
#fastcgi_pass unix:/var/run/phpfpm.sock;
#fastcgi_index index.php;
}
}
}
3.nginx master1的keepalived.conf如下
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.137.160
priority 180
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.137.164
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 52
mcast_src_ip 192.168.137.160
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.137.165
}
}
nginx master2的keepalived.conf如下
! Configuration File for keepalived
global_defs {
router_id LVS_MASTER
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.137.161
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.137.164
}
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 52
mcast_src_ip 192.168.137.161
priority 180
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.137.165
}
}
启动keepalived,nginx
services keepalived start
/usr/local/ngins/sbin/nginx
4.查看VIP
master1上执行 ip add list
master2上执行 ip add list
可以看到master1上绑定的vip为192.168.137.164,master2上绑定的vip为192.168.137.165.
需要考虑另外一个问题是当负载均衡nginx挂掉的时候,keepalived不能进行切换需要自行写监控脚本。
下雨了,别忘了打伞,湿身是小,淋病就麻烦啦*^_^* 修养的艺术,其实就是说谎的艺术。 长得真有创意,活得真有勇气! 爱她,就请为她做无痛人流手术! 有道理。。。 恋爱就是无数个饭局,结婚就是一个饭局。
页:
[1]