天天看點

Nginx+Keepalived實作 轉載

一、Keepalived簡介

    keepalived是一個類似于layer3, 4 & 5交換機制的軟體,也就是我們平時說的第3層、第4層和第5層交換。Keepalived的作用是檢測web伺服器的狀态,如果有一台web伺服器當機,或工作出現故障,Keepalived将檢測到,并将有故障的web伺服器從系統中剔除,當web伺服器工作正常後Keepalived自動将web伺服器加入到伺服器群中,這些工作全部自動完成,不需要人工幹涉,需要人工做的隻是修複故障的web伺服器。

                                                                             -----百科資料

二、規劃

1 2 3 4

OS:CentOS7

Nginx_Master: 192.168.10.128   Nginx主機

Nginx_BackUp: 192.168.10.145   Nginx備機

Nginx_VIP1: 192.168.10.111     虛拟IP位址

說明:

虛拟IP是外網通路的IP位址,通過keepalived設定,以及VRRP将虛拟IP位址綁定到主機和備機上,通過權重實作控制。當主機挂掉後,keepalived 釋放對主機的控制,備機接管虛拟IP位址。

三、下載下傳相關軟體包

1 2 3

# yum install -y gcc openssl openssl-devel

# wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz    ##下載下傳Keeplived

# wget http://nginx.org/download/nginx-1.9.4.tar.gz

四、編譯Nginx

①編譯

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

# groupadd -r nginx

# useradd -r -g nginx nginx

# tar -zxvf nginx-1.9.4.tar.gz

# cd nginx-1.9.4/

#  ./configure \

--prefix=

/usr

\

--sbin-path=

/usr/sbin/nginx

\

--conf-path=

/etc/nginx/nginx

.conf \

--error-log-path=

/var/log/nginx/error

.log \

--http-log-path=

/var/log/nginx/access

.log \

--pid-path=

/var/run/nginx/nginx

.pid  \

--lock-path=

/var/lock/nginx

.lock \

--user=nginx \

--group=nginx \

--with-http_ssl_module \

--with-http_flv_module \

--with-http_stub_status_module \

--with-http_gzip_static_module \

--http-client-body-temp-path=

/var/tmp/nginx/client/

\

--http-proxy-temp-path=

/var/tmp/nginx/proxy/

\

--http-fastcgi-temp-path=

/var/tmp/nginx/fcgi/

\

--http-uwsgi-temp-path=

/var/tmp/nginx/uwsgi

\

--http-scgi-temp-path=

/var/tmp/nginx/scgi

\

--with-pcre

# make

# make install

# /usr/sbin/nginx       ##開啟nginx

②簡單配置nginx

1 2 3 4

# vi /etc/nginx/nginx.conf 

