我們先來談?wù)刱eepalived的工作原理:
keepalived的工作原理基于三個(gè)層面,分別時(shí)IP層,TCP層和應(yīng)用層
IP
層:keepalived會(huì)定期向服務(wù)器集群中的服務(wù)器發(fā)送一個(gè)類似于ping程序的ICMP數(shù)據(jù)包。通俗理解就是我keepalived來ping你服
務(wù)器,我要是ping通了,那我知道你還活著,咱們可以繼續(xù)合作,如果不幸發(fā)現(xiàn)你掛了,那就得把這臺(tái)服務(wù)器踢了。該層就是確保服務(wù)器ip地址有效,一次來
判斷這臺(tái)服務(wù)器可否繼續(xù)在崗謀職。
TCP層:這一層和IP層類似,只不過IP層檢查的是IP,該層檢查TCP端口。keepalived在IP層
且報(bào)你服務(wù)器的IP有效,然后繼續(xù)看你的TCP端口,比如說你是web
server,那你的80端口就應(yīng)該開啟,如果keepalived發(fā)現(xiàn)你的80端口沒有啟動(dòng)。那么它就會(huì)認(rèn)為你已經(jīng)掛掉,然后剔除。
應(yīng)用層:這一層就是具體的工作程序了,keepalived是個(gè)恪盡職守的檢察官,應(yīng)用層的程序與用戶的設(shè)定息息相關(guān),keepalived則通過檢查服務(wù)器的運(yùn)行是否和用戶設(shè)定一致,如果不一致,keepalived就會(huì)宣判這臺(tái)服務(wù)器犧牲。
網(wǎng)站建設(shè)哪家好,找
創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、
微信小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了
利通免費(fèi)建站歡迎大家使用!
keepalived + nginx
主機(jī):172.25.44.3 server3
備機(jī):172.25.44.4 server4
配置虛擬ip
[root@server3 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0:0DEVICE=eth0:0TYPE=EthernetONBOOT=yesBOOTPROTO=staticDNS1=172.25.44.44IPADDR=172.25.44.100NETMASK=255.255.255.0GETWAY=172.25.44.44[root@server3 ~]# /etc/init.d/network restart[root@server3 ~]# ifconfigeth0 Link encap:Ethernet HWaddr 52:54:00:48:60:3E inet addr:172.25.44.4 Bcast:172.25.44.255 Mask:255.255.255.0 inet6 addr: fe80::5054:ff:fe48:603e/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:11926 errors:0 dropped:0 overruns:0 frame:0 TX packets:6533 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:25662550 (24.4 MiB) TX bytes:876645 (856.0 KiB)
eth0:0 Link encap:Ethernet HWaddr 52:54:00:48:60:3E inet addr:172.25.44.100 Bcast:172.25.44.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
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:36283 errors:0 dropped:0 overruns:0 frame:0 TX packets:36283 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:1982812 (1.8 MiB) TX bytes:1982812 (1.8 MiB)
主備機(jī)均需配置安裝nginx
安裝包:nginx-1.10.3.tar.gz
解壓安裝包:tar zxf nginx-1.10.3.tar.gz
[root@server3 software]# cd nginx-1.10.3 —prefix=/usr/local/lnmp/nignx
[root@server3 nginx-1.10.3]# ./configure
[root@server3 nginx-1.10.3]# make
[root@server3 nginx-1.10.3]# make install
[root@server3 nginx-1.10.3]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/
#創(chuàng)建nginx的軟鏈接
配置nginx
[root@server3 nginx-1.10.3]# cd /usr/local/lnmp/nginx/conf[root@server3 conf]# vim nginx.conf/#訪問8088端口是的發(fā)布目錄為/var/www/html,而nginx的默認(rèn)發(fā)布目錄還是/usr/local/lnmp/nginx/html
server { listen 8088;#監(jiān)聽8088端口 server_name localhost;
location / {
root /var/www/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/lnmp/nginx/html;
}
}
[root@server3 conf]# cd /var/www/html[root@server3 html]# vim index.html
Welcome to nginx!
Welcome to nginx! 172.25.44.3
[root@server3 html]# nginx
安裝keepalived
安裝包:keepalived-1.2.2.tar.gz
[root@server3 software]# wget
http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
[root@server3 software]# tar zxf keepalived-1.2.2.tar.gz
[root@server3 software]# cd keepalived-1.2.2
[root@server3 keepalived-1.2.2]# ./configure
[root@server3 keepalived-1.2.2]# make
[root@server3 keepalived-1.2.2]# make install
[root@server3 keepalived-1.2.2]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@server3 keepalived-1.2.2]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
[root@server3 keepalived-1.2.2]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@server3 keepalived-1.2.2]# cp -r /usr/local/etc/keepalived/ /etc/
配置leepalived
[root@server3 keepalived-1.2.2]# vim /var/www/html/index.html
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script “
#state MASTER:MASTER主機(jī),BACKUP備機(jī)#priority 100:優(yōu)先級(jí)指定,數(shù)字越大優(yōu)先級(jí)越高#virtual_ipaddress:指定vip#track_scrip:檢查端口[root@server3 keepalived-1.2.2]# /etc/init.d/keepalived start
備機(jī)次同步主機(jī)的操作步驟:keepalived的配置文件中,主機(jī)的state為MASTER,備機(jī)的state為BACKUP,備機(jī)的priority要小于主機(jī)
測(cè)試
驗(yàn)證nginx啟動(dòng)正常
訪問主機(jī)nginx的默認(rèn)頁訪問主機(jī)nginx的指定頁訪問備機(jī)nginx的默認(rèn)頁訪問備機(jī)nginx的指定頁訪問vip
主機(jī)server3的優(yōu)先級(jí)更高,在主機(jī)完好的情況下,有server3來負(fù)載nginx運(yùn)行[root@server3 software]# /etc/init.d/keepalived stop
Stopping keepalived: [ OK ]
當(dāng)主機(jī)的keeplived服務(wù)掛掉的時(shí)候,在則訪問vip,則由備機(jī)自動(dòng)負(fù)載nginx運(yùn)行[root@server4 conf]# ip a
1: lo: 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:48:60:3e brd ff:ff:ff:ff:ff:ff
inet 172.25.44.4/24 brd 172.25.44.255 scope global eth0
inet 172.25.254.104/24 brd 172.25.254.255 scope global eth0
inet 172.25.44.100/32 scope global eth0
inet 172.25.44.100/24 brd 172.25.44.255 scope global secondary eth0:0
inet6 fe80::5054:ff:fe48:603e/64 scope link
valid_lft forever preferred_lft forever
同時(shí)可以看到vip浮動(dòng)在備機(jī)server4上[root@server3 software]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
啟動(dòng)主機(jī)server3上的keepalived服務(wù),再次訪問vip,則nginx的負(fù)載運(yùn)行又回到主機(jī)server3上[root@server3 software]# ip a
1: lo: 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: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:b4:14:0d brd ff:ff:ff:ff:ff:ff
inet 172.25.44.3/24 brd 172.25.44.255 scope global eth0
inet 172.25.254.103/24 brd 172.25.254.255 scope global eth0
inet 172.25.44.100/32 scope global eth0
inet6 fe80::5054:ff:feb4:140d/64 scope link
valid_lft forever preferred_lft forever
該情況下查看ip,發(fā)現(xiàn)vip浮動(dòng)在主機(jī)server3上在應(yīng)用層出問題的情況下,如停掉主機(jī)server3的nginx服務(wù),則備機(jī)server4也會(huì)自動(dòng)接管nginx支持前端的訪問[root@server3 software]# killall nginx
問題補(bǔ)充:在備機(jī)keepalived的配合配置文件中,如果補(bǔ)修改state的情況下實(shí)驗(yàn)也是可以成功的,我覺得應(yīng)該時(shí)兩個(gè)機(jī)子通過priority優(yōu)先級(jí)競(jìng)爭(zhēng)。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
本文標(biāo)題:keepalived+nginx-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:
http://weahome.cn/article/depsoe.html