Nginx 四層代理
yum install nginx -y
yum install nginx-mod-stream -y
vim /etc/nginx/nginx.conf
編輯內容如下:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
load_module /usr/lib64/nginx/modules/ngx_stream_module.so;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
stream {
upstream lb {
server 172.16.1.5:3306;
}
log_format main '$remote_addr $remote_port - [$time_local] $status $protocol '
'"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"';
server {
listen 3306;
proxy_connect_timeout 60s;
proxy_timeout 60s;
proxy_pass lb;
# access_log logs/access.log main;
}
}
IPVS反向代理:
安裝IPVS核心模組
yum install ipvsadm
sudo modprobe ip_vs
setenforce 0
echo "1" > /proc/sys/net/ipv4/ip_forward
設定IPVS規則
sudo ipvsadm -A -t 192.168.0.1:80 -s rr
sudo ipvsadm -a -t 192.168.0.1:80 -r 192.168.0.2:80 -g
sudo ipvsadm -a -t 192.168.0.1:80 -r 192.168.0.3:80 -g
-m表示模式為NAT模式
-g標誌表示使用DR模式,即直接路由模式,它允許負載均衡器將流量直接轉發到後端伺服器
儲存IPVS規則
sudo ipvsadm-save > /etc/sysconfig/ipvsadm
應用IPVS規則
sudo systemctl restart ipvsadm
sudo apt-get install ipvsadm
新增LVS服務
ipvsadm -A -t 192.168.1.100:1521 -s rr
新增真實伺服器
ipvsadm -a -t 192.168.1.100:1521 -r 192.168.1.101:1521
ipvsadm -a -t 192.168.1.100:1521 -r 192.168.1.102:1521
設定連線持久化
ipvsadm -p 1000