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.查看文件以及解压文件

ls 

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

tar -xzf  jdk-21_linux-x64_bin.tar.gz -C  /usr/local
ls /usr/local/jdk-21.0.6/

bin conf include jmods legal lib LICENSE man README release

1.3配置jdk

vim /etc/profile
cat /etc/profile
export JAVA_HOME=/usr/local/jdk-21.0.6/
export PATH=$PATH:$JAVA_HOME/bin

1.4刷新文件配置生效

source /etc/profile

1.5验证jdk是否生效

java -version

1.6关闭防火墙以及selinux

systemctl disabled firewalld
setenforce 0

2.在tomact1安装配置Tomact

2.1下载tomact

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

2.2查看文件并解压

ls 

anaconda-ks.cfg apache-tomcat-11.0.5.tar.gz
jdk-21_linux-x64_bin.tar.gz

tar -xzf  apache-tomcat-11.0.5.tar.gz  -C /usr/local
ls /usr/local

2.3配置tomact相关环境

vim /etc/profile
cat /etc/profile
export TOMACT_HOME=/usr/local/apache-tomcat-11.0.5
export PATH=$PATH:$TOMACT_HOME/bin

2.4刷新配置

source /etc/profile

2.5启动tomact

startup.sh

3.在tomact2上安装jdk以及tomact

3.1复制jdk安装目录

scp  -r /usr/local/jdk-21.0.6/ [email protected]:/usr/local/ >/dev/null

3.2复制tomact安装目录

scp  -r /usr/local/apache-tomcat-11.0.5/ [email protected]:/usr/local > /dev/null

3.3复制/etc/profile文件

 scp /etc/profile [email protected]:/etc/profile/

3.4验证复制的目录

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

source /etc/profile
java -version

3.6启动tomact

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主页

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
cat index.jsp
tomact1 10.10.10.41

4.2修改tomact2主页

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
cat index.jsp
tpmact 10.10.10.42
curl 10.10.10.42:8080

tomact 10.10.10.42

5.搭建nginx服务

5.1在master上搭建nginx服务
5.1.1配置仓库

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挂载仓库

mount /dev/sr0 /mnt

5.1.3 下载nginx服务

dnf install nginx -y

5.1.4 配置nginx

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服务

systemctl start nginx

5.1.6测试

curl http://10.10.10.30

tomact1 10.10.10.41

curl http://10.10.10.30

tomact2 10.10.10.42

5.2在backup上搭建nginxf服务
5.2.1配置仓库

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挂载仓库

mount /dev/sr0 /mnt

5.2.3 下载nginx服务

dnf install nginx -y

5.2.4 配置nginx

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服务

systemctl start nginx

5.2.6测试

curl 10.10.10.32

tomact1 10.10.10.41

curl 10.10.10.32

tomact2 10.10.10.42

6.搭建keepalived

6.1在master上搭建keepalived
6.1.1下载keepalived服务

dnf install keepalived -y

6.1.2配置keepalived文件

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服务

dnf install keepalived -y

6.2.2配置keepalived文件

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:

systemctl start keepalived

backup:

systemctl start keepalived

6.4 修改nginx文件

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;
			}
		}
		
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服务

systemctl reatart nginx
curl 10.10.10.100

tomact1 10.10.10.41

curl 10.10.10.100

tomact2 10.10.10.42

7.配置nginx高可用

7.1 编写检测脚本

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给脚本赋予执行权限

chmod +x /etc/keepalived/check_nginx.sh

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

scp /etc/keepalived/check_nginx.sh [email protected]:/etc/keepalived/

7.4在backup中验证

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文件

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文件

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服务

systemctl reatart keepalived

8.测试高可用

8.1关闭master上nginx服务

systemctl stop nginx
curl 10.10.10.100

tomact1 10.10.10.41

curl 10.10.10.100

tomact210.10.10.42

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

systemctl stop keepalived
systemctl stop nginx
curl 10.10.10.30

tomact1 10.10.10.41

curl 10.10.10.32

tomact2 10.10.10.42

ps -ef | grep nginx

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

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 的高可用就搭建完成。

你可能感兴趣的:(云原生,nginx,tomcat,运维)