当前位置: 首页 > news >正文

三主热备架构

1.要求

在这里插入图片描述

角色主机名软件IP地址
用户client192.168.72.90
keepalivedvip192.168.72.100
masterserverAkeepalived, nginx192.168.72.30
backupserverBkeepalived, nginx192.168.72.31
backupserverCkeepalived, nginx192.168.72.32
webtomcat1tomcat192.168.72.41
webtomcat2tomcat192.168.72.42
webtomcat3tomcat192.168.72.43
DNS轮询dnsnginx192.168.72.11

2.环境准备

2.1修改主机名和ip地址
web:

hostnamectl hostname tomact1
nmcli c modify ens160 ipv4.method manual ipv4.address 192.168.72.41 ipv4.gateway 192.168.72.2 ipv4.dns 223.5.5.5 connection.autoconnect yes
hostnamectl hostname tomact2
nmcli c modify ens160 ipv4.method manual ipv4.address 192.168.72.42 ipv4.gateway 192.168.72.2 ipv4.dns 
223.5.5.5 connection.autoconnect yes
hostnamectl hostname tomact3
nmcli c modify ens160 ipv4.method manual ipv4.address 192.168.72.42 ipv4.gateway 192.168.72.2 ipv4.dns 
223.5.5.5 connection.autoconnect yes

master:

hostnamectl hostname master
nmcli c modify ens160 ipv4.method manual ipv4.address 192.168.72.30 ipv4.gateway 192.168.72.2 ipv4.dns 223.5.5.5 connection.autoconnect yes

bacup1:

hostnamectl hostname backup1
nmcli c modify ens160 ipv4.method manual ipv4.address 192.168.72.31 ipv4.gateway 192.168.72.2 ipv4.dns 223.5.5.5 connection.autoconnect yes

backup2:

hostnamectl hostname tomact1
nmcli c modify ens160 ipv4.method manual ipv4.address 192.168.72.32 ipv4.gateway 192.168.72.2 ipv4.dns 223.5.5.5 connection.autoconnect yes

dns:

hostnamectl hostname dns
nmcli c modify ens160 ipv4.method manual ipv4.address 192.168.72.11 ipv4.gateway 192.168.72.2 ipv4.dns 223.5.5.5 connection.autoconnect yes

2.2配置仓库(所有虚拟机)

[BaseOS]
name=BaseOS
baseurl=/mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppStream
baseurl=/mnt/AppStream
gpgcheck=0

2.3挂载仓库(所有虚拟机)

mount /dev/sr0 /mnt

2.4关闭防火墙和selinux(所有虚拟机)

systemctl stop firewalld
setenforce 0

3.配置tomact服务

3.1下载tomact
官网:https://tomcat.apache.org/
tomact1:

wgt  https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.5/bin/apache-tomcat-11.0.5.tar.gz
ls

apache-tomcat-11.0.5.tar.gz

3.2解压并配置环境

tar -xzf apache-tomcat-11.0.5.tar.gz -C /usr/local
vim /etc/profile
export TOMACT_HOME=/usr/local/apache-tomcat-11.0.5
export PAHT=$PATH:$TOMACT_HOME/bin

3.3下载jdk
官网:https://www.oracle.com

wget  https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
ls jdk-21_linux-x64_bin.tar.gz

3.4 解压并配置环境

tar- xzf jdk-21_linux-x64_bin.tar.gz -C /usr/local
vim /usr/local
export JAVA_HOME=/usr/local/jdk-21.0.6
export PATH=$PATH:$JAVA_HOME/bin
vim /usr/local/apache-tomcat-11.0.5/webapps/
ls

docs examples host-manager manager ROOT

rm -rf docs  examples  host-manager  manager  
cd ROOT
rm -rf *
vim index.jsp
tomact1 192.168.72.41

3.5启动文件

source /ect/profile
startup.sh
curl 192.168.72.41:8080

tomact 192.168.72.41

