chkconfig NetworkManager off
chkconfig network on
service NetworkManager stop
service network restart
–应用机器centos:172.16.88.127,172.16.88.137
主(A):172.16.88.127 root/*
备(B):172.16.88.137 root/*
–主备机器网络设置
vi /etc/sysctl.conf
net.ipv4.ip_forward = 0 #改成 net.ipv4.ip_forward = 1
net.ipv4.conf.lo.arp_ignore = 1 #添加
net.ipv4.conf.all.arp_ignore = 1 #添加
net.ipv4.conf.lo.arp_announce = 2 #添加
net.ipv4.conf.all.arp_announce =2 #添加
–在主备服务器安装keepalived
#wget http://www.keepalived.org/software/keepalived-1.2.4.tar.gz
#tar –zxvf keepalived-1.2.4.tar.gz
#./configure –prefix=/usr/local/keepalived
#make && make install
–将keepalived做成开启启动服务
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/keepalived
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
chkconfig keepalived on
–修改主备配置/etc/keepalived/keepalived.conf 上面的keepalived的配置文件
–MASTER
! Configuration File for keepalived
global_defs {
notification_email {
}
router_id LVS_DEVEL
}
vrrp_script Monitor_Nginx {
script "/root/monitor/monitor_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth1
mcast_src_ip 172.16.88.127
virtual_router_id 51
priority 101
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
Monitor_Nginx
}
virtual_ipaddress {
172.16.88.221
}
}
–BACKUP
! Configuration File for keepalived
global_defs {
notification_email {
}
router_id LVS_DEVEL
}
vrrp_script Monitor_Nginx {
script "/root/monitor/monitor_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
mcast_src_ip 172.16.88.137
virtual_router_id 51
priority 99
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
Monitor_Nginx
}
virtual_ipaddress {
172.16.88.221
}
}
–nginx挂掉重启的进程监控
vi /root/monitor/monitor_nginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/usr/local/bin/nginx
sleep 2
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
/etc/init.d/keepalived stop
fi
fi
–安装nginx
yum -y install pcre*
yum -y install zlib*
–下载nginx源码包
cd nginx-1.10.2
./configure --prefix=/usr/local/nginx
make && make install
—主机127配置nginx.conf
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
upstream 172.16.88.127 {
server 172.16.88.127:8808 weight=10 max_fails=2 fail_timeout=30s;
server 172.16.88.137:8808 weight=10 max_fails=2 fail_timeout=30s;
#ip_hash;
}
sendfile on;
keepalive_timeout 65;
server {
listen 84;
server_name 172.16.88.127;
large_client_header_buffers 4 16k;
client_max_body_size 300m;
client_body_buffer_size 128k;
proxy_connect_timeout 10;
proxy_read_timeout 300;
proxy_send_timeout 300;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
location / {
root html;
index index.html index.htm;
proxy_pass http://172.16.88.127;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
—备机137配置nginx.conf
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
upstream 172.16.88.137 {
server 172.16.88.127:8808 weight=10 max_fails=2 fail_timeout=30s;
server 172.16.88.137:8808 weight=10 max_fails=2 fail_timeout=30s;
#ip_hash;
}
sendfile on;
keepalive_timeout 65;
server {
listen 84;
server_name 172.16.88.137;
large_client_header_buffers 4 16k;
client_max_body_size 300m;
client_body_buffer_size 128k;
proxy_connect_timeout 10;
proxy_read_timeout 300;
proxy_send_timeout 300;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
location / {
root html;
index index.html index.htm;
proxy_pass http://172.16.88.137;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
–安装tomcat 到/data/apache-tomcat-7.0.73
server 172.16.88.127:8808
server 172.16.88.137:8808
–172.16.88.127:8808
vi /data/apache-tomcat-7.0.73/webapps/ROOT/a.html
<HTML>
<title>MASTERtitle>
<HEAD>hello 8808HEAD>
<BODY>172.16.88.127BODY>
HTML>
–172.16.88.137:8808
vi /data/apache-tomcat-7.0.73/webapps/ROOT/a.html
<HTML>
<title>BACKUPtitle>
<HEAD>hello 8808HEAD>
<BODY>172.16.88.127BODY>
HTML>
====最后===
service keepalived restart
访问:http:\172.16.88.221:84/a.html