网站架构负载均衡介绍
作用说明:
1)将前端用户并发访问压力分担 web01 web02 web03
2)将前端用户访问流量进行调度
实现负载均衡方法:
1)利用反向代理功能,实现负载均衡 nginx
特点:接收数据会经过反向代理服务处理后,再发送给后端
发送数据会经过反向代理服务处理后,再发送给用户
2)利用数据转发功能,实现负载均衡 lvs
特点:接收数据会经过数据转发服务器直接发送给后端
发送数据可以实现让web节点直接将数据发送给用户
补充:
反向代理:将外部用户请求可以代理进入到内部架构中 外(公网) --> 内web(私网)
正向代理:将内部用户请求可以代理出去到外部网络中 内(私网) --> 外网站(公网)
网站架构负载均衡部署
负载均衡服务器
第一个历程:安装反向代理服务
[root@lb01 ~]# vim /etc/yum.repos.d/nginx.repo
[root@web01 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@lb01 ~]# yum install -y nginx
第二个历程:编写nginx代理服务配置文件 vim proxy.conf
upstream web {
server 192.168.200.7:80;
#server 192.168.200.8:80;
#server 192.168.200.9:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
}
}
web集群节点:
web01:加载index.html
echo "www.nana.com web01" >/html/www/index.html
echo "bbs.nana.com web01" >/html/bbs/index.html
echo "blog.nana.com web01" >/html/blog/index.html
sed -i 's#index.php#index.html#g' /etc/nginx/conf.d/{default,bbs,blog}.conf
web02:加载index.html
echo "www.nana.com web02" >/html/www/index.html
echo "bbs.nana.com web02" >/html/bbs/index.html
echo "blog.nana.com web02" >/html/blog/index.html
web03:加载index.html
echo "www.nana.com web03" >/html/www/index.html
echo "bbs.nana.com web03" >/html/bbs/index.html
echo "blog.nana.com web03" >/html/blog/index.html
补充:创建web集群节点,需要保证三点信息一致
- 部署web服务程序一致(版本)
- 部署web服务配置一致
- 部署web服务代码一致
测试验证过程:
负载均衡上测试:
[root@lb01 conf.d]# curl -H host:www.nana.com 192.168.200.7
www.nana.com web01
[root@lb01 conf.d]# curl -H host:bbs.nana.com 192.168.200.7
bbs.nana.com web01
[root@lb01 conf.d]# curl -H host:blog.nana.com 192.168.200.7
blog.nana.com web01
用户访问负载均衡测试
做好域名解析
负载均衡调度算法介绍说明
轮训调度算法:rr — 将用户发出的请求平均分配到每一个web节点上 默认
权重轮询算法:wrr — 按照一定比例分配用户请求流量信息
[root@lb01 conf.d]# vim proxy.conf
upstream web {
server 192.168.200.7:80 weight=4; ---权重分配
#server 192.168.200.8:80 weight=1;
#server 192.168.200.9:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
}
}
避免会话中断算法: ip_hash — 定义静态调度算法 (容易造成负载不均)
定义最小的连接数: least_com — 可以避免单个web节点压力过高
max_fails=3 —最大失败次数信息 == 重试连接次数
作用:1)避免负载不均情况 2)做健康检查
fail_timeout=10s — 在一段时间之后,对异常节点进行再次测试
补充:50x状态码说明:
502:表示后端web节点无法接收到负载均衡请求(网络问题/负载过高)
关注后端节点状态信息
500:代码配置文件有异常
504:访问过程超时了 (网络相关/代码相关)
backup — 负载均衡热备参数(所有节点都出现故障,才会使用热备节点)
负载均衡反向代理配置参数
proxy_set_header:可以修改负载均衡向后端web节点访问过程中的HTTP请求报文信息
问题一:如何实现通过负载均衡访问不同的后端网站页面
location / {
proxy_pass http://web;
proxy_set_header Host $host;
}
}
问题二:日志文件中如何记录客户端真实IP地址
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
[root@web01 conf.d]# tail -f /var/log/nginx/access_www.log
192.168.200.5 - - [14/Apr/2022:09:42:16 +0800] "GET / HTTP/1.1" 200 19 "-" "curl/7.29.0" "-"
192.168.200.5 - - [14/Apr/2022:09:42:19 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0" "-"
192.168.200.5 - - [14/Apr/2022:09:52:20 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0" "192.168.200.1"
192.168.200.5 - - [14/Apr/2022:09:52:20 +0800] "GET / HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0" "192.168.200.1
proxy_next_upstream:当网站页面出现错误时,可以将访问请求切换到下一个节点进行访问
PS:nginx反向代理监控检查功能只能对网络通讯状态做健康检查,不能对页面访问情况做检查
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://web;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_403 http_404 http_429;
}
}
负载均衡企业应用配置介绍
实现动静分离访问过程
第一个历程:搭建web集群环境:
196.128.200.7 html/www/staic目录中数据 static集群
[root@web01 www]# mkdir /html/www/staic/
[root@web01 www]# echo "web01 www.nana.com-static" >/html/www/staic/index.html
196.128.200.8 html/www/upload目录中数据 upload集群
[root@web02 conf.d]# mkdir /html/www/upload -p
[root@web02 conf.d]# echo "web02 www.nana.com-upload" >/html/www/upload/index.html
196.128.200.9 html/www default集群
[root@web03 ~]# mkdir /html/www -p
[root@web03 ~]# echo "web01 www.nana.com-static" >/html/www/staic/index.html
-bash: /html/www/staic/index.html: 没有那个文件或目录
[root@web03 ~]# echo "web03 www.nana.com-default" >/html/www/index.html
第二个历程:编写负载均衡配置文件
upstream static {
server 192.168.200.7:80;
}
upstream upload {
server 192.168.200.8:80;
}
upstream default {
server 192.168.200.9:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_403 http_404 http_429;
}
location /static/ {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_403 http_404 http_429;
}
location /upload/ {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_403 http_404 http_429;
}
}
实现不同UA访问页面效果
第一个历程:搭建web集群环境:
192.168.200.7 手机端访问集群节点 手机页面信息 mobile集群
echo "moblie web page" >/html/www/index.html
192.168.200.8 PC端访问集群节点 PC端页面信息 pc集群
echo "PC web page" >/html/www/index.html
第二个历程:编写负载均衡配置文件
upstream mobile {
server 192.168.200.7:80;
}
upstream PC {
server 192.168.200.8:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_403 http_404 http_429;
if ($http_user_agent ~* "chrome|msie|firefox") {
proxy_pass http://pc;
}
if ($http_user_agent ~* "iphone|android") {
proxy_pass http://mobile;
}
}
}
网站架构代码上线部署(LNMU-Python)
第一个历程:解决软件服务依赖问题:
yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel gcc gcc-c++ zlib-devel python-devel sqlite-
第二个历程:升级系统python环境
wget https://www.python.org/ftp/python/3.8.5/Python-3.8.5.tgz
tar xf Python-3.8.5.tgz
cd Python-3.8.5
./configure --prefix=/usr/local/python3
make && make install
ln -sf /usr/local/python3/bin/python3 /usr/bin/python
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
vim /usr/bin/yum
#!/usr/bin/python 改变为 /usr/bin/python2.7
vim /usr/libexec/urlgrabber-ext-down
#!/usr/bin/python 改变为 /usr/bin/python2.7
第三个历程:下载uwsgi+django服务程序
pip3 install uwsgi
pip install django==2.2
第四个历程:上传代码文件到指定目录中
rz -y --> BBS.tar.gz
第五个历程:编写服务程序配置文件
nginx服务程序:
server {
listen 80;
server_name python.nana.com;
client_max_body_size 100M;
location /static {
alias /html/BBS/static/;
}
location /media {
alias /html/BBS/media;
}
location / {
index index.html;
include uwsgi_params;
uwsgi_pass 127.0.0.1:9999; ---动态请求发送给uwsgi服务程序
uwsgi_param UWSGI_SCRIPT BBS.wsgi;
uwsgi_param UWSGI_CHDIR /code/BBS;
}
}
uwsgi服务配置:
pip3 install -i https:pypi.doubanio.com/simple/ -r /html/BBS/requirement.txt ---项目环境依赖软件部署好
vim /html/BBS/BBS/uwsgi.ini
[uwsgi]
master = true
uid = root
gid = root
chdir = /html/BBS/ ---指定站点目录
processes = 3
socket = 127.0.0.1:9999 ---指定uwdgi服务程序监听地址和端口
pidfile = /tmp/BBS.pid ---指定服务器生成pid文件路径
daemonize = /html/BBS/logs/BBS.log ---生成服务日志文件
wsgi-file = BBS/wsgi.py ---加载wsgi.py文件 实现管理控制django程序服务
数据库操作配置:
数据导入恢复过程:
MariaDB [(none)]> create database bbs charset utf8mb4;
MariaDB [(none)]> use bbs
MariaDB [(none)]> source /code/BBS/bbs.sql ---开发人员写好 导入
编写代码文件和数据建立连接:
vim /code/BBS/BBS/settins.py
DATABASES = {
'default': {
'ENGINE' : 'django.db.backends.mysql',
'NAME' : 'bbs',
'HOST' : "127.0.0.1",
'USER' : 'root',
'PASSWORD' : '',
'PORT' : 3306,
}
}