nginx+keepalived实现双主负载均衡

简介:

测试环境如下:

系统:Ceentos 6.4 64位

nginx服务器 no1:172.16.4.7 vip:172.16.4.55

nginx服务器 no2:172.16.4.11 vip:172.16.4.56



No1、no2双机互信

#vim /etc/hosts

172.16.4.7 no1.test.com no1

172.16.4.11 no2.test.com no2



No1:

#ssh-keygen -t rsa -P ‘’

#ssh-copy-id -i .ssh/id_rsa.pub root@no2.test.com

No2:

#ssh-keygen -t rsa -P ‘’

#ssh-copy-id -i .ssh/id_rsa.pub root@no1.test.com

还要确保两个节点时间同步

一、Nginx+keepalived 安装

nginx-1.4.2.tar.gz

#yum -y install keepalived

1、安装编译所依赖的包pcre-devel openssl-devel

# yum -y install pcre-devel openssl-devel

2、解压并添加用户nginx,以nginx的身份运行服务

#tar xf nginx-1.4.2.tar.gz -C /usr/local/

#cd /usr/local/nginx-1.4.2

# groupadd -r nginx

# useradd -r -g nginx nginx

3、编译和安装

# ./configure \

--prefix=/usr \

--sbin-path=/usr/sbin/nginx \

--conf-path=/etc/nginx/nginx.conf \

--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--user=nginx \

--group=nginx \

--with-http_ssl_module \

--with-http_flv_module \

--with-http_stub_status_module \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/tmp/nginx/client/ \

--http-proxy-temp-path=/var/tmp/nginx/proxy/ \

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

--http-scgi-temp-path=/var/tmp/nginx/scgi \

--with-pcre

编译过程中如果提示我们还需要依赖某个包时,我们要安装对应的devel包

# make && make install

4、为nginx提供SysV init脚本

脚本存放位置/etc/rc.d/init.d/nginx

#!/bin/sh

#

# nginx - this script starts and stops the nginx daemon

#

# chkconfig: - 85 15

# description: Nginx is an HTTP(S) server, HTTP(S) reverse \

# proxy and IMAP/POP3 proxy server

# processname: nginx

# config: /etc/nginx/nginx.conf

# config: /etc/sysconfig/nginx

# pidfile: /var/run/nginx.pid

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {

# make required directories

user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

options=`$nginx -V 2>&1 | grep 'configure arguments:'`

for opt in $options; do

if [ `echo $opt | grep '.*-temp-path'` ]; then

value=`echo $opt | cut -d "=" -f 2`

if [ ! -d "$value" ]; then

# echo "creating" $value

mkdir -p $value && chown -R $user $value

fi

fi

done

}

start() {

[ -x $nginx ] || exit 5

[ -f $NGINX_CONF_FILE ] || exit 6

make_dirs

echo -n $"Starting $prog: "

daemon $nginx -c $NGINX_CONF_FILE

retval=$?

echo

[ $retval -eq 0 ] && touch $lockfile

return $retval

}

stop() {

echo -n $"Stopping $prog: "

killproc $prog -QUIT

retval=$?

echo

[ $retval -eq 0 ] && rm -f $lockfile

return $retval

}

restart() {

configtest || return $?

stop

sleep 1

start

}

reload() {

configtest || return $?

echo -n $"Reloading $prog: "

killproc $nginx -HUP

RETVAL=$?

echo

}

force_reload() {

restart

}

configtest() {

$nginx -t -c $NGINX_CONF_FILE

}

rh_status() {

status $prog

}

rh_status_q() {

rh_status >/dev/null 2>&1

}

case "$1" in

start)

rh_status_q && exit 0

$1

;;

stop)

rh_status_q || exit 0

$1

;;

restart|configtest)

$1

;;

reload)

rh_status_q || exit 7

$1

;;

force-reload)

force_reload

;;

status)

rh_status

;;

condrestart|try-restart)

rh_status_q || exit 0

;;

*)

echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

exit 2

esac

而后为此脚本赋予执行权限:

# chmod +x /etc/rc.d/init.d/nginx

添加至服务管理列表,并让其开机自动启动:

# chkconfig --add nginx

# chkconfig nginx on

而后就可以启动服务并测试了:

# service nginx start

二、no1 配置

# mkdir -p /web/htdocs

echo "172.16.4.7" > /web/htdocs/index.html

#vim /etc/nginx/nginx.conf

server {

listen 80;

server_name localhost;

location / {

root /web/htdocs;

index index.html index.htm;

}

配置完成后重新加载服务

#service nginx reload

测试使用curl命令请求或在浏览器访问

# curl 172.16.4.7

172.16.4.7

三、no1的Keepalived配置

# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

notification_email {

root@localhost

}

notification_email_from keepadmin@localhost

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script chk_nginx {

script "killall -0 nginx"

interval 2

weight -2

}

vrrp_instance VI_1 {

state MASTER

interface eth0

virtual_router_id 55

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.16.4.55

}

track_script {

chk_nginx

}

}

vrrp_instance VI_2 {

state BACKUP

interface eth0

virtual_router_id 56

priority 99

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.16.4.56

}

track_script {

chk_nginx

}

}

四、no2的nginx配置以及keepalived配置

可以将no1的配置发送到no2上

#cd /etc/keepalived

#scp keepalived.conf no2:/etc/keepalived/

然后在其keepalived配置中修改下面选项:

vrrp_instance VI_1 {

state BACKUP

interface eth0

virtual_router_id 55

priority 99

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.16.4.55

}

track_script {

chk_nginx

}

}

vrrp_instance VI_2 {

state MASTER

interface eth0

virtual_router_id 56

priority100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

172.16.4.56

}

track_script {

chk_nginx

}

}

五、测试

分别在2台nginx上重启启动nginx和keepalived服务,然后停掉其中一台的服务,查看资源能否转移到另一台nginx上,

查看方式:

#ip addr show

#ip a

查看no1:

191211863.png

查看no2:

191241477.png

将no1节点服务停掉,然后查看资源转移情况

191317306.png

191317309.png


再将no1的服务开启查看资源转移

191347244.png

#tail /var/log/messages

#tail -f /var/log/messages #实时查看,不会退出日志

或者在浏览器访问我们定义的vip查看,这里我们就不演示了




本文转自 宋鹏超 51CTO博客,原文链接:http://blog.51cto.com/qidian510/1301781,如需转载请自行联系原作者

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
相关文章
|
15天前
|
负载均衡 算法 应用服务中间件
面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
字节跳动面试题:Nginx有哪些负载均衡算法?Nginx位于七层网络结构中的哪一层?
31 0
|
5天前
|
负载均衡 应用服务中间件 nginx
Nginx 负载均衡
Nginx 负载均衡
20 2
|
2月前
|
负载均衡 Java 应用服务中间件
|
2月前
|
负载均衡 监控 应用服务中间件
Nginx负载均衡:你的网站流量翻倍利器
Nginx负载均衡:你的网站流量翻倍利器
43 0
|
2月前
|
消息中间件 关系型数据库 MySQL
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
使用Nginx的stream模块实现MySQL反向代理与RabbitMQ负载均衡
61 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高可用集群部署详细文档
|
5月前
|
应用服务中间件 Shell nginx
Nginx + keepalived 实现高可用 + 防盗链 + 动静分离(二)
Nginx + keepalived 实现高可用 + 防盗链 + 动静分离