您好,登錄后才能下訂單哦!
如何搭建keepalived+nginx+httpd+dns高可用雙主反向代理服務器,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
1.rs服務器安裝httpd,ip為:192.168.122.5,192.168.122.6
2.rs配置好web頁面并啟動服務
3.node1兩塊網卡,一塊是外網172.16.0.3,一個內網192.168.122.3
4.node2兩塊網卡,一塊是外網172.16.0.4,一個內網192.168.122.4
5.node1配置時間服務器,其余三臺來同步時間
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.兩節點安裝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 ##主節點標志
interface ens33
virtual_router_id 51 #集群1ID
priority 100 #點點優先級,越高就是主
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的備節點
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的主節點,這樣就夠成了雙主模式
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
}
}
到此服務已可以正常使用,測試
curl http://172.16.0.91
curl http://172.16.0.90 均可正常訪問到后端兩主機
當一臺主機出故障時自動降為備節點,另一臺會自動接管,服務不會宕機.
停止節點1的服務,查看節點的2rip
systemctl stop keepalived.service
ifconfig
journalctl -f -u keepalived.service 查看日志
11.建立nginx檢測腳本,當一臺ngix服務沒啟的時候同樣降為備節點,另一臺會自動接管,服務不會宕機.
vi /etc/keepalived/chk_nginx.sh
#!/bin/bash
#
killall -0 nginx || weight -10
增加可執行權限 chmod u+x chk_nginx.sh
12.配置調用126發郵件設置
1.獲取126SSL發送證書
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.配置調用126發郵件
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 --注意這是授權碼
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/root/.certs
13.通知腳本:當成主/備節點時都啟動nginx,兩個節點都一樣
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 ##當成為主節點時啟動nginx
notify master;;
backup)
systemctl start nginx ##因為雙主模式,所以當成為備節點時不能停止nginx,一定要啟動nginx,作為另一個主節點
notify backup;;
fault)
notify fault;;
*)
echo "error"
exit 1 ;;
esac
增加執行權限
chmod u+x notify.sh
測試成為備節點時通知郵件能不能正常發送
./notify.sh backup
14.在配置文件中全局配置下,集群配置上調用nginx檢測腳本,并持續追蹤.(見最終配置文件)
vrrp_script chk_nginx {
script "/etc/keepalived/chk_nginx.sh"
fall 3
rise 3
}
15.在集群內跟蹤檢測結果.(見最終配置文件)
track_script {
chk_down
chk_nginx
}
16.在兩個集群內部調用通知腳本. (見最終配置文件)
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,查看是否降為備節點,查看日志,查看是否郵件通知,查看ip,客戶端兩個VIP能否正常訪問.
18.最終配置文件
##節點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"
}
##節點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 服務器.ip:172.16.0.7.將兩個VIP 172.16.0.90/91 解析成www.bjs.io,并順序解析
yum -y install bind
##正向區域數據
vi /etc/named.conf
zone "bjs.io" IN {
type master;
file "bjs.io.zone";
};
##反向區域數據
zone "0.16.172.in-addr.arpa" IN {
type master;
file "0.16.172.in-addr.arpa";
};
##正向區域數據文件
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
##反向區域數據文件
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.
看完上述內容,你們掌握如何搭建keepalived+nginx+httpd+dns高可用雙主反向代理服務器的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。