设为首页 收藏本站
查看: 910|回复: 0

[经验分享] Nginx+keepalived 负载均衡高可用配置CentOS6.4

[复制链接]

尚未签到

发表于 2018-12-30 15:11:40 | 显示全部楼层 |阅读模式
  Nginx+keepalived 负载均衡高可用配置CentOS6.4
  
  
一:测试环境
服务器DELL-R720
系统版本:CentOS6.4 64bit
虚拟化 KVM  虚拟机系统CentOS6.4 64bit
虚拟机4台:
Nginx01nginx02web01web02
网络环境:
Nginx01:192.168.40.30
Nginx02:192.168.40.31
Web01:192.168.40.86
Web02:192.168.40.87
Vip192.168.40.32


二:服务配置
1>Nginx01
[root@nginx01~]#wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
[root@nginx01~]#rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
[root@nginx01~]#yum list |grep nginx
nginx-release-centos.noarch             6-0.el6.ngx                       installed
nginx.x86_64                           1.2.8-1.el6.ngx                   nginx  
nginx-debug.x86_64                     1.2.8-1.el6.ngx                   nginx  
[root@nginx01~]#yum -y install nginx
[root@nginx01~]#vi /etc/nginx/nginx.conf
user              nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
   
    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;

upstream  test  {
    server   192.168.40.86;
    server   192.168.40.87;
}

server{
        listen  80;
        server_name  192.168.40.32;
        location / {
        proxy_pass        http://test;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
        }

}

>Keepalived 配置信息
[root@nginx01~]#vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 192.168.200.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}

vrrp_script chk_http_port {
        script "/opt/nginx_pid.sh"   #nginx 服务检测脚本
        interval 2
        weight 2
}

vrrp_instance VI_1 {
    state MASTER   #nginx02修改为BACKUP
    interface eth0
    virtual_router_id 55
    priority 100      #nginx02 修改为 80  低于MASTER即可
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
}   
track_script {             #指定一下检测的脚本http_port
    http_port
    }
    virtual_ipaddress {
        192.168.40.32
    }
}

2>付上nginx服务检测脚本
[root@nginx01~]# vi /opt/nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
                /etc/init.d/nginx start
                sleep 3
                if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
                       killall keepalived
                fi
fi

3>nginx02配置与01相同,keepalived稍作修改

4>启动nginx01 02 上的nginx keepalived服务

三:测试
1>负载均衡测试

刷新

Down40.86这台web再测试只有87这台web响应。

2>HA 测试
Ip a 结果显示vipnginx01
[root@nginx01 ~]# ip a
1: lo:  mtu 16436 qdisc noqueue state UNKNOWN
    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:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:57:3b:80 brd ff:ff:ff:ff:ff:ff
    inet 192.168.40.30/24 brd 192.168.40.255 scope global eth0
    inet 192.168.40.32/32 scope global eth0
    inet6 fe80::5054:ff:fe57:3b80/64 scope link
       valid_lft forever preferred_lft forever

移动01这台nginx的配置文件然后downnginx服务,使其nginx服务不能启动
[root@nginx01 ~]#/etc/init.d/nginx status
nginx is stopped

然后ip a发现VIP地址已经飘到nginx02上去了
[root@nginx02 ~]# ip a
1: lo:  mtu 16436 qdisc noqueue state UNKNOWN
    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:  mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:95:45:5f brd ff:ff:ff:ff:ff:ff
    inet 192.168.40.31/24 brd 192.168.40.255 scope global eth0
    inet 192.168.40.32/32 scope global eth0
    inet6 fe80::5054:ff:fe95:455f/64 scope link
       valid_lft forever preferred_lft forever





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-657683-1-1.html 上篇帖子: 结合keepalived实现lvs的高可用群集故障自动转移 下篇帖子: Nginx+keepalived 实现高可用的负载均衡
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表