使用keepalived結合tomcat和nginx搭建三主熱備架構

角色主機名軟件IP地址
用戶client172.25.250.90
keepalivedVIP172.25.250.100
keepalivedVIP172.25.250.101
keepalivedVIP172.25.250.102
masterserverAkeepalived, nginx172.25.250.30
backupserverBkeepalived, nginx172.25.250.31
backupserverCkeepalived, nginx172.25.250.32
webtomcat1tomcat172.25.250.41
webtomcat2tomcat172.25.250.42
webtomcat3tomcat172.25.250.43
DNS輪詢dnsnginx172.25.250.11

環境準備,修改主機名、IP 地址以及掛載倉庫

????????

關閉所有虛擬機的防火墻和 selinux(這里用 dns 示例)

[root@dns ~]# systemctl stop firewalld
[root@dns ~]# setenforce 0
[root@dns ~]# mount /dev/sr0 /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.

修改客戶機

[root@localhost ~]# hostnamectl hostname client
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 172.25.250.90/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 
[root@localhost ~]# nmcli c up ens160

修改 master

[root@localhost ~]# hostnamectl hostname master
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 172.25.250.30/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 
[root@localhost ~]# nmcli c up ens160

修改 backup1

[root@localhost ~]# hostnamectl hostname backup1
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 172.25.250.31/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 
[root@localhost ~]# nmcli c up ens160

修改 backup2

[root@localhost ~]# hostnamectl hostname backup2
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 172.25.250.32/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 
[root@localhost ~]# nmcli c up ens160

修改 tomcat1

[root@localhost ~]# hostnamectl hostname tomcat1
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 172.25.250.41/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 
[root@localhost ~]# nmcli c up ens160

修改 tomcat2

[root@localhost ~]# hostnamectl hostname tomcat2
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 172.25.250.42/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 
[root@localhost ~]# nmcli c up ens160

修改 tomcat3

[root@localhost ~]# hostnamectl hostname tomcat3
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 172.25.250.43/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 
[root@localhost ~]# nmcli c up ens160

修改 dns

[root@localhost ~]# hostnamectl hostname dns
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual ipv4.addresses 172.25.250.11/24 ipv4.gateway 172.25.250.2 ipv4.dns 223.5.5.5 
[root@localhost ~]# nmcli c up ens160

搭建 Tomcat

tomcat1 配置 JDK

官網:https://www.oracle.com

#1. 下載JDK21
[root@tomcat1 ~]# wget https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
--2025-03-20 19:29:50--  https://download.oracle.com/java/21/latest/jdk-21_linux-x64_bin.tar.gz
Resolving download.oracle.com (download.oracle.com)... 23.58.108.145
Connecting to download.oracle.com (download.oracle.com)|23.58.108.145|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 197405999 (188M) [application/x-gzip]
Saving to: ‘jdk-21_linux-x64_bin.tar.gz’jdk-21_linux-x64_bin.tar.gz 100%[===========================================>] 188.26M  1.60MB/s    in 78s     2025-03-20 19:31:10 (2.42 MB/s) - ‘jdk-21_linux-x64_bin.tar.gz’ saved [197405999/197405999]# 查看下載文件
[root@tomcat1 ~]# ls
anaconda-ks.cfg  jdk-21_linux-x64_bin.tar.gz# 2. 解壓JDK21
[root@tomcat1 ~]# tar -zxf jdk-21_linux-x64_bin.tar.gz -C /usr/local
[root@tomcat1 ~]# ls /usr/local
bin  etc  games  include  jdk-21.0.6  lib  lib64  libexec  sbin  share  src
[root@tomcat1 ~]# ls /usr/local/jdk-21.0.6/
bin  conf  include  jmods  legal  lib  LICENSE  man  README  release# 3. 配置JDK
[root@tomcat1 ~]# vim /etc/profile
....
export JAVA_HOME=/usr/local/jdk-21.0.6/
export PATH=$PATH:$JAVA_HOME/bin# 4. 刷新文件讓配置生效
[root@tomcat1 ~]# source /etc/profile# 5. 驗證JDK是否安裝成功
[root@tomcat1 ~]# java -version
java version "21.0.6" 2025-01-21 LTS
Java(TM) SE Runtime Environment (build 21.0.6+8-LTS-188)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.6+8-LTS-188, mixed mode, sharing)

tomcat1 配置 Tomcat

官網:Apache Tomcat? - Welcome!https://tomcat.apache.org/

# 1. 下載tomcat
[root@tomcat1 ~]# wget https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.5/bin/apache-tomcat-11.0.5.tar.gz
--2025-03-20 19:47:28--  https://dlcdn.apache.org/tomcat/tomcat-11/v11.0.5/bin/apache-tomcat-11.0.5.tar.gz
Resolving dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644
Connecting to dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13860898 (13M) [application/x-gzip]
Saving to: ‘apache-tomcat-11.0.5.tar.gz’apache-tomcat-11.0.5.tar.gz 100%[===========================================>]  13.22M  67.3KB/s    in 3m 27s  2025-03-20 19:50:57 (65.2 KB/s) - ‘apache-tomcat-11.0.5.tar.gz’ saved [13860898/13860898][root@tomcat1 ~]# ls
anaconda-ks.cfg  apache-tomcat-11.0.5.tar.gz  jdk-21_linux-x64_bin.tar.gz# 2. 解壓tomcat
[root@tomcat1 ~]# tar -zxf apache-tomcat-11.0.5.tar.gz -C /usr/local/
[root@tomcat1 ~]# cd /usr/local/
[root@tomcat1 local]# ls
apache-tomcat-11.0.5  bin  etc  games  include  jdk-21.0.6  lib  lib64  libexec  sbin  share  src
[root@tomcat1 local]# mv apache-tomcat-11.0.5/ tomcat-11.0.5/
[root@tomcat1 local]# cd tomcat-11.0.5/
[root@tomcat1 tomcat-11.0.5]# ls
bin           conf             lib      logs    README.md      RUNNING.txt  webapps
BUILDING.txt  CONTRIBUTING.md  LICENSE  NOTICE  RELEASE-NOTES  temp         work# 3. 配置tomcat
[root@tomcat1 tomcat-11.0.5]# vim /etc/profile
......
export TOMCAT_HOME=/usr/local/tomcat-11.0.5
export PATH=$PATH:$TOMCAT_HOME/bin# 4. 刷新配置
[root@tomcat1 bin]# source /etc/profile# 5. 啟動tomcat
[root@tomcat1 ~]# 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.