server {

listen       80;

server_name  www.zhi.com;

Master:

1 2 3

# vi /usr/html/index.html

<h1>Success!<

/h1

>

<p>Welcome to LINUX Keepalived System 1!<

/p

>

BackUp:

1 2 3

# vi /usr/html/index.html

<h1>Success!<

/h1

>

<p>Welcome to LINUX Keepalived System 2!<

/p

>

五、編譯Keeplived

1 2 3

# tar -zxvf keepalived-1.2.19.tar.gz 

# cd keepalived-1.2.19

# ./configure --sysconf=/etc --prefix=/usr/local/keepalived

Nginx+Keepalived實作 轉載
1 2

# make

# make install

六、配置Keeplived

①設定開機自啟動并配置服務腳本

1 2 3

# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

# /etc/init.d/keepalived start

# systemctl start keepalived         ##以上皆可啟動keepalived

1 2

# chkconfig --add keepalived

# chkconfig keepalived on

1 2 3 4 5 6 7 8 9 10 11 12

# vi /etc/keepalived/check_nginx.sh      ##配置nginx服務監控腳本

!

/bin/bash

if

[ $(

ps

-C nginx --no-header |

wc

-l) -

eq

0 ]

then

/usr/sbin/nginx

fi

sleep

2

if

[ $(

ps

-C nginx --no-header |

wc

-l) -

eq

0 ]

then

/etc/init

.d

/keepalived

stop

fi

# chmod u+x /etc/keepalived/check_nginx.sh

注:該腳本檢測nginx服務正常開啟與否,使自動切換keepalived虛拟IP位址。如無該腳本,必須手動執行關閉keepalived方可切換。

②MASTER配置

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

# vi /etc/keepalived/keepalived.conf

! Configuration File 

for

keepalived

global_defs {

notification_email {

[email protected]               

##設定報警郵件位址,可以設定多個

}

notification_email_from [email protected]      

##設定郵件發送位址

smtp_server 127.0.0.1                 

##設定SMTP-Server位址

smtp_connect_timeout 30                 

##設定連接配接逾時時間

router_id LVS_DEVEL             

##運作keepalived服務的辨別,顯示在郵件主題的資訊

}

vrrp_script check_nginx {

script 

"/etc/keepalived/check_nginx.sh"

##Nginx服務監控腳本

interval 2                               

##腳本執行間隔

weight 2                              

##腳本執行結果導緻的優先級變更

}

vrrp_instance VI_1 {

state MASTER                      

##指定keepalived角色為主伺服器

interface eno16777736            

##指定HA監測網絡的接口

virtual_router_id 51              

##虛拟路由辨別,同一個vrrp執行個體使用唯一的辨別

priority 101                   

##定義優先級,數字越大,優先級越高      

advert_int 1               

##設定MASTER與BACKUP負載均衡器間同步檢查的時間間隔,機關秒

authentication {                     

##設定驗證類型和密碼

auth_type PASS                  

##設定驗證類型,主要有PASS和AH兩種

auth_pass 1111             

##設定驗證密碼,同一vrrp_instance密碼必須相同

}

track_script {

check_nginx                  

##檢測執行腳本

}

virtual_ipaddress {

192.168.10.111

/24

#設定虛拟IP位址,可設定多個虛拟IP位址

}

}

virtual_server 192.168.10.111 80 {     

##設定虛拟伺服器,指定虛拟IP位址和服務端口

delay_loop 6                        

##設定運作情況檢查時間,機關秒

lb_algo rr                       

##設定負載排程算法,rr即輪詢算法

lb_kind NAT                  

##設定LVS實作負載均衡的機制,有NAT、TUN、DR三個模式可選

nat_mask 255.255.255.0  

persistence_timeout 50            

##會話保持時間,機關秒

protocol TCP                         

##指定轉發協定類型,有TCP和UDP兩種

sorry_server 127.0.0.1 80

real_server 192.168.10.128 80 {   

##配置服務節點1,指定real server的真實IP和端口

weight 1                       

##配置服務節點的權值,數字越大,權值越高

HTTP_GET {

url {

path /

status_code 200        

##200狀态碼,表示伺服器已成功處理請求

}

connect_timeout 3            

##響應逾時時間

nb_get_retry 3                   

##重試次數

delay_before_retry 3               

##重試間隔

}

}

real_server 192.168.10.145 80 {

weight 1

HTTP_GET {

url {

path /

status_code 200

}

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

③BACKUP配置

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

# vi /etc/keepalived/keepalived.conf

! Configuration File 

for

keepalived

global_defs {

notification_email {

[email protected]

}

notification_email_from [email protected]

smtp_server 127.0.0.1

smtp_connect_timeout 30

router_id LVS_DEVEL

}

vrrp_script check_nginx {

script 

"/etc/keepalived/check_nginx.sh"

##Nginx服務監控腳本

interval 2

weight 2

}

vrrp_instance VI_1 {

state BACKUP

interface eno16777736

virtual_router_id 51

priority 100

advert_int 1

authentication {

auth_type PASS

auth_pass 1111

}

track_script {

check_nginx                  

##檢測腳本

}

virtual_ipaddress {

192.168.10.111

/24

}

}

virtual_server 192.168.10.111 80 {

delay_loop 6

lb_algo rr

lb_kind NAT

nat_mask 255.255.255.0

persistence_timeout 50

protocol TCP

sorry_server 127.0.0.1 80

real_server 192.168.10.128 80 {

weight 1

HTTP_GET {

url {

path /

status_code 200

}

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

real_server 192.168.10.145 80 {

weight 1

HTTP_GET {

url {

path /

status_code 200

}

connect_timeout 3

nb_get_retry 3

delay_before_retry 3

}

}

}

④檢視系統日志

1

# tail /var/log/messages

Nginx+Keepalived實作 轉載

注:這是192.168.10.145的日志(沒有同步時間),由于更改配置後沒有重新開機Master,是以BACKUP進行了搶占

Master:

1 2

# systemctl restart keepalived.service

# tail /var/log/messages

Nginx+Keepalived實作 轉載
1

# ip addr

Nginx+Keepalived實作 轉載

七、測試

浏覽器輸入www.zhi.com測試

Nginx+Keepalived實作 轉載

Master:

1

# nginx -s stop

Nginx+Keepalived實作 轉載

BackUp:

1

# ip addr

Nginx+Keepalived實作 轉載

虛拟IP位址已切換至備機了

重新整理www.zhi.com,可能反應較慢

Nginx+Keepalived實作 轉載

另:可配置nginx+keepalived主主模式,并在後端配置反向代理和負載均衡實作高可用

轉載于:https://www.cnblogs.com/liqing1009/p/4898547.html

繼續閱讀