您好,登錄后才能下訂單哦!
如何搭建keepalived+nginx+httpd+dns高可用雙主反向代理服務(wù)器,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
1.rs服務(wù)器安裝httpd,ip為:192.168.122.5,192.168.122.6
2.rs配置好web頁面并啟動服務(wù)
3.node1兩塊網(wǎng)卡,一塊是外網(wǎng)172.16.0.3,一個內(nèi)網(wǎng)192.168.122.3
4.node2兩塊網(wǎng)卡,一塊是外網(wǎng)172.16.0.4,一個內(nèi)網(wǎng)192.168.122.4
5.node1配置時間服務(wù)器,其余三臺來同步時間
6.nod1配置好nginx反向代理后端兩臺rs,并測試
7.nod2配置好nginx反向代理后端兩臺rs,并測試
yun -y install nginx
http {
upstream webservers {
server 192.168.122.5:80;
server 192.168.122.6:80;
}
server {
location / {
proxy_pass http://webservers;
}
}
8.兩節(jié)點安裝keepalived
9.node1配置高可用
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localdomain ##本地通知
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.1.1.33 ##多播地址
}
#集群1
vrrp_instance VI_1 {
state MASTER ##主節(jié)點標(biāo)志
interface ens33
virtual_router_id 51 #集群1ID
priority 100 #點點優(yōu)先級,越高就是主
advert_int 1
authentication {
auth_type PASS
auth_pass %^*AJOoj78j.
}
virtual_ipaddress {
172.16.0.90/16 dev ens33 label ens33:0 ##集群VIP
}
}
#集群2
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 44 #集群ID,唯一值,不能跟其他集群ID相同
priority 96
advert_int 1
authentication {
auth_type PASS
auth_pass J%(#Qjb78.
}
virtual_ipaddress {
172.16.0.91/16 dev ens33 label ens33:1
}
}
10.node2配置高可用
[root@node2 keepalived]# vi keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localdomain
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.1.1.33
}
vrrp_instance VI_1 {
state BACKUP ##集群1的備節(jié)點
interface ens33
virtual_router_id 51
priority 96
advert_int 1
authentication {
auth_type PASS
auth_pass %^*AJOoj78j.
}
virtual_ipaddress {
172.16.0.90/16 dev ens33 label ens33:0
}
}
vrrp_instance VI_2 {
state MASTER ##集群2的主節(jié)點,這樣就夠成了雙主模式
interface ens33
virtual_router_id 44
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass J%(#Qjb78.
}
virtual_ipaddress {
172.16.0.91/16 dev ens33 label ens33:1
}
}
到此服務(wù)已可以正常使用,測試
curl http://172.16.0.91
curl http://172.16.0.90 均可正常訪問到后端兩主機(jī)
當(dāng)一臺主機(jī)出故障時自動降為備節(jié)點,另一臺會自動接管,服務(wù)不會宕機(jī).
停止節(jié)點1的服務(wù),查看節(jié)點的2rip
systemctl stop keepalived.service
ifconfig
journalctl -f -u keepalived.service 查看日志
11.建立nginx檢測腳本,當(dāng)一臺ngix服務(wù)沒啟的時候同樣降為備節(jié)點,另一臺會自動接管,服務(wù)不會宕機(jī).
vi /etc/keepalived/chk_nginx.sh
#!/bin/bash
#
killall -0 nginx || weight -10
增加可執(zhí)行權(quán)限 chmod u+x chk_nginx.sh
12.配置調(diào)用126發(fā)郵件設(shè)置
1.獲取126SSL發(fā)送證書
mkdir -p /root/.certs/
cd /root/.certs/
echo -n | openssl s_client -connect smtp.126.com:465 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ~/.certs/qq.crt
certutil -A -n "GeoTrust Global CA" -t "C,," -d ~/.certs -i ~/.certs/qq.crt
certutil -L -d /root/.certs/
certutil -A -n "GeoTrust SSL CA - G3" -t "Pu,Pu,Pu" -d ./ -i qq.crt
2.配置postfix
vi /etc/postfix/main.cf
inet_interfaces = all
inet_protocols = all
systemctl enable postfix
systemctl restart postfix
3.配置調(diào)用126發(fā)郵件
vi /etc/mail.rc
set from=xxxxxxxx@126.com --郵箱用戶名
set smtp=smtps://smtp.126.com:465
set smtp-auth-user=xxxxxxxx@126.com --郵箱用戶名
set smtp-auth-password=ajbjs465785 --注意這是授權(quán)碼
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/root/.certs
13.通知腳本:當(dāng)成主/備節(jié)點時都啟動nginx,兩個節(jié)點都一樣
cd /etc/keepalived/
vi notify.sh
#!/bin/bash
#
contact='xxxxxxx@qq.com' --接收郵件的郵箱
notify() {
local mailsubject="$(hostname) to be $1,vip floating"
local mailbody="$(date +'%F %T'):vrrp transition,$(hostname) changed to be $1 "
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
systemctl start nginx ##當(dāng)成為主節(jié)點時啟動nginx
notify master;;
backup)
systemctl start nginx ##因為雙主模式,所以當(dāng)成為備節(jié)點時不能停止nginx,一定要啟動nginx,作為另一個主節(jié)點
notify backup;;
fault)
notify fault;;
*)
echo "error"
exit 1 ;;
esac
增加執(zhí)行權(quán)限
chmod u+x notify.sh
測試成為備節(jié)點時通知郵件能不能正常發(fā)送
./notify.sh backup
14.在配置文件中全局配置下,集群配置上調(diào)用nginx檢測腳本,并持續(xù)追蹤.(見最終配置文件)
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.sh"
fall 3
rise 3
}
15.在集群內(nèi)跟蹤檢測結(jié)果.(見最終配置文件)
track_script {
chk_down
chk_nginx
}
16.在兩個集群內(nèi)部調(diào)用通知腳本. (見最終配置文件)
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
16.查看日志
journalctl -f -u keepalived
17.測試停止node1,nginx,查看是否降為備節(jié)點,查看日志,查看是否郵件通知,查看ip,客戶端兩個VIP能否正常訪問.
18.最終配置文件
##節(jié)點1
[root@node1 keepalived]# cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localdomain
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.1.1.33
}
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.sh"
fall 3
rise 3
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass %^*AJOoj78j.
}
virtual_ipaddress {
172.16.0.90/16 dev ens33 label ens33:0
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 44
priority 96
advert_int 1
authentication {
auth_type PASS
auth_pass J%(#Qjb78.
}
virtual_ipaddress {
172.16.0.91/16 dev ens33 label ens33:1
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
##節(jié)點2
cat keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localdomain
}
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.1.1.33
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 96
advert_int 1
authentication {
auth_type PASS
auth_pass %^*AJOoj78j.
}
virtual_ipaddress {
172.16.0.90/16 dev ens33 label ens33:0
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 44
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass J%(#Qjb78.
}
virtual_ipaddress {
172.16.0.91/16 dev ens33 label ens33:1
}
track_script {
chk_nginx
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
19.dns 服務(wù)器.ip:172.16.0.7.將兩個VIP 172.16.0.90/91 解析成www.bjs.io,并順序解析
yum -y install bind
##正向區(qū)域數(shù)據(jù)
vi /etc/named.conf
zone "bjs.io" IN {
type master;
file "bjs.io.zone";
};
##反向區(qū)域數(shù)據(jù)
zone "0.16.172.in-addr.arpa" IN {
type master;
file "0.16.172.in-addr.arpa";
};
##正向區(qū)域數(shù)據(jù)文件
vi /var/named/bjs.io.zone
$TTL 1D
@ IN SOA ns1.bjs.io root.localdomain 2019011601 1H 10M 3D 1D
IN NS ns1
ns1 IN A 172.16.0.7
www IN A 172.16.0.90
www IN A 172.16.0.91
##反向區(qū)域數(shù)據(jù)文件
vi /var/named/0.16.172.in-addr.arpa
$TTL 1D
@ IN SOA ns1.bjs.io root.localdomain 2019011601 1H 10M 3D 1D
IN NS ns1.bjs.io.
7 IN PTR ns1.bjs.io.
90 IN PTR www.bjs.io.
91 IN PTR www.bjs.io.
看完上述內(nèi)容,你們掌握如何搭建keepalived+nginx+httpd+dns高可用雙主反向代理服務(wù)器的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。