Nginx+Keepalived主备负载均衡学习笔记

简介:

实验环境及软件版本:

CentOS版本:    6.6(2.6.32.-504.el6.x86_64)

nginx版本:     nginx-1.6.2

keepalived版本:keepalived-1.2.7


准备工作:

关闭防火墙:service iptables stop

            chkconfig iptables off

关闭selinux: sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' /etc/selinux/config

             init 6 重启系统一定要!



1.准备安装依赖包:

yum -y install pcre pcre-devel openssl openssl-devel zlib zlib-devel gcc gcc-c++ popt-devel

安装keepalived出现configure: error: Popt libraries is required

解决方法:yum -y install popt-devel

2.下载安装包

wget http://nginx.org/download/nginx-1.6.2.tar.gz

wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz

3.安装NginX

tar zxvf nginx-1.6.2.tar.gz

cd nginx-1.6.2

./configure

make && make install

4.安装keepalived

tar zxvf keepalived-1.2.7.tar.gz

cd keepalived-1.2.7

./configure

make 

make install

cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

mkdir /etc/keepalived

cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

cp /usr/local/sbin/keepalived /usr/sbin/

5.加入开机自启动服务

方法一

echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local

echo "/etc/init.d/keepalived start" >> /etc/rc.local

方法二

[root@nginx keepalived-1.2.7]# cat >>/etc/rc.local<<EOF

> /usr/local/nginx/sbin/nginx

> /etc/init.d/keepalived start

> EOF

[root@nginx keepalived-1.2.7]# cat /etc/rc.local

#!/bin/sh

#

# This script will be executed *after* all the other init scripts.

# You can put your own initialization stuff in here if you don't

# want to do the full Sys V style init stuff.


touch /var/lock/subsys/local

/usr/local/nginx/sbin/nginx

/etc/init.d/keepalived start

6.配置

  6.1 配置nginx

      两台接入服务器的nginx的配置完全一样,主要配置/usr/local/nginx/conf/nginx.conf的http.其中多域名指向是通过虚拟主机(配置http下面的server)实现;同一域名的不同虚拟目录通过每

  个server下面的不同location实现.

  http {

    include       mime.types;

    default_type  application/octet-stream;


    #log_format  main  '$remote_addr - $remote_user [$time_local] "$re

quest" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';


    #access_log  logs/access.log  main;


    sendfile        on;

    #tcp_nopush     on;


    #keepalive_timeout  0;

     keepalive_timeout  65;


    #gzip  on;


    upstream myserver{

      server 192.168.0.110:80  max_fails=3 fail_timeout=20s;

      server 192.168.0.111:80  max_fails=3 fail_timeout=20s;

      ip_hash;

    }


     server {

        listen       80;

        server_name  192.168.0.150;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {

            root /var/www/html;

            index index.php index.htm index.html;

            proxy_redirect off;

            proxy_set_header Host $host;

            proxy_set_header X-Real-IP $remote_addr;

            proxy_next_upstream http_500 http_502 http_503 error timeo

ut invalid_header;

            proxy_set_header X-Forwarded-For $remote_addr;

            proxy_pass   http://myserver;

        }

}

  6.2 配置keepalived

      按照上面的安装方法,keepalived的配置文件在/etc/keepalived/keepalived.conf.主、从服务器的

 配置相关联但有所不同。如下:

 Master:

  ! 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 smtp.etiantian.org

   smtp_connect_timeout 30

   router_id nginx_master

}


vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 51

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.0.150

        

    }

}


  Backup:

  ! 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 smtp.etiantian.org

   smtp_connect_timeout 30

   router_id nginx_backup

}


vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 51

    priority 99

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.0.150

        

    }

}


7.验证

      先后在主、从服务器上启动/usr/local/nginx/sbin/nginx

                              /etc/init.d/keepalived start

      在主服务器上查看是否已经绑定了虚拟IP(VIP):ip addr

      [root@nginx ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:af:00:4e brd ff:ff:ff:ff:ff:ff

    inet 192.168.0.130/24 brd 192.168.0.255 scope global eth0

    inet 192.168.0.150/32 scope global eth0

    inet6 fe80::20c:29ff:feaf:4e/64 scope link 

       valid_lft forever preferred_lft forever

      停止主服务器上的keepalived:/etc/init.d/keepalived stop然后在从服务器上查看是否已经绑

      定了虚拟IP(VIP):

       [root@nginx_back ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:21:b6:b1 brd ff:ff:ff:ff:ff:ff

    inet 192.168.0.131/24 brd 192.168.0.255 scope global eth0

    inet 192.168.0.150/32 scope global eth0

    inet6 fe80::20c:29ff:fe21:b6b1/64 scope link 

       valid_lft forever preferred_lft forever

      启动主服务器上的keepalived:/etc/init.d/keepalived start,再看看主服务器能否重新接管虚拟IP(VIP)

      [root@nginx ~]# ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000

    link/ether 00:0c:29:af:00:4e brd ff:ff:ff:ff:ff:ff

    inet 192.168.0.130/24 brd 192.168.0.255 scope global eth0

    inet 192.168.0.150/32 scope global eth0

    inet6 fe80::20c:29ff:feaf:4e/64 scope link 

       valid_lft forever preferred_lft forever



本文转自 linuxzkq 51CTO博客,原文链接:http://blog.51cto.com/linuxzkq/1631209

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
17天前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
32 0
|
7天前
|
负载均衡 应用服务中间件 nginx
Nginx 负载均衡
Nginx 负载均衡
21 2
|
2月前
|
tengine Rust 负载均衡
反向代理学习笔记(一) Nginx与反向代理绪论
反向代理学习笔记(一) Nginx与反向代理绪论
|
2月前
|
负载均衡 Java 应用服务中间件
|
2月前
|
负载均衡 监控 应用服务中间件
Nginx负载均衡:你的网站流量翻倍利器
Nginx负载均衡:你的网站流量翻倍利器
43 0
|
2月前
|
消息中间件 关系型数据库 MySQL
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
63 0
|
3月前
|
缓存 负载均衡 算法
【Nginx】Nginx 负载均衡
【1月更文挑战第25天】【Nginx】Nginx 负载均衡
|
3月前
|
负载均衡 应用服务中间件 nginx
【实践】使用Nginx作为GrayLog日志接入的负载均衡
【实践】使用Nginx作为GrayLog日志接入的负载均衡
48 0
|
6月前
|
应用服务中间件 nginx
服务搭建篇(四) 搭建基于Nginx + keepalived的高可用服务
比如我这里虚拟IP+nginx端口是 : 192.168.154.10:80 , 我直接访问 , 出来的是192.168.154.134服务器上的nginx , 因为我刚刚做了区分 , 所以这个时候就可以知道访问的是哪个机器的Nginx , 然后我们把134的Nginx停掉 , 然后再次访问 , 仍然可以使用 , 此时 ,出现的135的页面 , 也就是访问的是192.168.154.135的Nginx
92 0
|
8月前
|
tengine 负载均衡 应用服务中间件
Nginx+Keepalived高可用集群部署详细文档
Nginx+Keepalived高可用集群部署详细文档