之前已经安装完成,接下来我们配置keepalived。
假设我的ip地址如下:
server1:192.168.0.150
server2:192.168.0.157
vip:192.168.0.50
在两台服务器上分别执行如下命令
:
[@server_150 keepalived]# mkdir /etc/keepalived [@server_150 keepalived]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ [@server_150 keepalived]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ [@server_150 keepalived]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ |
对应操作的含义:
mkdir /etc/keepalived ##为keepalived创建配置文件目录,默认配置文件从/etc/keepalived下读取
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ ##复制keepalived的二进制命令
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ ##提供启动脚本
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ ##keepalived的主配置文件
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ ##复制keepalived的二进制命令
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ ##提供启动脚本
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ ##keepalived的主配置文件
编辑/etc/keepalived/keepalived.conf文件,主机修改内容如下:
! Configuration File for keepalived global_defs { notification_email { test@localhost.com } notification_email_from localhost@localhost.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.50 } } |
从配置如下:
! Configuration File for keepalived global_defs { notification_email { test@localhost.com } notification_email_from localhost@localhost.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 80 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.50 } } |
启动主从的keepalived:
在主从上分布执行:
[@server_150 ~]# keepalived start [@server_150 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 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 qlen 1000 link/ether 78:2b:cb:66:44:2a brd ff:ff:ff:ff:ff:ff inet 192.168.0.150/24 brd 10.10.79.255 scope global eth0 inet 192.168.0.50/32 scope global eth0 inet6 fe80::7a2b:cbff:fe66:442a/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 78:2b:cb:66:44:2c brd ff:ff:ff:ff:ff:ff inet 220.181.26.82/24 brd 220.181.26.255 scope global eth1 inet6 fe80::7a2b:cbff:fe66:442c/64 scope link valid_lft forever preferred_lft forever 4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000 link/ether 78:2b:cb:66:44:2e brd ff:ff:ff:ff:ff:ff 5: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000 link/ether 78:2b:cb:66:44:30 brd ff:ff:ff:ff:ff:ff 6: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0 |
从机:
[@server_157 ~]# keepalived start [@server_157 ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 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 qlen 1000 link/ether 78:2b:cb:65:7f:9f brd ff:ff:ff:ff:ff:ff inet 192.168.0.157/24 brd 10.10.79.255 scope global eth0 inet6 fe80::7a2b:cbff:fe65:7f9f/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 78:2b:cb:65:7f:a1 brd ff:ff:ff:ff:ff:ff inet 220.181.26.92/24 brd 220.181.26.255 scope global eth1 inet6 fe80::7a2b:cbff:fe65:7fa1/64 scope link valid_lft forever preferred_lft forever 4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000 link/ether 78:2b:cb:65:7f:a3 brd ff:ff:ff:ff:ff:ff 5: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000 link/ether 78:2b:cb:65:7f:a5 brd ff:ff:ff:ff:ff:ff 6: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0 |
现在停止从机的keepalived服务,再次查看主从ip addr,与上面的结果一样。
启动从机的keepalived服务,然后停止主机的keepalived服务。
再次查看主从的ip addr
主机:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 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 qlen 1000 link/ether 78:2b:cb:66:44:2a brd ff:ff:ff:ff:ff:ff inet 192.168.0.150/24 brd 10.10.79.255 scope global eth0 inet6 fe80::7a2b:cbff:fe66:442a/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 78:2b:cb:66:44:2c brd ff:ff:ff:ff:ff:ff inet 220.181.26.82/24 brd 220.181.26.255 scope global eth1 inet6 fe80::7a2b:cbff:fe66:442c/64 scope link valid_lft forever preferred_lft forever 4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000 link/ether 78:2b:cb:66:44:2e brd ff:ff:ff:ff:ff:ff 5: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000 link/ether 78:2b:cb:66:44:30 brd ff:ff:ff:ff:ff:ff 6: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0 |
从机:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 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 qlen 1000 link/ether 78:2b:cb:65:7f:9f brd ff:ff:ff:ff:ff:ff inet 192.168.0.157/24 brd 10.10.79.255 scope global eth0 inet 192.168.0.50/32 scope global eth0 inet6 fe80::7a2b:cbff:fe65:7f9f/64 scope link valid_lft forever preferred_lft forever 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 78:2b:cb:65:7f:a1 brd ff:ff:ff:ff:ff:ff inet 220.181.26.92/24 brd 220.181.26.255 scope global eth1 inet6 fe80::7a2b:cbff:fe65:7fa1/64 scope link valid_lft forever preferred_lft forever 4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000 link/ether 78:2b:cb:65:7f:a3 brd ff:ff:ff:ff:ff:ff 5: eth3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000 link/ether 78:2b:cb:65:7f:a5 brd ff:ff:ff:ff:ff:ff 6: sit0: <NOARP> mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0 |
发现虚ip已经从主机绑定到从机,配置测试完成。