[软件安装] yum install -y keepalived rpm -qa keepalived LB01配置文件 [[email protected]_01 ~]# cat /etc/keepalived/keepalived.conf !Configuration file for keepalived global_defs { notification_email { [email protected] } notification_email_from Lufeng. [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id lb01 } vrrp_script chk_nginx_proxy { script "/home/lufeng/tools/chk_nginx_proxy.sh" interval 2 weight 2 } vrrp_instance VI_BBS { state MASTER interface eth0 virtual_router_id 55 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.1.1.45/24 dev eth0 label eth0:1 } track_script { chk_nginx_proxy } } vrrp_instance VI_WWW { state BACKUP interface eth0 virtual_router_id 56 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.1.1.46/24 dev eth0 label eth0:2 } } LB02配置文件 [[email protected]_02 tools]# cat /etc/keepalived/keepalived.conf !Configuration file for keepalived global_defs { notification_email { [email protected] } notification_email_from Lufeng. [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id lb02 } vrrp_instance VI_BBS { state BACKUP interface eth0 virtual_router_id 55 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.1.1.45/24 dev eth0 label eth0:1 } } vrrp_script chk_nginx_proxy { script "/home/lufeng/tools/chk_nginx_proxy.sh" interval 2 weight 2 } vrrp_instance VI_WWW { state MASTER interface eth0 virtual_router_id 56 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.1.1.46/24 dev eth0 label eth0:2 } track_script { chk_nginx_proxy } } 检查nginx脚本: [[email protected]_02 tools]# cat /home/lufeng/tools/chk_nginx_proxy.sh #!/bin/sh if [ `netstat -lntup|grep nginx|wc -l` -ne 1 ];then /etc/init.d/keepalived stop fi 记得加上执行权限: [[email protected]_02 tools]# chmod +x /home/lufeng/tools/chk_nginx_proxy.sh 补充VIP添加及查看方式: 设置网卡的ip别名 #ifconfig eth0:0 192.168.6.100 netmask 255.255.255.0 up #ifconfig eth0:1 173.173.173.173 netmask 255.255.255.0 up #ifconfig eth0:2 119.110.120.1 netmask 255.255.255.0 up 先进命令: #ip addr add 192.168.0.118/24 dev eth0 查看 #ip add|grep 192.168.0 清除ip别名 #ifconfg eth0:0 down #ifconfg eth0:1 down #ifconfg eth0:2 down ...... 将别名保存到配置文件: 方法1: 1.将增加ip别名的命令填写到/etc/rc.local文件中 即将所有如这样ifconfig eth0:0 192.168.6.100 netmask 255.255.255.0 up的命令一行行全部填写到rc.local文件中去,rc.local文件是作用于linux开机自动执行命令,详细用法g或b一下,这里不详细说明。 这个方法最方便,但不太可靠,有时会失灵。 方法2: #vi ifcfg-eth0:0 创建eth0:0的配置文件,并将刚才复制的字段粘贴到这个配置文件,按照下文的介绍修改部分信息,修改完成后保存退出 以下是网卡配置文件的部分信息说明,其他类似信息默认即可: ############################################################################## # 3Com Corporation 3c905B 100BaseTX [Cyclone]??? //硬件型号,忽略不计 DEVICE=eth0:0???????????????? //虚拟网络接口,随意???????????????????????????????????? ONBOOT=yes??????????????????? //系统启动时激活 BOOTPROTO=static?? ??? ????? //使用静态ip地址???????????????? IPADDR=192.168.6.100????????? //该虚拟网络接口的ip别名,随意 NETMASK=255.255.255.0???????? //子网掩码,对应ip别名 GATEWAY=192.168.6.1?????????? //网关,对应ip别名 HWADDR=00:10:5A:5E:B1:E4????? //网卡MAC地址,无需更改??????????????????? USERCTL=no??????????????????? //是否给予非root用户设备管理权限 ############################################################################## 设置必要开机自启动: [[email protected]_02 tools]# echo "/application/nginx/sbin/nginx">>/etc/rc.local [[email protected]_02 tools]# echo "/etc/init.d/keepalived start">>/etc/rc.local [[email protected]_02 tools]# cat /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don‘t # want to do the full Sys V style init stuff. touch /var/lock/subsys/local /application/nginx/sbin/nginx /etc/init.d/keepalived start
时间: 2024-10-27 00:40:39