配置好,打開瀏覽器,輸入 http://172.25.250.41:8080,如果能夠看到貓,表示 tomcat 安裝成功。

?tomcat2 配置 JDK 和 Tomcat

# 1. 復制jdk安裝目錄
[root@tomcat1 ~]# scp -r /usr/local/jdk-21.0.6/ root@172.25.250.42:/usr/local/ > /dev/null
The authenticity of host '172.25.250.42 (172.25.250.42)' can't be established.
ED25519 key fingerprint is SHA256:zQRVAzxowh+vQParI9tLut0o4tqknS8RIH86Oa4QB/A.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.25.250.42' (ED25519) to the list of known hosts.
root@172.25.250.42's password: # 2. 復制tomcat安裝目錄
[root@tomcat1 ~]# scp -r /usr/local/tomcat-11.0.5/ root@172.25.250.42:/usr/local/ > /dev/null
root@172.25.250.42's password: # 3. 復制/etc/profile文件
[root@tomcat1 ~]# scp /etc/profile root@172.25.250.42:/etc
root@172.25.250.42's password: 
profile                                    100% 2053   452.9KB/s   00:00    # 4. 驗證復制的目錄
[root@tomcat2 ~]# ls /usr/local/
bin  games    jdk-21.0.6  lib64    sbin   src
etc  include  lib         libexec  share  tomcat-11.0.5# 5. 刷新/etc/profile
[root@tomcat2 ~]# source /etc/profile# 6. 查看JAVA是否安裝完成
[root@tomcat2 ~]# java -version
java version "21.0.6" 2025-01-21 LTS
Java(TM) SE Runtime Environment (build 21.0.6+8-LTS-188)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.6+8-LTS-188, mixed mode, sharing)# 7. 啟動tomcat
[root@tomcat2 ~]# 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.

配置好,打開瀏覽器,輸入 http://172.25.250.42:8080,如果能夠看到貓,表示 tomcat 安裝成功。

?tomcat3 配置 JDK 和 Tomcat

# 1. 復制jdk安裝目錄
[root@tomcat1 ~]# scp -r /usr/local/jdk-21.0.6/ root@172.25.250.43:/usr/local/ > /dev/null
The authenticity of host '172.25.250.43 (172.25.250.43)' can't be established.
ED25519 key fingerprint is SHA256:zQRVAzxowh+vQParI9tLut0o4tqknS8RIH86Oa4QB/A.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.25.250.42' (ED25519) to the list of known hosts.
root@172.25.250.43's password: # 2. 復制tomcat安裝目錄
[root@tomcat1 ~]# scp -r /usr/local/tomcat-11.0.5/ root@172.25.250.43:/usr/local/ > /dev/null
root@172.25.250.43's password: # 3. 復制/etc/profile文件
[root@tomcat1 ~]# scp /etc/profile root@172.25.250.43:/etc
root@172.25.250.43's password: 
profile                                    100% 2053   452.9KB/s   00:00    [root@tomcat3 ~]# ls /usr/local
bin  games    jdk-21.0.6  lib64    sbin   src
etc  include  lib         libexec  share  tomcat-11.0.5[root@tomcat3 ~]# source /etc/profile[root@tomcat3 ~]# java -version
java version "21.0.6" 2025-01-21 LTS
Java(TM) SE Runtime Environment (build 21.0.6+8-LTS-188)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.6+8-LTS-188, mixed mode, sharing)[root@tomcat3 ~]# 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.

配置好,打開瀏覽器,輸入 http://172.25.250.43:8080,如果能夠看到貓,表示 tomcat 安裝成功。

修改 tomcat 的主頁

tomcat1

[root@tomcat1 ~]# cd /usr/local/tomcat-11.0.5/webapps/
[root@tomcat1 webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@tomcat1 webapps]# rm -rf docs  examples  host-manager  manager
[root@tomcat1 webapps]# ls
ROOT
[root@tomcat1 webapps]# cd ROOT/
[root@tomcat1 ROOT]# ls
asf-logo-wide.svg  bg-nav.png    index.jsp          tomcat.svg
bg-button.png      bg-upper.png  RELEASE-NOTES.txt  WEB-INF
bg-middle.png      favicon.ico   tomcat.css
[root@tomcat1 ROOT]# rm -rf *
[root@tomcat1 ROOT]# vim index.jsp
[root@tomcat1 ROOT]# cat index.jsp
tomcat 172.25.250.41

修改后,在瀏覽器里面刷新頁面,得到以下界面

?tomcat2

[root@tomcat2 ~]# cd /usr/local/tomcat-11.0.5/webapps/
[root@tomcat2 webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@tomcat2 webapps]# rm -rf docs examples host-manager manager
[root@tomcat2 webapps]# ls
ROOT
[root@tomcat2 webapps]# cd ROOT/
[root@tomcat2 ROOT]# ls
asf-logo-wide.svg  bg-nav.png    index.jsp          tomcat.svg
bg-button.png      bg-upper.png  RELEASE-NOTES.txt  WEB-INF
bg-middle.png      favicon.ico   tomcat.css
[root@tomcat2 ROOT]# rm -rf *
[root@tomcat2 ROOT]# vim index.jsp
[root@tomcat2 ROOT]# cat index.jsp 
tomcat 172.25.250.42

修改后,在瀏覽器里面刷新頁面,得到以下界面

tomcat3

[root@tomcat3 ~]# cd /usr/local/tomcat-11.0.5/webapps/
[root@tomcat3 webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@tomcat3 webapps]# rm -rf docs examples host-manager manager
[root@tomcat3 webapps]# ls
ROOT
[root@tomcat3 webapps]# cd ROOT/
[root@tomcat3 ROOT]# ls
asf-logo-wide.svg  bg-nav.png    index.jsp          tomcat.svg
bg-button.png      bg-upper.png  RELEASE-NOTES.txt  WEB-INF
bg-middle.png      favicon.ico   tomcat.css
[root@tomcat3 ROOT]# rm -rf *
[root@tomcat3 ROOT]# vim index.jsp
[root@tomcat3 ROOT]# cat index.jsp 
tomcat 172.25.250.43

