Nginx 负载均衡 apache https 之 keepalived 配置
继续我的负载均衡配置,今天配置到nginx 的主备模式,采用 keepalived我的配置如下:
1、安装 keepalived,作为Nginx 的HA
keepalived 下载地址 http://www.keepalived.org/download.html,我下载的是 keepalived-1.1.20.tar.gz,
2、解压 安装
# tar -xzvf keepalived-1.1.20.tar.gz
# cd keepalived-1.1.20
# ./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 #编写配置文件(主ngingx配置)
! Configuration File for keepalived
global_defs {
notification_email {
test@test.com
}
notification_email_from keepalived@chtopnet.com
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 10.2.2.136 #主ngingx ip
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
10.2.2.137 #vip ip地址
}
}
启动keepalived
#service keepalived start
查看 keepalived状态
# ps -ef | grep keepalived
root 4783 1 0 09:49 ? 00:00:00 keepalived -D
root 4784 4783 0 09:49 ? 00:00:01 keepalived -D
root 5191 4896 0 13:43 pts/2 00:00:00 grep keepalived
查看日志
Nov 27 07:18:59 localhost Keepalived: Starting Keepalived v1.1.20 (11/27,2010)
Nov 27 07:18:59 localhost Keepalived: Starting VRRP child process, pid=6940
Nov 27 07:18:59 localhost Keepalived_vrrp: Registering Kernel netlink reflector
Nov 27 07:18:59 localhost Keepalived_vrrp: Registering Kernel netlink command channel
Nov 27 07:18:59 localhost Keepalived_vrrp: Registering gratutious ARP shared channel
Nov 27 07:18:59 localhost Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Nov 27 07:18:59 localhost Keepalived_vrrp: Configuration is using : 35726 Bytes
Nov 27 07:18:59 localhost Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Nov 27 07:19:00 localhost Keepalived_vrrp: VRRP sockpool:
Nov 27 07:19:01 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Nov 27 07:19:02 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Nov 27 07:19:02 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Nov 27 07:19:02 localhost avahi-daemon: Registering new address record for 10.2.2.137 on eth0.
Nov 27 07:19:02 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.2.2.137
查看ip,ip浮动已经绑定到主nginx上了,当主机宕机,备机工作是,浮动ip会绑定到备机上
# ip addr
1: lo: <LOOPBACK,UP,LOWER_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 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:6a:9d:0e brd ff:ff:ff:ff:ff:ff
inet 10.2.2.136/21brd 10.2.7.255 scope global eth0
inet 10.2.2.137/32scope global eth0
inet6 fe80::20c:29ff:fe6a:9d0e/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
这样主ngingx服务器就配置好了,然后配置备机
3、配置步骤同上,解压,安装,只是配置文件略有不同
# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
test@test.com
}
notification_email_from keepalived@chtopnet.com
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 10.2.2.127
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
10.2.2.137
}
}
查看备机日志
# tail /var/log/messages
Nov 26 14:19:24 localhost Keepalived: Starting Keepalived v1.1.20 (11/26,2010)
Nov 26 14:19:24 localhost Keepalived_vrrp: Registering Kernel netlink reflector
Nov 26 14:19:24 localhost Keepalived_vrrp: Registering Kernel netlink command channel
Nov 26 14:19:24 localhost Keepalived_vrrp: Registering gratutious ARP shared channel
Nov 26 14:19:24 localhost Keepalived: Starting VRRP child process, pid=5282
Nov 26 14:19:24 localhost Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Nov 26 14:19:24 localhost Keepalived_vrrp: Configuration is using : 35734 Bytes
Nov 26 14:19:24 localhost Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Nov 26 14:19:24 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Nov 26 14:19:24 localhost Keepalived_vrrp: VRRP sockpool:
验证:
一直ping 10.2.2.137 ,然后将主nginx的network停掉,可以看到大概有2个timeout,BACKUP在极短的时间内接替了工作。再将主的network开启,可以看到主又继续接替了BACKUP,继续工作
查看备机的日志
# tail /var/log/messages
Nov 26 14:19:24 localhost Keepalived_vrrp: Configuration is using : 35734 Bytes
Nov 26 14:19:24 localhost Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Nov 26 14:19:24 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Nov 26 14:19:24 localhost Keepalived_vrrp: VRRP sockpool:
Nov 26 14:24:43 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Nov 26 14:24:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Nov 26 14:24:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Nov 26 14:24:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.2.2.137
Nov 26 14:24:44 localhost avahi-daemon: Registering new address record for 10.2.2.137 on eth0.
Nov 26 14:24:49 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.2.2.137
配置完成!
参考文档
http://sunqi.iyunv.com/blog/735528
http://network.iyunv.com/art/201007/209823_2.htm
页:
[1]