nginx+keepalived双master负载均衡配置
生活随笔
收集整理的這篇文章主要介紹了
nginx+keepalived双master负载均衡配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
????? 部署了一下nginx+keepalived 負載均衡總結如下主負載均衡器負責轉發,而備份負載均衡器則處于等待狀態,只有主負載均衡器失效,備份負載均衡器才承擔用戶請求轉發任務。在多負載均衡應用場景下,將兩個負載均衡器都轉發用戶請求,其主要用意在于提高資源的利用率。
nginx互為主備。
? 環境如下:
nginx master 1:192.168.137.160
nginx master 2:192.168.137.161
VIP1:192.168.137.164
VIP2:192.168.137.165
1.在nginx master上安裝nginx和keepalived,安裝方法不再復述
安裝完成后將Keepalived安裝成Linux服務。依次執行下列命令
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/????
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/????
mkdir /etc/keepalived????
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/????
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
chkconfig --level 2345 keepalived on
2.master1,master2的nginx配置文件簡略如下
user????www www;
worker_processes 8;
error_log????/services/web/log/nginx_error.log????crit;
pid????????????????logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
????????????use epoll;
????????????worker_connections 51200;
}
http
{
????????????.................
????????????
???????? upstream backend
???????? {
????????? #ip_hash;
????????? server 192.168.137.179:80;
????????? server 192.168.137.180:80;
???????? }
????????????server
????????????{
????????????????????????????listen???????????? 80;
????????????????????????????server_name localhost;
????????????????????????????location /
????????????????????????????{
???????? root /services/web/htdocs;
???????? index index.html index.php index.htm;
???????? proxy_redirect off;
???????? proxy_set_header Host $Host;
???????? proxy_set_header X-Real-IP $remote_addr;
???????? proxy_set_header X-Forwarded-For proxy_add_x_forwarded_for;
???????? proxy_pass http://backend;
???????? #include fastcgi.conf;
???????? #fastcgi_pass unix:/var/run/phpfpm.sock;
???????? #fastcgi_index index.php;
????????????????????????????}
????????????}
}
3.nginx master1的keepalived.conf如下
! Configuration File for keepalived
global_defs {
???? router_id LVS_MASTER
}
vrrp_instance VI_1 {
????????state MASTER
????????interface eth0
????????virtual_router_id 51
????????mcast_src_ip 192.168.137.160
????????priority 180
????????advert_int 1
????????authentication {
????????????????auth_type PASS
????????????????auth_pass 1111
????????}
????????virtual_ipaddress {
????????????????192.168.137.164
????????}
}
vrrp_instance VI_2 {
????????state BACKUP
????????interface eth0
????????virtual_router_id 52
????????mcast_src_ip 192.168.137.160
????????priority 150
????????advert_int 1
????????authentication {
????????????????auth_type PASS
????????????????auth_pass 1111
????????}
????????virtual_ipaddress {
????????????????192.168.137.165
????????}
}
? nginx master2的keepalived.conf如下
! Configuration File for keepalived
global_defs {
???? router_id LVS_MASTER
}
vrrp_instance VI_1 {
????????state BACKUP
????????interface eth0
????????virtual_router_id 51
????????mcast_src_ip 192.168.137.161
????????priority 150
????????advert_int 1
????????authentication {
????????????????auth_type PASS
????????????????auth_pass 1111
????????}
????????virtual_ipaddress {
????????????????192.168.137.164
????????}
}
vrrp_instance VI_1 {
????????state MASTER
????????interface eth0
????????virtual_router_id 52
????????mcast_src_ip 192.168.137.161
????????priority 180
????????advert_int 1
????????authentication {
????????????????auth_type PASS
????????????????auth_pass 1111
????????}
????????virtual_ipaddress {
????????????????192.168.137.165
????????}
}
啟動keepalived,nginx
services keepalived start
/usr/local/ngins/sbin/nginx
4.查看VIP
?master1上執行 ip add list
master2上執行 ip add list
可以看到master1上綁定的vip為192.168.137.164,master2上綁定的vip為192.168.137.165.
需要考慮另外一個問題是當負載均衡nginx掛掉的時候,keepalived不能進行切換需要自行寫監控腳本。
轉載于:https://blog.51cto.com/qq15570453/1162616
總結
以上是生活随笔為你收集整理的nginx+keepalived双master负载均衡配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows平板电脑将取代苹果在企业市
- 下一篇: 问题和收获