实验环境
centos6.8_x64
nginx+keeplive+haproxy_master 192.168.1.108
nginx+keeplive 192.168.1.103
keeplie_vip 192.168.1.200
实验软件
nginx-1.1.1.tar.gz
keepalived-1.2.1.tar.gz
haproxy-1.8.10.tar.gz
软件安装
ntpdate ntp.aliyun.com && clock -w
cp -pv /etc/sysctl.conf /etc/sysctl.conf.bak
cat >> /etc/sysctl.conf << EOF
> net.ipv4.tcp_syncookies = 1
> net.ipv4.tcp_tw_reuse = 1
> net.ipv4.tcp_tw_recycle = 1
> net.ipv4.tcp_fin_timeout = 10
> net.ipv4.ip_forward= 1
> EOF && sysctl -p
yum install -y gcc gcc-c++* make* cmake* zlib* openssl* popt* nmap* ntp* lrzsz* pcre
yum install -y autoconf* automake* libxml* ncurses* libmcrypt* ipvsadm* libnl-devel* kernel-devel
tar zxvf nginx-1.1.1.tar.gz
cd nginx-1.1.1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module
make -j4 && make install
ln -s /usr/local/sbin/nginx /bin
nginx -t && nginx -s reload
nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
nginx -c /usr/local/nginx/conf/nginx.conf
nginx -s stop && nginx 停止服务重启
echo > /usr/local/nginx/html/index.html && echo web1 > /usr/local/nginx/html/index.html master端192.168.1.108
echo > /usr/local/nginx/html/index.html && echo web2 > /usr/local/nginx/html/index.html slave端192.168.1.103操作
tar zxvf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1
./configure --prefix=/usr/local/keeplived
--with-kernel-dir=/usr/src/kernels/2.6.32-696.1.1.el6.x86_64
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
Use VRRP Framework : Yes 3个必须是yes
make -j4 && make install
cp -pv /usr/local/keeplived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ && chmod +x /etc/rc.d/init.d/keepalived
cp -pv /usr/local/keeplived/etc/sysconfig/keepalived /etc/sysconfig/
cp -pv /usr/local/keeplived/etc/keepalived/keepalived.conf /etc/keepalived/
cp -pv /usr/local/keeplived/sbin/keepalived /usr/sbin/
chmod +x /usr/sbin/keepalived
/etc/init.d/nginx start && chkconfig --level 35 nginx on
service keepalived start && chkconfig --level 35 keeplived on
cat /etc/keepalived/keepalived.conf master端 192.168.1.108配置
! Configuration File for keepalived
global_defs {
notification_email {
xxxx@126.com 通告邮箱
}
notification_email_from xxx@126.com
smtp_server smtp.126.com
smtp_connect_timeout 30
router_id master
}
vrrp_script chk_http_port {
script "/root/chk_nginx.sh"
interval 2
weight 2
}
track_script {
chk_http_port
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 101
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200 虚拟IP
}
}
cat /etc/keepalived/keepalived.conf slave端 192.168.1.103 配置
! Configuration File for keepalived
global_defs {
notification_email {
xxx@126.com 通告邮箱
}
notification_email_from xxxx@126.com
smtp_server smtp.126.com
smtp_connect_timeout 30
router_id backup
}
vrrp_script chk_http_port {
script "/root/chk_nginx.sh"
interval 2
weight 2
}
track_script {
chk_http_port
}
vrrp_instance VI_1 {
state MASTER/BACKUP master为主主模式 backup为主从模式
interface eth0
virtual_router_id 101 主主 主从 模式route_id相同
priority 100/99 主主 100/100优先级相同 主从100/99从优先级比主小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.200 虚拟IP
}
}
/etc/rc.d/init.d/keepalived restart
touch chk_nginx.sh
chmod +x chk_nginx.sh
vim chk_nginx.sh
#!/bin/sh
# check nginx server status
NGINX=/usr/local/nginx/sbin/nginx
PORT=80
nmap localhost -p $PORT | grep "$PORT/tcp open"
#echo $?
if [ $? -ne 0 ];then
$NGINX -s stop
$NGINX
sleep 3
nmap localhost -p $PORT | grep "$PORT/tcp open"
[ $? -ne 0 ] && /etc/init.d/keepalived stop
fi
sh chk_nginx.sh
80/tcp open http
crontab -e
*/5 * * * * root sh /root/root/chk_nginx.sh
service crond/ntpd restart
cp /etc/sysctl.conf /etc/sysctl.conf.bak
sed -i "s/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g" /etc/sysctl.conf 配置路由转发功能 0关闭 1开启
sysctl -p
ip addr | grep 192.168.1.200 master/slave 相同配置
inet 192.168.1.200/32 scope global eth0 虚拟ip启动成功
tar zxvf haproxy-1.8.10.tar.gz
cd haproxy-1.8.10
uname -a
Linux centos6 2.6.32-642.el6.x86_64
make TARGET=linux26 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
cp -pv /root/haproxy-1.8.10/examples/haproxy.init /etc/init.d/haproxy
useradd -s /sbin/nologin haproxy && chown -R haproxy.haproxy /usr/local/haproxy
cp -pv /usr/local/haproxy/sbin/haproxy /usr/bin/
chmod +x /etc/init.d/haproxy
chkconfig --level 35 haproxy on centos6操作
cp -pv /root/haproxy-1.8.10/contrib/systemd/haproxy.service.in /lib/systemd/system/haproxy.service
systemctl enable haproxy && systemctl restart haproxy centos7操作
touch /usr/local/haproxy/haproxy.cfg 默认没有配置文件
cat /usr/local/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
maxconn 60000 最大用户接入数
chroot /usr/local/haproxy
daemon
user haproxy
group haproxy
pidfile /usr/local/haproxy/haproxy.pid
defaults
log 127.0.0.1 local3
mode http
option dontlognull
option httpclose
option httplog
option forwardfor
option redispatch
timeout connect 5000
timeout client 5000
timeout server 5000
timeout check 2000
maxconn 60000 最大用户接入数
retries 3
listen web_proxy :8880 web页面配置 nginx默认端避免80造成IP冲突 修改为8880
server www1 192.168.1.108:80 weight 5 check inter 2000 rise 2 fall 5
server www2 192.168.1.103:80 weight 5 check inter 2000 rise 2 fall 5
listen mysql
bind 0.0.0.0:7306 7306为代理数据库虚拟端口
mode tcp
server mysql
server mysql1 192.168.1.108:3306
server mysql2 192.168.1.103:3306 192.168.1.202/203后端mysql数据库ip
listen redis
bind 0.0.0.0:6379 clientVIP
mode tcp
server redis1 192.168.1.108:7000 check inter 3000 fall 3 rise 5
server redis2 192.168.1.103:7001 check inter 3000 fall 3 rise 5
listen rabbitmq_cluster mq集群
bind 0.0.0.0:5671
mode tcp
balance roundrobin
server mq1 192.168.1.108:5672 check inter 5000 rise 2 fall 3 weight 1
server mq2 192.168.1.103:5672 check inter 5000 rise 2 fall 3 weight 1
backend tomcat_web
mode http
balance roundrobin
server web1 192.168.1.202:8080 cookie web1 check inter 1500 rise 3 fall 3 weight 1
server web2 192.168.1.203:8080 cookie web2 check inter 1500 rise 3 fall 3 weight 2
listen stats
bind 192.168.1.15:8880
stats enable
stats uri /stats
stats auth admin:admin 验证用户名/验证
stats hide-version 隐藏版本号
stats refresh 2s 页面刷新时间
nohup haproxy -D -f /usr/local/haproxy/haproxy.cfg & 启动服务
pkill haproxy && haproxy -f /usr/local/haproxy/haproxy.cfg & 杀死进程 重启服务
echo "nohup haproxy -D -f /usr/local/haproxy/haproxy.cfg &" >> /etc/rc.d/rc.local
cp -pv /etc/rsyslog.conf /etc/rsyslog.conf.bak haproxy输入日志设置
touch /var/log/haproxy.log
sed -i 's/#$ModLoad imudp/$ModLoad imudp/g' /etc/rsyslog.conf
sed -i 's/#$UDPServerRun 514/$UDPServerRun 514/g' /etc/rsyslog.conf
echo "local3.* /var/log/haproxy.log" >> /etc/rsyslog.conf
cp -pv /etc/sysconfig/rsyslog /etc/sysconfig/rsyslog.bak
echo SYSLOGD_OPTIONS="-r -m 0" > /etc/sysconfig/rsyslog
serive rsyslog restart && chkconfig --level 35 rsyslog on
netstat -tuplna | grep LISTEN
tcp 0 0 192.168.1.102:8880 0.0.0.0:* LISTEN 31388/haproxy udp 0 0 0.0.0.0:37970 0.0.0.0:* 31388/haproxy
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11343/nginx
udp 0 0 0.0.0.0:514 0.0.0.0:* 1970/rsyslogd
http://192.168.1.200 刷新,停止master端nginx 刷新看看是不是切换到了slave主机