1. 程式人生 > >利用keepalived實現nginx調度器高可用(一)

利用keepalived實現nginx調度器高可用(一)

efault fig html oot 服務 emctl alt rom rip

利用keepalived實現nginx調度器高可用

聲明:提供四臺主機,其中兩臺nginx做前端調度器(一臺做主調度器,一臺做備用調度器),

另外兩臺主機做web服務器向外提供http服務;

框架如圖:

技術分享圖片

1.在兩臺nginx上配置nginx反代服務

# vim /etc/nginx/nginx.conf

在http上下文中添加下文:

upstream webser {
server 172.16.1.12:80 weight=1;

server 172.16.1.13:80 weight=1;
}
server {
listen 80 default_server;

location / {
proxy_pass http://webser;
}

}

配置完成後,啟動nginx服務,使配置生效;


2.配置keepalived+nginx_master(172.16.1.11主機):(前提,安裝keepalived : # yum install keepalived)

# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1

smtp_connect_timeout 30
router_id drct1
vrrp_mcast_group4 224.0.100.18
}
vrrp_script check_httpd {
script "killall -0 nginx && exit 0 || exit 1"
interval 1
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass cxqLUKJh
}
virtual_ipaddress {
172.16.1.254/16
}
track_script {
check_httpd
}
}

啟動keepalived服務:

# systemctl start keepalived.service


3.配置keepalived+nginx_backup(172.16.1.13主機):

# vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id drct2
vrrp_mcast_group4 224.0.100.18
}
vrrp_script check_httpd {
script "killall -0 nginx && exit 0 || exit 1"
interval 1
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass cxqLUKJh
}
virtual_ipaddress {
172.16.1.254/16
}
track_script {
check_httpd
}
}

配置完成後,啟動keepalived服務:

# systemctl start keepalived


4.配置後端web服務器

1)安裝httpd

# yum install http

2) 做web頁面:

webserver1:

vim /var/www/html/index.html

Real Server 1


webserver2:

vim /var/www/html/index.html

Real Server 2

3) 啟動http服務:

# systemctl start httpd


5.用一個客戶端測試:

# for i in {1..10}; do curl http://172.16.1.254;done
Real Server 2
Real Server 1
Real Server 2
Real Server 1
Real Server 2
Real Server 1
Real Server 1
Real Server 2
Real Server 1
Real Server 2


















利用keepalived實現nginx調度器高可用(一)