用NGINX做负载均衡,keepalived高可用
實驗環境,四臺虛擬機,兩臺做負載均衡,兩臺做RS
IP地址:兩臺負載均衡分別為:10.0.0.7;10.0.0.8(高可用keepalived)
兩臺 RS主機地址為: 10.0.0.9;10.0.0.10
系統:centos6.6
介紹說明
實現Nginx負載均衡的組件主要有兩個,
ngx_http_proxy_module proxy代理模塊,用于把請求拋給服務器節點或者upstream服務池
ngx_http_unpstream_module 負載均衡模塊,可以實現網站的負載均衡功能以及節點的健康檢查
其中安裝過程如下,
#安裝Nginx需要的依賴包 yum -y install openssl openssl-devel pcre pcre-devel #下載Nginx源碼包 wget -q http://nginx.org/download/nginx-1.6.3.tar.gz #解壓Nginx源碼包 tar xvf nginx-1.6.3.tar.gz #進入解壓之后的Nginx目錄 cd nginx-1.6.3 #創建Nginx的組groupadd nginx #創建Nginx的用戶,并且不允許登錄操作系統 useradd -s /sbin/nologin -g nginx nginx #進行編譯 ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.6.3 --with-http_stub_status_module --with-http_ssl_module #編譯后安裝 make && make install #創建一個軟連接 ln -s /usr/local/nginx-1.6.3/sbin/nginx /etc/init.d/nginx啟動Nginx服務
/usr/local/nginx-1.6.3/sbin/nginx -c /usr/local/nginx-1.6.3/conf/nginx.conf
添加80端口到防火墻,被允許訪問
sed -i ‘10i -A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT’ /etc/sysconfig/iptables
重啟防火墻
/etc/init.d/iptables restart
其中,兩臺RS的nginx.conf配置如下:
#Nginx的進程數 worker_processes 1; events {worker_connections 1024; } #主配置文件 http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for" '; #進行虛擬主機等配置的模塊server {listen 80;server_name bbs.etiantian.org;location / {root html/bbs;index index.html index.htm;}access_log logs/access_bbs.log main;}server {listen 80;server_name www.etiantian.org;location / {root html/www;index index.html index.htm;}access_log logs/access_bbs.log main;} }然后分別在兩臺上執行以下命令
[root@web01 ~]# mkdir /usr/local/nginx-1.6.3/html/{www,bbs} [root@web01 ~]# for dir in www bbs;do echo "`ifconfig eth0|grep -o "10.0.0.[109]."` $dir " > /usr/local/nginx-1.6.3/html/$dir/index.html;done [root@web01 ~]# for dir in www bbs;do cat /usr/local/nginx-1.6.3/html/$dir/index.html ;doneweb02主機上執行以下
[root@web01 ~]# mkdir /usr/local/nginx-1.6.3/html/{www,bbs} [root@web02 ~]# for dir in www bbs;do echo "`ifconfig eth0|grep -o "10.0.0.[109]."` $dir " > /usr/local/nginx-1.6.3/html/$dir/index.html;done [root@web02 ~]# for dir in www bbs;do cat /usr/local/nginx-1.6.3/html/$dir/index.html ;done然后在主備負載均衡器:10.0.0.7,8兩臺機器上配置nginx.conf文件
[root@lb01 ~]# vim /usr/local/nginx-1.6.3/conf/nginx.conf worker_processes 1;events {worker_connections 1024; }http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65; #定義web服務池,其中包含了兩個節點upstream www_server_pools {server 10.0.0.9:80 weight=1;server 10.0.0.10:80 weight=1;}server {listen 80;server_name www.etiantian.org;location / { #訪問web服務器池的節點proxy_pass http://www_server_pools;}}}測試
由于我本實驗沒有dns域名服務器解析IP地址,所以我們得要在hosts文件里面添加ip和對應的域名
首先在兩臺RS/etc/hosts分別加入
然后在Nginx主負載均衡服務器上/etc/hosts
10.0.0.7 www.etiantian.orgVRRP是虛擬路由冗余協議,它是為了解決靜態路由的單點故障的
VRRP是通過一種競選協議機制來將路由任務交給某臺VRRP路由器的
VRRP用IP多播的方式實現高可用之間的通信
VRRP工作是主節點發包,備節點接包,檔備節點收不到主節點發的數據包的時候,就啟動接管程序接管主節點的資源。備節點可以有很多個,通過優先級競選,但一般keepalive系統運維中都是一對存在的
開始安裝keepalived軟件
yum -y install keepalived
/etc/init.d/keepalived start
修改配置文件
主節點
1 ! Configuration File for keepalived
2
3 global_defs {
4 notification_email {
5 919497370@qq.com
6 #failover@firewall.loc
7 #sysadmin@firewall.loc
8 }
9 notification_email_from Alexandre.Cassen@firewall.loc
10 smtp_server smtp.qq.com
11 smtp_connect_timeout 30
12 router_id lb01
13 }
14
15 vrrp_instance VI_1 {
16 state MASTER
17 interface eth0
18 virtual_router_id 55
19 priority 150
20 advert_int 1
21 authentication {
22 auth_type PASS
23 auth_pass 1111
24 }
25 virtual_ipaddress {
26 #192.168.200.16
27 #192.168.200.17
28 #192.168.200.18
29 10.0.0.12/24 dev eth0 label eth0:1
30 }
31 }
備節點
1 ! Configuration File for keepalived23 global_defs {4 notification_email {5 919497370@qq.com6 #failover@firewall.loc7 #sysadmin@firewall.loc8 }9 notification_email_from Alexandre.Cassen@firewall.loc10 smtp_server smtp.qq.com11 smtp_connect_timeout 3012 router_id lb0213 }1415 vrrp_instance VI_1 {16 state BACKUP17 interface eth018 virtual_router_id 5519 priority 10020 advert_int 121 authentication {22 auth_type PASS23 auth_pass 111124 }25 virtual_ipaddress {26 #192.168.200.1627 #192.168.200.1728 #192.168.200.1829 10.0.0.12/24 dev eth0 label eth0:130 }31 }32轉載于:https://www.cnblogs.com/sujc-blogs/p/9722485.html
總結
以上是生活随笔為你收集整理的用NGINX做负载均衡,keepalived高可用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: anaconda下安装新包一直报错(‘p
- 下一篇: Codeforces Gym101518