Linux Centos7 keepalived + nginx負載均衡

首先須要服務器關都閉防火牆和selinuxhtml

1,準備四臺nginx服務器,兩臺作代理,兩臺作後端真實服務器。linux

2,配置兩臺真實服務器
第一臺:nginx

[root@vm-4 ~]# systemctl start nginx
    [root@vm-4 ~]# systemctl enable nginx
    [root@vm-4 conf.d]# echo "server111" > /usr/share/nginx/html/index.html
    測試:
    [root@vm-4 conf.d]# curl localhost
    server111

第二臺:vim

[root@vm-5 ~]# systemctl start nginx
    [root@vm-5 ~]# systemctl enable nginx
    [root@vm-5 conf.d]# echo "server222" > /usr/share/nginx/html/index.html
    測試:
    [root@vm-5 conf.d]# curl localhost
    server222

3,配置兩臺nginx代理服務器 後端

[root@vm-2 ~]# systemctl start nginx
    [root@vm-2 ~]# systemctl enable nginx
    [root@vm-2 ~]# cd /etc/nginx/conf.d/
    [root@vm-2 conf.d]# cp default.conf proxy.conf
    [root@vm-2 conf.d]# mv default.conf default.conf.bak
    [root@vm-2 conf.d]# vi upstream.conf
    upstream nginx_pool {
    server 10.30.161.241:80 weight=1 max_fails=2 fail_timeout=2;
    server 10.30.161.242:80 weight=1 max_fails=2 fail_timeout=2;
    }
    [root@vm-2 conf.d]# vi proxy.conf 
     location / {
     proxy_pass http://nginx_pool;
     proxy_redirect default;
     proxy_set_header Host $http_host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header REMOTE-HOST $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

將nginx的配置文件拷貝到另外一臺代理服務器中:bash

[root@vm-2 conf.d]# scp proxy.conf upstream.conf 10.30.161.214:/etc/nginx/conf.d/
    分別重啓nginx:
    systemctl restart nginx

4, 兩臺代理服務器分別安裝軟件服務器

master:
    [root@vm-2 conf.d]# yum install -y keepalived
    [root@vm-2 conf.d]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
    [root@vm-2 conf.d]# vim /etc/keepalived/keepalived.conf

配置keepalived,將裏面內容所有刪除,添加下列內容:curl

! Configuration File for keepalived
global_defs {
     router_id director1   #輔助改成director2
}

vrrp_instance VI_1 {
        state MASTER        #定義主仍是備
        interface ens33     #VIP綁定接口
        virtual_router_id 80  #整個集羣的調度器一致
        priority 100         #back改成50
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                10.30.161.200/24   #設置VIP
        }
}
                backup:
                [root@yum_nginx ~]# cp /etc/keepalived/keepalived.conf
                [root@yum_nginx ~]# vi /etc/keepalived/keepalived.conf
                配置keepalived,將裏面內容所有刪除,添加下列內容:  
! Configuration File for keepalived

global_defs {
     router_id directory2
}

vrrp_instance VI_1 {   
        state BACKUP    
        interface ens33  
        nopreempt        
        virtual_router_id 80  
        priority 50   
        advert_int 1   
        authentication {  
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                10.30.161.200/24
        }
}

五、兩臺代理服務器分別啓動keepalivedide

[root@yum_nginx ~]# systemctl enable keepalived.service
    [root@yum_nginx ~]# systemctl start keepalived.service

六、檢測是否成功測試

在master上 ip a  可顯示VIP爲10.30.161.200/24
  2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:29:a6:12 brd ff:ff:ff:ff:ff:ff
inet 10.30.161.51/24 brd 10.30.161.255 scope global dynamic ens33
   valid_lft 168379sec preferred_lft 168379sec
inet 10.30.161.200/24 scope global secondary ens33
   valid_lft forever preferred_lft forever
inet6 fe80::8cfe:8d87:6478:baee/64 scope link 
   valid_lft forever preferred_lft forever
inet6 fe80::d9c7:8228:5b45:afed/64 scope link tentative dadfailed 
   valid_lft forever preferred_lft forever

用curl訪問VIP,正常輪詢

[root@vm-5 conf.d]# curl 10.30.161.200
server111
[root@vm-5 conf.d]# curl 10.30.161.200
server222
[root@vm-5 conf.d]# curl 10.30.161.200
server111
[root@vm-5 conf.d]# curl 10.30.161.200
server222
 測試當一臺master故障
 [root@vm-2 conf.d]# systemctl stop keepalived
 ip a 發現VIP偏移到backup代理服務器上

到此:
能夠解決心跳故障keepalived
但不能解決Nginx服務故障

  1. 擴展對調度器Nginx健康檢查(可選)兩臺都設置
    思路:
    讓Keepalived以必定時間間隔執行一個外部腳本,腳本的功能是當Nginx失敗,則關閉本機的Keepalived
    (1) script

    [root@nginx-proxy-master ~]# vim /etc/keepalived/check_nginx_status.sh

    #!/bin/bash
    #+檢查nginx進程是否存在
    counter=$(ps -C nginx --no-heading|wc -l) #此行有服務名
    if [ "${counter}" = "0" ]; then
    #嘗試啓動一次nginx,中止5秒後再次檢測
    systemctl start nginx #啓動服務
    sleep 5
    counter=$(ps -C nginx --no-heading|wc -l) #此行有服務名
    if [ "${counter}" = "0" ]; then
    #若是啓動沒成功,就殺掉keepalive觸發主備切換
    service keepalived stop
    fi
    fi
    [root@nginx-proxy-master ~]# chmod a+x /etc/keepalived/check_nginx_status.sh

(2). keepalived使用script

! Configuration File for keepalived

global_defs {
     router_id director1
}
vrrp_script check_nginx {     #健康檢測模塊調用
     script "/etc/keepalived/check_nginx_status.sh"  #指定腳本
     interval 5  #檢查頻率,秒
}

vrrp_instance VI_1 {
        state MASTER
        interface ens33
        virtual_router_id 80
        priority 100
        advert_int 1
        authentication {  
                auth_type PASS
                auth_pass 1111
        }
        virtual_ipaddress {
                192.168.246.16/24
        }
        track_script { 引用腳本
                check_nginx
        }
}

注:必須先啓動nginx,再啓動keepalived

相關文章
相關標籤/搜索