日期:2014-05-17  浏览次数:20946 次

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状态

[root@localhost src]#? 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: [ifindex(2), proto(112), fd(9,10)]
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[2832]: 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会绑定到备机上

?

[root@localhost ~]# 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/21 brd 10.2.7.255 scope global eth0
??? inet 10.2.2.137/32 scope global eth0
??? inet6 fe80::20c:29ff:fe6a:9d0e/64 scope link