阿哥论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 433|回复: 0

Nginx+keepalived 脚本安装主从双机热备自动切换解决方案

[复制链接]

2019

主题

1

好友

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

优秀会员 助人为乐 辛勤工作 技术精英 多才多艺 优秀班竹 灌水天才 星球管理 宣传大使 灌水之王 财富勋章 版主勋章 动漫勋章 勤奋会员 论坛精英 PS高手 心 8 闪游皮肤 双鱼座 8★8➹ 志愿者 乖

发表于 2014-5-20 18:26:32 |显示全部楼层
nginx+keepalived 脚本安装主从双机热备自动切换解决方案
Nginx+keepalived主从双机热备自动切换解决方案

测试环境如下:
系统:Ceentos 6.4 64位
主nginx服务器:192.168.122.5
备nginx服务器:192.168.122.6
VIP:192.168.122.15

一、Nginx+keepalived 安装脚本安装
  1. #!/bin/bash
  2. # author: kuangl
  3. # mail: kuangl@orient-media.com
  4. # description: The installation of Nginx files.
  5. # -------------------------------------------------------- #
  6. ## Nginx_install
  7. # -------------------------------------------------------- #
  8. # Nginx installation
  9. #CURRENT_PATH=$(pwd)
  10. for i in $(rpm -q gcc gcc-C++ kernel-devel openssl-devel zlib-devel popt-devel popt-static libnl-devel wget make |grep 'not installed' | awk '{print $2}')
  11. do
  12. yum -y install $i
  13. done
  14. [ -d /root/software ]
  15. [ "$?" != 0 ] && mkdir /root/software
  16. cd /root/software
  17. [ !-e pcre-8.33.tar.gz ] && wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
  18. tar -zxvf pcre-8.33.tar.gz
  19. cd pcre-8.33
  20. ./configure
  21. make && make install
  22. echo $? || [ $? != 0] || echo" installation pcrefailed" || exit 1
  23. cd /root/software
  24. [ ! -e nginx-1.2.9.tar.gz ] && wget http://nginx.org/download/nginx-1.2.9.tar.gz
  25. tar -zxvf nginx-1.2.9.tar.gz
  26. cd nginx-1.2.9
  27. ./configure--prefix=/usr/local/nginx --with-http_ssl_module --with-http_sub_module --with-http_stub_status_module--with-http_gzip_static_module
  28. make && make install
  29. echo $? || [ $? != 0] || echo" installationnginxfailed" || exit 1
  30. # -------------------------------------------------------- #
  31. ## Keepalived_intsall
  32. # -------------------------------------------------------- #
  33. # Keepalived installation
  34. cd /root/softwarae
  35. [ ! -e keepalived-1.2.4.tar.gz ] &&wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz
  36. tar -zxvf keepalived-1.2.4.tar.gz
  37. cd keepalived-1.2.4
  38. ln -s /usr/src/kernels/$(uname -r) /usr/src/kernels/linux
  39. ./configure --prefix=/usr--bindir=/usr/bin--sbindir=/usr/bin--libexecdir=/usr/libexec --localstatedir=/var --libdir=/lib64--infodir=/usr/share/info--sysconfdir=/etc --mandir=/usr/local/share/man--with-kernel-dir=/usr/src/kernels/linux
  40. make && make install
  41. echo $? || [ $? != 0] || print " installation keepalivedfailed" || exit 1
  42. chkconfig --add keepalived
  43. chkconfig --level 345 keepalived on
复制代码
二、主Nginx 配置
  1. [root@node5 conf]# mkdir -p /var/www/html
  2. [root@node5 conf]# cat "192.168.122.5" > /var/www/html/index.html
  3. [root@node5 conf]# vim nginx.conf
复制代码
  1. [root@node5 conf]# ../sbin/nginx -s reload
  2. [root@node5 conf]# curl http://192.168.122.5
  3. 192.168.122.5
复制代码
三、主Keepalived配置
  1. [root@node6 conf]# vim /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. notification_email {
  5. 404060@qq.com
  6. 138162@139.com
  7. }
  8. notification_email_from 404060@qq.com
  9. smtp_server 127.0.0.1
  10. smtp_connect_timeout 30
  11. router_id LVS_DEVEL
  12. }
  13. vrrp_script chk_nginx {
  14. script "/etc/keepalived/chk_nginx.keepalived.sh"
  15. interval 2
  16. weight 2
  17. }
  18. vrrp_instance VI_1 {
  19. state MASTER
  20. interface eth0
  21. virtual_router_id 51
  22. priority 200
  23. advert_int 1
  24. authentication {
  25. auth_type PASS
  26. auth_pass kuangling
  27. }
  28. track_script {
  29. chk_nginx.keepalived
  30. }
  31. virtual_ipaddress {
  32. 192.168.122.15
  33. }
  34. }
复制代码
四、备nginx配置
  1. [root@node6 conf]# mkdir -p /var/www/html
  2. [root@node6 conf]# cat "192.168.122.6" > /var/www/html/index.html
  3. [root@node6 conf]# vim nginx.conf
复制代码
  1. [root@node6 conf]# ../sbin/nginx -s reload
  2. [root@node6 conf]# curl http://192.168.122.6
  3. 192.168.122.6
复制代码
五、备keepalived配置
  1. [root@node6 conf]# vim /etc/keepalived/keepalived.conf
  2. ! Configuration File for keepalived
  3. global_defs {
  4. notification_email {
  5. 404060@qq.com
  6. 138162@139.com
  7. }
  8. notification_email_from 404060@qq.com
  9. smtp_server 127.0.0.1
  10. smtp_connect_timeout 30
  11. router_id LVS_DEVEL
  12. }
  13. vrrp_script chk_nginx {
  14. script "/etc/keepalived/chk_nginx.keepalived.sh"
  15. interval 2
  16. weight 2
  17. }
  18. vrrp_instance VI_1 {
  19. state BACKUP
  20. interface eth0
  21. virtual_router_id 51
  22. priority 150
  23. advert_int 1
  24. authentication {
  25. auth_type PASS
  26. auth_pass kuangling
  27. }
  28. track_script {
  29. chk_nginx.keepalived
  30. }
  31. virtual_ipaddress {
  32. 192.168.122.15
  33. }
  34. }
复制代码
六、分别在2nginx服务器上添加检测脚本:
  1. [root@node6 conf]# vim /etc/keepalived/chk_nginx.keepalived.sh
  2. #!/bin/bash
  3. # description:
  4. # 定时查看Nginx是否存在,如果不存在则启动Nginx
  5. # 如果启动失败,则停止keepalived
  6. status=`ps -C nginx --no-header |wc -l`
  7. if [ $status -eq 0 ];then
  8. /usr/local/nginx/sbin/nginx
  9. sleep 3
  10. if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
  11. killall keepalived
  12. fi
  13. fi
  14. [root@node6 conf]#chmod +x /etc/keepalived/chk_nginx.keepalived.sh
复制代码
七、测试分别在2台nginx上启动nginx和keepalived服务,然后分别用ip a 查看ip


本文出自 “&思远晨曦” 博客,请务必保留此出处http://kling.blog.51cto.com/3320545/1240359










Nginx+keepalived 脚本安装主从双机热备自动切换解决方案

该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

回顶部