1. 程式人生 > >Nginx+Keepalived雙主架構實現

Nginx+Keepalived雙主架構實現

detail available star inet6 onf www ln -s ipaddress route

參照 https://www.jianshu.com/p/da26df4f7d60


Keepalived+Nginx實現高可用Web負載均衡

Master 192.168.0.69 nginx、keepalived Centos7.4
backup 192.168.0.70 nginx、keepalived Centos7.4
vip(虛擬IP)
192.168.0.180


1、兩臺都安裝nginx,參照安裝nginx文檔


2、 兩臺都安裝Keepalived


yum install -y libnl libnl-devel libnfnetlink-devel popt-devel


cd /usr/local/src/

tar -zxvf keepalived-1.2.18.tar.gz

cd keepalived-1.2.18

./configure --prefix=/usr/local/keepalived

make && make install

3、將keepalived安裝成Linux系統服務

mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/
ln -s /usr/local/keepalived/sbin/keepalived /usr/local/sbin/
chkconfig keepalived on


4、修改keepalived配置文件


接下來就是配置了,很簡單,之前的刪除,直接復制下面配置文件

先是主服務器:

vi /etc/keepalived/keepalived.conf

global_defs
{
notification_email #通知email,根據實際情況配置
{
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id node1 #節點名標識,主要用於通知中
}


vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx.sh" #在這裏添加腳本鏈接
interval 3 #腳本執行間隔
weight 2 #腳本結果導致的優先級變更
}


vrrp_instance VI_NODE_1 {
state MASTER #配置為主服務器
interface ens33 #通訊網卡
virtual_router_id 100 #路由標識
priority 200 #優先級,0-254
advert_int 5 #通知間隔,實際部署時可以設置小一點,減少延時

authentication {
auth_type PASS
auth_pass 123456 #驗證密碼,用於通訊主機間驗證
}

track_script {
chk_http_port #添加腳本執行
}

virtual_ipaddress {
192.168.0.180 #虛擬ip,可以定義多個
}
}

vrrp_instance VI_NODE_2 {
state MASTER #配置為主服務器
interface ens33 #通訊網卡
virtual_router_id 101 #路由標識
priority 100 #優先級,0-254
advert_int 5 #通知間隔,實際部署時可以設置小一點,減少延時

authentication {
auth_type PASS
auth_pass 123456 #驗證密碼,用於通訊主機間驗證
}

track_script {
chk_http_port #添加腳本執行
}

virtual_ipaddress {
192.168.0.181 #虛擬ip,可以定義多個
}
}

接下是從服務器設置:

vi /etc/keepalived/keepalived.conf

global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
stmp_connect_timeout 30


router_id node2
}


vrrp_script chk_http_port {
script "/usr/local/keepalived/nginx.sh" #在這裏添加腳本鏈接
interval 3 #腳本執行間隔
weight 2 #腳本結果導致的優先級變更
}


vrrp_instance VI_NODE_1 {
state BACKUP #與主服務器對應
interface ens33 #從服務器的通信網卡
virtual_router_id 100 #路由標識,和主服務器相同
priority 100 #優先級,小於主服務器即可
advert_int 5 #這裏是接受通知間隔,與主服務器要設置相同

authentication {
auth_type PASS
auth_pass 123456 #驗證密碼,與主服務器相同
}


track_script {
chk_http_port #添加腳本執行
}

virtual_ipaddress {
192.168.0.180 #虛擬IP,也要和主服務器相同
}
}


vrrp_instance VI_NODE_2 {
state BACKUP #與主服務器對應
interface ens33 #從服務器的通信網卡
virtual_router_id 101 #路由標識,和主服務器相同
priority 100 #優先級,小於主服務器即可
advert_int 5 #這裏是接受通知間隔,與主服務器要設置相同

authentication {
auth_type PASS
auth_pass 123456 #驗證密碼,與主服務器相同
}


track_script {
chk_http_port #添加腳本執行
}

virtual_ipaddress {
192.168.0.181 #虛擬IP,也要和主服務器相同
}
}

5 兩個節點配置check_ngixn腳本

當腳本檢測到nginx沒有運行的時候會嘗試去啟動nginx以此,如果失敗則停掉keepalived進程

vi /usr/local/keepalived/nginx.sh

#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx #nginx命令的路徑
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi


6、啟動keepalived
[root@bogon ~]# service keepalived start
Starting keepalived (via systemctl): [ OK ]


7、查看虛擬ip在哪臺上, 註意過兩分鐘才出現vip

[root@bogon ~]# ip a
[root@bogon ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:24:21:c4 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.69/24 brd 192.168.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.0.180/32 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.0.181/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::d675:3ae7:5113:ad71/64 scope link noprefixroute
valid_lft forever preferred_lft forever


8、我們修改兩個nginx的首頁信息,在首頁中加入各自的IP地址

vi /usr/local/nginx/html/index.html

<h1>Welcome to nginx! 192.168.0.69</h1>


9、訪問:http://192.168.0.180/ http://192.168.0.181/

Welcome to nginx! 192.168.0.69

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

10、高可用切換


在192.168.0.69停止keepalived , 查看192.168.0.180飄到了192.168.0.70上

service keepalived stop


1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:ba:67:74 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.70/24 brd 192.168.0.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet 192.168.0.180/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::6c33:a5d6:2ea9:d781/64 scope link noprefixroute
valid_lft forever preferred_lft forever
inet6 fe80::d675:3ae7:5113:ad71/64 scope link tentative noprefixroute dadfailed
valid_lft forever preferred_lft forever

報錯解決

1、
[root@bogon keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived

*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS


yum -y install libnl libnl-devel


2、

configure: error: libnfnetlink headers missing

yum install -y libnfnetlink-devel


3、

安裝keepalived出錯./configure: error: Popt libraries is required

yum? -y? install? popt-devel

4、

[root@bogon keepalived-1.2.8]# /etc/rc.d/init.d/keepalived start
Starting keepalived (via systemctl): Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.
[FAILED]

[root@bogon keepalived-1.2.8]# ln -s /usr/local/keepalived/sbin/keepalived /usr/bin/
[root@bogon keepalived-1.2.8]# /etc/init.d/keepalived start
Starting keepalived (via systemctl): [ OK ]

Nginx+Keepalived雙主架構實現