1. 程式人生 > >linux新增虛擬ip(手動vip和keepalived方式)

linux新增虛擬ip(手動vip和keepalived方式)

1,手動新增vip

1,ifconfig檢視當前活動網絡卡。如:eth0

2,執行

ifconfig eth0:0 166.111.69.100 netmask 255.255.255.0 up

進行vip新增

3,執行ifconfig檢視是否生效

4,測試 ping 166.111.69.100

5,寫在/etc/rc.local裡進行開機自動設定

2,第三方工具keepalived增加vip

1,下載與安裝

下載:http://download.csdn.net/detail/u014749692/8396867

安裝:

tar xf keepalived-1.2.12.tar.gz            cd keepalived-1.2.12 ./configure --prefix=/usr/local/keepalived make &&  make install cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

2,配置:

vim

/etc/keepalived/keepalived.conf 如下:

! Configuration File for keepalived

global_defs {      notification_email {      [email protected]    }    notification_email_from [email protected]    smtp_server 127.0.0.1    smtp_connect_timeout 30    router_id MySQL-HA }

vrrp_instance VI_1 {     state BACKUP     interface eth1     virtual_router_id 51     priority 150     advert_int 1     nopreempt

    authentication {     auth_type PASS     auth_pass 1111     }

    virtual_ipaddress {         192.168.0.88     } }

紅色區域為需要修改的地方:

notification_email :變更後通知的email

notification_email_from :傳送email

router_id MySQL-HA :路由器組id,區域網中需要共享該vip的伺服器,該配置要一致。

state BACKUP:在keepalived中2種模式,分別是master->backup模式和backup->backup模式。這兩種模式有很大區別。在master->backup模式下,一旦主庫宕機,虛擬ip會自動漂移到從庫,當主庫修復後,keepalived啟動後,還會把虛擬ip搶佔過來,即使設定了非搶佔模式(nopreempt)搶佔ip的動作也會發生。在backup->backup模式下,當主庫宕機後虛擬ip會自動漂移到從庫上,當原主庫恢復和keepalived服務啟動後,並不會搶佔新主的虛擬ip,即使是優先順序高於從庫的優先級別,也不會發生搶佔。

interface eth1:具體網絡卡,通過ifconfig確認具體網絡卡。

priority 150:表示設定的優先順序為150

nopreempt:非搶佔模式

啟動:

/etc/init.d/keepalived start ; tail -f /var/log/messages

顯示如下表示成功:

Starting keepalived:                                       [  OK  ] Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Registering gratuitous ARP shared channel Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Opening file '/etc/keepalived/keepalived.conf'. Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Configuration is using : 62976 Bytes Apr 20 20:26:18 192 Keepalived_vrrp[9472]: Using LinkWatch kernel netlink reflector... Apr 20 20:26:18 192 Keepalived_vrrp[9472]: VRRP_Instance(VI_1) Entering BACKUP STATE Apr 20 20:26:18 192 Keepalived_vrrp[9472]: VRRP sockpool: [ifindex(3), proto(112), unicast(0), fd(10,11)] Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP 192.168.80.138 added Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP 192.168.0.60 added Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP fe80::20c:29ff:fe9d:6a9e added Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Netlink reflector reports IP fe80::20c:29ff:fe9d:6aa8 added Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Registering Kernel netlink reflector Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Registering Kernel netlink command channel Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Opening file '/etc/keepalived/keepalived.conf'. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Configuration is using : 7231 Bytes Apr 20 20:26:18 192 kernel: IPVS: Registered protocols (TCP, UDP, AH, ESP) Apr 20 20:26:18 192 kernel: IPVS: Connection hash table configured (size=4096, memory=64Kbytes) Apr 20 20:26:18 192 kernel: IPVS: ipvs loaded. Apr 20 20:26:18 192 Keepalived_healthcheckers[9471]: Using LinkWatch kernel netlink reflector...

測試:

1,ping命令測試。

2,一臺伺服器停止keepalived服務進行ip漂移測試。

如果失敗,請考慮防火牆原因,參考url: http://blog.chinaunix.net/uid-20794884-id-5704461.html