tomact2
复制相关配置文件(tomact1上进行)

scp -r /usr/local/jdk-21.0.6/ root@192.168.72.42:/usr/local
scp -r /usr/local/apache-tomcat-11.0.5/ root@192.168.72.42:/usr/local
scp -r /etc/profile root@192.168.72.42:/etx/profile

验证复制的目录

source /etc/profile
startup.sh

修改tomact主页

cd /usr/local/apache-tomcat-11.0.5/webapps/
ls

docs examples host-manager manager ROOT

rm -rf docs examples host-manager manager 
cd ROOT
rm -rf *
vim index.jsp
tomact2 192.168.72.42

启动tomact

startup.sh
curl 192.168.72.42

> tomact2 3192.168.72.42

tomact3:
复制相关配置文件(tomact1上进行)

scp -r /usr/local/jdk-21.0.6/ root@192.168.72.43:/usr/local
scp -r /usr/local/apache-tomcat-11.0.5/ root@192.168.72.43:/usr/local
scp -r /etc/profile root@192.168.72.43:/etx/profile

验证复制的目录

source /etc/profile
startup.sh

修改tomact主页

cd /usr/local/apache-tomcat-11.0.5/webapps/
ls

docs examples host-manager manager ROOT

rm -rf docs examples host-manager manager 
cd ROOT
rm -rf *
vim index.jsp
tomact3 192.168.72.43

启动tomact

startup.sh
curl 192.168.72.43

> tomact 3192.168.72.43

4.keepalived和nginx相关服务

4.1下载nginx和keepalived
mastr:

dnf install nginx -y
dnf install keepalived

4.2配置nginx

vim /etc/nginx/conf.d/master.conf
upstream tpmact1 {
	server 192.168.72.41:8080;
	server 192.168.72.42:8080;
	server 192.168.72.43:8080;
}
server{
	server_name 192.168.72.30;
	listen 80;
	access_log /var/log/nginx/master_access.log;
	error_log /var/log/nginx/master_error.log;
	location / {
			proxy_pass http://tomact1
		}
}

4.3配置keepalived

vim /etc/keepalived/keepalived.conf
global_defs {
   router_id master
}

vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.100
    }
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.101
    }
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 60
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.102
    }
}

4.4配置nginx高可用

vim /etc/keepalived/check_nginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-header | wc -l)
if [$counter -eq 0]; then
	systemctl start nginx
	if [`ps  -C --no-header | wc -l` -eq 0]; then
			systemctl stop keepalived
	fi
fi

4.5给脚本赋予执行权限

chmod +x /etc/keepalived/check_nginx.sh

4.6修改keepalived.conf文件

vim /etc/keepalived/keepalived.conf
global_defs {
   router_id master
}
vrrp_script check_nginx{
		script  "/etc/keepalived/check_nginx.sh"
		interval 2
	}
vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.100
    }
    track check_nginx{
    	check_nginx
    }
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.101
    }
     track check_nginx{
    	check_nginx
    }
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 60
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.102
    }
     track check_nginx{
    	check_nginx
    }
}

4.7启动keepalived和nginx

systemctl start keepalived
systemctl start nginx

4.8 测试

curl192.168.72.30

> tomact1 192.168.72.41 tomact1 192.168.72.42 tomact1 192.168.72.43

backup1:
下载nginx和keepalived

dnf install nginx -y
dnf install keepalived -y

复制相关配置文件(master上进行)

scp /etc/keepalived/keepalived.conf root@192.168.72.31:/etc/keepalived/keepalived.conf/
scp /etc/keepalived/check_nginx.sh root@192.168.72.31:/etc/keepalived/

查看脚本文件

ls /etc/keepalived/check_nginx.sh

修改keepalived.conf文件

vim /etc/keepalived/keepalived.conf
global_defs {
   router_id backup
}
vrrp_script check_nginx{
		script  "/etc/keepalived/check_nginx.sh"
		interval 2
	}
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 60
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.100
    }
    track check_nginx{
    	check_nginx
    }
}
vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.101
    }
     track check_nginx{
    	check_nginx
    }
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.102
    }
     track check_nginx{
    	check_nginx
    }
}