?修改后,在瀏覽器里面刷新頁面,得到以下界面

搭建 nginx

master?配置 nginx

#下載nginx
[root@master ~]# dnf install -y nginx
Updating Subscription Management repositories.
Unable to read consumer identityThis system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.Last metadata expiration check: 0:01:12 ago on Thu 27 Mar 2025 07:53:25 PM CST.
Dependencies resolved.
=============================================================================Package                  Architecture Version               Repo       Size
=============================================================================
Installing:nginx                    x86_64       2:1.20.1-20.el9       app        40 k
Installing dependencies:nginx-core               x86_64       2:1.20.1-20.el9       app       574 knginx-filesystem         noarch       2:1.20.1-20.el9       app        11 kredhat-logos-httpd       noarch       90.4-2.el9            app        18 kTransaction Summary
=============================================================================
Install  4 PackagesTotal size: 643 k
Installed size: 1.8 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transactionPreparing        :                                                     1/1 Running scriptlet: nginx-filesystem-2:1.20.1-20.el9.noarch             1/4 Installing       : nginx-filesystem-2:1.20.1-20.el9.noarch             1/4 Installing       : nginx-core-2:1.20.1-20.el9.x86_64                   2/4 Installing       : redhat-logos-httpd-90.4-2.el9.noarch                3/4 Installing       : nginx-2:1.20.1-20.el9.x86_64                        4/4 Running scriptlet: nginx-2:1.20.1-20.el9.x86_64                        4/4 Verifying        : nginx-2:1.20.1-20.el9.x86_64                        1/4 Verifying        : nginx-core-2:1.20.1-20.el9.x86_64                   2/4 Verifying        : nginx-filesystem-2:1.20.1-20.el9.noarch             3/4 Verifying        : redhat-logos-httpd-90.4-2.el9.noarch                4/4 
Installed products updated.Installed:nginx-2:1.20.1-20.el9.x86_64                                               nginx-core-2:1.20.1-20.el9.x86_64                                          nginx-filesystem-2:1.20.1-20.el9.noarch                                    redhat-logos-httpd-90.4-2.el9.noarch                                       Complete!#配置nginx
[root@master ~]# vim /etc/nginx/conf.d/master.conf
[root@master ~]# cat /etc/nginx/conf.d/master.conf 
upstream tomcat {server  172.25.250.41:8080;server  172.25.250.42:8080;server  172.25.250.43:8080;
}       
server {listen  80;server_name     172.25.250.30;access_log      /var/log/nginx/master_access.log;error_log       /var/log/nginx/master_error.log;location / {proxy_pass      http://tomcat;}
}#啟動nginx
[root@master ~]# systemctl start nginx#測試
[root@master ~]# curl http://172.25.250.30
tomcat 172.25.250.41
[root@master ~]# curl http://172.25.250.30
tomcat 172.25.250.42
[root@master ~]# curl http://172.25.250.30
tomcat 172.25.250.43
[root@master ~]# curl http://172.25.250.30
tomcat 172.25.250.41
[root@master ~]# curl http://172.25.250.30
tomcat 172.25.250.42
[root@master ~]# curl http://172.25.250.30
tomcat 172.25.250.43
[root@master ~]# curl http://172.25.250.30
tomcat 172.25.250.41
[root@master ~]# curl http://172.25.250.30
tomcat 172.25.250.42

backup1??配置 nginx

#下載nginx
[root@backup1 ~]# dnf install -y nginx
Updating Subscription Management repositories.
Unable to read consumer identityThis system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.Last metadata expiration check: 0:19:33 ago on Thu 27 Mar 2025 07:39:23 PM CST.
Dependencies resolved.
=============================================================================Package                  Architecture Version               Repo       Size
=============================================================================
Installing:nginx                    x86_64       2:1.20.1-20.el9       app        40 k
Installing dependencies:nginx-core               x86_64       2:1.20.1-20.el9       app       574 knginx-filesystem         noarch       2:1.20.1-20.el9       app        11 kredhat-logos-httpd       noarch       90.4-2.el9            app        18 kTransaction Summary
=============================================================================
Install  4 PackagesTotal size: 643 k
Installed size: 1.8 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transactionPreparing        :                                                     1/1 Running scriptlet: nginx-filesystem-2:1.20.1-20.el9.noarch             1/4 Installing       : nginx-filesystem-2:1.20.1-20.el9.noarch             1/4 Installing       : nginx-core-2:1.20.1-20.el9.x86_64                   2/4 Installing       : redhat-logos-httpd-90.4-2.el9.noarch                3/4 Installing       : nginx-2:1.20.1-20.el9.x86_64                        4/4 Running scriptlet: nginx-2:1.20.1-20.el9.x86_64                        4/4 Verifying        : nginx-2:1.20.1-20.el9.x86_64                        1/4 Verifying        : nginx-core-2:1.20.1-20.el9.x86_64                   2/4 Verifying        : nginx-filesystem-2:1.20.1-20.el9.noarch             3/4 Verifying        : redhat-logos-httpd-90.4-2.el9.noarch                4/4 
Installed products updated.Installed:nginx-2:1.20.1-20.el9.x86_64                                               nginx-core-2:1.20.1-20.el9.x86_64                                          nginx-filesystem-2:1.20.1-20.el9.noarch                                    redhat-logos-httpd-90.4-2.el9.noarch                                       Complete!#配置nginx
[root@backup1 ~]# vim /etc/nginx/conf.d/backup1.conf
[root@backup1 ~]# cat /etc/nginx/conf.d/backup1.conf
upstream backup1 {server  172.25.250.41:8080;server  172.25.250.42:8080;server  172.25.250.43:8080;
}       
server {listen  80;server_name     172.25.250.31;access_log      /var/log/nginx/backup_access.log;error_log       /var/log/nginx/backup_error.log;location / {proxy_pass      http://backup1;}       
}#啟動nginx
[root@backup1 ~]# systemctl start nginx#測試
[root@backup1 ~]# curl 172.25.250.31
tomcat 172.25.250.41
[root@backup1 ~]# curl 172.25.250.31
tomcat 172.25.250.42
[root@backup1 ~]# curl 172.25.250.31
tomcat 172.25.250.43
[root@backup1 ~]# curl 172.25.250.31
tomcat 172.25.250.41
[root@backup1 ~]# curl 172.25.250.31
tomcat 172.25.250.42
[root@backup1 ~]# curl 172.25.250.31
tomcat 172.25.250.43

backup2? 配置?nginx

#下載nginx
[root@backup2 ~]# dnf install -y nginx
Updating Subscription Management repositories.
Unable to read consumer identityThis system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.Last metadata expiration check: 0:17:57 ago on Thu 27 Mar 2025 07:47:18 PM CST.
Dependencies resolved.
=============================================================================Package                  Architecture Version               Repo       Size
=============================================================================
Installing:nginx                    x86_64       2:1.20.1-20.el9       app        40 k
Installing dependencies:nginx-core               x86_64       2:1.20.1-20.el9       app       574 knginx-filesystem         noarch       2:1.20.1-20.el9       app        11 kredhat-logos-httpd       noarch       90.4-2.el9            app        18 kTransaction Summary
=============================================================================
Install  4 PackagesTotal size: 643 k
Installed size: 1.8 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transactionPreparing        :                                                     1/1 Running scriptlet: nginx-filesystem-2:1.20.1-20.el9.noarch             1/4 Installing       : nginx-filesystem-2:1.20.1-20.el9.noarch             1/4 Installing       : nginx-core-2:1.20.1-20.el9.x86_64                   2/4 Installing       : redhat-logos-httpd-90.4-2.el9.noarch                3/4 Installing       : nginx-2:1.20.1-20.el9.x86_64                        4/4 Running scriptlet: nginx-2:1.20.1-20.el9.x86_64                        4/4 Verifying        : nginx-2:1.20.1-20.el9.x86_64                        1/4 Verifying        : nginx-core-2:1.20.1-20.el9.x86_64                   2/4 Verifying        : nginx-filesystem-2:1.20.1-20.el9.noarch             3/4 Verifying        : redhat-logos-httpd-90.4-2.el9.noarch                4/4 
Installed products updated.Installed:nginx-2:1.20.1-20.el9.x86_64                                               nginx-core-2:1.20.1-20.el9.x86_64                                          nginx-filesystem-2:1.20.1-20.el9.noarch                                    redhat-logos-httpd-90.4-2.el9.noarch                                       Complete!#配置nginx
[root@backup2 ~]# vim /etc/nginx/conf.d/backup2.conf
[root@backup2 ~]# cat /etc/nginx/conf.d/backup2.conf
upstream backup2 {server  172.25.250.41:8080;server  172.25.250.42:8080;server  172.25.250.43:8080;
}       
server {listen  80;server_name     172.25.250.32;access_log      /var/log/nginx/backup_access.log;error_log       /var/log/nginx/backup_error.log;location / {proxy_pass      http://backup2;}       
}#啟動nginx
[root@backup2 ~]# systemctl start nginx#測試
[root@backup2 ~]# curl 172.25.250.32
tomcat 172.25.250.41
[root@backup2 ~]# curl 172.25.250.32
tomcat 172.25.250.42
[root@backup2 ~]# curl 172.25.250.32
tomcat 172.25.250.43
[root@backup2 ~]# curl 172.25.250.32
tomcat 172.25.250.41
[root@backup2 ~]# curl 172.25.250.32
tomcat 172.25.250.42

?dns 配置 nginx

#下載nginx
[root@dns ~]# dnf install -y nginx
Updating Subscription Management repositories.
Unable to read consumer identityThis system is not registered with an entitlement server. You can use "rhc" or "subscription-manager" to register.Last metadata expiration check: 0:55:27 ago on Thu 27 Mar 2025 07:39:25 PM CST.
Dependencies resolved.
=============================================================================Package                  Architecture Version               Repo       Size
=============================================================================
Installing:nginx                    x86_64       2:1.20.1-20.el9       app        40 k
Installing dependencies:nginx-core               x86_64       2:1.20.1-20.el9       app       574 knginx-filesystem         noarch       2:1.20.1-20.el9       app        11 kredhat-logos-httpd       noarch       90.4-2.el9            app        18 kTransaction Summary
=============================================================================
Install  4 PackagesTotal size: 643 k
Installed size: 1.8 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transactionPreparing        :                                                     1/1 Running scriptlet: nginx-filesystem-2:1.20.1-20.el9.noarch             1/4 Installing       : nginx-filesystem-2:1.20.1-20.el9.noarch             1/4 Installing       : nginx-core-2:1.20.1-20.el9.x86_64                   2/4 Installing       : redhat-logos-httpd-90.4-2.el9.noarch                3/4 Installing       : nginx-2:1.20.1-20.el9.x86_64                        4/4 Running scriptlet: nginx-2:1.20.1-20.el9.x86_64                        4/4 Verifying        : nginx-2:1.20.1-20.el9.x86_64                        1/4 Verifying        : nginx-core-2:1.20.1-20.el9.x86_64                   2/4 Verifying        : nginx-filesystem-2:1.20.1-20.el9.noarch             3/4 Verifying        : redhat-logos-httpd-90.4-2.el9.noarch                4/4 
Installed products updated.Installed:nginx-2:1.20.1-20.el9.x86_64                                               nginx-core-2:1.20.1-20.el9.x86_64                                          nginx-filesystem-2:1.20.1-20.el9.noarch                                    redhat-logos-httpd-90.4-2.el9.noarch                                       Complete!#修改配置文件
[root@dns ~]# vim /etc/nginx/conf.d/dns.conf
[root@dns ~]# cat /etc/nginx/conf.d/dns.conf
upstream web {server 172.25.250.100;server 172.25.250.101;server 172.25.250.102;
}       
server {listen  80;server_name     172.25.250.11;access_log      /var/log/nginx/dns_access.log;error_log       /var/log/nginx/dns_error.log;location / {proxy_pass      http://web;}       
}#啟動nginx
[root@dns ~]# systemctl start nginx#測試
[root@dns ~]# for ((i=1;i<=10;i++)); do curl 172.25.250.11;done;
tomcat 172.25.250.42
tomcat 172.25.250.41
tomcat 172.25.250.42
tomcat 172.25.250.43
tomcat 172.25.250.42
tomcat 172.25.250.43
tomcat 172.25.250.41
tomcat 172.25.250.43
tomcat 172.25.250.41
tomcat 172.25.250.42
[root@dns ~]# 

搭建 keepalived

master 配置 keepalived

#下載keepalived
[root@master ~]# dnf install -y keepalived
……
Complete!#配置keepalived
[root@master ~]# vim /etc/keepalived/keepalived.conf 
[root@master ~]# cat /etc/keepalived/keepalived.conf 
global_defs {router_id LVS_master
}vrrp_instance VI_1 {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.100}
}vrrp_instance VI_2 {state BACKUPinterface ens160virtual_router_id 52priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.101}
}vrrp_instance VI_3 {state BACKUPinterface ens160virtual_router_id 53priority 60advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.102}
}#啟動keepalived
[root@master ~]# systemctl start keepalived#查看漂移IP
[root@master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000link/ether 00:0c:29:3c:4d:e9 brd ff:ff:ff:ff:ff:ffaltname enp3s0inet 172.25.250.30/24 brd 172.25.250.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet 172.25.250.100/32 scope global ens160valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe3c:4de9/64 scope link noprefixroute valid_lft forever preferred_lft forever

backup1 配置 keepalived

#下載nginx
[root@backup1 ~]# dnf install -y keepalived#從master上拷貝配置文件過去,方便后續的修改且不容易修改錯
[root@master ~]# scp /etc/keepalived/keepalived.conf root@172.25.250.31:/etc/keepalived/keepalived.conf
The authenticity of host '172.25.250.31 (172.25.250.31)' can't be established.
ED25519 key fingerprint is SHA256:zQRVAzxowh+vQParI9tLut0o4tqknS8RIH86Oa4QB/A.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.25.250.31' (ED25519) to the list of known hosts.
root@172.25.250.31's password: 
keepalived.conf                            100%  780   242.7KB/s   00:00    [root@backup1 ~]# vim  /etc/keepalived/keepalived.conf 
[root@backup1 ~]# cat /etc/keepalived/keepalived.conf 
global_defs {router_id LVS_backup
}vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 60advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.100}
}vrrp_instance VI_2 {state MASTERinterface ens160virtual_router_id 52priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.101}
}vrrp_instance VI_3 {state BACKUPinterface ens160virtual_router_id 53priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.102}
}[root@backup1 ~]# ip ad
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000link/ether 00:0c:29:b5:c5:e2 brd ff:ff:ff:ff:ff:ffaltname enp3s0inet 172.25.250.31/24 brd 172.25.250.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet 172.25.250.101/32 scope global ens160valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:feb5:c5e2/64 scope link noprefixroute valid_lft forever preferred_lft forever

