keepalived+nginx+tomcat高可用

1.要求

角色 主机名 软件 IP地址
用户 client 192.168.72.90
keepalived vip 192.168.72.100
master master keepalived, nginx 192.168.72.30
backup backup keepalived, nginx 192.168.72.32
web tomcat1 tomcat 192.168.72.41
web tomcat2 tomcat 192.168.72.42

1.搭建Tomcat

1.1下载jdk

wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
1.2.查看文件以及解压文件

bash 复制代码
ls 

anaconda-ks.cfg jdk-21_linux-x64_bin.tar.gz

bash 复制代码
tar -xzf  jdk-21_linux-x64_bin.tar.gz -C  /usr/local
bash 复制代码
ls /usr/local/jdk-21.0.6/

bin conf include jmods legal lib LICENSE man README release

1.3配置jdk

bash 复制代码
vim /etc/profile
cat /etc/profile
export JAVA_HOME=/usr/local/jdk-21.0.6/
export PATH=$PATH:$JAVA_HOME/bin

1.4刷新文件配置生效

bash 复制代码
source /etc/profile

1.5验证jdk是否生效

bash 复制代码
java -version

1.6关闭防火墙以及selinux

bash 复制代码
systemctl disabled firewalld
setenforce 0

2.在tomact1安装配置Tomact

2.1下载tomact

bash 复制代码
wget https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.5/bin/apache-tomcat-11.0.5.tar.gz

2.2查看文件并解压

bash 复制代码
ls 

anaconda-ks.cfg apache-tomcat-11.0.5.tar.gz

jdk-21_linux-x64_bin.tar.gz

bash 复制代码
tar -xzf  apache-tomcat-11.0.5.tar.gz  -C /usr/local
ls /usr/local

2.3配置tomact相关环境

bash 复制代码
vim /etc/profile
cat /etc/profile
export TOMACT_HOME=/usr/local/apache-tomcat-11.0.5
export PATH=$PATH:$TOMACT_HOME/bin

2.4刷新配置

bash 复制代码
source /etc/profile

2.5启动tomact

bash 复制代码
startup.sh

3.在tomact2上安装jdk以及tomact

3.1复制jdk安装目录

bash 复制代码
scp  -r /usr/local/jdk-21.0.6/ [email protected]:/usr/local/ >/dev/null

3.2复制tomact安装目录

bash 复制代码
scp  -r /usr/local/apache-tomcat-11.0.5/ [email protected]:/usr/local > /dev/null

3.3复制/etc/profile文件

bash 复制代码
 scp /etc/profile [email protected]:/etc/profile/

3.4验证复制的目录

bash 复制代码
ls /usr/local 

bin etc games include jdk-21.0.6 lib lib64 libexec sbin share

src apache-tomcat-11.0.5

3.5刷新/etc/profile

bash 复制代码
source /etc/profile
java -version

3.6启动tomact

bash 复制代码
startup.sh

Using CATALINA_BASE: /usr/local/tomcat-11.0.5 Using CATALINA_HOME:

/usr/local/tomcat-11.0.5 Using CATALINA_TMPDIR:

/usr/local/tomcat-11.0.5/temp Using JRE_HOME:

/usr/local/jdk-21.0.6/ Using CLASSPATH:

/usr/local/tomcat-11.0.5/bin/bootstrap.jar:/usr/local/tomcat-11.0.5/bin/tomcat-juli.jar Using CATALINA_OPTS: Tomcat started.

4.修改tomact主页

4.1修改tomact1主页

bash 复制代码
cd /usr/local/apache-tomcat-11.0.5/webapps/
bash 复制代码
ls

docs examples host-manager manager ROOT

bash 复制代码
rm -rf  docs  examples  host-manager  manager 
bash 复制代码
cd /ROOT
rm -rf *
bash 复制代码
vim index.jsp
cat index.jsp
tomact1 10.10.10.41

4.2修改tomact2主页

bash 复制代码
cd /usr/local/apache-tomcat-11.0.5/webapps/
ls

docs examples host-manager manager ROOT

bash 复制代码
rm -rf docs  examples  host-manager  manager
bash 复制代码
cd /ROOT
rm -rf *
vim index.jsp
cat index.jsp
tpmact 10.10.10.42
bash 复制代码
curl 10.10.10.42:8080

