Nginx负载均衡器+keepalived

Nginx负载均衡组件模块

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

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

 

upstream backend {  #名字可以更改
    server backend1.example.com       weight=5;   #每行都是web服务器,ip地址,端口号,weight权重, backup热备
    server backend2.example.com:8080;
    server unix:/tmp/backend3;

    server backup1.example.com:8080   backup;
    server backup2.example.com:8080   backup;
}

server {                    # 指定域名虚拟主机下面 ,配置proxy_pass,就指定到backend
    location / {
        proxy_pass http://backend;
    }
}

 

安装反向代理(就是安装nginx)

两台服务器一起装  130,131IP

安装依赖包

yum install openssl openssl-devel pcre pcre-devel -y

安装nginx

useradd www -s /sbin/nologin  -M

  mkdir -p /home/daxian/tools/

cd /home/daxian/tools/
wget http://nginx.org/download/nginx-1.6.3.tar.gz
tar xf nginx-1.6.3.tar.gz 
cd nginx-1.6.3
./configure --user=www --group=www --with-http_ssl_module  --with-http_stub_status_module  --prefix=/application/nginx-1.6.3/

make  && make install
ln -s /application/nginx-1.6.3/ /application/nginx 

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

编辑nginx.conf

vim nginx.confupstream www {

server 192.168.70.126:80 weight=1;
server 192.168.70.127:80 weight=1;

}
server {
listen 80;
server_name www.daxian.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://www;

proxy_set_header Host $host;   
proxy_set_header X-Forwarded-For $remote_addr;#如果查看nginx日志,显示的都是131代理服务器的IP地址,应该显示用户访问的地址


}

启动

../sbin/nginx

 测试

[root@lo-02 ~]# curl www.daxian.com
apache www
[root@lo-02 ~]# curl www.daxian.com
nginx wwww
[root@lo-02 ~]# curl www.daxian.com
apache www
[root@lo-02 ~]# curl www.daxian.com
nginx wwww
[root@lo-02 ~]# curl www.daxian.com
apache www

 

将配置复制到 lb02上,配置双台nginx负载均衡

同样的配置就略过了,关闭lb01 就可以实现双台负载

 
安装keepalived,两台服务器都要安装

yum install keepalived -y

 

默认配置文件

vim /etc/keepalived/keepalived.conf 

130的配置

global_defs {
notification_email {
26472@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
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 #ID priority 150 #优先级 advert_int 1 #心跳间隔 authentication { auth_type PASS #通信密码 auth_pass 1111 }

virtual_ipaddress {#ip地址
10.0.0.3/24 dev eth0 label eth0:1
}

 

131的配置

global_defs {
notification_email {
26472@qq.com
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL1
}

vrrp_instance VI_1 {
    state BACKUP       #主
    interface eth0     #网卡
    virtual_router_id 51   #ID  一定要一样
    priority 100       #优先级
    advert_int 1       #心跳间隔
    authentication { 
        auth_type PASS   #通信密码
        auth_pass 1111
    }

virtual_ipaddress {#ip地址
10.0.0.3/24 dev eth0 label eth0:1
}

启动

/etc/init.d/keepalived start

 

检查IP

130的

ip addr|grep 10.0.0.3
inet 10.0.0.3/24 scope global eth0:1

131的

 ip addr|grep 10.0.0.3

(如果131也有显示IP,就说明裂脑了)

 

配置成功了。可以关闭一台服务器进行测试

 

posted @ 2018-03-12 10:09  大仙儿没溜儿  阅读(365)  评论(0编辑  收藏  举报