=>软件以及环境介绍
操作系统 centos6.5
nginx 1.5.7
keepalived 1.2.9
挂载安装光盘里面的文件到系统中
虚拟机IP 192.168.1.106 192.168.1.108
=>开启网络端口(防止后续端口不能用问题)
A、vi /etc/selinux/config
B、设置 SELINUX=disabled 保存
C、执行 setenforce 0
=>设置IPTABLES
A、vi /etc/sysconfig/iptables
B、-A INPUT -m state --state NEW -m tcp -p tcp --dport 80:9000 -j ACCEPT
C、/etc/init.d/iptables restart
=>安装nginx(以192.168.1.106为例)
(root 用户)
1、 tar xvzf nginx-1.5.7.tar.gz
查找openssl是否安装的路径
whereis openssl
openssl: /usr/bin/openssl /usr/lib/openssl /usr/include/openssl /usr/share/man/man1/openssl.1ssl.gz
cd nginx-1.5.7
./configure --prefix=/opt/nginx --with-openssl=/usr/include/openssl/ --sbin-path=/opt/nginx/nginx
报如下错误:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
make
make install
2、 安装pcre-8.30
rpm -ivh pcre-7.8-6.el6.x86_64.rpm
rpm -ivh pcre-devel-7.8-6.el6.x86_64.rpm
(最新下载地址 ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/放到
tar xvzf pcre-8.30.tar.gz ,光盘中Packages中有)
3、 重新安装Nginx
4、 cd /opt/nginx
5、运行nginx ./nginx
6、
6、 在浏览器中输入http://127.0.0.1
显示welcome to nginx 安装成功
=>安装keepalived
前言
最近因业务扩展,需要将当前的apache 转为nginx(web), 再在web前端放置nginx(负载均衡)。同时结合keepalived 对前端nginx实现HA。
nginx进程基于于Master+Slave(worker)多进程模型,自 身具有非常稳定的子进程管理功能。在Master进程分配模式下,Master进程永远不进行业务处理,
只是进行任务分发,从而达到Master进程的存 活高可靠性,Slave(worker)进程所有的业务信号都 由主进程发出,Slave(worker)进程所有的超时任务都会
被Master中止,属于非阻塞式任务模型。
Keepalived是Linux下面实现VRRP 备份路由的高可靠性运行件。基于Keepalived设计的服务模式能够真正做到主服务器和备份服务器故障时IP瞬间无缝交接。
二者结合,可以构架出比较稳定的软件lb方案。
A、tar xvzf keepalived-1.2.9.tar.gz
B、cd keepalived-1.2.9
C、./configure
Keepalived configuration
------------------------
Keepalived version : 1.2.9
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lssl -lcrypto -lcrypt
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : No
Use VRRP Framework : Yes
Use VRRP VMAC : Yes
SNMP support : No
SHA1 support : No
Use Debug flags : No
D、例如出现下面情况,将不支持lvs!
Keepalived configuration
------------------------
Keepalived version : 1.1.15
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : No
IPVS sync daemon support : No
Use VRRP Framework : Yes
Use LinkWatch : No
Use Debug flags : No
解决办法,指定kernel目录:
./configure --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
./configure --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5-i686/(rhel5.4_32平台)
E、make && make install
=>nginx+keepalived高可用性负载均衡
前言
最近因业务扩展,需要将当前的apache 转为nginx(web), 再在web前端放置nginx(负载均衡)。同时结合keepalived 对前端nginx实现HA。
nginx进程基于于Master+Slave(worker)多进程模型,自 身具有非常稳定的子进程管理功能。在Master进程分配模式下,Master进程永远不进行业务处理,
只是进行任务分发,从而达到Master进程的存 活高可靠性,Slave(worker)进程所有的业务信号都 由主进程发出,Slave(worker)进程所有的超时任务都会
被Master中止,属于非阻塞式任务模型。
Keepalived是Linux下面实现VRRP 备份路由的高可靠性运行件。基于Keepalived设计的服务模式能够真正做到主服务器和备份服务器故障时IP瞬间无缝交接。
二者结合,可以构架出比较稳定的软件lb方案。
nginx master(主nginx服务器) 192.168.1.106
nginx backup(备nginx服务器) 192.168.1.108
vip(被切换的虚拟IP) 192.168.1.107
A、创建目录 mkdir /etc/keepalived
cd /etc/keepalived
编写nginx脚本(当nginx死掉的时候,尝试启动nginx,如果尝试失败,
则杀死keepalived进程,备份服务器上面发现主服务器keepalived进程死掉了之后,就会启动nginx,切换虚拟IP
)
vi nginx_pid.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
/opt/nginx/nginx
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
保存到/etc/keepalived下面
设置脚本权限 chmod +x /etc/keepalived/nginx_pid.sh
B、keeaplived 配置(默认keepalived启动的时候加载的路径/etc/keepalived/keepalived.conf)
vi /etc/keepalived/keepalived.conf
主机(192.168.1.106)的配置文件
global_defs {
notification_email {
user@example.com
}
notification_email_from mail@example.org
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101 #备份机的权重要低于101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.107
}
track_script {
chk_nginx
}
}
备份机(192.168.1.108)配置文件
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100 #备份机的权重要低于主机
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.107
}
track_script {
chk_nginx
}
}
C、 cp /usr/local/sbin/keepalived /usr/sbin/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
D、启动keepalived
keepalived
或者指定配置文件启动 keepalived –f /usr/local/etc/keepalived/keepalived.conf
E、查看启动日志tail -f /var/log/message
[root@localhost keepalived]# tail -f /var/log/messages
Dec 14 11:51:33 localhost Keepalived_healthcheckers[9221]: Registering Kernel netlink command channel
Dec 14 11:51:33 localhost Keepalived_healthcheckers[9221]: Opening file '/etc/keepalived/keepalived.conf'.
Dec 14 11:51:33 localhost Keepalived_vrrp[9222]: Registering gratuitous ARP shared channel
Dec 14 11:51:33 localhost Keepalived_vrrp[9222]: Opening file '/etc/keepalived/keepalived.conf'.
Dec 14 11:51:33 localhost Keepalived_vrrp[9222]: Configuration is using : 64084 Bytes
Dec 14 11:51:33 localhost Keepalived_vrrp[9222]: Using LinkWatch kernel netlink reflector...
Dec 14 11:51:33 localhost Keepalived_healthcheckers[9221]: Configuration is using : 5933 Bytes
Dec 14 11:51:33 localhost Keepalived_healthcheckers[9221]: Using LinkWatch kernel netlink reflector...
Dec 14 11:51:34 localhost Keepalived_vrrp[9222]: VRRP_Instance(VI_1) Transition to MASTER STATE
Dec 14 11:51:35 localhost Keepalived_vrrp[9222]: VRRP_Instance(VI_1) Entering MASTER STATE
F、查看 虚拟IP
ip a
=>测试
A、访问192.168.1.107
查看两台nginx访问日志发现是在192.168.1.106虚拟机上面
tail -f /opt/nginx/logs/access.log
A、杀掉192.168.1.106 上面的nginx
/opt/nginx/nginx -s stop
你会发现192.168.1.106上面的nginx自动beikeepalived重启了
C、杀掉192.168.1.106上面的keepalived
killall keepalived
再访问192.168.1.107
查看两台nginx的日志
发现访问到192.168.1.108 虚拟机上面了
D、启动192.168.1.106上面的keepalived 又会切换回来
=>vritualbox拷贝虚拟机(由于图像界面clone选项导致uuid重复,故采用 VBoxManage )
先要把虚拟机关掉,然后执行克隆
nginx+keepalived 高可用方案
最新推荐文章于 2025-03-13 16:23:57 发布
本文详细介绍如何使用Nginx和Keepalived在CentOS 6.5环境下搭建高可用性的负载均衡系统。文章包括安装配置Nginx、Keepalived的过程,设置网络端口及防火墙规则,以及通过Keepalived实现Nginx的主备切换等关键步骤。
2678

被折叠的 条评论
为什么被折叠?



