1. ?考試系統項目集群架構圖
負載均衡 | 說明 |
7層負載 | 通過nginx對http請求進行轉發(uri,ua,類型) |
4層負載 | 對端口負載均衡(后端) |
2. 📝環境準備
角色 | 主機 | ip |
負載均衡 | lb01/lb02 | 172.16.1.5/172.16.1.6 |
前端web集群 | web01/web02 | 172.16.1.7/172.16.1.8 |
后端web集群 | web03/web04 | 172.16.1.9/172.16.1.10 |
數據庫 | db02 | 172.16.1.52 |
3. 🔐部署數據庫(db02)
3.1. 安裝數據庫
1.解壓,環境準備
mkdir -p /app/tools/ /app/data/3306/
tar xf mysql-8.0.28-linux-glibc2.12-x86_64.tar.xz -C /app/tools/
ln -s /app/tools/mysql-8.0.28-linux-glibc2.12-x86_64/ /app/tools/mysql2.安裝依賴
yum install ncurses ncurses-devel libaio-devel openssl openssl-devel -y#3.配置文件,用戶
useradd -s /sbin/nologin -M mysql 4.設置配置文件
cat>/etc/my.cnf<<'EOF'
[mysqld]
user=mysql
basedir=/app/tools/mysql/
datadir=/app/data/3306/
port=3306
socket=/tmp/mysql.sock [client]
socket=/tmp/mysql.sock
EOF
3.2. 數據庫基礎配置
5.修改配置和數據目錄的所有者.
chown mysql.mysql /etc/my.cnf
chown -R mysql.mysql /app/data/3306
chown -R root.root /app/tools/mysql/6.配置PATH環境變量
echo 'export PATH=/app/tools/mysql/bin:$PATH' >>/etc/profile
source /etc/profile
#7.檢查 不提示命令找不到就是正常的.
mysql -V
3.3. 初始化數據庫
#8.初始化
mysqld --initialize-insecure --user=mysql \
--basedir=/app/tools/mysql/ --datadir=/app/data/3306/
echo $?--initialize-insecure 以不安全方式初始化,root空密碼,不加這個選項就是安全,創建root隨機密碼.--user=mysql 用戶
--basedir=/app/tools/mysql/ 安裝目錄
--datadir=/app/data/3306/ 指定數據目錄
與配置文件中一致.
3.4. 設置開機自啟動
9.拷貝已經準備好的啟動管理文件
cp /app/tools/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld10.修改啟動腳本中的basedir和datadir
#basedir=
#datadir=sed -i '/^basedir=/s#basedir=#basedir=/app/tools/mysql/#g' /etc/init.d/mysqld
sed -i '/^datadir=/s#datadir=#datadir=/app/data/3306/#g' /etc/init.d/mysqld 11.檢查
egrep '^basedir=|^datadir=' /etc/init.d/mysqld
basedir=/app/tools/mysql/
datadir=/app/data/3306/12.開機自啟動服務,運行服務 (有些提示可以忽略)
#/etc/init.d/mysqld start (備用)systemctl enable mysqld
systemctl start mysqld13.檢查
ps -ef | grep mysql
ss -lntup | grep mysql
4. 🚀啟動jar項目(web03/web04)
- 準備application-prod.yml文件(正式環境)
- 準備xzs-3.9.0.jar
1.先手動前臺運行.
mkdir -p /app/code/exam/backend/
cd /app/code/exam/backend/ java -jar xxx.jar 2.前臺運行
java -Duser.timezone=Asia/Shanghai -jar -Dspring.profiles.active=prod xzs-3.9.0.jar
xxxx=prod === 讀取application-prod.yml配置文件
xxxx=dev === 讀取application-dev.yml配置文件3.后臺運行
nohup java -Duser.timezone=Asia/Shanghai -jar -Dspring.profiles.active=prod xzs-3.9.0.jar &
4.1. 🔄開機自啟動(web03/web04)
- 步驟
1.書寫jar包管理腳本
bash /server/scripts/exam.sh start
運行nohup java -Duser.timezone=Asia/Shanghai -jar -Dspring.profiles.active=prod xzs-3.9.0.jar >/dev/null 2>&1 &bash /server/scripts/exam.sh stop
關閉指定的服務bash /server/scripts/exam.sh restart
先關閉然后再啟動服務bash /server/scripts/exam.sh status
檢查是否運行2.systemctl調用腳本
[Unit]
Description=exam
After=network.target
[Service]
Type=forking
#EnvironmentFile=/etc/sysconfig/tomcat
ExecStart=bash /server/scripts/exam.sh start
ExecStop=bash /server/scripts/exam.sh stop
[Install]
WantedBy=multi-user.target
- 腳本
#1.vars
service=xzs-3.9.0.jar
dir=/app/code/exam/backend/
choose=$1
logfile=${dir}exam.log
time=`date +%F_%T`
jar_file=${dir}${service}#2.function
function srv_start() {pid=`ps -ef | grep -w xzs-3.9.0 | grep -v grep | awk '{print $2}'`pidcount=`ps -ef | grep -w xzs-3.9.0 | grep -v grep | awk '{print $2}' | wc -l`if [ -f ${jar_file} ];thenif [ -z ${pid} ];thencd ${dir}nohup java -Duser.timezone=Asia/Shanghai -jar -Dspring.profiles.active=prod xzs-3.9.0.jar >/dev/null 2>&1 &fielseexitfireturn $?
}function srv_stop() {pid=`ps -ef | grep -w xzs-3.9.0 | grep -v grep | awk '{print $2}'`pidcount=`ps -ef | grep -w xzs-3.9.0 | grep -v grep | awk '{print $2}' | wc -l`if [ ${pidcount} -gt 0 ];thenkill ${pid}fi
}function srv_restart() {srv_stopsleep 1srv_start
}function srv_status() {pid=`ps -ef | grep -w xzs-3.9.0 | grep -v grep | awk '{print $2}'`pidcount=`ps -ef | grep -w xzs-3.9.0 | grep -v grep | awk '{print $2}' | wc -l`if [ ${pidcount} -ge 1 ];thenecho -e "\E[1;32mrunning pid:[${pid}] ${service}\E[0m"elseecho -e "\E[1;31mstoped ${service}\E[0m"fi
}#3.case
case "${choose}" instart) srv_start ;;stop) srv_stop ;;restart) srv_restart ;;status) srv_status ;;*) echo input error
esac
5. 🍀接入4層負載均衡(lb01/lb02)
5.1. 檢查是否支持4層負載均衡
nginx -V |& grep stream
--with-stream
5.2. 負載均衡配置
1.修改yum源
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true2.安裝nginx
yum -y install nginx3.修改nginx配置文件
vim /etc/nginx/nginx.conf
stream {upstream exam_pools {server 10.0.0.9:8000;server 10.0.0.10:8000;hash $remote_addr consistent;}log_format basic '$remote_addr [$time_local] ''$protocol $status $bytes_sent $bytes_received ''$session_time';access_log /var/log/nginx-l4.log basic;server {listen 8000;proxy_pass exam_pools;}
}4.重啟nginx
nginx -t
systemctl enable nginx --now
systemctl reload nginx5.檢查
ss -lntup | grep 8000
6. 🌏部署前端web(web01/web02)
1.修改yum源
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true2.安裝nginx
yum -y install nginx3.修改配置文件
vim /etc/nginx/conf.d/exam.conf
server {listen 80;server_name admin.zhubl.xyz;root /app/code/exam/front/admin/;location / {index index.html;}location /api/ {proxy_pass http://10.0.0.4:8000;}
}server {listen 80;server_name student.zhubl.xyz;root /app/code/exam/front/student/;location / {index index.html;}location /api/ {proxy_pass http://10.0.0.4:8000;}
}4.創建目錄
mkdir /app/code/exam/front/5.拷貝前端代碼到站點目錄
unzip exam-web-前端.zip
mv exam-web-前端/* /app/code/exam/front/6.重啟nginx
nginx -t
systemctl enable nginx --now
systemctl reload nginx
7. 🍀接入7層負載(lb01/lb02)
1.修改yum源
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true2.安裝nginx
yum -y install nginx3.修改nginx配置文件
vim /etc/nginx/conf.d/exam.conf
upstream l7_pools {server 10.0.0.7:80;server 10.0.0.8:80;hash $remote_addr consistent;
}
server {listen 80;server_name admin.zhubl.xyz;location / {proxy_pass http://l7_pools;proxy_set_header Host $http_host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-Ip $remote_addr;}
}
server {listen 80;server_name student.zhubl.xyz;location / {proxy_pass http://l7_pools;proxy_set_header Host $http_host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-Ip $remote_addr;}
}4.重啟nginx
nginx -t
systemctl enable nginx --now
systemctl reload nginx
8. 🚀接入高可用(lb01)
1.安裝keepalived
yum -y install keepalived2.備份配置文件
cp /etc/keepalived/keepalived.conf{,.bak}3.修改配置文件
vim /etc/keepalived/keepalived.conf
global_defs {router_id lb01
}
vrrp_script check_lb.sh { script /server/scripts/check_lb.shinterval 2weight 1user root
}vrrp_instance vip_3 { state MASTER interface ens33virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASSauth_pass 1111 }virtual_ipaddress { 10.0.0.3 dev ens33 label ens33:1}track_script {check_lb.sh }
}vrrp_instance vip_4 {state BACKUPinterface ens33virtual_router_id 52priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.0.0.4 dev ens33 label ens33:2}
}4.編寫腳本
vim /server/scripts/check_lb.sh
#1.vars
count=`ps -ef | grep nginx | wc -l`#2.stop keepalived
if [ ${count} -eq 1 ];thensystemctl stop keepalived
fi5.重啟keepalived
systemctl restart keepalived.service
9. 🚀接入高可用(lb02)
1.安裝keepalived
yum -y install keepalived2.備份配置文件
cp /etc/keepalived/keepalived.conf{,.bak}3.修改配置文件
vim /etc/keepalived/keepalived.conf
global_defs {router_id lb02
}
vrrp_script check_lb.sh {script /server/scripts/check_lb.shinterval 2weight 1user root
}vrrp_instance vip_3 {state BACKUPinterface ens33virtual_router_id 51priority 50advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.0.0.3 dev ens33 label ens33:1}
}vrrp_instance vip_4 {state MASTERinterface ens33virtual_router_id 52priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {10.0.0.4 dev ens33 label ens33:2}track_script {check_lb.sh}
}4.編寫腳本
vim /server/scripts/check_lb.sh
#1.vars
count=`ps -ef | grep nginx | wc -l`#2.stop keepalived
if [ ${count} -eq 1 ];thensystemctl stop keepalived
fi5.重啟keepalived
systemctl restart keepalived.service
10. 🌏瀏覽器訪問
http://student.zhubl.xyz/ 學生端 student 123456
http://admin.zhubl.xyz/ 管理端 admin 123456