tomact 10.10.10.42

5.搭建nginx服务

5.1在master上搭建nginx服务

5.1.1配置仓库

bash 复制代码
vim /etc/yum/repos.d/master.repo
cat etc/yum/repos.d/master.repo
[BaseOS]
name=BaseOS
baseurl=/mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppStream
baseurl=/mnt/AppStream
gpgcheck=0

5.1.2挂载仓库

bash 复制代码
mount /dev/sr0 /mnt

5.1.3 下载nginx服务

bash 复制代码
dnf install nginx -y

5.1.4 配置nginx

bash 复制代码
vim /etc/nginx/conf.d/master.conf
cat /etc/nginx/conf.d/master.conf
upstream tomact1{
	server_name 10.10.10.41:8080;
	server_name 10.10.10.42:8080;
}
server{
	server_name 10.10.10.30;
	listen 80;
	access_log /var/log/nginx/master_access.log;
	error_log /var/log/nginx/master_error.log;
	location / {
			proxy_pass http://tomact1;
		}
}

5.1.5启动nginx服务

bash 复制代码
systemctl start nginx

5.1.6测试

bash 复制代码
curl http://10.10.10.30

tomact1 10.10.10.41

bash 复制代码
curl http://10.10.10.30

tomact2 10.10.10.42

5.2在backup上搭建nginxf服务

5.2.1配置仓库

bash 复制代码
vim /etc/yum/repos.d/master.repo
cat etc/yum/repos.d/master.repo
[BaseOS]
name=BaseOS
baseurl=/mnt/BaseOS
gpgcheck=0
[AppStream]
name=AppStream
baseurl=/mnt/AppStream
gpgcheck=0

5.2.2挂载仓库

bash 复制代码
mount /dev/sr0 /mnt

5.2.3 下载nginx服务

bash 复制代码
dnf install nginx -y

5.2.4 配置nginx

bash 复制代码
vim /etc/nginx/conf.d/backup.conf
cat /etc/nginx/conf.d/backup.conf
upstream tomact2 {
	server 10.10.10.41:8080;
	server 10.10.10.42:8080;
	}
	server {
		server_name 10.10.10.42;
		listen 80;
		access_log /var/log/nginx/baskup_access.log;
		error_log /var/log/nginx/backup_error.log;
		location / {
			proxy_pass http://tomact2;
			}
		}

5.2.5启动nginx服务

bash 复制代码
systemctl start nginx

5.2.6测试

bash 复制代码
curl 10.10.10.32

tomact1 10.10.10.41

bash 复制代码
curl 10.10.10.32

tomact2 10.10.10.42

6.搭建keepalived

6.1在master上搭建keepalived

6.1.1下载keepalived服务

bash 复制代码
dnf install keepalived -y

6.1.2配置keepalived文件

bash 复制代码
vim /etc/keepalived/keepalived.conf
cat /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 {
        10.10.10.100
    }
}

6.2在backup上配置keepalived

6.2.1下载keepalived服务

bash 复制代码
dnf install keepalived -y

6.2.2配置keepalived文件

bash 复制代码
vim /etc/keepalived/keepalived.conf
cat /etc/keepalived/keepalived.conf
global_defs {
   router_id  backup
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.100
    }
}

6.3启动keepalived服务

master:

bash 复制代码
systemctl start keepalived

backup:

bash 复制代码
systemctl start keepalived

6.4 修改nginx文件

bash 复制代码
vim /etc/nginx/conf.d/master.conf
cat /etc/nginx/conf.d/master.conf
upstream tomact2 {
	server 10.10.10.41:8080;
	server 10.10.10.42:8080;
	}
	server {
		server_name 10.10.10.100;
		listen 80;
		access_log /var/log/nginx/baskup_access.log;
		error_log /var/log/nginx/backup_error.log;
		location / {
			proxy_pass http://tomact2;
			}
		}
		
bash 复制代码
vim /etc/nginx/conf.d/backup.conf
cat /etc/nginx/conf.d/backup.conf
upstream tomact2 {
	server 10.10.10.41:8080;
	server 10.10.10.42:8080;
	}
	server {
		server_name 10.10.10.100;
		listen 80;
		access_log /var/log/nginx/baskup_access.log;
		error_log /var/log/nginx/backup_error.log;
		location / {
			proxy_pass http://tomact2;
			}
		}