backup2 配置 keepalived

#下載keepalived
[root@backup2 ~]# dnf install -y keepalived#從master上拷貝配置文件過去,方便后續的修改且不容易修改錯
[root@master ~]# scp /etc/keepalived/keepalived.conf root@172.25.250.32:/etc/keepalived/keepalived.conf
The authenticity of host '172.25.250.32 (172.25.250.32)' can't be established.
ED25519 key fingerprint is SHA256:zQRVAzxowh+vQParI9tLut0o4tqknS8RIH86Oa4QB/A.
This host key is known by the following other names/addresses:~/.ssh/known_hosts:1: 172.25.250.31
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '172.25.250.32' (ED25519) to the list of known hosts.
root@172.25.250.32's password: 
keepalived.conf                            100%  780   275.3KB/s   00:00    #修改配置文件
[root@backup2 ~]# vim /etc/keepalived/keepalived.conf 
[root@backup2 ~]# cat /etc/keepalived/keepalived.conf 
global_defs {router_id LVS_master
}vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.100}
}vrrp_instance VI_2 {state BACKUPinterface ens160virtual_router_id 52priority 60advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.101}
}vrrp_instance VI_3 {state MASTERinterface ens160virtual_router_id 53priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.102}
}#啟動keepalived
[root@backup2 ~]# systemctl start keepalived#查看漂移IP
[root@backup2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000link/ether 00:0c:29:1d:25:2d brd ff:ff:ff:ff:ff:ffaltname enp3s0inet 172.25.250.32/24 brd 172.25.250.255 scope global noprefixroute ens160valid_lft forever preferred_lft foreverinet 172.25.250.102/32 scope global ens160valid_lft forever preferred_lft foreverinet6 fe80::20c:29ff:fe1d:252d/64 scope link noprefixroute valid_lft forever preferred_lft forever

修改nginx

master

[root@master ~]# vim /etc/nginx/conf.d/master.conf 
[root@master ~]# cat /etc/nginx/conf.d/master.conf 
upstream tomcat {server  172.25.250.41:8080;server  172.25.250.42:8080;server  172.25.250.43:8080;
}       
server {listen  80;server_name     172.25.250.100;access_log      /var/log/nginx/master_access.log;error_log       /var/log/nginx/master_error.log;location / {proxy_pass      http://tomcat;}
}[root@master ~]# systemctl restart nginx
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.41
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.42
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.43
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.41

backup1

[root@backup1 ~]# vim /etc/nginx/conf.d/backup1.conf 
[root@backup1 ~]# cat /etc/nginx/conf.d/backup1.conf 
upstream backup1 {server  172.25.250.41:8080;server  172.25.250.42:8080;server  172.25.250.43:8080;
}       
server {listen  80;server_name     172.25.250.101;access_log      /var/log/nginx/backup_access.log;error_log       /var/log/nginx/backup_error.log;location / {proxy_pass      http://backup1;}       
}[root@backup1 ~]# systemctl restart nginx
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.41
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.42
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.43

backup2

[root@backup2 ~]# vim /etc/nginx/conf.d/backup2.conf 
[root@backup2 ~]# cat /etc/nginx/conf.d/backup2.conf 
upstream backup2 {server  172.25.250.41:8080;server  172.25.250.42:8080;server  172.25.250.43:8080;
}       
server {listen  80;server_name     172.25.250.102;access_log      /var/log/nginx/backup_access.log;error_log       /var/log/nginx/backup_error.log;location / {proxy_pass      http://backup2;}       
}
[root@backup2 ~]# systemctl restart nginx
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.41
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.42
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.43

配置 nginx 高可用

編寫檢測腳本

[root@master ~]# vim /etc/keepalived/check_nginx.sh
[root@master ~]# cat /etc/keepalived/check_nginx.sh
#!/bin/bash
counter=$(ps -C nginx --no-header|wc -l)
if [ $counter -eq 0 ]; thensystemctl start nginxif [ `ps -C nginx --no-header|wc -l` -eq 0 ]; thensystemctl stop keepalivedfi
fi

給腳本文件賦予可執行權限

[root@master ~]# chmod +x /etc/keepalived/check_nginx.sh 
[root@master ~]# ll /etc/keepalived/check_nginx.sh
-rwxr-xr-x. 1 root root 191 Mar 27 20:47 /etc/keepalived/check_nginx.sh

將腳本文件復制到 backup1 和 backup2 主機中

#拷貝到backup1上
[root@master ~]# scp /etc/keepalived/check_nginx.sh root@172.25.250.32:/etc/keepalived/
root@172.25.250.32's password: 
check_nginx.sh                             100%  191    63.8KB/s   00:00    #拷貝到backup2上
[root@master ~]# scp /etc/keepalived/check_nginx.sh root@172.25.250.31:/etc/keepalived/
root@172.25.250.31's password: 
check_nginx.sh                             100%  191    83.1KB/s   00:00    #在backup1上驗證是否拷貝成功
[root@backup1 ~]# ll /etc/keepalived/
total 8
-rwxr-xr-x. 1 root root 191 Mar 27 20:48 check_nginx.sh
-rw-r--r--. 1 root root 780 Mar 27 20:25 keepalived.conf#在backup2上驗證是否拷貝成功
[root@backup2 ~]# ll /etc/keepalived/
total 8
-rwxr-xr-x. 1 root root 191 Mar 27 20:48 check_nginx.sh
-rw-r--r--. 1 root root 780 Mar 27 20:27 keepalived.conf

修改 master 主機上的 keepavlied.conf 文件

????????在該文件上增加執行腳本的函數以及調用

[root@master ~]# vim /etc/keepalived/keepalived.conf
[root@master ~]# cat /etc/keepalived/keepalived.conf
global_defs {router_id LVS_master
}vrrp_script chk_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 {172.25.250.100}track_script {chk_nginx}
}vrrp_instance VI_2 {state BACKUPinterface ens160virtual_router_id 52priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.101}track_script {chk_nginx}
}vrrp_instance VI_3 {state BACKUPinterface ens160virtual_router_id 53priority 60advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.102}track_script {chk_nginx}
}

修改 backup1 主機上的 keepavlied.conf 文件

[root@backup1 ~]# vim /etc/keepalived/keepalived.conf 
[root@backup1 ~]# cat /etc/keepalived/keepalived.conf 
global_defs {router_id LVS_backup
}vrrp_script chk_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 {172.25.250.100}track_script {chk_nginx}
}vrrp_instance VI_2 {state MASTERinterface ens160virtual_router_id 52priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.101}track_script {chk_nginx}
}vrrp_instance VI_3 {state BACKUPinterface ens160virtual_router_id 53priority 80advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.102}track_script {chk_nginx}
}

