nginx+keepalived 高可用负载均衡

来源:互联网 发布:网络语泥垢是什么意思 编辑:程序博客网 时间:2024/06/05 19:19

废话就不多说了,nginx安装与配置,还有负载均衡呢,可以看我写的另一篇文章《nginx负载均衡实战》,还有关于负载均衡呢,大家可以看一下我写的另外两篇文章,一个是《lvs+keepalived负载均衡》,另一个是《haproxy+keepalived负载均衡》,三种负载均衡的区别呢,可以看一下我转载的一篇文章《软件级负载均衡器(LVS/HAProxy/Nginx)的特点简介和对比》,下面直接进入配置步骤:

1.系统环境

系统版本:CentOS release 5.9 (Final) x86 32位    nginx版本:   1.2.8  keepalived版本:    1.2.4主keepalived:192.168.207.130从keepalived:192.168.207.131VIP:192.168.207.140 WEB_1:192.168.207.129 80端口 WEB_2:192.168.207.130 8080端口 WEB_3:192.168.207.131 8080端口

2.自定义nginx配置文件


在192.168.207.130和192.168.207.131上操作

useradd nginxvi /usr/local/nginx/conf/nginx.conf

内容如下:

#运行用户    user nginx nginx;    #启动进程    worker_processes 2;    #全局错误日志及PID文件    error_log logs/error.log notice;    pid logs/nginx.pid;    #工作模式及每个进程连接数上限    events {        use epoll;        worker_connections 1024;     #所以nginx支持的总连接数就等于worker_processes * worker_connections  }    #设定http服务器,利用它的反向代理功能提供负载均衡支持    http {        #设定mime类型        include mime.types;  #这个是说nginx支持哪些多媒体类型,可以到conf/mime.types查看支持哪些多媒体      default_type application/octet-stream;   #默认的数据类型       #设定日志格式          log_format main '$remote_addr - $remote_user [$time_local] '       '"$request" $status $bytes_sent '       '"$http_referer" "$http_user_agent" '       '"$gzip_ratio"';          log_format download '$remote_addr - $remote_user [$time_local] '       '"$request" $status $bytes_sent '       '"$http_referer" "$http_user_agent" '       '"$http_range" "$sent_http_content_range"';        #设定请求缓冲        client_header_buffer_size 1k;        large_client_header_buffers 4 4k;        #开启gzip模块        #gzip on;        #gzip_min_length 1100;        #gzip_buffers 4 8k;        #gzip_types text/plain;        #output_buffers 1 32k;        #postpone_output 1460;        #设定access log        access_log logs/access.log main;        client_header_timeout 3m;        client_body_timeout 3m;        send_timeout 3m;        sendfile on;        tcp_nopush on;        tcp_nodelay on;        keepalive_timeout 65;        #设定负载均衡的服务器列表          upstream mysvr {            #weigth参数表示权值,权值越高被分配到的几率越大           server 192.168.207.129:80 weight=5;            server 192.168.207.130:8080 weight=5;            server 192.168.207.131:8080 weight=5;      }        server { #这个是设置web服务的,监听8080端口          listen        8080;          server_name    192.168.207.131;          #这个根据系统ip变化        index     index.html index.htm;          root        /var/www/html;          #error_page     500 502 503 504    /50x.html;          #location = /50x.html {          #    root     html;          #}          }       #设定虚拟主机        server {            listen 80;            server_name 192.168.207.140;                   #这里是VIP        #charset gb2312;            #设定本虚拟主机的访问日志            access_log logs/three.web.access.log main;            #如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid            #如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好            #location ~ ^/(img|js|css)/{            #   root /data3/Html;            #   expires 24h;          #}               #对 "/" 启用负载均衡            location / {                proxy_pass http://mysvr;  #以这种格式来使用后端的web服务器              proxy_redirect off;                proxy_set_header Host $host;                proxy_set_header X-Real-IP $remote_addr;                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                client_max_body_size 10m;                client_body_buffer_size 128k;                proxy_connect_timeout 90;                proxy_send_timeout 90;                proxy_read_timeout 90;                proxy_buffer_size 4k;                proxy_buffers 4 32k;                proxy_busy_buffers_size 64k;                proxy_temp_file_write_size 64k;          }            #设定查看Nginx状态的地址 ,在安装时要加上--with-http_stub_status_module参数          location /NginxStatus {                stub_status on;                access_log on;                auth_basic "NginxStatus";                auth_basic_user_file conf/htpasswd;     #设置访问密码,htpasswd -bc filename username password          }      }  }   