6.5重启nginx服务

bash 复制代码
systemctl reatart nginx
curl 10.10.10.100

tomact1 10.10.10.41

bash 复制代码
curl 10.10.10.100

tomact2 10.10.10.42

7.配置nginx高可用

7.1 编写检测脚本

bash 复制代码
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 nginx --no-header | wc -l` -eq 0]; rhen
	systemctl stop keepalived
	fi
fi

7.2给脚本赋予执行权限

bash 复制代码
chmod +x /etc/keepalived/check_nginx.sh

7.3将脚本文件复制到backup主机中

bash 复制代码
scp /etc/keepalived/check_nginx.sh [email protected]:/etc/keepalived/

7.4在backup中验证

bash 复制代码
ll /etc/keepalived

-rwxr-xr-x. 1 root root 191 Mar 20 21:15 check_nginx.sh

-rw-r--r--. 1 root root 286 Mar 20 20:58 keepalived.conf

7.5修改master主机上keepalived.conf文件

bash 复制代码
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 {
        10.10.10.100
    }
    track_script {
    	check_nginx
    	}
}

7.6修改backup中keepalived.conf文件

bash 复制代码
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 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.10.10.100
    }
    track_script {
    	check_nginx
    	}
}

7.7重启keepalived服务

bash 复制代码
systemctl reatart keepalived

8.测试高可用

8.1关闭master上nginx服务

bash 复制代码
systemctl stop nginx
bash 复制代码
curl 10.10.10.100

tomact1 10.10.10.41

bash 复制代码
curl 10.10.10.100

tomact210.10.10.42

bash 复制代码
ps -ef  | grep nginx

root 3471 1 0 21:23 ? 00:00:00 nginx: master

process /usr/sbin/nginx nginx 3472 3471 0 21:23 ?

00:00:00 nginx: worker process nginx 3473 3471 0 21:23 ?

00:00:00 nginx: worker process nginx 3474 3471 0 21:23 ?

00:00:00 nginx: worker process

发现,当我们关闭 nginx 服务后,我们的脚本就会执行,从而自动帮我们把 nginx 服务启动起来。
8.2关闭keepalived和nginx

bash 复制代码
systemctl stop keepalived
systemctl stop nginx
bash 复制代码
curl 10.10.10.30

tomact1 10.10.10.41

bash 复制代码
curl 10.10.10.32

tomact2 10.10.10.42

bash 复制代码
ps -ef | grep nginx

root 3745 1489 0 21:25 pts/0 00:00:00 grep --color=auto

nginx

bash 复制代码
systemctl start keepalived
ps -ef | grep nginx

root 3789 1 0 21:26 ? 00:00:00 nginx: master

process /usr/sbin/nginx nginx 3790 3789 0 21:26 ?

00:00:00 nginx: worker process nginx 3791 3789 0 21:26 ?

00:00:00 nginx: worker process nginx 3792 3789 0 21:26 ?

00:00:00 nginx: worker process

至此,keepalived + nginx + tomcat 的高可用就搭建完成。

相关推荐
知远同学1 小时前
docker学习笔记2-最佳实践
运维·docker·容器
哈哈幸运2 小时前
MySQL运维三部曲初级篇:从零开始打造稳定高效的数据库环境
linux·运维·数据库·mysql·性能优化
黑心老人2 小时前
Mac OS系统下kernel_task占用大量CPU资源导致系统卡顿
linux·运维·服务器·macos
光算科技3 小时前
服务器在国外国内用户访问慢会影响谷歌排名吗?
运维·服务器·c++
口嗨农民工3 小时前
ubuntu18.04启动不了修复
linux·运维·ubuntu
YUELEI1183 小时前
Centos9 安装 nginx 及配置
nginx·centos
塔能物联运维3 小时前
双轮驱动能源革命:能源互联网与分布式能源赋能工厂能效跃迁
大数据·运维
GalaxyPokemon4 小时前
Muduo网络库实现 [十六] - HttpServer模块
linux·运维·服务器·网络
461K.5 小时前
spark与hadoop的区别
大数据·运维·hadoop·分布式·spark·intellij-idea
Zfox_5 小时前
Git 进阶之路:高效协作之分支管理
大数据·linux·运维·c++·git·elasticsearch