一,简介
nginx是一款轻量级的web服务器和反向代理服务器,它拥有较强的并发能力,较为简单灵活的配置,以及超强的性能。nginx越来越受到人们的关注,国内许多站点都采用nginx作为其web服务器或者反向代理。
keepalived是linux中使用vrrp协议实现高可用功能的一个软件,vrrp可以将两台或者多台路由设备虚拟成一台设备,并提供vip供外网访问,内部通过相应算法选举出MASTER,将vip配置在MASTER上,向外提供相应服务,当MASTER宕机时,BACKUP在一定时间内接收不到MASTER传递过来的信息时,此时,选举会再次发生,BACKUP节点就会取代MASTER节点,成为新的MASTER,并继续向外提供服务,这就实现了服务的高可用性。
二,实验环境
两台linux主机,系统版本为redhat5.8 32位
node1的ip地址为: 192.168.1.85
node2的ip地址为: 192.168.1.86
vip为: 192.168.1.90
keepalived软件版本 keepalived-1.2.7.tar.gz 下载地址:http://www.keepalived.org/download.html
nginx软件版本为:nginx-1.4.7.tar.gz 下载地址为 http://nginx.org/
三,实验过程
1,解压并安装相应软件
keepalived:
tar xf keepalived-1.2.7.tar.gz cd keepalived-1.2.7 ./configure make make install
(1)拷贝相应文件到相应目录中
cp /usr/local/sbin/keepalived /usr/sbin/ cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/etc/keepalived/ /etc/ -r
(2)编辑keepalived.conf配置文件
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bkup 保存一份配置
编辑如下
! Configuration File for keepalived global_defs { notification_email { root@localhost #定义收件人 } notification_email_from keepalived@localhost #定义发件人 smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "killall -0 nginx" #检测nginx是否在线 interval 1 #检测周期为1秒 weight 2 #如果nginx不在线,则权重降低2 } vrrp_script chk_keepalived_down { script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0" #测试keepalived的主备切换情况 interval 1 weight 2 } vrrp_instance VI_1 { state MASTER #node1为主节点 interface eth0 #监听的接口 virtual_router_id 52 #虚拟路由ID,两端必须一致
priority 101 #优先级,MASTER端要高于BACKUP端 advert_int 1 #通告周期 1 秒 authentication { auth_type PASS #认证方式 auth_pass 1111 #认证秘钥 } virtual_ipaddress { 192.168.1.90 #vip的设定 } track_script { #要执行的检测脚本 chk_nginx chk_mantaince_down }
keepalived.conf中还有许多内容,但都是为lvs准备,这里并没有启动lvs功能,所以都删掉了。
node2的配置和node1类似,只不过要把state MASTER改为BACKUP,把优先级调的比MASTER小一些即可。
nginx:
(1)解决依赖关系
yum install pcre-devel -y
(2)添加用户和组
groupadd -r nginx useradd -r -g nginx nginx
(3)解压并编译安装
tar xf nginx-1.4.7.tar.gz cd nginx-1.4.7 ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
make && make install
(4)修改下主页后就可以启动服务了
在两个节点上启动keepalived
service keepalived start
在node1上查看日志
tail /var/log/messages Aug 19 19:12:16 localhost Keepalived_vrrp[27587]: VRRP_Script(chk_nginx) succeeded Aug 19 19:12:16 localhost Keepalived_vrrp[27587]: VRRP_Script(chk_mantaince_down) succeeded Aug 19 19:12:17 localhost Keepalived_vrrp[27587]: VRRP_Instance(VI_1) Transition to MASTER STATE Aug 19 19:12:18 localhost Keepalived_vrrp[27587]: VRRP_Instance(VI_1) Entering MASTER STATE Aug 19 19:12:18 localhost Keepalived_vrrp[27587]: VRRP_Instance(VI_1) setting protocol VIPs. Aug 19 19:12:18 localhost Keepalived_healthcheckers[27586]: Netlink reflector reports IP 192.168.1.90 added Aug 19 19:12:18 localhost avahi-daemon[3327]: Registering new address record for 192.168.1.90 on eth0. Aug 19 19:12:18 localhost Keepalived_vrrp[27587]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.90 Aug 19 19:12:18 localhost Keepalived_vrrp[27587]: Netlink reflector reports IP 192.168.1.90 added Aug 19 19:12:23 localhost Keepalived_vrrp[27587]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.90
可以看到node1被选举为master,vip配置在node1上
使用ip addr 查看
... 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:0c:29:a3:12:75 brd ff:ff:ff:ff:ff:ff inet 192.168.1.85/24 brd 192.168.1.255 scope global eth0 inet 192.168.1.90/32 scope global eth0
查看node2上日志信息
Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: Registering Kernel netlink reflector Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: Registering Kernel netlink command channel Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: Registering gratuitous ARP shared channel Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: Opening file '/etc/keepalived/keepalived.conf'. Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: Configuration is using : 40529 Bytes Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: Using LinkWatch kernel netlink reflector... Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: VRRP_Instance(VI_1) Entering BACKUP STATE Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)] Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: VRRP_Script(chk_nginx) succeeded Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: VRRP_Script(chk_mantaince_down) succeeded
为backup状态
此时打开浏览器访问vip 192.168.1.90 提供页面的是node1
将node1上的nginx服务down掉
service nginx stop
再次查看node1 上的日志
Aug 19 19:18:15 localhost Keepalived_vrrp[27587]: VRRP_Script(chk_nginx) failed Aug 19 19:18:16 localhost Keepalived_vrrp[27587]: VRRP_Instance(VI_1) Received higher prio advert Aug 19 19:18:16 localhost Keepalived_vrrp[27587]: VRRP_Instance(VI_1) Entering BACKUP STATE Aug 19 19:18:16 localhost Keepalived_vrrp[27587]: VRRP_Instance(VI_1) removing protocol VIPs. Aug 19 19:18:16 localhost Keepalived_healthcheckers[27586]: Netlink reflector reports IP 192.168.1.90 removed Aug 19 19:18:16 localhost avahi-daemon[3327]: Withdrawing address record for 192.168.1.90 on eth0. Aug 19 19:18:16 localhost Keepalived_vrrp[27587]: Netlink reflector reports IP 192.168.1.90 removed Aug 19 19:18:17 localhost Keepalived_vrrp[27587]: VRRP_Script(chk_nginx) succeeded
此时,node1已经变为backup状态,vip也已经被移除
查看node2的日志信息
Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: VRRP_Script(chk_nginx) succeeded Aug 19 19:12:17 localhost Keepalived_vrrp[16800]: VRRP_Script(chk_mantaince_down) succeeded Aug 19 19:18:16 localhost Keepalived_vrrp[16800]: VRRP_Instance(VI_1) forcing a new MASTER election Aug 19 19:18:17 localhost Keepalived_vrrp[16800]: VRRP_Instance(VI_1) Transition to MASTER STATE Aug 19 19:18:18 localhost Keepalived_vrrp[16800]: VRRP_Instance(VI_1) Entering MASTER STATE Aug 19 19:18:18 localhost Keepalived_vrrp[16800]: VRRP_Instance(VI_1) setting protocol VIPs. Aug 19 19:18:18 localhost Keepalived_vrrp[16800]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.90
node2成为master节点,整个切换过程完成。