Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名
Keepalivedt做高可用,其高可用,都是站在服务器脚本去说的高可用,而不是服务的角度,也就是说,如果服务器DOWN机或者网络出现故障,高可用是可以实现自动切换的。如果运行的服务,比如Nginx挂掉,这些高可用软件是意识不到的,需要自己写脚本去实现服务的切换
实验拓扑
主nginx负载均衡器 192.168.0.102
辅nginx负载均衡器 192.168.0.100
VIP地址 192.168.0.103
实验环境
centos-5.5
实验软件
gcc gcc-c++
pcre-8.10.tar.gz
nginx-1.1.1.tar.gz
keepalived-1.2.1.tar.gz
软件安装
主nginx负载均衡器 192.168.0.102
tar zxvf pcre-8.10.tar.gz
cd pcre-8.10
./configure
make
make install
tar zxvf nginx-1.1.1.tar.gz
cd nginx-1.1.1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module
make
make install
cd /usr/local/nginx/sbin/
./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfu
如果看到这行提示,证明nginx配置没有错误
cd /usr/local/nginx/sbin/
./nginx 启动nginx
tar zxvf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1
./configure --prefix=/usr/local/keeplived
make
make install
cp -p /usr/local/keeplived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp -p /usr/local/keeplived/etc/sysconfig/keepalived /etc/sysconfig/
cp -p /usr/local/keeplived/sbin/keepalived /bin/
chkconfig --add keepalived
chmod 755 /etc/init.d/keepalived
chkconfig keepalived on
service keepalived start
Starting keepalived: [ OK ]
netstat -tuplna | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11343/nginx
验证
输入 http://服务器ip
如果出现这个证明nginx,安装完成
辅nginx负载均衡器 192.168.0.100
tar zxvf pcre-8.10.tar.gz
cd pcre-8.10
./configure
make
make install
tar zxvf nginx-1.1.1.tar.gz
cd nginx-1.1.1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module
make
make install
cd /usr/local/nginx/sbin/
./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfu
如果看到这行提示,证明nginx配置没有错误
cd /usr/local/nginx/sbin/
./nginx 启动nginx
tar zxvf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1
./configure --prefix=/usr/local/keeplived
make
make install
cp -p /usr/local/keeplived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp -p /usr/local/keeplived/etc/sysconfig/keepalived /etc/sysconfig/
cp -p /usr/local/keeplived/sbin/keepalived /bin/
chkconfig --add keepalived
chmod 755 /etc/init.d/keepalived
chkconfig keepalived on
service keepalived start
Starting keepalived: [ OK ]
netstat -tuplna | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11343/nginx
验证
输入 http://服务器ip
如果出现这个证明nginx,安装完成
配置
主keeplive负载均衡器 192.168.0.102
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER 主机为MASTER,备用机为BACKUP
interface eth0 HA监测网络接口
virtual_router_id 51 主、备机的virtual_router_id必须相同
priority 100 主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高
advert_int 1 VRRP Multicast广播周期秒数
authentication {
auth_type PASS VRRP认证方式
auth_pass 1111 密码
}
virtual_ipaddress {
192.168.200.16 VRRP HA虚拟地址
touch /root/nginx_pid.sh
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/usr/local/nginx/sbin/nginx
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall keepalived
fi
fi
chmod +x nginx_pid.sh
service keepalived restart
Stopping keepalived: [ OK ]
Starting keepalived: [ OK ]
备keeplive负载均衡器 192.168.0.100
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP 主机为MASTER,备用机为BACKUP
interface eth0 HA监测网络接口
virtual_router_id 51 主、备机的virtual_router_id必须相同
priority 90 主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高
advert_int 1 VRRP Multicast广播周期秒数
authentication {
auth_type PASS VRRP认证方式
auth_pass 1111 密码
}
virtual_ipaddress {
192.168.200.16 VRRP HA虚拟地址
service keepalived restart
Stopping keepalived: [ OK ]
Starting keepalived: [ OK ]
到此为止nginx+keep搭建完成