Ubuntu下配置虚拟IP地址
我配置虚拟IP地址的目的是为了实现高可用性,那是后话,先看看Ubuntu12.04下如何设置虚拟IP地址。
首先看一下我的Ubuntu的IP:
www.zhishiwu.com
[plain]
chenshu@sloop2:~$ ifconfig
eth0 Link encap:Ethernet HWaddr d0:67:e5:ef:5a:6e
inet addr:10.112.18.158 Bcast:10.112.18.255 Mask:255.255.255.0
inet6 addr: fe80::d267:e5ff:feef:5a6e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:341661 errors:0 dropped:0 overruns:0 frame:0
TX packets:242801 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:328287656 (328.2 MB) TX bytes:31858367 (31.8 MB)
Interrupt:17
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:17904 errors:0 dropped:0 overruns:0 frame:0
TX packets:17904 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2160236 (2.1 MB) TX bytes:2160236 (2.1 MB)
ping一下191,发现网络中没有,就用它做虚拟IP吧。
sudo -s之后,执行下面的命令,
www.zhishiwu.com
[plain]
ifconfig eth0:0 10.112.18.191 netmask 255.255.255.0 up
确保虚拟IP地址和本机IP地址在一个网段。
再来看一下是否成功:
[plain]
ifconfig
eth0 Link encap:Ethernet HWaddr d0:67:e5:ef:5a:6e
inet addr:10.112.18.158 Bcast:10.112.18.255 Mask:255.255.255.0
inet6 addr: fe80::d267:e5ff:feef:5a6e/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:347634 errors:0 dropped:0 overruns:0 frame:0
TX packets:246488 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:335797145 (335.7 MB) TX bytes:32178089 (32.1 MB)
Interrupt:17
eth0:0 Link encap:Ethernet HWaddr d0:67:e5:ef:5a:6e
inet addr:10.112.18.191 Bcast:10.112.18.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:17
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:17953 errors:0 dropped:0 overruns:0 frame:0
TX packets:17953 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:2164072 (2.1 MB) TX bytes:2164072 (2.1 MB)
本机测试: www.zhishiwu.com
[plain]
ping 10.112.18.191
PING 10.112.18.191 (10.112.18.191) 56(84) bytes of data.
64 bytes from 10.112.18.191: icmp_req=1 ttl=64 time=0.028 ms
换台机器测试:
[plain]
edward@edward-catamaran:~$ ping 10.112.18.191
PING 10.112.18.191 (10.112.18.191) 56(84) bytes of data.
64 bytes from 10.112.18.191: icmp_req=1 ttl=64 time=0.916 ms
64 bytes from 10.112.18.191: icmp_req=2 ttl=64 time=0.258 ms
删除虚拟IP的方法。
ifconfig eth0:0 down
后面还要做什么?当然是保存配置,保证重启后虚拟IP也还在。
直接在/etc/network/interfaces文件中添加:
[plain]
auto lo
iface lo inet loopback
www.zhishiwu.com
auto eth0:0
iface eth0:0 inet static
name Ethernet alias LAN card
address 10.112.18.191
netmask 255.255.255.0
broadcast 10.112.18.255
network 10.112.18.0
重新启动网络服务。
/etc/init.d/networking restart