Nginx负载均衡高可用keepalived服务实践

 

Nginx负载均衡组件模块

实现Nginx负载均衡的组件主要有两个:

ngx_http_proxy_module proxy代理模块,用于把请求后抛给服务器节点或upstream服务器池

ngx_http_upstream_module 负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查

 

Nginx反向代理负载均衡安装

安装Nginx软件:

1、安装依赖软件包

yum install -y openssl openssl-devel pcre pcre-devel  #安装

rpm -qa openssl openssl-devel pcre pcre-devel  #查看

2、安装Nginx软件包

useradd ceshi

mkdir /home/ceshi/tools

cd /home/ceshi/tools/

useradd -s /sbin/nologin -M nginx

wget -q http://mirrors.sohu.com/nginx/nginx-1.6.3.tar.gz

tar xf nginx-1.6.3.tar.gz

cd nginx-1.6.3

./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3 --with-http_ssl_module --with-http_stub_status_module

make

make install

ln -s /application/nginx-1.6.3/ /application/nginx

 

cd /application/nginx/conf/

egrep -v "#|^$" nginx.conf.default

egrep -v "#|^$" nginx.conf.default >nginx.conf

vim nginx.conf

添加以下内容:

upstream www_server_pools {

    server 10.0.0.7:80 weight=1;

    server 10.0.0.8:80 weight=1;

}

 

proxy_pass http://www_server_pools;

 

编辑后的配置文件

worker_processes  1;

events {

    worker_connections  1024;

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream www_server_pools {

    server 10.0.0.7:80 weight=1;

    server 10.0.0.8:80 weight=1;

    }

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

            proxy_pass http://www_server_pools;

        }

    }

}        

/application/nginx/sbin/nginx -t

/application/nginx/sbin/nginx -s reload

 

测试一:

反向代理服务器lb01:

[root@lb01 ~]# vim /application/nginx/conf/nginx.conf

server_name  www.etiantian.org;  #在server标签中添加网址做测试

[root@lb01 ~]# /application/nginx/sbin/nginx -s reload

客户端测试lb02:

[root@lb02 ~]# vim /etc/hosts

172.16.1.5   lb01 www.etiantian.org etiantian.org bbs.eti

antian.org blog.etiantian.org

[root@lb02 ~]# curl www.etiantian.org

www

[root@lb02 ~]# for n in `seq 100`;do curl www.etiantian.org;sleep 1;done  #测试负载均衡

apache www

www

apache www

www

[root@lb02 ~]# for n in `seq 100`;do curl www.etiantian.org;sleep 1;done  #当其中一台网页服务器宕机

www

apache www

www

apache www

apache www

 

测试二:

反向代理服务器lb01:

[root@lb01 ~]# vim /application/nginx/conf/nginx.conf

server_name  www.etiantian.org;  修改为 server_name  bbs.etiantian.org;

[root@lb01 ~]# /application/nginx/sbin/nginx -s reload

客户端测试lb02:

[root@lb02 ~]# for n in `seq 100`;do curl bbs.etiantian.org;sleep 1;done   

apache www

www    

注意:还是www,因为lb请求web服务器请求报文没有host,虽然没有host,但是wab会先看请求有没有host,有host时就按host给对应的网页,没有host就会给第一个配置的网页

解决办法:

反向代理服务器lb01:

[root@lb01 ~]# vim /application/nginx/conf/nginx.conf

proxy_set_header Host  $host;

[root@lb01 ~]# /application/nginx/sbin/nginx -s reload

客户端测试lb02:

[root@lb02 ~]# for n in `seq 100`;do curl bbs.etiantian.org;sleep 1;done

apache bbs

bbs

 

启动Nginx报错

[root@lb01 conf]# /application/nginx/sbin/nginx

nginx: [emerg] getpwnam("nginx") failed

原因:没有安装nginx用户导致的无法启动

[root@lb01 conf]# useradd -s /sbin/nologin -M nginx

[root@lb01 conf]# id nginx

uid=501(nginx) gid=501(nginx) groups=501(nginx)

[root@lb01 conf]# /application/nginx/sbin/nginx    

[root@lb01 conf]# lsof -i :80

COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

nginx   3710  root    6u  IPv4  19718      0t0  TCP *:http (LISTEN)

nginx   3711 nginx    6u  IPv4  19718      0t0  TCP *:http (LISTEN)

 

客户端访问出错:

[root@lb02 ~]# curl www.etiantian.org

curl: (7) couldn't connect to host

原因:这样的错误可能是主机不可到达,或者端口不可到达。

ping OK只代表主机可以到达。

端口不可到达可能是由于HTTP 服务器未启动或者监听在其他端口入8080上了。

还有一个可能是防火墙没开放80端口的访问权限。

[root@lb01 ~]# service iptables stop

iptables: Setting chains to policy ACCEPT: filter       [  OK  ]

iptables: Flushing firewall rules:                      [  OK  ]

iptables: Unloading modules:                            [  OK  ]

[root@lb01 ~]# chkconfig iptables off

客户端访问:

[root@lb02 ~]# curl www.etiantian.org

apache www

 

 

实现负载均衡:

负载均衡服务器lb01和lb02:

vim /application/nginx/conf/nginx.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

 

    upstream server_pools {

        server 10.0.0.7:80 weight=1;

        server 10.0.0.8:80 weight=1;

    }

 

    server {

        listen       80;

        server_name  bbs.etiantian.org;

        location / {

            proxy_pass http://server_pools;

            proxy_set_header Host  $host;

            proxy_set_header X-Forwarded-For $remote_addr;

        }

    }

    server {

        listen       80;

        server_name  blog.etiantian.org;

        location / {

            proxy_pass http://server_pools;

            proxy_set_header Host  $host;

            proxy_set_header X-Forwarded-For $remote_addr;

        }

    }

}

 

ip addr add 10.0.0.3/24 dev eth0 label eth0:0

 

主服务器lb01的配置

yum install keepalived -y

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   notification_email {

   [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 150

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

    10.0.0.3/24 dev eth0 label eth0:1

    }

}

 

scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/

ifconfig eth0:0 down

/etc/init.d/keepalived start

ip addr|grep 10.0.0.3

 

从服务器lb01的配置:

yum install keepalived -y

vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

 

global_defs {

   notification_email {

   [email protected]

   }

   notification_email_from [email protected]

   smtp_server 192.168.200.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL1  #DEVEL更改为DEVEL1不能和主的冲突

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 100  #150更改为100,主的要优先

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

    10.0.0.3/24 dev eth0 label eth0:1

    }

}

 

/application/nginx/sbin/nginx

/etc/init.d/keepalived start

ip addr|grep 10.0.0.3

猜你喜欢

转载自blog.csdn.net/qq_41816540/article/details/81366072
今日推荐