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

方法二

[[email protected] keepalived-1.2.7]# cat >>/etc/rc.local<<EOF

> /usr/local/nginx/sbin/nginx

> /etc/init.d/keepalived start

> EOF

[[email protected] 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 {

[email protected]

[email protected]

[email protected]

}

notification_email_from [email protected]

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 {

[email protected]

[email protected]

[email protected]

}

notification_email_from [email protected]

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

[[email protected] ~]# 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):

[[email protected]_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)

[[email protected] ~]# 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

时间: 2025-01-02 15:01:36

Nginx+Keepalived主备负载均衡学习笔记的相关文章

nginx+keepalived构建主备负载均衡代理服务器

一.架构 二.唠叨一会原理: 1.nginx Nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能.在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发, 从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都 由主进程发出,Slave(worker)进程所有的超时任务都会被Master中止,属于非阻塞式任务模型. 2.keepalived Keepalived是Linux下面实现V

keepalived+nginx双机热备+负载均衡

keepalived+nginx双机热备+负载均衡 最近因业务扩展,需要将当前的apache 转为nginx(web), 再在web前端放置nginx(负载均衡).同时结合keepalived 对前端nginx实现HA. nginx进程基于于Master+Slave(worker)多进程模型,自身具有非常稳定的子进程管理功能.在Master进程分配模式下,Master进程永远不进行业务处理,只是进行任务分发,从而达到Master进程的存活高可靠性,Slave(worker)进程所有的业务信号都

配置nginx+keepalived高可用负载均衡的时候。主从服务器都出现了VIP 阿里云问题

配置nginx+keepalived高可用负载均衡的时候.主从服务器都出现了VIP 出现这问题的场景是在阿里VPS云服务器网络环境中,因为路由交换层禁用了ARP的广播限制,造成KEEPALIVE主备协议无法通过广播的方式进行通信,造成主备两台服务器都强占HAVIP地址,出现同时两台服务器都有VIP地址的情况出现,必须通过配置来指定IP的两台服务器间进行通讯(阿里说明文档中解释只能支持两台使用同一个HAVIP地址),基于以下方法可以的情况下,多备方式用同样的方式也应该可行 ,有需要的兄弟可以测试下

Nginx+Keepalived主备切换(包含nginx服务停止)

原文地址:http://blog.sina.com.cn/s/blog_79ac6aa80101bmed.html Nginx+Keepalived主备切换(包含nginx服务停止) 环境: VM中4台CentOS 本机PC充当测试机win8 步骤: 1.搭建 nginx+keepalived主备切换(不考虑主nginx停掉现象)文章中的环境 2.修改两台部署的keepalived的配置文件 3.编写观察nginx进程情况的脚本内容 #!/bin/bash A=`ps -C nginx --no

Nginx+Keepalived 主备高可用 安装与配置

环境说明:操作系统:CentOS6.7 x86_64Nginx版本:nginx-1.9.7Keepalived版本:keepalived-1.2.24 主nginx + Keepalived :10.219.24.26备nginx + Keepalived :10.219.24.23虚拟IP:10.219.24.100后端tomcat_1:10.219.24.21:8080后端tomcat_2:10.219.24.21:8081 架构与原理: 前端双 Nginx + Keepalived ,Ng

nginx+keepalived+tomcat+memcache负载均衡搭建小集群

最近一段时间一直在研究高可用高并发负载均衡分布式集群等技术,先前发布了lvs基于网络第四次协议搭建的小集群,现在用空刚好搭建了一个基于nginx搭建的小集群. 我准备了四台机器,情况如下 机器名称 机器IP 安装软件 角色 虚拟ip 描述 vmm01 192.168.252.11 Nginx.keepalived Nginx主机 192.168.252.200 反向代理到tomcat1和tomcat2 vmm04 192.168.252.14 Nginx.keepalived Nginx备机 主

Nginx+Keepalived高可用负载均衡

转自 https://www.jianshu.com/p/da26df4f7d60 Keepalived+Nginx实现高可用Web负载均衡 Master 192.168.0.69 nginx.keepalived Centos7.4 backup 192.168.0.70 nginx.keepalived Centos7.4 vip(虚拟IP) 192.168.0.180 192.168.0.181 1.两台都安装nginx,参照安装nginx文档 2. 两台都安装Keepalived yum

nginx + keepalived 高可用负载均衡

虚拟IP:    192.168.2.52 nginx-1:   192.168.2.50 nginx-2:   192.168.2.51 关闭/etc/sysconfig/selinux SELINUX=disabled nginx-1 yum install keepalived vim /etc/keepalived/keepalived.conf--------------------------------------------------- ! Configuration File

nginx+keepalived 主备搭建

nginx1 ip:192.168.12.4 #MASTERnginx2 ip:192.168.12.10 #BACKUPnginx_vip :192.168.12.100 原理可参考:http://www.keepalived.org/documentation.html系统为CentOS7 1.配置一下yum源 curl -L http://mirrors.aliyun.com/repo/Centos-7.repo > /etc/yum.repos.d/CentOS-Base.repocur