负载均衡部署BBS

一:部署后端服务(web服务器)

  • 设备需求
lb服务器		192.168.15.5
web01服务器	192.168.15.7
web02服务器	192.168.15.8
web03服务器	192.168.15.9
db服务器		192.168.15.61(必不可少)
m01服务器		192.168.15.81

二:部署Python(web)

1、创建用户
[root@web01 opt]# groupadd django -g 888
[root@web01 opt]# useradd django -u 888 -g 888 -r -M -s /bin/sh

2、安装依赖软件
[root@web01 opt]# yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y

三:部署Django和uwsgi

3、安装Django和uwsgi
[root@web01 opt]# pip3 install django==1.11
[root@web01 opt]# pip3 install uwsgi
[root@web01 opt]# pip3 install pymysql


web01将bbs项目代码传给 另其他web服务器
[root@web01 opt]# scp bbs.tar.gz 172.16.1.9:/opt/

4、创建项目
解压

web02 wed03 服务器
cd /opt
[root@web02 opt]# tar -xf bbs.tar.gz


web服务器
[root@web03 opt]# cd bbs
[root@web03 bbs]# vim bbs/settings.py 
ALLOWED_HOSTS = ['*']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bbs',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '172.16.1.61',
        'PORT': 3306,
        'CHARSET': 'utf8'
    }
}

# 启动测试
[root@web01 bbs]# python3 manage.py runserver 0.0.0.0:8000

四:配置并启动

5、编辑项目配置文件
mkdir /opt/linux
[root@localhost ~]# vim /opt/bbs/myweb_uwsgi.ini 
[uwsgi]
# 端口号
socket            = :8000
# 指定项目的目录
chdir           = /opt/bbs
# wsgi文件路径
wsgi-file       = bbs/wsgi.py
# 模块wsgi路径
module          = bbs.wsgi
# 是否开启master进程
master          = true
# 工作进程的最大数目
processes       = 4
# 结束后是否清理文件
vacuum          = true

6、启动uwsgi
[root@web01 linux]# uwsgi -d --ini myweb_uwsgi.ini --uid 666

-d 	  : 以守护进程方式运行
--ini : 指定配置文件路径
--uid : 指定uid

TCP 服务

7、编辑Nginx配置文件
[root@localhost ~]# vim /etc/nginx/conf.d/python.conf 
server {
    listen 80;
    server_name py.test.com;
    location / { 
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
        uwsgi_read_timeout 2;
        uwsgi_param UWSGI_SCRIPT bbs.wsgi;
        uwsgi_param UWSGI_CHDIR /opt/bbs;
        index  index.html index.htm;
        client_max_body_size 35m;
    }
}

8、重启Nginx配置
systemctl restart nginx

五:部署负载均衡(lb01)

[root@lb01 conf.d]# vim python.conf 
upstream bbs {
    server 172.16.1.7:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.8:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.9:80 max_fails=3 fail_timeout=3s;
}

server {
    listen 80;
    server_name py.test.com;
    location / {
        proxy_pass http://bbs;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404; 
        include /etc/nginx/proxy_params;
    }
}

六:(web服务器 lb服务器)

1.测试
nginx -t
2.重启
systemctl restart nginx
3.DNS解析
192.168.15.5	py.test.com
4.网址测试
py.test.com

image

七:总结,负载均衡

负载均衡原理
负载均衡的意思就是有几台服务器或者几个服务,通过设备或者软件,将外部来的连接均匀的分配到这几个服务器或者服务上面,使服务器的负载平均,使外部访问基本达到快速。

1.客户端浏览网址 负载均衡代理
2.负载均衡将外部链接均匀的分配到web服务器上
3.使服务器到达负载均衡

tail -f /var/log/nginx/access.log

image

posted @ 2022-01-06 23:54  AlexEvans  阅读(72)  评论(0编辑  收藏  举报