搭建高可用负载均衡架构—QQ农场项目
1、事前准备
负载均衡调度器+keepalived
test-01:192.168.10.11
test-02:192.168.10.12
nginx+php
test-03:192.168.10.13
test-04:192.168.10.14
MySQL数据库
test-05:192.168.10.15
NFS网络共享存储
test-06:192.168.10.16
关闭防火墙与SELinux、关闭NetworkManager、关闭防火墙firewalld。
2、部署nginx(test-01、test-02、test-03、test-04)
1、 避免:Nginx 与 httpd 发生端口冲突
卸载:可能已经安装的 httpd,从而避免发生<端口冲突>
(rpm -qa | grep -P "^httpd-([0-9].)+") && rpm -e --nodeps httpd || echo "未安装"
2、 下载:Nginx 源码安装包,并解压
cd ~
which wget || yum install -y wget
wget http://nginx.org/download/nginx-1.19.7.tar.gz
3、安装其他依赖
yum install -y gcc pcre-devel zlib-devel
4、创建运行账户nginx
useradd -M -s /sbin/nologin nginx
2.3 安装nginx
-
解压nginx-xx.tar.gz包
tar -axf nginx-1.19.7.tar.gz
-
进入解压目录,执行./configure 设置安装路径和运行账户
cd ~/nginx-1.19.7 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
-
make&&make install
make && make install
-
配置网页
cat >/usr/local/nginx/conf/nginx.conf <<EOF worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; charset utf-8; server { listen 80; server_name localhost; include conf.d/*.conf; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } EOF #创建辅助配置文件目录 [ -d /usr/local/nginx/conf/conf.d ] || mkdir -p /usr/local/nginx/conf/conf.d
2.3 设置环境变量,同时设置开机自启
1、设置变量
cat > /etc/profile.d/nginx.sh<<EOF
export PATH="/usr/local/nginx/sbin:\$PATH"
EOF
2、刷新环境
source /etc/profile
3、启停:Nginx 服务进程
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local ## 设置:开机自启动
chmod +x /etc/rc.d/rc.local
nginx 或 nginx -c /usr/local/nginx/conf/nginx.conf ## 启动:Nginx 服务
2.4 访问
curl 192.168.10.11
curl 192.168.10.12
curl 192.168.10.13
curl 192.168.10.14
3、反向代理+负载均衡(test-01、test-02)
Nginx upstream 上游模块(负载均衡)
vim /usr/local/nginx/conf/nginx.conf
....
http {
upstream test { ##test是服务器池的命名,不要有下划线
server 192.168.10.13:80; ##上游服务器ip:端口
server 192.168.10.14:80;
}
...
}
Nginx proxy反向代理模块
vim /usr/local/nginx/conf/nginx.conf
...
location / {
proxy_pass http://test;
proxy_pass http://test;
}
...
保存后出去重载文件
nginx -s reload
4、安装部署Keepalived软件(test-01、test-02)
1、安装keepalived软件
yum install keepalived -y
设置Keepalived配置文件
vim /etc/keepalived/keepalived.conf
test-01操作
第一步:按冒号:进入末行模式输入set nu 使文件带行号显示,然后使用35gg切换光标到35行,然后按dG,删除35行以 后的所有内容
第二步:了解一下配置文件中每一行的含义19~34行
...
# vrrp_strict #将这个第十四行注释掉
...
vrrp_instance farm {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100
}
}
设置完成后,保存并退出
第三步:启动Keepalived软件
systemctl start keepalived
systemctl status keepalived
test-02操作
第一步:按冒号:进入末行模式输入set nu 使文件带行号显示,然后使用35gg切换光标到35行,然后按dG,删除35行以 后的所有内容
第二步:了解一下配置文件中每一行的含义19~34行
...
# vrrp_strict #将这个第十四行注释掉
...
vrrp_instance farm {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100
}
}
设置完成后,保存并退出
第三步:启动Keepalived软件
systemctl start keepalived
systemctl status keepalived
让Keepalived可以监测Nginx服务(test-01、test-02)
第一步:编写nginx.sh脚本,自动检测Nginx的运行状态
mkdir /scripts
vim /scripts/nginx.sh
#!/bin/bash
nginx_status=`ps -C nginx --no-header |wc -l`
if [ $nginx_status -eq 0 ]; then
systemctl stop keepalived
fi
第二步:给nginx.sh文件添加一个可执行权限
chmod +x /scripts/nginx.sh
第三步:在test-01、test-02两台机器的keepalived.conf文件中配置 nginx.sh脚本
vim /etc/keepalived/keepalived.conf
...
vrrp_script check_nginx {
script /scripts/nginx.sh ## 指定:<外部脚本>的<调用路径>
interval 1 ## 设置:<健康跟踪检测>的<时间间隔>,默认为 1 秒
}
...
以上只是在keepalived配置文件中定义了一个检测脚本,但是还没有真正的调用。
第四步:在vrrp_instance标签中真正调用check_nginx这个检测脚本
vrrp_instance VI_1 {
...
track_script {
check_nginx
}
...
}
第五步:设置完成后,重启keepalived
systemctl enable keepalived
systemctl restart keepalived
5、配置nginx+php(test-03、test-04)
1、安装php服务和插件
yum -y install php php-fpm php-curl php-intl php-mysql
2、修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
...
location / {
root /farm;
index index.php index.htm;
}
...
cat > /usr/local/nginx/conf/conf.d/farm.conf <<EOF
location ~ \.php$ {
root /farm;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
EOF
3、创建发布目录
mkdir /farm
4、配置我们的php服务
vim /etc/php.ini
#short_open_tag,将211行short_open_tag = Off 改为 on
5、重启服务
nginx -s reload
systemctl enable php-fpm
systemctl restart php-fpm
6、配置网络共享存储实现Web网页数据一致(test-06)
1、安装共享存储软件NFS
yum -y install nfs-utils rpcbind
2、启动软件&设置开机自启
systemctl enable nfs
systemctl start nfs
3、创建共享目录设置权限
mkdir /php
setfacl -R -m u:nfsnobody:rwx /php
4、将目录设置共享
echo '/php *(rw)' > /etc/exports.d/farm.exports
exportfs -r
systemctl restart nfs
5、Web机器下载nfs客户端(test-03、test-04)
yum -y install nfs-utils
6、挂载该项目目录(test-03、test-04)
mount 192.168.10.16:/php /farm
7、配置农场动态页面
1、下载老师发的QQ农场数据包并上传解压(test-03)
cd ~
yum -y install lrzsz ##然后将farm-ucenter1.5.zip包进行上传
yum -y install unzip
unzip farm-ucenter1.5.zip ##解压完会有一个upload目录
cp -r upload/* /farm
cd /farm
chmod 777 * -R
2、将/farm目录中qqfarm.sql文件通过scp命令发送给test-05的MySQL机器
scp /farm/qqfarm.sql 192.168.10.15:/root/
PS:解决报错问题
之前许多同学做错了导致数据库连接出问题,所以我们需要
1、删除/farm目录中所有的文件
rm -rf /farm/*
然后执行第七大点的操作
报错后数据库的解决办法在数据库节点下面
8、配置MySQL数据库(test-05)
1、MySQL的安装与初始化设置
wget https://dev.mysql.com/get/mysql80-community-release-el7-4.noarch.rpm
rpm -ivh mysql80-community-release-el7-4.noarch.rpm
#进入mysql的yum修改文件,打开5.7
vim /etc/yum.repos.d/mysql-community.repo
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch
enabled=1 # 将MySQL5.7的enable的值改为1,即为启动;0表示关闭
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch
enabled=0 # 将MySQL8.0的enable的值改为0,即为关闭;1表示启动
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
...
#安装mysql
yum -y install mysql-server mysql
systemctl restart mysqld
#设置MySQL登录密码
安装好后root的初始密码都在 /var/log/mysqld.log 文件中
grep password /var/log/mysqld.log #过滤密码
2022-09-26T02:53:20.494809Z 1 [Note] A temporary password is generated for root@localhost: 4nj,G/V;c+xd
#注意:冒号之后的所有字符串全都是密码
#初始密码只用来登录,进入mysql后无法操作,需要修改密码
语法:
mysqladmin -u root -p'旧密码' password '新密码' #这个不需要进入mysql内就能修改
案例:
mysqladmin -u root -p'4nj,G/V;c+xd' password 'Blue@123'
2、创建farm库
mysql -uroot -p‘Blue@123’
mysql> create database farm;
mysql> exit
3、在MySQL服务端,账号登陆并将传输的数据文件导入到新建的farm库中
mysql -u root -p'Blue@123' farm < /root/qqfarm.sql
4、允许web服务端登陆数据库,需要数据库更改授权,然后重新加载授权表,注意数据库登陆进去以后,命令结尾都要加上;
mysql -uroot -p‘Blue@123’
create user 'root'@'%' identified by 'Blue@123';
grant all on *.* to 'root'@'%';
flush privileges;
exit
PS:数据库报错的解决办法
我们需要登陆MySQL,删除原来在farm库,创建新库然后导入
mysql -uroot -p‘Blue@123’
drop database farm;
然后重复执行一遍第八大点的2、3、4小点
9、web服务器端重新启动(test-03、test-04)
nginx -s reload
systemctl restart php-fpm
五、浏览器访问
1、在浏览器端输入服务端地址访问,根据提示修改权限
注意数据库服务器一栏填写数据库机器在ip地址
去浏览器输入自己的IP地址
这里下面三个选一个当主页
点完成后会开一个新界面,我们就可以浏览了,这里我们点网络家园
可以看到我们的qq农场,那到底能不能显示出来呢?我们试一试