修改 backup2 主機上的 keepavlied.conf 文件

[root@backup2 ~]# vim /etc/keepalived/keepalived.conf 
[root@backup2 ~]# cat /etc/keepalived/keepalived.conf 
global_defs {router_id LVS_master
}vrrp_script chk_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 {172.25.250.100}track_script {chk_nginx}
}vrrp_instance VI_2 {state BACKUPinterface ens160virtual_router_id 52priority 60advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.101}track_script {chk_nginx}
}vrrp_instance VI_3 {state MASTERinterface ens160virtual_router_id 53priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {172.25.250.102}track_script {chk_nginx}
}

重啟 keepalived 服務(配置的三臺主機都要重啟)

[root@master ~]# systemctl restart keepalived.service[root@backup1 ~]# systemctl restart keepalived.service[root@backup2 ~]# systemctl restart keepalived.service

測試高可用

? 關閉 master 主機上的 nginx 服務

[root@master ~]# systemctl stop nginx
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.41
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.42
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.43
[root@master ~]# ps -ef | grep nginx
root       14539       1  0 21:03 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      14540   14539  0 21:03 ?        00:00:00 nginx: worker process
nginx      14541   14539  0 21:03 ?        00:00:00 nginx: worker process
nginx      14542   14539  0 21:03 ?        00:00:00 nginx: worker process
nginx      14543   14539  0 21:03 ?        00:00:00 nginx: worker process
root       14587   12840  0 21:03 pts/1    00:00:00 grep --color=auto nginx