启动nginx和keepalived


```bash
systemctl start nginx
systemctl start keepalived
测试:

```bash
curl 192.168.72.31

> tomact1 192.168.72.41 tomact1 192.168.72.42 tomact1 192.168.72.43

backup2:
下载nginx和keepalived

dnf install nginx -y
dnf install keepalived -y

复制相关配置文件(master上进行)

scp /etc/keepalived/keepalived.conf root@192.168.72.32:/etc/keepalived/keepalived.conf/
scp /etc/keepalived/check_nginx.sh root@192.168.72.32:/etc/keepalived/

查看脚本文件

ls /etc/keepalived/check_nginx.sh

修改keepalived.conf文件

vim /etc/keepalived/keepalived.conf
global_defs {
   router_id backup
}
vrrp_script check_nginx{
		script  "/etc/keepalived/check_nginx.sh"
		interval 2
	}
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 80
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.100
    }
    track check_nginx{
    	check_nginx
    }
}
vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 60
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.101
    }
     track check_nginx{
    	check_nginx
    }
}
vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.72.102
    }
     track check_nginx{
    	check_nginx
    }
}

启动nginx和keepalived


```bash
systemctl start nginx
systemctl start keepalived

测试:

```bash
curl 192.168.72.32

> tomact1 192.168.72.41 tomact1 192.168.72.42 tomact1 192.168.72.43

5.dns相关配置:

5.1下载nginx

dnf install nginx -y

5.2配置文件

vim /etc/nginx/conf.d/dns.conf
upstream web {
		server 192.168.72.100;
		server 192.168.72.101;
		server 192.168.72.102;
	}
server {
		server_name 192.168.72.11;
		listen 80;
		access_log /var/log/ngonx/dns_access.log;
		error_log /var/log/nginx/dns_error.log;
		location / {
				proxy_pass http://web;
			}
	}

5.3 启动nginx

systemctl start nginx

5.4 测试

curl 1912.168.72.100

> tomact1 192.168.72.41 tomact1 192.168.72.42 tomact1 192.168.72.43

至此,keepalived+nginx+tomact的高可用轮询搭建完成

相关文章:

  • 原生微信小程序基础语法--快速总结
  • 架构师面试(二十):CAP 定理
  • 自定义mavlink 生成wireshark wlua插件错误(已解决)
  • 【拒绝算法PUA】LeetCode 2116. 判断一个括号字符串是否有效
  • VLM理解(一)——视觉文本信息的标注与数据集制作过程
  • 第十一章 | 智能合约主网部署与验证详解
  • 6、linux c 线程 -下
  • 同旺科技USB to I2C 适配器 ---- 多从机设备混合调试
  • 关于解决Ubuntu终端及系统字体大小的问题
  • Java 24 学习
  • WebSocket:现代实时通信协议的深度解析与实践
  • Mysql笔记
  • el-table单元格编辑,动态增删行,回车/上下左右箭头切换单元格
  • cpp-友元
  • 马科维茨均值—方差理论推导过程
  • 分布式系统设计陷阱,白话CAP理论
  • CVE-2020-0796:永恒之黑实战学习
  • Redis + 布隆过滤器解决缓存穿透问题
  • 微服务 - 中级篇
  • WebLogic中间件常见漏洞
  • 野猪穿过江苏电视台楼前广场,被抓捕后送往红山森林动物园
  • 视频丨伊朗港口爆炸事件灭火工作已完成80%
  • 识味顺德︱顺德菜的醉系列与火滋味
  • 国家市场监管总局:组织销毁侵权假冒服装、食药等3300吨
  • 牧原股份一季度归母净利润44.91亿元,同比扭亏为盈
  • 【社论】上海经济开门红:不偏科、挑大梁