web高可用实战
项目实战
项目构架图
环境准备
六台服务器
192.168.60.131 master
192.168.60.132 backup
192.168.60.133 nginx-web01
192.168.60.134 nginx-web02
192.168.60.135 tomcat
192.168.60.136 php
所有服务器关闭防火墙和selinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
开始
一、Tomcat搭建
修改hostname
hostnamectl set-hostname tomcat
1.安装jdk
安装 Tomcat & JDK
安装时候选择 tomcat 软件版本要与程序开发使用的版本一致。jdk 版本要进行与 tomcat 保持一致。
上传jdk到服务器中
1)安装相关的工具
yum install -y vim unzip wget net-tools
2)解压压缩包到指定目录
tar zxvf jdk-8u321-linux-x64.tar.gz -C /usr/local/
3)移动至该目录下并改名
cd /usr/local/
mv jdk1.8.0_321/ java
4)设置环境变量
[root@tomcat local]# vim /etc/profile
JAVA_HOME=/usr/local/java //指定java安装目录
PATH=$JAVA_HOME/bin:$PATH //用于指定java系统查找命令的路径
export JAVA_HOME PATH //类的路径,在编译运行java程序时,如果有调用到其他类的时候,在classpath中寻找需要的类。
source /etc/profile //让环境变量生效
5)测试jdk是否安装成功
[root@tomcat local]# java -version
java version "1.8.0_321"
Java(TM) SE Runtime Environment (build 1.8.0_321-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.321-b07, mixed mode)
2.安装Tomcat
将tomcat安装包上传到服务器中
安装 Tomcat & JDK
安装时候选择 tomcat 软件版本要与程序开发使用的版本一致。jdk 版本要进行与 tomcat 保持一致。
1)解压压缩包到指定目录
tar zxvf apache-tomcat-9.0.83.tar.gz -C /usr/local/
2)移动至该目录下并改名
cd /usr/local/
mv apache-tomcat-9.0.83/ tomcat
3)设置环境变量
echo 'export TOMCAT_HOME=/application/tomcat'>>/etc/profile
source /etc/profile //使环境变量生效
4)查看tomcat是否安装成功
[root@tomcat local]# /usr/local/tomcat/bin/version.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Server version: Apache Tomcat/9.0.83
Server built: Nov 9 2023 20:57:42 UTC
Server number: 9.0.83.0
OS Name: Linux
OS Version: 3.10.0-1160.71.1.el7.x86_64
Architecture: amd64
JVM Version: 1.8.0_321-b07
JVM Vendor: Oracle Corporation
5)tomcat管理
/usr/local/tomcat/bin/startup.sh //开启tomcat
/usr/local/tomcat/bin/startup.sh //关闭tomcat
6)查看tomcat是否正常启动
[root@tomcat local]# netstat -lntp |grep java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1764/java
tcp6 0 0 :::8080 :::* LISTEN 1764/java
tomcat启动完成
打开浏览器192.168.60.135:8080 可以看到tomcat的浏览界面
二、php搭建
修改hostname
hostnamectl set-hostname php
1.yum安装php7.1
1)安装相关工具
yum install -y vim wget net-tools
2)安装相关包
rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum install -y php71w-xsl php71w php71w-ldap php71w-cli php71w-common php71w-devel php71w-gd php71w-pdo php71w-mysql php71w-mbstring php71w-bcmath php71w-mcrypt
3)安装php
yum install -y php71w-fpm
4)启动php
systemctl start php-fpm
systemctl enable php-fpm
2.安装nginx
1)修改nginx配置文件
[root@php ~]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
}
}
2)创建index.php
文件
[root@php ~]# vim /usr/share/nginx/html/index.php
<?php
phpinfo();
?>
重启nginx服务
systemctl restart nginx
打开浏览器192.168.60.136 可以看到php版本界面
三、nginx搭建
因为nginx-web01和nginx-web02在项目中负责代理后端服务,所以两者步骤相同
修改hostname
hostnamectl set-hostname nginx-web01 //192.168.60.133
hostnamectl set-hostname nginx-web02 //192.168.60.134
1.安装nginx
2.配置负载均衡
1)创建upstream.conf
文件
vim /etc/nginx/conf.d/upstream.conf
添加如下配置
upstream web {
server 192.168.60.135:8080;
server 192.168.60.136:80;
}
2)修改default.conf
文件
vim /etc/nginx/conf.d/default.conf
修改
location / {
proxy_pass http://web;
}
3)更改缓存时间
vim /etc/nginx/nginx.conf
把
keepalive_timeout 65;
修改为
keepalive_timeout 0;
4)重启nginx
systemctl restart nginx
打开浏览器192.168.60.133
打开浏览器192.168.60.134
刷新
四、keepalived搭建
修改hostname
hostnamectl set-hostname master
hostnamectl set-hostname backup
1.安装ipvsadm
1)安装可用的工具(都做)
yum install -y vim wget net-tools
2)安装ipvsadm(都做)
yum -y install ipvsadm keepalived
3)修改配置文件(都做)
vim /etc/keepalived/keepalived.conf
master
! Configuration File for keepalived
global_defs {
router_id lvs-keepalived-master #辅助改为lvs-backup
}
vrrp_instance VI_1 {
state MASTER
interface ens33 #VIP绑定接口
virtual_router_id 80 #VRID 同一组集群,主备一致
priority 100 #本节点优先级,辅助改为50
advert_int 1 #检查间隔,默认为1s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.60.130/32
}
}
virtual_server 192.168.60.130 80 { #LVS配置
delay_loop 6 #健康检查rs时间间隔
lb_algo rr #LVS调度算法
lb_kind DR #LVS集群模式(路由模式)
protocol TCP #健康检查使用的协议
real_server 192.168.60.133 80 {
weight 1
inhibit_on_failure #当该节点失败时,把权重设置为0,而不是从IPVS中删除
TCP_CHECK { #健康检查
connect_port 80 #检查的端口
connect_timeout 3 #连接超时的时间
}
}
real_server 192.168.60.134 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_timeout 3
connect_port 80
}
}
}
backup
! Configuration File for keepalived
global_defs {
router_id lvs-keepalived-slave
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 80
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.60.130/32
}
}
virtual_server 192.168.60.130 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.60.133 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_port 80
connect_timeout 3
}
}
real_server 192.168.60.134 80 {
weight 1
inhibit_on_failure
TCP_CHECK {
connect_timeout 3
connect_port 80
}
}
}
4)启动keepalived(都做)
systemctl start keepalived
systemctl enable keepalived
5)查看(都做)
ipvsadm -Ln
出现如下,才算成功
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.60.130:80 rr persistent 20
-> 192.168.60.133:80 Route 1 0 0
-> 192.168.60.134:80 Route 0 0 0
2.两台nginx操作
1)给本地网卡添加VIP(都做)
ip addr add dev lo 192.168.60.130/32
2)设置IPv4网络的arp_ignore
参数
echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf
3)立即应用更改
sysctl -p
4)重启nginx
systemctl start nginx
打开浏览器192.168.60.130
查看是否成功
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 木偶
评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果