關閉 master 主機上的 keepalived 和 nginx

[root@master ~]# systemctl stop keepalived.service
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.41
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.42
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.43[root@master ~]# systemctl stop nginx
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.41
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.42
[root@master ~]# curl 172.25.250.100
tomcat 172.25.250.43
[root@master ~]# ps -ef | grep nginx
root       14623   12840  0 21:04 pts/1    00:00:00 grep --color=auto nginx[root@master ~]#  systemctl start keepalived.service
[root@master ~]# ps -ef | grep nginx
root       14636       1  0 21:05 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      14637   14636  0 21:05 ?        00:00:00 nginx: worker process
nginx      14638   14636  0 21:05 ?        00:00:00 nginx: worker process
nginx      14639   14636  0 21:05 ?        00:00:00 nginx: worker process
nginx      14640   14636  0 21:05 ?        00:00:00 nginx: worker process
root       14653   12840  0 21:05 pts/1    00:00:00 grep --color=auto nginx

? 關閉 backup1?主機上的 nginx 服務?

[root@backup1 ~]# systemctl stop nginx
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.41
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.42
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.43
[root@backup1 ~]# ps -ef | grep nginx
root       15182       1  0 21:08 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      15183   15182  0 21:08 ?        00:00:00 nginx: worker process
nginx      15184   15182  0 21:08 ?        00:00:00 nginx: worker process
nginx      15185   15182  0 21:08 ?        00:00:00 nginx: worker process
nginx      15186   15182  0 21:08 ?        00:00:00 nginx: worker process
root       15226   12839  0 21:08 pts/1    00:00:00 grep --color=auto nginx

