集群介绍
keepalived介绍
keepalived介绍
用keepalived配置高可用集群
用keepalived配置高可用集群
用keepalived配置高可用集群
用keepalived配置高可用集群
用keepalived配置高可用集群
首先准备两台机器都安装上nginx和keepalived
##########master主上操作
[root@wsl-001 nginx-1.14.0]# > /etc/keepalived/keepalived.conf
[root@wsl-001 nginx-1.14.0]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
test@qq.com
}
notification_email_from test@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh"
interval 3
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass test123456
}
virtual_ipaddress {
172.16.79.100
}
track_script {
chk_nginx
}
}
[root@wsl-001 nginx-1.14.0]# vim /usr/local/sbin/check_ng.sh
#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
/etc/init.d/nginx start
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
[root@wsl-001 nginx-1.14.0]# chmod 755 /usr/local/sbin/check_ng.sh
[root@wsl-001 nginx-1.14.0]# systemctl start keepalived
[root@wsl-001 nginx-1.14.0]# ps aux |grep keep
root 5533 0.0 0.0 118608 1384 ? Ss 23:03 0:00 /usr/sbin/keepalived -D
root 5534 0.0 0.1 127468 3280 ? S 23:03 0:00 /usr/sbin/keepalived -D
root 5535 0.0 0.1 127408 2828 ? S 23:03 0:00 /usr/sbin/keepalived -D
root 5622 0.0 0.0 112676 984 pts/1 S+ 23:03 0:00 grep --color=auto keep
[root@wsl-001 nginx-1.14.0]# /etc/init.d/nginx stop
Stopping nginx (via systemctl): [ 确定 ]
[root@wsl-001 nginx-1.14.0]# ps aux|grep nginx
root 5880 0.0 0.0 20508 604 ? Ss 23:04 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 5881 0.0 0.1 22952 3200 ? S 23:04 0:00 nginx: worker process
nobody 5882 0.0 0.1 22952 3200 ? S 23:04 0:00 nginx: worker process
root 5887 0.0 0.0 112676 984 pts/1 R+ 23:04 0:00 grep --color=auto nginx
[root@wsl-001 nginx-1.14.0]# iptables -nvL
Chain INPUT (policy ACCEPT 9091 packets, 11M bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 6553 packets, 1292K bytes)
pkts bytes target prot opt in out source destination
[root@wsl-001 nginx-1.14.0]# getenforce
Disabled
[root@wsl-001 nginx-1.14.0]# systemctl stop firewalld
###########backup从上操作
[root@wsl-001 nginx-1.14.0]# > /etc/keepalived/keepalived.conf
[root@wsl-001 nginx-1.14.0]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
test@qq.com
}
notification_email_from test@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/usr/local/sbin/check_ng.sh"
interval 3
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass test123456
}
virtual_ipaddress {
172.16.79.100
}
track_script {
chk_nginx
}
}
[root@wsl-001 nginx-1.14.0]# vim /usr/local/sbin/check_ng.sh
#!/bin/bash
#时间变量,用于记录日志
d=`date --date today +%Y%m%d_%H:%M:%S`
#计算nginx进程数量
n=`ps -C nginx --no-heading|wc -l`
#如果进程为0,则启动nginx,并且再次检测nginx进程数量,
#如果还为0,说明nginx无法启动,此时需要关闭keepalived
if [ $n -eq "0" ]; then
/etc/init.d/nginx start
n2=`ps -C nginx --no-heading|wc -l`
if [ $n2 -eq "0" ]; then
echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log
systemctl stop keepalived
fi
fi
[root@wsl-001 nginx-1.14.0]# chmod 755 /usr/local/sbin/check_ng.sh
[root@wsl-001 nginx-1.14.0]# systemctl start keepalived
[root@wsl-001 nginx-1.14.0]# systemctl start keepalived
[root@wsl-001 nginx-1.14.0]# ps aux|grep keep
root 14818 0.0 0.0 118608 1384 ? Ss 23:13 0:00 /usr/sbin/keepalived -D
root 14819 0.0 0.1 127468 3272 ? S 23:13 0:00 /usr/sbin/keepalived -D
root 14820 0.1 0.1 127408 2828 ? S 23:13 0:00 /usr/sbin/keepalived -D
root 14877 0.0 0.0 112676 980 pts/0 S+ 23:13 0:00 grep --color=auto keep
[root@wsl-001 nginx-1.14.0]# ps aux|grep nginx
root 14846 0.0 0.0 20508 604 ? Ss 23:13 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 14847 0.0 0.1 22952 3196 ? S 23:13 0:00 nginx: worker process
nobody 14848 0.0 0.1 22952 3196 ? S 23:13 0:00 nginx: worker process
root 14909 0.0 0.0 112676 980 pts/0 S+ 23:13 0:00 grep --color=auto nginx
[root@wsl-001 nginx-1.14.0]# iptables -nvL
Chain INPUT (policy ACCEPT 10344 packets, 13M bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 7191 packets, 637K bytes)
pkts bytes target prot opt in out source destination
[root@wsl-001 nginx-1.14.0]# getenforce
Disabled
[root@wsl-001 nginx-1.14.0]# systemctl stop firewalld
image.png
image.png
image.png
在MASTER主上操作
[root@wsl-001 ~]# systemctl stop keepalived
[root@wsl-001 ~]# /etc/init.d/nginx stop
Stopping nginx (via systemctl): [ 确定 ]
image.png
image.png
image.png
扩展
heartbeat和keepalived比较http://blog.csdn.net/yunhua_lee/article/details/9788433
DRBD工作原理和配置 http://502245466.blog.51cto.com/7559397/1298945
mysql+keepalived http://lizhenliang.blog.51cto.com/7876557/1362313
网友评论