3.自定义keepalived配置文件

vi /etc/keepalived/keepalived.conf

内容如下:

global_defs {   notification_email {        root@localhost.localdomain   }   notification_email_from notify@keepalived.com   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_DEVEL}vrrp_script chk_http_port {                script "/etc/keepalived/check_nginx.sh"         ###监控脚本                interval 2                             ###监控时间                weight 2                                ###目前搞不清楚}vrrp_instance VI_1 {        state MASTER                            ### 设置为 主        interface eth0                             ### 监控网卡        virtual_router_id 51                    ### 这个两台服务器必须一样        priority 101                                 ### 权重值 MASTRE 一定要高于 BAUCKUP        authentication {                     auth_type PASS                     auth_pass 1111        }        track_script {                chk_http_port                     ### 执行监控的服务        }        virtual_ipaddress {             192.168.207.140                            ###    VIP 地址        }}

4.写自定义脚本

vi /etc/keepalived/check_nginx.sh

内容如下:
!/bin/bashA=`ps -C nginx --no-header |wc -l`                ## 查看是否有 nginx进程 把值赋给变量Aif [ $A -eq 0 ];then                                         ## 如果没有进程值得为 零        /usr/local/nginx/sbin/nginx        sleep 3        if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then                  /etc/init.d/keepalived stop                       ## 则结束 keepalived 进程        fifi

这里是检查nginx是否启动好,如果没有启动,先启动 nginx,隔了3秒后还没有启动好,则将keepalived进程也关闭,这样从keepalived就能接手过去了,提供高可用性,在这里呢,keepalived服务是提供高可用性,而nginx是提供后端web服务器的负载均衡。

这里还要给脚本加上执行权限,如下

chmod +x /etc/keepalived/check_nginx.sh

5.启动服务,并测试


在这里先说一下啊,在WEB_1上,我是使用系统自带的apache昨晚web服务器的,比较省事,这样呢,我只要启动好主从keepalived就ok了,因为它会利用check_nginx.sh脚本来自动启动nginx。

都启动好了。

访问http://192.168.207.140就可以轮训访问后端的三台web服务器内容啦

这里我们把主keepalived服务给关掉,来测试高可用性

然后会在从keepalived服务器上的/var/log/messages看到这样的日志

Apr 19 17:42:44 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATEApr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATEApr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.Apr 19 17:42:45 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140Apr 19 17:42:45 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 addedApr 19 17:42:45 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 addedApr 19 17:42:45 localhost avahi-daemon[4204]: Registering new address record for 192.168.207.140 on eth0.

继续访问http://192.168.207.140,依旧可以访问后端的三台web服务器

然后再把原主keepalived打开,可以观察到原从keepalived服务器的日志显示
Apr 19 17:42:50 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.207.140Apr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advertApr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATEApr 19 17:44:06 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.Apr 19 17:44:06 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.207.140 removedApr 19 17:44:06 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.207.140 removedApr 19 17:44:06 localhost avahi-daemon[4204]: Withdrawing address record for 192.168.207.140 on eth0.

说明有恢复了原来的主从结果。

生产环境中,后端的机器也可能会挂掉,但是呢,这就不用你操心啦,nginx会自动把session分配到好的后端web服务器上的啦

ok,到这里全部结束了,实践亲测,祝君成功

原创粉丝点击