真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

搭建高可用集群---keepalived的介紹及簡單配置

本文索引:

十多年的蘭坪網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。營銷型網(wǎng)站建設(shè)的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整蘭坪建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“蘭坪網(wǎng)站設(shè)計”,“蘭坪網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。集群介紹 keepalived介紹 keepalived配置高可用集群
集群介紹

根據(jù)功能劃分為2類:高可用和負(fù)載均衡

高可用集群:通常為兩臺服務(wù)器,一臺工作,另外一臺作為冗余。當(dāng)提供服務(wù)的機(jī)器宕機(jī),冗余將接替繼續(xù)提供服務(wù)。按可用的效率衡量高可用,例如“4個9”即99.99%的可用性,在99.99%的時間上不允許服務(wù)器宕機(jī),當(dāng)服務(wù)器宕機(jī)后作為冗余的服務(wù)器將立即接替主服務(wù)器提供服務(wù),切換的時間間隔也較短,給用戶造成的影響較小。實現(xiàn)高可用的開源軟件有:heartbeat、keepalived。

負(fù)載均衡集群:通常需要有一臺服務(wù)器作為分發(fā)器,它負(fù)責(zé)把用戶的請求分發(fā)給后端的服務(wù)器集群進(jìn)行處理,在這個集群里,除了分發(fā)器外,就是給用戶提供服務(wù)的服務(wù)器,并且這些服務(wù)器的數(shù)量至少為2臺。實現(xiàn)負(fù)載均衡的開源軟件有LVS、keepalived、haproxy、nginx,商業(yè)的負(fù)載均衡器有F5、Netscaler,優(yōu)點為高并發(fā)量、高穩(wěn)定性。


keepalived介紹

keepalived通過VRRP(Virtual Router Redundancy Protocol,虛擬路由冗余協(xié)議)來實現(xiàn)高可用。

在這個協(xié)議里會將多臺功能相同的路由器組成一個小組,這個小組里會有1個master角色和N個backup角色。master會通過組播的形式向各個backup發(fā)送VRRP協(xié)議的數(shù)據(jù)包,當(dāng)backup收不到master發(fā)來的VRRP數(shù)據(jù)包時,就會認(rèn)為master已宕機(jī)。此時就需要根據(jù)各個backup的優(yōu)先級來決定哪個backup成為新的master。

keepalived有三個模塊,分別是core、check和vrrp。其中core模塊是keepalived的核心,負(fù)責(zé)主進(jìn)程的啟動、維護(hù)及全局配置文件的加載和解析;check模塊負(fù)責(zé)健康檢查;vrrp模塊用來實現(xiàn)VRRP協(xié)議。


keepalived配置

test1:192.168.65.133 test2:192.168.65.134 test1作為master,test2作為backup

test1/test2都安裝keepalived和nginx

# 如果主機(jī)內(nèi)已經(jīng)源碼安裝過nginx就不需要再安裝了 [root@test1 ~]# yum install -y keepalived [root@test1 ~]# yum install -y nginx [root@test2 ~]# yum install -y keepalived [root@test2 ~]# yum install -y nginx

更改配置文件

# 默認(rèn)安裝keepalived后會有一個keepalived.conf,這里清空內(nèi)容后輸入下列的代碼 [root@test1 ~]# > /etc/keepalived/keepalived.conf [root@test1 ~]# vim /etc/keepalived/keepalived.conf # 全局定義參數(shù) global_defs { # 出現(xiàn)問題時發(fā)郵件,郵件地址自定義 notification_email { 1245626656@qq.com } notification_email_from 1245626656@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } # 用來檢測服務(wù)是否正常 vrrp_script chk_nginx { # 自定義腳本:檢測服務(wù)是否正常 script "/usr/local/sbin/check_ng.sh" # 檢測間隔3s interval 3 } # 定義相關(guān)master信息 vrrp_instance VI_1 { # 角色定義:主為MASTER,從為BACKUP state MASTER # 定義發(fā)送數(shù)據(jù)包的網(wǎng)卡 interface ens33 # 定義路由器id virtual_router_id 51 # 權(quán)重 priority 100 advert_int 1 # 定義認(rèn)證信息 authentication { auth_type PASS auth_pass test1 } # 定義公有ip,正常時master綁定,master宕機(jī)后backup綁定 virtual_ipaddress { 192.168.188.100 } # 加載檢測腳本,對于上面 track_script { chk_nginx } }

