主机信息:
Nginx代理:192.168.1.201
Nginx代理:192.168.1.204
Nginx_web_1:192.168.1.202
Nginx_web_2:192.168.1.203
vip1:192.168.1.205
vip1:192.168.1.206
1.安装
___________________________________________________________
cd /usr/local/src/ wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz tar xf keepalived-1.2.20.tar.gz cd keepalived-1.2.20 ./configure --prefix=/usr/local/keepalived make && make install cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived chmod +x /etc/init.d/keepalived
#启动脚本
-----------------------------------------------配置
sed -i "15d;14a . /usr/local/keepalived/etc/sysconfig/keepalived" /etc/init.d/keepalived 同时在上述行下添加以下内容(将keepavlied主程序所在路径导入到环境变量PATH中): sed -i '15a PATH="$PATH:/usr/local/keepalived/sbin"' /etc/init.d/keepalived sed -i '16a export PATH' /etc/init.d/keepalived #修改/etc/init.d/keepalived, 寻找大约15行左右的. /etc/sysconfig/keepalived sed -i '14d;13a KEEPALIVED_OPTIONS="-D -f /etc/keepalived/keepalived.conf"' /usr/local/keepalived/etc/sysconfig/keepalived #修改/usr/local/keepalived/etc/sysconfig/keepalived文件,设置正确的服务启动参数
2.单实例配置
___________________________________________________________
----------------------------------------------Master配置
mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf vim /etc/keepalived/keepalived.conf #主配置文件 ! configuration File for keepalived global_defs { notification_email { szk5043@fomail.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id 1 #不同keepalived.conf此ID需唯一 } vrrp_instance VI_1 { #示例名字为VI-1,相同实例的备节点名字要和这个相同 state MASTER #状态为MASTER,备节点为BACKUP interface eth0 #通信接口为eth0 virtual_router_id 1 #实例ID,主从需配置一致 priority 150 #优先级为150,备节点优先级必须比此数字低 advert_int 1 #通告检查间隔时间1秒 authentication { auth_type PASS #PASS认证类型,此参数备节点设置和主节点相同 auth_pass 1234 #密码是1234,此参数备节点设置和主节点相同 } virtual_ipadderss { 192.168.1.205/24 dev eth0 label eth0:1 } }
-----------------------------------------备配置文件
vim /etc/keepalived/keepalived.conf #备配置文件 ! Configuration File for keepalived global_defs { notification_email { szk5043@foxmail.com } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id 1 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 1 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.205/24 dev eth0 label eth0:1 } }
-----------------------------------M/S切换测试
[root@lab-1-C6 keepalived-1.2.20]# /etc/init.d/keepalived stop
停止 keepalived: [确定]
[root@lab-4-C6 keepalived-1.2.20]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:4a:30:dd brd ff:ff:ff:ff:ff:ff
inet 192.168.1.204/24 brd 192.168.1.255 scope global eth0
inet 192.168.1.205/24 scope global secondary eth0:1
inet6 fe80::20c:29ff:fe4a:30dd/64 scope link
valid_lft forever preferred_lft forever
日志:
Feb 21 11:35:41 lab-4-C6 Keepalived_vrrp[2597]: VRRP_Instance(VI_1) Entering BACKUP STATE
Feb 21 11:35:41 lab-4-C6 Keepalived_vrrp[2597]: VRRP sockpool: [ifindex(2), proto(112), unicast(0), fd(10,11)]
Feb 21 11:35:55 lab-4-C6 Keepalived_vrrp[2597]: VRRP_Instance(VI_1) Transition to MASTER STATE
Feb 21 11:35:56 lab-4-C6 Keepalived_vrrp[2597]: VRRP_Instance(VI_1) Entering MASTER STATE
Feb 21 11:35:56 lab-4-C6 Keepalived_vrrp[2597]: VRRP_Instance(VI_1) setting protocol VIPs.
Feb 21 11:35:56 lab-4-C6 Keepalived_vrrp[2597]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.1.205
Feb 21 11:35:56 lab-4-C6 Keepalived_healthcheckers[2596]: Netlink reflector reports IP 192.168.1.205 added
3.多实例配置
___________________________________________________________
#主配置
--------------------
#前面类似部分略 vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 1 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.205/24 dev eth0 label eth0:1 } vrrp_instance VI_2 { state BACKUP interface eth0 virtual_router_id 1 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.206/24 dev eth0 label eth0:2 }
#备配置
---------------------------------
vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 1 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.205/24 dev eth0 label eth0:1 } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 1 priority 150 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.206/24 dev eth0 label eth0:2 }
转载于:https://blog.51cto.com/szk5043/1766216