關閉 backup1 主機上的 keepalived 和 nginx?

[root@backup1 ~]# systemctl stop keepalived.service 
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.41
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.42
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.43[root@backup1 ~]# systemctl stop nginx
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.41
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.42
[root@backup1 ~]# curl 172.25.250.101
tomcat 172.25.250.43
[root@backup1 ~]# ps -ef | grep nginx
root       15289   12839  0 21:09 pts/1    00:00:00 grep --color=auto nginx[root@backup1 ~]# systemctl start keepalived.service 
[root@backup1 ~]# ps -ef | grep nginx
root       15317       1  0 21:09 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      15318   15317  0 21:09 ?        00:00:00 nginx: worker process
nginx      15319   15317  0 21:09 ?        00:00:00 nginx: worker process
nginx      15320   15317  0 21:09 ?        00:00:00 nginx: worker process
nginx      15321   15317  0 21:09 ?        00:00:00 nginx: worker process
root       15330   12839  0 21:09 pts/1    00:00:00 grep --color=auto nginx

? 關閉 backup2?主機上的 nginx 服務?

[root@backup2 ~]# systemctl stop nginx
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.41
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.42
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.43
[root@backup2 ~]# ps -ef | grep nginx
root       15635       1  0 21:12 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      15636   15635  0 21:12 ?        00:00:00 nginx: worker process
nginx      15637   15635  0 21:12 ?        00:00:00 nginx: worker process
nginx      15638   15635  0 21:12 ?        00:00:00 nginx: worker process
nginx      15639   15635  0 21:12 ?        00:00:00 nginx: worker process
root       15691   12839  0 21:12 pts/1    00:00:00 grep --color=auto nginx

關閉 backup2 主機上的 keepalived 和 nginx?

[root@backup2 ~]# systemctl stop keepalived.service 
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.41
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.42
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.43[root@backup2 ~]# systemctl stop nginx
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.41
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.42
[root@backup2 ~]# curl 172.25.250.102
tomcat 172.25.250.43
[root@backup2 ~]# ps -ef | grep nginx
root       15746   12839  0 21:13 pts/1    00:00:00 grep --color=auto nginx[root@backup2 ~]# systemctl start keepalived.service 
[root@backup2 ~]# ps -ef | grep nginx
root       15759       1  0 21:13 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx      15760   15759  0 21:13 ?        00:00:00 nginx: worker process
nginx      15761   15759  0 21:13 ?        00:00:00 nginx: worker process
nginx      15762   15759  0 21:13 ?        00:00:00 nginx: worker process
nginx      15763   15759  0 21:13 ?        00:00:00 nginx: worker process
root       15772   12839  0 21:13 pts/1    00:00:00 grep --color=auto nginx

根據以上操作得出結論:當我們關閉 nginx 服務后,編寫的腳本就會執行,從而自動幫我們把 nginx 服務啟動起來。

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/74022.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/74022.shtml
英文地址,請注明出處:http://en.pswp.cn/web/74022.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

STRUCTBERT:將語言結構融入預訓練以提升深度語言理解

【摘要】最近&#xff0c;預訓練語言模型BERT&#xff08;及其經過穩健優化的版本RoBERTa&#xff09;在自然語言理解&#xff08;NLU&#xff09;領域引起了廣泛關注&#xff0c;并在情感分類、自然語言推理、語義文本相似度和問答等各種NLU任務中達到了最先進的準確率。受到E…

leetcode_977. 有序數組的平方_java

977. 有序數組的平方https://leetcode.cn/problems/squares-of-a-sorted-array/ 1.題目 給你一個按 非遞減順序 排序的整數數組 nums&#xff0c;返回 每個數字的平方 組成的新數組&#xff0c;要求也按 非遞減順序 排序。 示例 1&#xff1a; 輸入&#xff1a;nums [-4,-1…

Nginx—nginx.conf 配置結構詳解

一、nginx.conf 配置結構 函數 說明 main 全局配置 event 配置工作模式以及連接數 http http模塊相關配置 server 虛擬主機配置&#xff0c;可以有多個 location 路由規則&#xff0c;表達式 upstream 集群、內網服務器&#xff08;負載均衡也在這里邊配&#xff…

斐波那契數列----C語言

關于斐波那契 已知&#xff1a; 問題背景&#xff1a;一對兔子從第3個月開始每月生一對新兔子&#xff0c;新兔子同樣在第3個月開始繁殖。 關鍵觀察&#xff1a; 第1個月&#xff1a;1對&#xff08;初始兔子&#xff09;。 第2個月&#xff1a;1對&#xff08;未成熟&#…

vulhub靶場—— Tomcat8

目錄 一、漏洞描述 二、靶場搭建 三、漏洞復現 1、弱密碼 2、文件上傳 一、漏洞描述 環境描述&#xff1a; Tomcat 支持后臺部署 war 文件&#xff0c;可以直接將 webshell 部署到 web 目錄下。tomcat 默認的管理頁面 manager 使用 basic 認證用戶名和密碼登錄&#xff0…

使用 Spring AI Aliabab Module RAG 構建 Web Search 應用

使用 Spring AI Alibaba 構建大模型聯網搜索應用 Spring AI 實現了模塊化 RAG 架構&#xff0c;架構的靈感來自于論文“模塊化 RAG&#xff1a;將 RAG 系統轉變為類似樂高的可重構框架”中詳述的模塊化概念。 Spring AI 模塊化 RAG 體系 總體上分為以下幾個步驟&#xff1a; …

一些練習 C 語言的小游戲