編輯檢測腳本

# 檢測服務(wù)是否正常,腳本所在路徑在/etc/keepalived/keepalived.conf內(nèi)定義 [root@test1 ~]# vim /usr/local/sbin/check_ng.sh #!/bin/bash #時間變量,用于記錄日志 d=`date --date today +%Y%m%d_%H:%M:%S` #計算nginx進(jìn)程數(shù)量 n=`ps -C nginx --no-heading | wc -l` #如果進(jìn)程為0,則啟動nginx,并且再次檢測nginx進(jìn)程數(shù)量, #如果還為0,說明nginx無法啟動,此時需要關(guān)閉keepalived if [ $n -eq "0" ]; then /etc/init.d/nginx start n2=`ps -C nginx --no-heading|wc -l` # 執(zhí)行啟動nginx后發(fā)現(xiàn)nginx啟動不成功,就關(guān)閉keepalived服務(wù),并記錄日志 if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fi fi [root@test1 ~]# chmod 755 /usr/local/sbin/check_ng.sh [root@test1 ~]# systemctl stop firewalld [root@test1 ~]# setenforce 0

如果你master上的nginx是源碼編譯的,那么對應(yīng)腳本上啟動nginx的命令要做相應(yīng)修改,此外master上的防火墻和selinux必須關(guān)閉,否則keepalived無法拉起nginx。

keepalived的日志文件記錄在/var/log/messages文件內(nèi)

[root@test1 data]# less /var/log/messages ... Jan 16 19:58:06 test1 Keepalived_vrrp[5547]: Registering gratuitous ARP shared channel Jan 16 19:58:06 test1 Keepalived_vrrp[5547]: Opening file \'/etc/keepalived/keepalived.conf\'. Jan 16 19:58:06 test1 kernel: show_signal_msg: 53 callbacks suppressed Jan 16 19:58:06 test1 kernel: keepalived[5547]: segfault at 0 ip (null) sp 00007fff063621b8 error 14 in libgcc_s-4.8.5-20150702.so.1[7fd2356f4000+15000] Jan 16 19:58:06 test1 Keepalived_vrrp[5547]: VRRP_Instance(VI_ 1) removing protocol VIPs. Jan 16 19:58:06 test1 Keepalived[4983]: Keepalived_vrrp exited due to segmentation fault (SIGSEGV). Jan 16 19:58:06 test1 Keepalived[4983]: Please report a bug at https://github.com/acassen/keepalived/issues Jan 16 19:58:06 test1 Keepalived[4983]: and include this log from when keepalived started, what happened Jan 16 19:58:06 test1 Keepalived[4983]: immediately before the crash, and your configuration file. Jan 16 19:58:06 test1 Keepalived[4983]: VRRP child process(5547) died: Respawning Jan 16 19:58:06 test1 Keepalived[4983]: Starting VRRP child process, pid=5548 Jan 16 19:58:06 test1 Keepalived_vrrp[5548]: Registering Kernel netlink reflector Jan 16 19:58:06 test1 Keepalived_vrrp[5548]: Registering Kernel netlink command channel Jan 16 19:58:06 test1 Keepalived_vrrp[5548]: Registering gratuitous ARP shared channel Jan 16 19:58:06 test1 Keepalived_vrrp[5548]: Opening file \'/etc/keepalived/keepalived.conf\'. ...

公有ip的查看

[root@test1 data]# ip addr ... 2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000 ... inet 192.168.65.100/32 scope global ens33 valid_lft forever preferred_lft forever ... 配置backup從服務(wù)器

修改配置文件

