Nginx+keepalived负载均衡

 nginx有很强代理功能,但是一台nginx就形成了单点,现在使用keepalived来解决这个问题,keepalived的故障转移时间很短,而且配置简单,这也是选择keepalived的一个主要原因,建议日PV值小的中小型企业web均可采用如下方案实行,下面直接上安装步骤:
一、环境:
centos4.8、nginx-0.7.62、keepalived-1.1.19
master:192.168.10.61
slave:192.168.10.62
vip:192.168.10.60

二、安装nginx(master和slave安装步骤一样)
1.Tcmalloc 优化Nginx性能
64位操作系统请先安装libunwind库,32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API。

# tar zxvf libunwind-0.99-alpha.tar.gz
# cd libunwind-0.99-alpha/
# CFLAGS=-fPIC ./configure
# make CFLAGS=-fPIC
# make CFLAGS=-fPIC install

2、安装google-perftools:
# tar zxvf google-perftools-0.97.tar.gz
# cd google-perftools-0.97/
# ./configure
# make && make install
# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
# /sbin/ldconfig
# lsof -n|grep tcmalloc
在编译Nginx时添加参数--with-google_perftools_module

3.安装 pcre
#tar zxvf pcre-7.8.tar.gz
#cd pcre-7.8
#./configure
#make && make install
4、安装nginx
# tar zxvf nginx-0.7.62.tar.gz
# cd nginx-0.7.62
# ./configure --with-http_stub_status_module --with-google_perftools_module --prefix=/usr/local/nginx
# make && make install
启动 nginx
# /usr/local/nginx/sbin/nginx
然后我们用IE测试一下 http://IP地址或域名 就可以看到nginx的默认的页面证明nginx已经启动
关闭 nginx
# kill -QUIT `cat /usr/local/nginx/logs/nignx.pid`
重启 nginx
# kill -HUP `cat /usr/local/nginx/logs/nignx.pid`
三、安装keepalived

# tar zxvf keepalived-1.1.19.tar.gz
# cd keepalived-1.1.19
# ./configure --prefix=/usr/local/keepalived
# make
# make install
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# mkdir /etc/keepalived
# cd /etc/keepalived/
# vi keepalived.conf
! Configuration File for keepalived
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_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.10.62    <==辅nginx的IP地址
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.10.60                      <==VIP地址
}
}
# service keepalived start

通过命令看一下
# ip a
1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:af:08:1d brd ff:ff:ff:ff:ff:ff
inet 192.168.10.61/24 brd 192.168.10.255 scope global eth0
inet 192.168.10.60/32 scope global eth0
inet6 fe80::20c:29ff:feaf:81d/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
说明vip已经启动,这样主服务器就配置好了,辅机的配置大致一样,除了配置文件有少部分的变化,下面贴出辅机的配置文件:

! Configuration File for keepalived
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_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
mcast_src_ip 192.168.10.61              <==主nginx的IP的地址
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass yahunet
}
virtual_ipaddress {
192.168.10.60
}
}
# ip a
1: lo: <LOOPBACK,UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:3f:60:89 brd ff:ff:ff:ff:ff:ff
inet 192.168.10.62/24 brd 192.168.10.255 scope global eth0
inet 192.168.10.60/32 scope global eth0
inet6 fe80::20c:29ff:fe3f:6089/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
四、测试
测试其效果方法很简单,分别在主辅机上建立不同的主页,index.html的内容分别为192.168.10.61,192.168.10.62,
然后用客户机上 http://192.168.10.60,主机down掉后辅机会马上接替提供服务,主机恢复后会马上再接替回来。

你可能感兴趣的:(nginx,keepalived,职场,负载,休闲)