一些練習 C 語言的小游戲 — 1. 猜數字游戲 描述&#xff1a;程序隨機生成一個數字&#xff0c;玩家需要猜測這個數字&#xff0c;并根據提示&#xff08;太高或太低&#xff09;調整猜測&#xff0c;直到猜中為止。 功能點&#xff1a; 隨機數生成 (rand() 函數)。循環和…

關于中文編程的一些思考

隨著信息化與數字化的發展&#xff0c;工業4.0時代亦將徐徐到來。當計算機的普及程度越來越高&#xff0c;數據的產生、傳輸、處理等變得越來越快、越來越大量的時候&#xff0c;人們想要自動化辦公的愿望也越來越強烈&#xff0c;希望能將自身從耗費腦力但是重復繁瑣的工作中解…

golang 日志log與logrus

目錄 一、Go 標準庫 log 詳解 1. 功能特點 2. 常用函數 3. 示例代碼 4. 優勢和局限 二、第三方庫 logrus 詳解 1. 功能特點 2. 核心功能 3. 示例代碼 4. 優勢和擴展性 三、總結 1. 何時選擇 log&#xff1f; 2. 何時選擇 logrus&#xff1f; 3. 對比總結 一、Go 標…

消費品行業創新創業中品類創新與數字化工具的融合:以開源 AI 智能客服、AI 智能名片及 S2B2C 商城小程序為例

摘要&#xff1a; 本文聚焦于消費品行業的創新與創業&#xff0c;深入探討“選擇大于努力”這一觀點&#xff0c;強調品類選擇在品牌發展中的關鍵作用。同時&#xff0c;詳細分析了品類創新對于新消費品牌崛起以及傳統品牌轉型的重要意義。在此基礎上&#xff0c;引入開源 AI 智…

Razer macOS v0.4.10快速安裝

鏈接點這里下載最新的 .dmg 文件。將下載的 .dmg 映像文件拖入 應用程序 文件夾中。若首次打開時出現安全警告【什么扔到廢紙簍】&#xff0c;這時候點擊 Mac 的“系統偏好設置”-> “安全性與隱私”-> “通用”&#xff0c;然后點擊底部的 “打開”。【或者仍然打開】 對…

Flask項目部署:Flask + uWSGI + Nginx

目錄 1,網絡架構 2,環境安裝 2.1,安裝yum:Shell軟件包管理器 2.2 安裝python 2.3 安裝uWSGI 2.4 安裝Flask 3,上傳工程包到服務器,打包Flask項目 4,創建和配置 uwsgi 配置文件 uwsgi.ini 4.1配置文件 4.2配置文件注釋詳解 5,啟動服務 6,安裝nginx 7,nginx配置 8,…

[FPGA基礎學習]實現流水燈與按鍵暫停

FPGA實現LED流水燈 1.vscode的安裝和使用 vscode下載 Visual Studio Code - Code Editing. Redefined vscode插件&#xff08;Verilog-HDL/SystemVerilog&#xff09;下載 quartus綁定vscode 2.用6個LED完成周期為1秒的跑馬燈效果 流水燈模塊設計 時鐘輸入 DE2-115開發板…

【TensorRT】TensorRT從安裝到推理——Python 環境下 MobileNetV4 三分類任務

我想開發一個基于深度學習的分類小軟件&#xff0c;逐漸了解到了TensorRT在模型推理速度上的優勢&#xff0c;經過一下午資料的查找實現了將onnx模型轉為TensorRT格式模型的推理及測試過程。將實現過程記錄下來方便日后查看。 本文實驗設備是MX350顯卡 2G顯存 一 、安裝Tenso…

1.兩數之和(Java)

1. 題目描述 LeetCode 1. 兩數之和&#xff08;Two Sum&#xff09; 給定一個整數數組 nums 和一個目標值 target&#xff0c;請你在該數組中找出和為目標值的那兩個整數&#xff0c;并返回它們的索引。 示例 1&#xff1a; 輸入&#xff1a;nums [2,7,11,15], target 9 …

《深入探索 Python 數據分析:用 Pandas 高效處理與可視化大型數據集》

《深入探索 Python 數據分析:用 Pandas 高效處理與可視化大型數據集》 引言:從零到分析高手 數據是當代社會最寶貴的資源,而數據分析技能是現代職業人不可或缺的一部分。在數據科學的領域中,Python 已成為當之無愧的“首選語言”,其強大的生態系統和簡潔的語法讓人如虎添…

將樹莓派5當做Ollama服務器,C#調用generate的API的示例

其實完全沒這個必要&#xff0c;性能用腳后跟想都會很差。但基于上一篇文章的成果&#xff0c;來都來了就先簡單試試吧。 先來看看這個拼夕夕上五百多塊錢能達到的效果&#xff1a; 只要對速度沒要求&#xff0c;那感覺就還行。 Ollama默認只在本地回環&#xff08;127.0.0…

python基礎學習二(列表及字典的使用)

文章目錄 列表列表的創建獲取列表中的多個元素判斷列表中元素是否存在列表元素的添加操作列表元素的刪除操作列表元素的修改列表的排序列表生成式 字典字典的創建字典的常規操作字典的常用操作字典的視圖操作字典元素的遍歷字典的特點字典的生成式 列表 一個對象由id&#xff0…

Android設計模式之代理模式

一、定義&#xff1a; 為其他對象提供一種代理以控制對這個對象的訪問。 二、角色組成&#xff1a; Subject抽象主題&#xff1a;聲明真是主題與代理的共同接口方法&#xff0c;可以是一個抽象類或接口。 RealSubject真實主題&#xff1a;定義了代理表示的真實對象&#xff0c…

國外計算機證書推薦(考證)(6 Sigma、AWS、APICS、IIA、Microsoft、Oracle、PMI、Red Hat)

文章目錄 證書推薦1. 六西格瑪 (6 Sigma)2. 亞馬遜網絡服務 (AWS)3. 美國生產與庫存控制學會 (APICS)4. 內部審計師協會 (IIA)5. 微軟 (Microsoft)6. 甲骨文 (Oracle)7. 項目管理協會 (PMI)8. 紅帽 (Red Hat) 證書推薦 1. 六西格瑪 (6 Sigma) 介紹&#xff1a;六西格瑪是一種…