在之前一篇使用nginx搭建高可用的解決方案的時(shí)候,很多同學(xué)會(huì)問,如果nginx掛掉怎么辦淋昭,比如下面這張圖:
你可以清楚的看到,如果192.168.2.100這臺(tái)機(jī)器掛掉了诀姚,那么整個(gè)集群就下線了响牛,這個(gè)問題該怎么解決呢?赫段?呀打? 簡(jiǎn)單的想想確實(shí)不大好處理,因?yàn)槟?/p>
的webBrowser總得要訪問一個(gè)ip地址糯笙,對(duì)吧贬丛。。這個(gè)問題怎么破呢给涕?
一:?jiǎn)栴}分析
如果你有一些網(wǎng)絡(luò)底子的話豺憔,就會(huì)明白,你給一個(gè)不在本網(wǎng)段的機(jī)器發(fā)送請(qǐng)求的話够庙,這個(gè)請(qǐng)求會(huì)先經(jīng)過你的網(wǎng)關(guān)IP恭应,然后通過網(wǎng)關(guān)IP傳給對(duì)方的網(wǎng)關(guān)IP,然
后網(wǎng)關(guān)IP會(huì)將請(qǐng)求轉(zhuǎn)給它所在局域網(wǎng)的主機(jī)耘眨,當(dāng)然我的網(wǎng)關(guān)IP和對(duì)方的網(wǎng)關(guān)IP之間可能有很多跳的路由地址昼榛,大概的流程就是下面這樣:
如果你不信的話,可以用tracert 看看你到www.ctrip.com的路由總過程剔难。
從上圖中可以看到胆屿,從我當(dāng)前主機(jī)到ctrip.com一共有20跳,第一條是192.168.2.1偶宫,這個(gè)就是我的路由器IP非迹,也就是我的網(wǎng)關(guān)IP。
二:虛擬路由冗余協(xié)議
好了纯趋,說了這么多有什么用呢憎兽?其實(shí)大家仔細(xì)觀察這張圖,你會(huì)想我能不能在網(wǎng)關(guān)IP上做一些手腳呢吵冒? 可喜的是如今的路由器基本上都支持一個(gè)叫做
VRRP(虛擬路由冗余協(xié)議)唇兑,這一協(xié)議的作用你可以理解成把網(wǎng)關(guān)IP虛擬化成一個(gè)網(wǎng)關(guān)IP集群,就好像獸族劍圣的鏡像技能桦锄,這里面有master扎附,也有slave,
然后局域網(wǎng)內(nèi)的主機(jī)設(shè)置的都是虛擬的masterIP(VIP)结耀,剛好keepealived就是一個(gè)實(shí)現(xiàn)VRRP的一款應(yīng)用程序留夜,你需要,我專業(yè)图甜,大家就這樣走到一塊了碍粥。
三:keepalived搭建一覽
1. 下載:從官網(wǎng)上找到當(dāng)前最新的版本1.4.2。http://www.keepalived.org/software/keepalived-1.4.2.tar.gz黑毅。
配置機(jī)器: 192.168.23.156 【centos】
192.168.23.157 【centos】
[root@localhost app]# wget http://www.keepalived.org/software/keepalived-1.4.2.tar.gz
--2018-03-10 04:04:06-- http://www.keepalived.org/software/keepalived-1.4.2.tar.gz
Resolving www.keepalived.org (www.keepalived.org)... 37.59.63.157, 2001:41d0:8:7a9d::1
Connecting to www.keepalived.org (www.keepalived.org)|37.59.63.157|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 738096 (721K) [application/x-gzip]
Saving to: ‘keepalived-1.4.2.tar.gz’
100%[==================================================================>] 738,096 5.24KB/s in 4m 44s
2. 然后把相關(guān)依賴裝起來:yum install -y openssl openssl-devel嚼摩。
[root@localhost app]# yum install -y openssl openssl-devel
Loaded plugins: fastestmirror, langpacks
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#52 - "Empty reply from server"
base | 3.6 kB 00:00:00
epel/x86_64/metalink | 7.8 kB 00:00:00
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock error was
14: curl#52 - "Empty reply from server"
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
updates/7/x86_64/primary_db | 6.9 MB 00:01:10
3. 接下來繼續(xù)解壓,最后安裝三板斧: ./configure --prefix=/usr/app/keepalived && make && make install。
[root@localhost app]# tar -zxvf keepalived-1.4.2.tar.gz
[root@localhost app]# ls
keepalived-1.4.2? keepalived-1.4.2.tar.gz
[root@localhost app]# cd keepalived-1.4.2
[root@localhost keepalived-1.4.2]# ls
aclocal.m4? bin_install? configure? ? COPYING? genhash? ? keepalived? ? ? ? ? Makefile.am? README.md
ar-lib? ? ? ChangeLog? ? configure.ac? depcomp? INSTALL? ? keepalived.spec.in? Makefile.in? snap
AUTHOR? ? ? compile? ? ? CONTRIBUTORS? doc? ? ? install-sh? lib? ? ? ? ? ? ? ? missing? ? ? TODO
[root@localhost keepalived-1.4.2]#
[root@localhost keepalived-1.4.2]# ./configure --prefix=/usr/app/keepalived && make && make install
安裝好了之后枕面,你就會(huì)看到如下的內(nèi)容愿卒,那就恭喜你,安裝成功了潮秘。
Keepalived configuration
------------------------
Keepalived version? ? ? : 1.4.2
Compiler? ? ? ? ? ? ? ? : gcc
Preprocessor flags? ? ? :
Compiler flags? ? ? ? ? : -Wall -Wunused -Wstrict-prototypes -Wextra -g -O2 -fPIE -D_GNU_SOURCE
Linker flags? ? ? ? ? ? : -pie
Extra Lib? ? ? ? ? ? ? ? :? -lcrypto? -lssl
Use IPVS Framework? ? ? : Yes
IPVS use libnl? ? ? ? ? : No
IPVS syncd attributes? ? : No
IPVS 64 bit stats? ? ? ? : No
fwmark socket support? ? : Yes
Use VRRP Framework? ? ? : Yes
Use VRRP VMAC? ? ? ? ? ? : Yes
Use VRRP authentication? : Yes
With ip rules/routes? ? : Yes
SNMP vrrp support? ? ? ? : No
SNMP checker support? ? : No
SNMP RFCv2 support? ? ? : No
SNMP RFCv3 support? ? ? : No
DBUS support? ? ? ? ? ? : No
SHA1 support? ? ? ? ? ? : No
Use Debug flags? ? ? ? ? : No
smtp-alert debugging? ? : No
Use Json output? ? ? ? ? : No
Stacktrace support? ? ? : No
Memory alloc check? ? ? : No
libnl version? ? ? ? ? ? : None
Use IPv4 devconf? ? ? ? : No
Use libiptc? ? ? ? ? ? ? : No
Use libipset? ? ? ? ? ? : No
init type? ? ? ? ? ? ? ? : systemd
Build genhash? ? ? ? ? ? : Yes
Build documentation? ? ? : No
4. 安裝好了之后琼开,在/usr/app/keepalived/etc/keepalived目錄下有一個(gè)keepalived.conf文件,現(xiàn)在你要做的事情就是將它c(diǎn)opy到/etc/keepalived文件夾下就可以了枕荞。
1.[root@localhost keepalived]# ls
2.keepalived.conf samples
3.[root@localhost keepalived]# pwd
4./usr/app/keepalived/etc/keepalived
5.[root@localhost keepalived]# mkdir -p /etc/keepalived
6.[root@localhost keepalived]# cp ./keepalived.conf /etc/keepalived/keepalived.conf
5. 接下來我們改一下配置文件柜候。
在192.168.23.156機(jī)器中的配置文件,修改如下:
【原來】
global_defs {
? notification_email {
? ? acassen@firewall.loc
? ? failover@firewall.loc
? ? sysadmin@firewall.loc
? }
? notification_email_from Alexandre.Cassen@firewall.loc
? smtp_server 192.168.200.1
? smtp_connect_timeout 30
? router_id LVS_DEVEL
? vrrp_skip_check_adv_addr
? vrrp_strict
? vrrp_garp_interval 0
? vrrp_gna_interval 0
}
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.200.16
? ? ? ? 192.168.200.17
? ? ? ? 192.168.200.18
? ? }
}
【修改】
global_defs {
? notification_email {
? ? acassen@firewall.loc
? ? failover@firewall.loc
? ? sysadmin@firewall.loc
? }
? notification_email_from Alexandre.Cassen@firewall.loc
? smtp_server 192.168.200.1
? smtp_connect_timeout 30
? router_id NodeA
? vrrp_skip_check_adv_addr
? vrrp_strict
? vrrp_garp_interval 0
? vrrp_gna_interval 0
}
vrrp_instance VI_1 {
? ? state MASTER
? ? interface ens33
? ? virtual_router_id 51
? ? priority 150
? ? advert_int 1
? ? authentication {
? ? ? ? auth_type PASS
? ? ? ? auth_pass 1111
? ? }
? ? virtual_ipaddress {
? ? ? ? 192.168.2.200
? ? }
}
其中要注意的就是:
《1》priority 150
節(jié)點(diǎn)的優(yōu)先級(jí)躏精,master要比slave高渣刷。
《2》interface ens33
ens33大家可以通過ipconfig查看一下自己的網(wǎng)卡。
[root@localhost ~]# ifconfigbr-11757db6abf5: flags=4099 mtu 1500
? ? ? ? inet 172.22.0.1? netmask 255.255.0.0? broadcast 0.0.0.0
? ? ? ? ether 02:42:c2:e0:52:10? txqueuelen 0? (Ethernet)
? ? ? ? RX packets 0? bytes 0 (0.0 B)
? ? ? ? RX errors 0? dropped 0? overruns 0? frame 0
? ? ? ? TX packets 0? bytes 0 (0.0 B)
? ? ? ? TX errors 0? dropped 0 overruns 0? carrier 0? collisions 0
br-875e3c64ec79: flags=4099 mtu 1500
? ? ? ? inet 172.23.0.1? netmask 255.255.0.0? broadcast 0.0.0.0
? ? ? ? ether 02:42:4e:43:5b:a0? txqueuelen 0? (Ethernet)
? ? ? ? RX packets 0? bytes 0 (0.0 B)
? ? ? ? RX errors 0? dropped 0? overruns 0? frame 0
? ? ? ? TX packets 0? bytes 0 (0.0 B)
? ? ? ? TX errors 0? dropped 0 overruns 0? carrier 0? collisions 0
br-904f2c62861e: flags=4099 mtu 1500
? ? ? ? inet 172.19.0.1? netmask 255.255.0.0? broadcast 0.0.0.0
? ? ? ? ether 02:42:6d:80:36:58? txqueuelen 0? (Ethernet)
? ? ? ? RX packets 0? bytes 0 (0.0 B)
? ? ? ? RX errors 0? dropped 0? overruns 0? frame 0
? ? ? ? TX packets 0? bytes 0 (0.0 B)
? ? ? ? TX errors 0? dropped 0 overruns 0? carrier 0? collisions 0
br-b0028a425959: flags=4099 mtu 1500
? ? ? ? inet 172.21.0.1? netmask 255.255.0.0? broadcast 0.0.0.0
? ? ? ? ether 02:42:68:51:95:99? txqueuelen 0? (Ethernet)
? ? ? ? RX packets 18? bytes 1458 (1.4 KiB)
? ? ? ? RX errors 0? dropped 0? overruns 0? frame 0
? ? ? ? TX packets 41? bytes 3920 (3.8 KiB)
? ? ? ? TX errors 0? dropped 0 overruns 0? carrier 0? collisions 0
br-c4a09a75fc67: flags=4099 mtu 1500
? ? ? ? inet 172.18.0.1? netmask 255.255.0.0? broadcast 0.0.0.0
? ? ? ? ether 02:42:92:f9:2d:65? txqueuelen 0? (Ethernet)
? ? ? ? RX packets 4? bytes 340 (340.0 B)
? ? ? ? RX errors 0? dropped 0? overruns 0? frame 0
? ? ? ? TX packets 4? bytes 340 (340.0 B)
? ? ? ? TX errors 0? dropped 0 overruns 0? carrier 0? collisions 0
br-f0fb207788a0: flags=4163mtu 1500 inet 172.20.0.1 netmask 255.255.0.0 broadcast 0.0.0.0 inet6 fe80::42:86ff:fe1e:c970 prefixlen 64 scopeid 0x20ether 02:42:86:1e:c9:70 txqueuelen 0 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker0: flags=4099 mtu 1500
? ? ? ? inet 172.17.0.1? netmask 255.255.0.0? broadcast 0.0.0.0
? ? ? ? ether 02:42:8f:8c:a9:a7? txqueuelen 0? (Ethernet)
? ? ? ? RX packets 0? bytes 0 (0.0 B)
? ? ? ? RX errors 0? dropped 0? overruns 0? frame 0
? ? ? ? TX packets 0? bytes 0 (0.0 B)
? ? ? ? TX errors 0? dropped 0 overruns 0? carrier 0? collisions 0
ens33: flags=4163mtu 1500?
?inet 192.168.23.157 netmask 255.255.255.0 broadcast 192.168.23.255?
?inet6 fe80::20c:29ff:fe54:4f5a prefixlen 64 scopeid 0x20
ether 00:0c:29:54:4f:5a txqueuelen 1000 (Ethernet)?
?RX packets 10899 bytes 11349012 (10.8 MiB)?
?RX errors 0 dropped 0 overruns 0 frame 0?
?TX packets 5575 bytes 599717 (585.6 KiB)?
?TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73mtu 65536?
?inet 127.0.0.1 netmask 255.0.0.0?
?inet6 ::1 prefixlen 128 scopeid 0x10?
?loop txqueuelen 0 (Local Loopback)
? ? ? ? RX packets 4? bytes 340 (340.0 B)
? ? ? ? RX errors 0? dropped 0? overruns 0? frame 0
? ? ? ? TX packets 4? bytes 340 (340.0 B)
? ? ? ? TX errors 0? dropped 0 overruns 0? carrier 0? collisions 0
veth4d72ad4: flags=4163mtu 1500?
?inet6 fe80::d4fb:fcff:feeb:cd7c prefixlen 64 scopeid 0x20
ether d6:fb:fc:eb:cd:7c txqueuelen 0 (Ethernet)?
?RX packets 16 bytes 1248 (1.2 KiB)?
?RX errors 0 dropped 0 overruns 0 frame 0?
?TX packets 43 bytes 4130 (4.0 KiB)?
?TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
vethe634b1c: flags=4163mtu 1500?
?inet6 fe80::e0c6:88ff:fe1c:f4a1 prefixlen 64 scopeid 0x20
ether e2:c6:88:1c:f4:a1 txqueuelen 0 (Ethernet)?
?RX packets 18 bytes 1458 (1.4 KiB)?
?RX errors 0 dropped 0 overruns 0 frame 0?
?TX packets 41 bytes 3920 (3.8 KiB)?
?TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099 mtu 1500
? ? ? ? inet 192.168.122.1? netmask 255.255.255.0? broadcast 192.168.122.255
? ? ? ? ether 00:00:00:00:00:00? txqueuelen 0? (Ethernet)
? ? ? ? RX packets 0? bytes 0 (0.0 B)
? ? ? ? RX errors 0? dropped 0? overruns 0? frame 0
? ? ? ? TX packets 0? bytes 0 (0.0 B)
? ? ? ? TX errors 0? dropped 0 overruns 0? carrier 0? collisions 0
[root@localhost ~]#
《3》virtual_ipaddress 192.168.23.200
設(shè)置好虛擬IP(VIP)為:192.168.23.200
同樣的道理矗烛,在192.168.23.157設(shè)置如下:
global_defs {
? notification_email {
? ? acassen@firewall.loc
? ? failover@firewall.loc
? ? sysadmin@firewall.loc
? }
? notification_email_from Alexandre.Cassen@firewall.loc
? smtp_server 192.168.200.1
? smtp_connect_timeout 30
? router_id NodeB
? vrrp_skip_check_adv_addr
? vrrp_strict
? vrrp_garp_interval 0
? vrrp_gna_interval 0
}?
vrrp_instance VI_1 {
? ? state BACKUP
? ? interface ens33
? ? virtual_router_id 51
? ? priority 100
? ? advert_int 1
? ? authentication {
? ? ? ? auth_type PASS
? ? ? ? auth_pass 1111
? ? }?
? ? virtual_ipaddress {
? ? ? ? 192.168.23.200
? ? }?
}
7. 接下來就可以啟動(dòng)keepalived了辅柴。
[root@localhost sbin]# ./keepalived -D
[root@localhost sbin]# ps -ef | grep keepalived
root? ? ? 46611005:41?00:00:00./keepalived -D
root? ? ? 46624661005:41?00:00:00./keepalived -D
root? ? ? 46634661005:41?00:00:00./keepalived -D
root? ? ? 46734300005:41pts/000:00:00grep --color=auto keepalived
五:檢測(cè)
1. 通過ip a 看看當(dāng)前ens33網(wǎng)卡上是否綁定了192.168.23.200虛擬IP。
2. 然后通過arp -a 查看當(dāng)前的vip映射到的物理(mac)地址高诺,可以看到當(dāng)前的vip映射到的是192.168.23.156上面碌识。
C:Usershxc>arp -a
接口: 192.168.23.1 --- 0x6
Internet 地址 物理地址 類型
192.168.23.156 00-0c-29-75-7e-20 動(dòng)態(tài)
192.168.23.157 00-0c-29-54-4f-5a 動(dòng)態(tài)
192.168.23.200 00-0c-29-75-7e-20 動(dòng)態(tài)
192.168.23.255 ff-ff-ff-ff-ff-ff 靜態(tài)
224.0.0.22 01-00-5e-00-00-16 靜態(tài)
224.0.0.251 01-00-5e-00-00-fb 靜態(tài)
224.0.0.252 01-00-5e-00-00-fc 靜態(tài)
239.11.20.1 01-00-5e-0b-14-01 靜態(tài)
239.255.255.250 01-00-5e-7f-ff-fa 靜態(tài)
255.255.255.255 ff-ff-ff-ff-ff-ff 靜態(tài)
3. 然后我們把156這臺(tái)機(jī)器關(guān)閉了,可以看到當(dāng)前的vip已經(jīng)漂移到了157這臺(tái)機(jī)器上了虱而。
C:Usershxc>arp -a
接口: 192.168.23.1 --- 0x6
Internet 地址 物理地址 類型
192.168.23.156 00-0c-29-75-7e-20 動(dòng)態(tài)
192.168.23.157 00-0c-29-54-4f-5a 動(dòng)態(tài)
192.168.23.200 00-0c-29-54-4f-5a 動(dòng)態(tài)
192.168.23.255 ff-ff-ff-ff-ff-ff 靜態(tài)
224.0.0.22 01-00-5e-00-00-16 靜態(tài)
224.0.0.251 01-00-5e-00-00-fb 靜態(tài)
224.0.0.252 01-00-5e-00-00-fc 靜態(tài)
239.11.20.1 01-00-5e-0b-14-01 靜態(tài)
239.255.255.250 01-00-5e-7f-ff-fa 靜態(tài)
255.255.255.255 ff-ff-ff-ff-ff-ff 靜態(tài)
好了筏餐,這個(gè)就是本篇所說的所有內(nèi)容,希望對(duì)您有幫助牡拇。