使用nginx+keepalived做双机热备+负载均衡

2250阅读 0评论2016-06-30 douyaqiang123
分类:服务器与存储

一、keepalived作用说明
Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。

二、keepalived的安装
我们采用源代码的方式来安装keepalived.

点击(此处)折叠或打开

  1. cd /usr/local/src
  2. wget
  3. tar -xvf keepalived-1.2.20.tar.gz
  4. cd keepalived-1.2.20
  5. ./configure --prefix=/usr/local/keepalived
  6. make
  7. make install


1.建立服务启动脚本,以便使用service命令控制;
(1)复制文件
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived  /etc/init.d/keepalived
(2)添加执行权限
chmod +x /etc/init.d/keepalived

2.修改/etc/init.d/keepalived文件
将/etc/sysconfig/keepalived修改为.  /usr/local/keepalived/etc/sysconfig/keepalived

3.在上述行下添加内容(将keepalived主程序所在路径导入到环境变量PATH中)
PATH="$PATH:/usr/local/keepalived/sbin"
export PATH

4.修改/usr/local/keepalived/etc/sysconfig/keepalived文件,设置正确的服务启动参数
KEEPALIVED_OPTIONS="-D -f /usr/local/keepalived/etc/keepalived/keepalived.conf"

5.至此,keepalived配置完毕,启动
service keepalived restart

6.设置服务为开机启动
chkconfig keepalived on


三、keepalived的配置

1.主机说明
本次共涉及到4台主机。
主机1(安装nginx+keepalived)为主负载均衡机器,IP:192.168.109.210
主机2(安装nginx+keepalived)为备负载均衡机器,IP:192.168.109.207
主机3(安装apache)为Web服务器,IP:192.168.109.208
主机4(安装apache)为Web服务器,IP:192.168.109.209

2.搭建说明

(1)首先使用主机1为负载均衡机器(停掉主机2上的nginx),并启动主机3、4上的apache。
(2)通过配置主机1上nginx,使其实现负载均衡,让所有请求均匀分配到主机3、4上来处理请求;
(3)在实现负载均衡后,为了实现服务持续可用,需对负载均衡器配置主从切换。当主机1出现故障时,通过keepalived迅速切换到主机2上。
(4)主机3和主机4上分别在/var/www/html目录下放置有页面index.html,页面内容为

web1.test.com


web2.test.com



3.搭建步骤

(1)配置nginx,主机1和主机2的配置文件相同
配置文件名:/usr/local/nginx/conf/nginx.conf

点击(此处)折叠或打开

  1. #配置进程数,一般与CPU核心数相同或者核心的倍数
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #access_log logs/access.log main;
  7. events {
  8.        #设置单个进程的最大连接数
  9.        worker_connections 1024;
  10.        }
  11. http {
  12.         include mime.types;
  13.         default_type application/octet-stream;
  14.         sendfile on;
  15.         keepalive_timeout 65;
  16.         # access_log logs/access.log main;
  17.         server {
  18.             listen 80;
  19.             server_name localhost;
  20.             location / {
  21.                 root html;
  22.                 #自定义错误页面
  23.                 index index.html index.htm;
  24.             }
  25.             error_page 500 502 503 504 /50x.html;
  26.             location = /50x.html {
  27.                 root html;
  28.             }
  29. }
  30.     #设置负载均衡服务器组
  31.     upstream myserv {
  32.     server 192.168.109.208:80 weight=1;
  33.     server 192.168.109.209:80 weight=1;
  34.     }
  35.     server {
  36.         #设置监听VIP(虚拟IP),,该IP在主机3、4的配置文件中有配置
  37.         listen 192.168.109.215:80;
  38.         server_name myserv;
  39.         # access_log /var/logs/access.log main;
  40.         location / {
  41.                 index index.html index.htm;
  42.                 #设置反向代理
  43.                 proxy_pass http://myserv;
  44.                 proxy_set_header X-Real-IP $remote_addr;
  45.         }
  46.     }
  47. }
(2)配置keepalived

说明:分别在主机1和2上安装keepalived. 其中1为主,2为备。配置文件名:/etc/keepalived/keepalived.conf
         
(1)主机1上的keepalived.conf

