Nginx+keepalived負載均衡高可用
阿新 • • 發佈:2022-05-18
Nginx+keepalived負載均衡高可用
結構圖
環境:
主 伺服器:192.168.239.10
備 伺服器:192.168.239.20
Web 伺服器1:192.168.239.40
Web 伺服器2:192.168.239.30
vip:192.168.239.100
客戶端:192.168.239.200
配置兩臺節點web伺服器(192.168.239.30、192.168.239.40 )
配置第一臺節點伺服器(192.168.239.30)
這裡我用的tomcat作為web伺服器當然也可以用nginx與apache作為web伺服器
#首先將tomcat所需軟體包拉入opt目錄並關閉防火牆 systemctl stop firewalld systemctl disable firewalld setenforce 0 #安裝jdk cd /opt rpm -ivh jdk-8u201-linux-x64.rpm #設定jdk的環境變數 vim /etc/profile.d/java.sh export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64 export JRE_HOME=$JAVA_HOME/jre export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH source /etc/profile.d/java.sh java -version #安裝tomcat tar zxvf apache-tomcat-8.5.16.tar.gz mkdir /usr/local/tomcat #建立目錄作為tomcat配置配置路徑 mv apache-tomcat-8.5.16 /usr/local/tomcat/ #配置tomcat環境變數 vim /etc/profile.d/tomcat.sh export CATALINA_HOME1=/usr/local/tomcat/tomcat export CATALINA_BASE1=/usr/local/tomcat/tomcat export TOMCAT_HOME1=/usr/local/tomcat/tomcat source /etc/profile.d/tomcat.sh #修改 tomcat1 例項中的 startup.sh 和 shutdown.sh 檔案,新增 tomcat 環境變數 vim /usr/local/tomcat/apache-tomcat-8.5.16/bin/startup.sh ----------------------------------------------------------------------------- export CATALINA_BASE=$CATALINA_BASE export CATALINA_HOME=$CATALINA_HOME export TOMCAT_HOME=$TOMCAT_HOME vim /usr/local/tomcat/apache-tomcat-8.5.16/bin/shutdown.sh ----------------------------------------------------------------------------- export CATALINA_BASE=$CATALINA_BASE export CATALINA_HOME=$CATALINA_HOME export TOMCAT_HOME=$TOMCAT_HOME #開啟服務到此tomcat就安裝好了 /usr/local/tomcat/apache-tomcat-8.5.16/bin/startup.sh #新增站點目錄檔案 mkdir /usr/local/tomcat/apache-tomcat-8.5.16/webapps/test vim /usr/local/tomcat/apache-tomcat-8.5.16/webapps/test/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("this is dynamic web1");%> </body> </html> #由於主機名 name 配置都為 localhost,需要刪除前面的 HOST 配置 vim /usr/local/tomcat/apache-tomcat-8.5.16/conf/server.xml <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false"> <Context docBase="/usr/local/tomcat/apache-tomcat-8.5.16/webapps/test/" path="" reloadable="true" /> #開啟服務 /usr/local/tomcat/apache-tomcat-8.5.16/bin/shutdown.sh /usr/local/tomcat/apache-tomcat-8.5.16/bin/startup.sh
配置第二臺節點伺服器(192.168.239.40)
配置與第一臺tomcat一致唯一不同站點目錄網頁文件用於區分實現負載均衡
vim /usr/local/tomcat/apache-tomcat-8.5.16/webapps/test/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test2 page</title> </head> <body> <% out.println("this is dynamic web2");%> </body> </html>
訪問測試
配置負載排程器(主:192.168.239.10)
#關閉防火牆 systemctl stop firewalld.service setenforce 0 #安裝相關依賴包 yum -y install pcre-devel zlib-devel gcc gcc-c++ make #建立執行使用者 useradd -M -s /sbin/nologin nginx #安裝nginx cd /opt tar zxvf nginx-1.12.0.tar.gz -C /opt/ cd nginx-1.12.0/ ./configure --prefix=/usr/local/nginx --user=nginx --grou=nginx --with-http_stub_status_module --with-stream && make && make install #優化路徑 ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #將nginx加入服務並給執行許可權 vim /lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target chmod 754 /lib/systemd/system/nginx.service #開啟服務 systemctl start nginx.service systemctl enable nginx.service #配置四層代理 vim /usr/local/nginx/conf/nginx.conf stream { log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent'; access_log logs/stream_access.log main; upstream backend_server { #新增節點伺服器地址 server 192.168.239.30:8080; server 192.168.239.40:8080; } server { listen 8080; #監聽埠 proxy_pass backend_server; } } systemctl restart nginx #安裝keepalived yum install -y keepalved cd /etc/keepalived #修改配置檔案 vim keepalived.conf #定義全域性引數 global_defs { notification_email { [email protected] [email protected] [email protected] } notification_email_from [email protected] smtp_server 172.0.0.1 #修改,郵件服務指向本地 smtp_connect_timeout 30 router_id NGINX_01 #指定伺服器(路由器)的名稱,主備伺服器名稱須不同,主為LVS_01,備為LVS_02 } vrrp_instance VI_1 { #定義VRRP熱備例項引數 state MASTER #指定熱備狀態,主為MASTER,備為BACKU interface ens33 #修改,指定承載vip地址的物理介面 virtual_router_id 51 #修改,指定虛擬路由器的ID號,每個熱備組保持一致 priority 100 #指定優先順序數值越大優先順序越高 advert_int 1 #通告間隔秒數(心跳頻率) authentication { #定義認證資訊,每個熱備組保持一致 auth_type PASS #認證型別 auth_pass 1111 #修改,指定驗證密碼,主備伺服器保持一致 } virtual_ipaddress { #指定群集vip地址 192.168.239.100 } } #編寫服務控制簡本當nginx服務掛掉是自動關閉keepalved服務並給指令碼許可權 vim check_nginx.sh #!/bin/bash count=$(ps -ef | grep nginx | egrep -cv "grep"|$$) if [ $count -eq 0 ];then systemctl stop keepalived fi chmod +x check_nginx.sh #修改配置檔案 vim keepalived.conf #檢測指令碼 vrrp_script check_nginx { script "/etc/keepalived/check_nginx.sh" #心跳執行的指令碼,檢測nginx是否啟動 track_script { #呼叫檢測指令碼 check_nginx } systemctl start keepalived.service ip addr
配置負載排程器(備:192.168.239.10)
#關閉防火牆
systemctl stop firewalld.service
setenforce 0
#yum安裝nginx
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
yum install nginx -y
#配置四層代理
cd /etc/nginx/
vim nginx.conf
stream {
log_format main '$remote_addr $upstream_addr - [$time_local] $status #upstream_bytes_sent';
access_log logs/stream_access.log main;
upstream backend_server {
server 192.168.239.30:8080; #新增節點伺服器
server 192.168.239.40:8080;
}
server {
listen 8080; #監聽地址
proxy_pass backend_server;
}
}
#建立日誌檔案目錄
mkdir logs
#開啟服務
systemctl restart nginx
#安裝keepalived服務
yum install -y leepalived
#修改配置檔案與主差不多隻要在其基礎修改幾處即可
cd /etc/keepalived
vim keepalived.conf
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 172.0.0.1
smtp_connect_timeout 30
router_id NGINX_02 #修改此處指定伺服器(路由器)的名稱,主備伺服器名稱須不同,
}
vrrp_instance VI_1 {
state BACKUP #指定熱備狀態,主為MASTER,備為BACKU
interface ens33
virtual_router_id 51
priority 90 #修改此行指定優先順序數值越大優先順序越高
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.239.100
}
}
#編寫服務控制簡本當nginx服務掛掉是自動關閉keepalved服務並給指令碼許可權
vim check_nginx.sh
#!/bin/bash
count=$(ps -ef | grep nginx | egrep -cv "grep"|$$)
if [ $count -eq 0 ];then
systemctl stop keepalived
fi
chmod +x check_nginx.sh
vim keepalived.conf
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh" #心跳執行的指令碼,檢測nginx是否啟動
track_script {
check_nginx #呼叫檢測指令碼
}
systemctl start keepalived.service
客戶機測試
關閉主負載排程器的keepalived服務再次測試
此時備負載排程器則會將自己轉為Master裝置
再次訪問任然可以訪問