1.要求
角色 | 主机名 | 软件 | IP地址 |
---|---|---|---|
用户 | client | 192.168.72.90 | |
keepalived | vip | 192.168.72.100 | |
master | serverA | keepalived, nginx | 192.168.72.30 |
backup | serverB | keepalived, nginx | 192.168.72.31 |
backup | serverC | keepalived, nginx | 192.168.72.32 |
web | tomcat1 | tomcat | 192.168.72.41 |
web | tomcat2 | tomcat | 192.168.72.42 |
web | tomcat3 | tomcat | 192.168.72.43 |
DNS轮询 | dns | nginx | 192.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 MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.72.100}
}
vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.72.101}
}
vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 60advert_int 1authentication {auth_type PASSauth_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]; thensystemctl start nginxif [`ps -C --no-header | wc -l` -eq 0]; thensystemctl stop keepalivedfi
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 MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.72.100}track check_nginx{check_nginx}
}
vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.72.101}track check_nginx{check_nginx}
}
vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 60advert_int 1authentication {auth_type PASSauth_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 BACKUPinterface ens160virtual_router_id 51priority 60advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.72.100}track check_nginx{check_nginx}
}
vrrp_instance VI_1 {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.72.101}track check_nginx{check_nginx}
}
vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 80advert_int 1authentication {auth_type PASSauth_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 BACKUPinterface ens160virtual_router_id 51priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.72.100}track check_nginx{check_nginx}
}
vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 60advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.72.101}track check_nginx{check_nginx}
}
vrrp_instance VI_1 {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_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的高可用轮询搭建完成