Nginx、haproxy实现负载均衡 (rockylinux8.6+nginx1.20.2、rockylinux8.6+haproxy2.7)
时间:2023-03-01 18:50 来源:linux.it.net.cn 作者:IT
序号 主机 系统 作用 备注
1 stu1:192.168.3.81 rockylinux8.6最小化安装 nginx负载
2 stu2:192.168.3.82 rockylinux8.6最小化安装 real server
3 stu3:192.168.3.83 rockylinux8.6最小化安装 real server
一、基础环境
1、关闭selinux及设置防火墙
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
reboot
2、修改/etc/hosts文件
echo '192.168.3.81 stu1' >> /etc/hosts
echo '192.168.3.82 stu2' >> /etc/hosts
echo '192.168.3.83 stu3' >> /etc/hosts
3、配置yum源
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
-i.bak \
/etc/yum.repos.d/Rocky-*.repo
dnf makecache
4、安装基础软件包
yum install -y vim net-tools bash-completion
一、安装nginx(stu1)
1、安装依赖包
yum install gcc gcc-c++ openssl openssl-devel pcre pcre-devel zlib zlib-devel make -y
2、安装nginx
wget https://nginx.org/download/nginx-1.20.2.tar.gz
tar -xvf nginx-1.20.2.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.20.2/
./configure --prefix=/usr/local/nginx --with-http_dav_module \
--with-http_stub_status_module --with-http_addition_module \
--with-http_sub_module --with-http_flv_module --with-http_mp4_module
make -j 4
make install
useradd -u 8000 -s /sbin/nologin nginx
/usr/local/nginx/sbin/nginx
netstat -antup | grep nginx
访问 http://192.168.3.81
nginx操作
/usr/local/nginx/sbin/nginx -t 检查配置文件
/usr/local/nginx/sbin/nginx -s reload 重新加载配置文件
/usr/local/nginx/sbin/nginx -s stop 关闭nginx
3、配置nginx分发器
cat > /usr/local/nginx/conf/nginx.conf << EOF
user nginx nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
if (\$request_uri ~* \.html$){
proxy_pass http://htmlservers;
}
if (\$request_uri ~* \.php$){
proxy_pass http://phpservers;
}
proxy_pass http://picservers;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upstream htmlservers {
server 192.168.3.82:80;
server 192.168.3.83:80;
}
upstream phpservers{
server 192.168.3.82:80;
server 192.168.3.83:80;
}
upstream picservers {
server 192.168.3.82:80;
server 192.168.3.83:80;
}
}
EOF
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
二、安装http+php
1、安装stu2
yum install httpd php -y
echo 'stu2' > /var/www/html/index.html
cat > /var/www/html/stu.php << EOF
stu2.cn-php
<?php
phpinfo();
?>
EOF
注:上传图片重命名至 /var/www/html/stu.jpg
systemctl start httpd
2、安装stu3
yum install httpd php -y
echo 'stu3' > /var/www/html/index.html
cat > /var/www/html/stu.php << EOF
stu3.cn-php
<?php
phpinfo();
?>
EOF
注:上传图片重命名至 /var/www/html/stu.jpg
systemctl start httpd
3、测试访问
http://192.168.3.82
http://192.168.3.82/stu.php
http://192.168.3.82/stu.jpg
http://192.168.3.83
http://192.168.3.83/stu.php
http://192.168.3.83/stu3.jpg
三、测试nginx负载
1、负载测试
http://192.168.3.81
刷新
http://192.168.3.81/stu.php
刷新
http://192.168.3.81/stu.jpg
刷新
停止stu2测试
systemctl stop httpd
http://192.168.3.81/stu.php
http://192.168.3.81/stu.jpg
全部都为stu3
stu2
systemctl start httpd
注:nginx负载策略
轮询
upstream backserver {
server 192.168.3.82;
server 192.168.3.83;
}
权重
upstream backserver {
server 192.168.3.82 weight=1;
server 192.168.3.83 weight=2;
}
IP_HASH
upstream backserver {
ip_hash;
server 192.168.3.82;
server 192.168.3.83;
}
2、性能测试
stu2
ab -n 1000 -c 1000 http://192.168.3.83/index.html
ab -n 2000 -c 2000 http://192.168.3.83/index.html
ulimit -a
ulimit -n
ulimit -n 10240
ab -n 2000 -c 2000 http://192.168.3.83/index.html
3、停止nginx代理服务(stu1)
/usr/local/nginx/sbin/nginx -s stop
四、haproxy负载搭建(stu1)
1、搭建
wget https://github.com/haproxy/haproxy/archive/refs/tags/v2.7-dev7.tar.gz
tar -xvf haproxy-2.7-dev7.tar.gz
cd haproxy-2.7-dev7/
uname -r
make TARGET=linux4180 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
ls /usr/local/haproxy/
mkdir /usr/local/haproxy/etc
id nobody
cat > /usr/local/haproxy/etc/haproxy.cfg << EOF
global
log 127.0.0.1 local0
maxconn 4096
chroot /usr/local/haproxy
uid 65534 #所属运行的用户uid
gid 65534 #所属运行的用户组
daemon #以后台形式运行haproxy
pidfile /usr/local/haproxy/run/haproxy.pid #将所有进程写入pid文件
defaults
log global
log 127.0.0.1 local3 #日志文件的输出定向。产生的日志级别为local3. 系统中local1-7,用户自己定义
mode http #工作模式,默认采用http模式,可配置成tcp作4层消息转发
option httplog #日志类别,记载http日志
option httpclose #每次请求完毕后主动关闭http通道
option dontlognull #不记录空连接,产生的日志
option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch #当serverid对应的服务器异常,强制定向到其他健康服务器
retries 2 #2次连接失败就认为服务器不可用,主要通过后面的check检查
maxconn 2000 #最大连接数
balance roundrobin #负载均衡算法
stats uri /haproxy-stats #haproxy 监控页面的访问地址 # 可通过 http://localhost:80/haproxy-stats 访问
timeout connect 5000 #连接超时时间。 单位:ms 毫秒
timeout client 50000 #客户端连接超时时间
timeout server 50000 #服务器端连接超时时间
mode http
option httpchk GET /index.html #健康检测
frontend http #前端配置,http名称可自定义
bind 0.0.0.0:80 #发起http请求80端口,会被转发到设置的ip及端口
default_backend http_back #转发到后端 写上后端名称
backend http_back #后端配置,名称上下关联
server s1 192.168.3.82:80 weight 3 check #后端的主机 IP &权衡
server s2 192.168.3.83:80 weight 3 check #后端的主机 IP &权衡
EOF
echo 'local3.* /var/log/haproxy.log' >> /etc/rsyslog.conf
echo 'local0.* /var/log/haproxy.log' >> /etc/rsyslog.conf
systemctl restart rsyslog
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
ps -axu | grep haproxy
netstat -antup | grep 80
2、测试
http://192.168.3.81/haproxy-stats
http://192.168.3.81
刷新
http://192.168.3.81/stu.php
(责任编辑:IT)
序号 主机 系统 作用 备注
1 stu1:192.168.3.81 rockylinux8.6最小化安装 nginx负载
2 stu2:192.168.3.82 rockylinux8.6最小化安装 real server
3 stu3:192.168.3.83 rockylinux8.6最小化安装 real server
一、基础环境
1、关闭selinux及设置防火墙
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
reboot
2、修改/etc/hosts文件
echo '192.168.3.81 stu1' >> /etc/hosts
echo '192.168.3.82 stu2' >> /etc/hosts
echo '192.168.3.83 stu3' >> /etc/hosts
3、配置yum源
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
-i.bak \
/etc/yum.repos.d/Rocky-*.repo
dnf makecache
4、安装基础软件包
yum install -y vim net-tools bash-completion
一、安装nginx(stu1)
1、安装依赖包
yum install gcc gcc-c++ openssl openssl-devel pcre pcre-devel zlib zlib-devel make -y
2、安装nginx
wget https://nginx.org/download/nginx-1.20.2.tar.gz
tar -xvf nginx-1.20.2.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.20.2/
./configure --prefix=/usr/local/nginx --with-http_dav_module \
--with-http_stub_status_module --with-http_addition_module \
--with-http_sub_module --with-http_flv_module --with-http_mp4_module
make -j 4
make install
useradd -u 8000 -s /sbin/nologin nginx
/usr/local/nginx/sbin/nginx
netstat -antup | grep nginx
访问 http://192.168.3.81
nginx操作
/usr/local/nginx/sbin/nginx -t 检查配置文件
/usr/local/nginx/sbin/nginx -s reload 重新加载配置文件
/usr/local/nginx/sbin/nginx -s stop 关闭nginx
3、配置nginx分发器
cat > /usr/local/nginx/conf/nginx.conf << EOF
user nginx nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
if (\$request_uri ~* \.html$){
proxy_pass http://htmlservers;
}
if (\$request_uri ~* \.php$){
proxy_pass http://phpservers;
}
proxy_pass http://picservers;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upstream htmlservers {
server 192.168.3.82:80;
server 192.168.3.83:80;
}
upstream phpservers{
server 192.168.3.82:80;
server 192.168.3.83:80;
}
upstream picservers {
server 192.168.3.82:80;
server 192.168.3.83:80;
}
}
EOF
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
二、安装http+php
1、安装stu2
yum install httpd php -y
echo 'stu2' > /var/www/html/index.html
cat > /var/www/html/stu.php << EOF
stu2.cn-php
<?php
phpinfo();
?>
EOF
注:上传图片重命名至 /var/www/html/stu.jpg
systemctl start httpd
2、安装stu3
yum install httpd php -y
echo 'stu3' > /var/www/html/index.html
cat > /var/www/html/stu.php << EOF
stu3.cn-php
<?php
phpinfo();
?>
EOF
注:上传图片重命名至 /var/www/html/stu.jpg
systemctl start httpd
3、测试访问
http://192.168.3.82
http://192.168.3.82/stu.php
http://192.168.3.82/stu.jpg
http://192.168.3.83
http://192.168.3.83/stu.php
http://192.168.3.83/stu3.jpg
三、测试nginx负载
1、负载测试
http://192.168.3.81
刷新
http://192.168.3.81/stu.php
刷新
http://192.168.3.81/stu.jpg
刷新
停止stu2测试
systemctl stop httpd
http://192.168.3.81/stu.php
http://192.168.3.81/stu.jpg
全部都为stu3
stu2
systemctl start httpd
注:nginx负载策略
轮询
upstream backserver {
server 192.168.3.82;
server 192.168.3.83;
}
权重
upstream backserver {
server 192.168.3.82 weight=1;
server 192.168.3.83 weight=2;
}
IP_HASH
upstream backserver {
ip_hash;
server 192.168.3.82;
server 192.168.3.83;
}
2、性能测试
stu2
ab -n 1000 -c 1000 http://192.168.3.83/index.html
ab -n 2000 -c 2000 http://192.168.3.83/index.html
ulimit -a
ulimit -n
ulimit -n 10240
ab -n 2000 -c 2000 http://192.168.3.83/index.html
3、停止nginx代理服务(stu1)
/usr/local/nginx/sbin/nginx -s stop
四、haproxy负载搭建(stu1)
1、搭建
wget https://github.com/haproxy/haproxy/archive/refs/tags/v2.7-dev7.tar.gz
tar -xvf haproxy-2.7-dev7.tar.gz
cd haproxy-2.7-dev7/
uname -r
make TARGET=linux4180 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
ls /usr/local/haproxy/
mkdir /usr/local/haproxy/etc
id nobody
cat > /usr/local/haproxy/etc/haproxy.cfg << EOF
global
log 127.0.0.1 local0
maxconn 4096
chroot /usr/local/haproxy
uid 65534 #所属运行的用户uid
gid 65534 #所属运行的用户组
daemon #以后台形式运行haproxy
pidfile /usr/local/haproxy/run/haproxy.pid #将所有进程写入pid文件
defaults
log global
log 127.0.0.1 local3 #日志文件的输出定向。产生的日志级别为local3. 系统中local1-7,用户自己定义
mode http #工作模式,默认采用http模式,可配置成tcp作4层消息转发
option httplog #日志类别,记载http日志
option httpclose #每次请求完毕后主动关闭http通道
option dontlognull #不记录空连接,产生的日志
option forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
option redispatch #当serverid对应的服务器异常,强制定向到其他健康服务器
retries 2 #2次连接失败就认为服务器不可用,主要通过后面的check检查
maxconn 2000 #最大连接数
balance roundrobin #负载均衡算法
stats uri /haproxy-stats #haproxy 监控页面的访问地址 # 可通过 http://localhost:80/haproxy-stats 访问
timeout connect 5000 #连接超时时间。 单位:ms 毫秒
timeout client 50000 #客户端连接超时时间
timeout server 50000 #服务器端连接超时时间
mode http
option httpchk GET /index.html #健康检测
frontend http #前端配置,http名称可自定义
bind 0.0.0.0:80 #发起http请求80端口,会被转发到设置的ip及端口
default_backend http_back #转发到后端 写上后端名称
backend http_back #后端配置,名称上下关联
server s1 192.168.3.82:80 weight 3 check #后端的主机 IP &权衡
server s2 192.168.3.83:80 weight 3 check #后端的主机 IP &权衡
EOF
echo 'local3.* /var/log/haproxy.log' >> /etc/rsyslog.conf
echo 'local0.* /var/log/haproxy.log' >> /etc/rsyslog.conf
systemctl restart rsyslog
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/etc/haproxy.cfg
ps -axu | grep haproxy
netstat -antup | grep 80
2、测试
http://192.168.3.81/haproxy-stats
http://192.168.3.81
刷新
http://192.168.3.81/stu.php
|