点击(此处)折叠或打开

  1. ! Configuration File for keepalived
  2. global_defs {
  3.     notification_email {
  4.         #设置邮件收件人
  5.         linuxedu@foxmail.com
  6.         mageedu@126.com
  7.     }
  8.     #设置邮件发件人
  9.     notification_email_from kanotify@magedu.com
  10.     smtp_connect_timeout 3
  11.     smtp_server 127.0.0.1
  12.     router_id LVS_DEVEL
  13. }
  14. cript chk_schedown {
  15.     script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
  16.     interval 2
  17.     weight -2
  18. }
  19. #第一种主备切换方案[当监测到主的nginx为0时,启动它;如果启动不了就杀掉主的keepalived]
   vrrp_script chk_http_port {
#    script "/usr/local/nginx/nginx_check.sh"
#    interval 2
#    weight -2
#}
   #第二种主备切换方案[当监测到主的nginx进程是否存活,若没有则降低权重]
   vrrp_script chk_http_port {
   script "killall -0 nginx"
   interval 2
   weight -2
}

  1. rrp_script chk_schedown {
  2. vrrp_instance VI_1 {
  3.     interface eth0
  4.     #配置主从,MASTER为主,BACKUP为备
  5.     state MASTER
  6.     #设置优先级,数值越大,优先级越高,都是主比备大
  7.     priority 101
  8.     virtual_router_id 51
  9.     garp_master_delay 1
  10.     authentication {
  11.     auth_type PASS
  12.     auth_pass password
  13.     }
  14.     track_interface {
  15.     eth0
  16.     }
  17.     #设置虚拟IP地址,主备虚拟IP地址相同
  18.     virtual_ipaddress {
  19.     192.168.109.215
  20.     }
  21.     track_script {
  22.     chk_schedown
  23.     }
  24. }
(2)备机2上的keepalived.conf

点击(此处)折叠或打开

  1. ! Configuration File for keepalived
  2. global_defs {
  3.     notification_email {
  4.         #设置邮件收件人
  5.         linuxedu@foxmail.com
  6.         mageedu@126.com
  7.     }
  8. #设置邮件发件人
  9. notification_email_from kanotify@magedu.com
  10. smtp_connect_timeout 3
  11. smtp_server 127.0.0.1
  12. router_id LVS_DEVEL
  13. }
  14. cript chk_schedown {
  15.     script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
  16.     interval 2
  17.     weight -2
  18. }
  19. rrp_script chk_schedown {
  20. vrrp_instance VI_1 {
  21.     interface eth0
  22.     #配置主从,MASTER为主,BACKUP为备
  23.     state BACKUP
  24.     #设置优先级,数值越大,优先级越高,都是主比备大
  25.     priority 100
  26.     virtual_router_id 51
  27.     garp_master_delay 1
  28.     authentication {
  29.     auth_type PASS
  30.     auth_pass password
  31.     }
  32.     track_interface {
  33.     eth0
  34.     }
  35.     #设置虚拟IP地址,主备虚拟IP地址相同
  36.     virtual_ipaddress {
  37.     192.168.109.215
  38.     }
  39.     track_script {
  40.     chk_schedown
  41.     }
  42. }
至此,keepalived配置完毕!

4.测试验证

(1)验证负载均衡
在浏览器地址栏输入虚拟IP:192.168.109.215,看是否会看到主机3和主机4对应的页面内容:


并且这2个页面是交替出现。此时即到达了负载均衡。

(2)验证keepalived主备切换

停止主机1上的nginx和keepalived,然后重新刷新页面,看是否还能打开,若能打开,说明keepalived切换成功。
注意:同一台机器上的nginx和keepalived必须同时停止才可以!!不能只停nginx!不然不会生效!



5.编写nginx监控脚本

点击(此处)折叠或打开

  1. vim /usr/local/nginx/nginx_check.sh
  2. #!/bin/bash
  3. # varsion 0.0.2
  4. # 根据一网友说这样做不科学,如果nginx服务起来了,但是我把keepalived 杀掉了,我的理由是,如果nginx死掉了,我觉得就很难在起来,再有就是nagios当然要给你报警了啊。不过这位同学说的有道理,所以就稍加改了一下脚本
  5. A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A
  6. if [ $A -eq 0 ];then ## 如果没有进程值得为零
  7.         /usr/local/nginx/sbin/nginx
  8.         sleep 3
  9.         if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  10.             killall keepalived ## 则结束 keepalived 进程
  11.         fi
  12. fi
参考地址:http://deidara.blog.51cto.com/400447/302402/



上一篇:不小心删除数据--MySQL数据恢复
下一篇:运维经理的运维经验总结