负载均衡部署BBS
- 以web01,web02,web03,lb01为例
1. 部署后端服务
1.1 部署数据库(db01)
1、安装数据库
[root@db01 ~]
2、启动数据库
[root@db01 ~]
[root@db01 ~]
3、远程连接MySQL数据
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> CREATE DATABASE `bbs` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Query OK, 1 row affected (0.00 sec)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cad14473e7be58f8d8874ef7586b9e99.png)
1.2 部署Python(web01-03)
1、创建用户
[root@web01 opt]
[root@web01 opt]
2、安装依赖软件
[root@web01 opt]
1.3 部署Django(web01-03)
1、安装Django和uwsgi
[root@web01 opt]
[root@web01 opt]
[root@web01 opt]
2、创建项目
[root@web01 opt]
[root@web01 opt]
[root@web03 bbs]
[root@web03 bbs]
/opt/bbs
[root@web03 bbs]
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]
[root@web01 bbs]
[root@web01 bbs]
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2846bb189030fe9164da9c2ae5434953.png)
1.4 配置uwsgi(web01-03)
[root@localhost ~]
[uwsgi]
socket = :8000
chdir = /opt/bbs
wsgi-file = bbs/wsgi.py
module = bbs.wsgi
master = true
processes = 4
vacuum = true
1.5 配置nginx并启动(web01-03)
2. 编辑项目配置文件
[root@localhost ~]
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;
}
}
2. 启动uwsgi
[root@web01 bbs]
3. 重启Nginx配置
[root@web01 bbs]
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c92be1bf0e0ab629b9db6c8543b01885.png)
2. 部署lb01
[root@lb01 ~]
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 10s;
proxy_read_timeout 10s;
proxy_send_timeout 10s;
proxy_buffering on;
proxy_buffer_size 8k;
proxy_buffers 8 8k;
3. 部署负载均衡(lb01)
[root@lb01 conf.d]
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;
}
}