# 配置前關(guān)閉防火墻及selinux [root@test1 ~]# systemctl stop firewalld [root@test1 ~]# setenforce 0 [root@test2 ~]# cat /etc/keepalived/keepalived.conf global_defs { notification_email { 1245626656@qq.com } notification_email_from 1245626656@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/usr/local/sbin/check_ng.sh" interval 3 } vrrp_instance VI_1 { # 配置為backup機(jī) state BACKUP interface ens33 # router_id與master一致 virtual_router_id 51 # 權(quán)重較master要小 priority 90 advert_int 1 authentication { auth_type PASS auth_pass test2 } # 這樣設(shè)置公有ip,master宕機(jī)后,backup機(jī)綁定 virtual_ipaddress { 192.168.65.100 } track_script { chk_nginx } }

配置檢測腳本

[root@test2 ~]# vim /usr/local/sbin/check_ng.sh #!/bin/bash #時間變量,用于記錄日志 d=`date --date today +%Y%m%d_%H:%M:%S` #計算nginx進(jìn)程數(shù)量 n=`ps -C nginx --no-heading | wc -l` if [ $n -eq "0" ]; then # backup機(jī)上的nginx使用yum安裝的,啟動目錄不同 systemctl start nginx n2=`ps -C nginx --no-heading|wc -l` # 執(zhí)行啟動nginx后發(fā)現(xiàn)nginx啟動不成功,就關(guān)閉keepalived服務(wù),并記錄日志 if [ $n2 -eq "0" ]; then echo "$d nginx down,keepalived will stop" >> /var/log/check_ng.log systemctl stop keepalived fi fi # 配置權(quán)限 [root@test1 ~]# chmod 755 /usr/local/sbin/check_ng.sh 區(qū)分主從內(nèi)的nginx

修改master上nginx默認(rèn)主機(jī)的默認(rèn)網(wǎng)頁內(nèi)容為“master web”;backup上nginx默認(rèn)主頁的內(nèi)容為“backup web”;訪問公有ip看顯示的是哪個服務(wù)器的默認(rèn)網(wǎng)頁。

# 修改默認(rèn)虛擬主機(jī)上的默認(rèn)網(wǎng)頁內(nèi)容,這個要根據(jù)自己的實際情況修改 [root@test1 ~]# echo "master web server" > /data/wwwroot/default/index.html # yum 安裝的nginx的默認(rèn)網(wǎng)頁路徑是/usr/share/nginx/html/index.html [root@test2 ~]# echo "backup web server" > /usr/share/nginx/html/index.html 測試高可用

master信息

[root@test1 data]# curl -x127.0.0.1:80 test1.com master web server [root@test1 data]# curl -x127.0.0.1:80 test1.com -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 16 Jan 2018 13:36:35 GMT Content-Type: text/html Content-Length: 18 Last-Modified: Tue, 16 Jan 2018 13:33:27 GMT Connection: keep-alive ETag: "5a5dff27-12" Accept-Ranges: bytes # 當(dāng)前公用ip綁定在test1上 [root@test1 data]# ip addr 1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:d0:81:f5 brd ff:ff:ff:ff:ff:ff inet 192.168.65.133/24 brd 192.168.65.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.65.100/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::893d:23f6:69dd:1d54/64 scope link valid_lft forever preferred_lft forever

backup信息

[root@test2 ~]# curl -x127.0.0.1:80 test2.com backup web server [root@test2 ~]# curl -x127.0.0.1:80 test2.com -I HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Tue, 16 Jan 2018 13:35:49 GMT Content-Type: text/html Content-Length: 18 Last-Modified: Tue, 16 Jan 2018 13:35:33 GMT Connection: keep-alive ETag: "5a5dffa5-12" Accept-Ranges: bytes 模擬主宕機(jī):關(guān)閉master上的keepalived 模擬主恢復(fù)服務(wù):重啟master上的keepalived



標(biāo)題名稱:搭建高可用集群---keepalived的介紹及簡單配置
鏈接分享:http://weahome.cn/article/cpegio.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部