In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces Keepalived+Nginx+Tomcat how to achieve high availability Web cluster, the article is very detailed, has a certain reference value, interested friends must read it!
Implementation of High availability Web Cluster with Keepalived+Nginx+Tomcat
I. the installation process of Nginx
1. Download the Nginx installation package and install the dependent environment package
(1) install C++ compilation environment
Yum-y install gcc # C++
(2) install pcre
Yum-y install pcre-devel
(3) install zlib
Yum-y install zlib-devel
(4) install Nginx
Navigate to the location of the nginx unzipped file and execute the compile and install command
[root@localhost nginx-1.12.2] # pwd/usr/local/nginx/nginx-1.12.2 [root@localhost nginx-1.12.2] #. / configure & & make & & make install
(5) start Nginx
After the installation is complete, look for the location of the directory where the installation is completed.
[root@localhost nginx-1.12.2] # whereis nginxnginx: / usr/local/nginx [root@localhost nginx-1.12.2] #
Go to the Nginx subdirectory sbin to start Nginx
[root@localhost sbin] # lsnginx [root@localhost sbin] #. / nginx & [1] 5768 [root@localhost sbin] #
Check to see if Nginx is started
Or check the startup of Nginx through the process
[root@localhost sbin] # ps-aux | grep nginxroot 5769 0.00.0 20484? Ss 14:03 0:00 nginx: master process. / nginxnobody 5770 0.0 0.0 23012 1620? S 14:03 0:00 nginx: worker processroot 5796 0.0 112668 972 pts/0 R + 14:07 0:00 grep-- color=auto nginx [1] + complete. / nginx [root@localhost sbin] #
At this point the Nginx installation is complete and the startup is successful.
(6) Nginx Quick Startup and Boot Startup configuration
Edit the Nginx Quick launch script [pay attention to the Nginx installation path, you need to make changes according to your own NGINX path]
[root@localhost init.d] # vim / proxy and IMAP/POP3 proxy server# processname proxy and IMAP/POP3 proxy server# processname: nginx# config: / etc/nginx/nginx.conf# config: / usr/local/nginx/conf/nginx.conf# pidfile: / usr/local/nginx/logs/nginx.pid # Source function library.. / etc/rc.d/init.d/functions # Source networking configuration.. / etc/sysconfig/network # Check that networking is up. ["$NETWORKING" = "no"] & & exit 0nginx = "/ usr/local/nginx/sbin/nginx" prog=$ (basename $nginx) NGINX_CONF_FILE= "/ usr/local/nginx/conf/nginx.conf" [- f / etc/sysconfig/nginx] & &. / etc/sysconfig/nginxlockfile=/var/lock/subsys/nginx make_dirs () {# make required directories user= `$nginx-V 2 > & 1 | grep "configure arguments:" | sed's / [^ *] *-- user=\ ([^] *\). * /\ 1if [- z "`grep $user / etc/ passwd`]; then useradd-M-s / bin/nologin $user fi options=` $nginx-V 2 > & 1 | grep 'configure arguments:' `for opt in $options Do if [`echo $opt | grep'. *-temp-path' `]; then value= `echo $opt | cut-d "=" f 2` if [!-d "$value"]; then # echo "creating" $value mkdir-p $value & & chown-R $user $value fi fi done} start () {[- x $nginx] | exit 5 [- f $NGINX_CONF_FILE] | exit 6 make_dirs echo-n $"Starting $prog:" daemon $nginx-c $NGINX_CONF_FILE retval=$? Echo [$retval-eq 0] & & touch $lockfile return $retval} stop () {echo-n $"Stopping $prog:" killproc $prog-QUIT retval=$? Echo [$retval-eq 0] & & rm-f $lockfile return $retval} restart () {# configtest | | return $? Stop sleep 1 start} reload () {# configtest | | return $? Echo-n $"Reloading $prog:" killproc $nginx-HUP RETVAL=$? Echo} force_reload () {restart} configtest () {$nginx-t-c $NGINX_CONF_FILE} rh_status () {status $prog} rh_status_q () {rh_status > / dev/null 2 > & 1} case "$1" instart) rh_status_q & & exit 0 $1Tracterstop) rh_status_q | | exit 0 $1x reload | configtest) $1x reload) rh_status_q | | exit 7 $1tf forcehouse reload) force_reload;;status rh_status) Condrestart | try-restart) rh_status_q | | exit 0 *) echo $"Usage: $0 {start | stop | status | restart | condrestart | try-restart | reload | force-reload | configtest}" exit 2esac
Authorize and join boot boot for startup script
[root@localhost init.d] # chmod-R 777 / etc/rc.d/init.d/nginx [root@localhost init.d] # chkconfig nginx
Start Nginx
[root@localhost init.d] #. / nginx start
Add Nginx to the system environment variable
[root@localhost init.d] # echo 'export PATH=$PATH:/usr/local/nginx/sbin' > > / etc/profile & & source / etc/profile
Nginx command [service nginx (start | stop | restart)]
[root@localhost init.d] # service nginx startStarting nginx (via systemctl): [OK]
Tips: shortcut command
Service nginx (start | stop | restart)
II. Installation and configuration of KeepAlived
1. Install Keepalived dependent environment
Yum install-y popt-devel yum install-y ipvsadmyum install-y libnl*yum install-y libnf*yum install-y openssl-devel
two。 Compile Keepalived and install
[root@localhost keepalived-1.3.9] #. / configure [root@localhost keepalived-1.3.9] # make & & make install
3. Install Keepalive as a system service
[root@localhost etc] # mkdir / etc/keepalived [root@localhost etc] # cp / usr/local/keepalived/etc/keepalived/keepalived.conf / etc/keepalived/
Manually copy the default configuration file to the default path
[root@localhost etc] # mkdir / etc/keepalived [root@localhost etc] # cp / usr/local/keepalived/etc/sysconfig/keepalived / etc/sysconfig/ [root@localhost etc] # cp / usr/local/keepalived/etc/keepalived/keepalived.conf / etc/keepalived/
Create a soft link for keepalived
[root@localhost sysconfig] # ln-s / usr/local/keepalived/sbin/keepalived / usr/sbin/
Set Keepalived Boot self-boot
[root@localhost sysconfig] # chkconfig keepalived on Note: forwarding the request to "systemctl enable keepalived.service". Created symlink from / etc/systemd/system/multi-user.target.wants/keepalived.service to / usr/lib/systemd/system/keepalived.service
Start the Keepalived service
[root@localhost keepalived] # keepalived-D-f / etc/keepalived/keepalived.conf
Turn off the Keepalived service
[root@localhost keepalived] # killall keepalived
III. Cluster planning and construction
Environmental preparation:
CentOS 7.2
Keepalived Version 1.4.0-December 29, 2017
Nginx Version: nginx/1.12.2
Tomcat Version:8
Cluster planning list
Virtual machine IP description Keepalived+ Nginx1 [Master] 192.168.43.101Nginx Server 01Keeepalive + Nginx [backup] 192.168.43.102Nginx Server 02Tomcat01192.168.43.103Tomcat Web Server01Tomcat02192.168.43.104Tomcat Web Server02VIP192.168.43.150 Virtual Drift IP
1. Change the Tomcat default welcome page to identify the toggle Web
Change the ROOT/index.jsp information of the TomcatServer01 node, add the TomcatIP address, and add the Nginx value, that is, modify node 192.168.43.103 as follows:
${pageContext.servletContext.serverInfo} (192.168.224.103)
Change the TomcatServer02 node ROOT/index.jsp information, add the TomcatIP address, and add the Nginx value, that is, modify the node 192.168.43.104 information as follows:
${pageContext.servletContext.serverInfo} (192.168.224.104)
two。 Start the Tomcat service to view the IP information of the Tomcat service. Nginx is not started at this time, so request-header has no Nginx information.
3. Configure Nginx proxy information
1. Configure Master node [192.168.43.101] proxy information
Upstream tomcat {server 192.168.43.103 weight=1; server 192.168.43.104 weight=1;} server {location / {proxy_pass http://tomcat; proxy_set_header X-NGINX "NGINX-1";} #. Other omissions}
two。 Configure Backup node [192.168.43.102] proxy information
Upstream tomcat {server 192.168.43.103 weight=1; server 192.168.43.104 weight=1;} server {location / {proxy_pass http://tomcat; proxy_set_header X-NGINX "NGINX-2";} #. Other omissions}
3. Start the Master node Nginx service
[root@localhost init.d] # service nginx startStarting nginx (via systemctl): [OK]
When you visit 192.168.43.101, you can see the alternating display of 103node and 104node Tcomat, indicating that the Nginx service has loaded the request on the two tomcat.
4. Similarly, configure the Nginx information of Backup [192.168.43.102]. After starting Nginx, you can see that the Backup node has the effect of load after visiting 192.168.43.102.
4. Configure Keepalived script information
1. Add check_nginx.sh files under the Master node and Slave node / etc/keepalived directory to check the inventory status of Nginx and add keepalived.conf files
The check_nginx.sh file information is as follows:
#! / bin/bash# time variable, used to log d = `date-date today +% Y%m%d_%H:%M:% S` # calculate the number of nginx processes n = `ps-C nginx-- no-heading | wc-l` # if the process is 0, start nginx and detect the number of nginx processes again. # if it is still 0, nginx cannot be started, and you need to shut down keepalivedif [$n-eq "0] Then / etc/rc.d/init.d/nginx start N2 = `ps-C nginx-- no-heading | wc-l`if [$N2-eq "0"]; then echo "$d nginx down,keepalived will stop" > > / var/log/check_ng.log systemctl stop keepalived fifi
After the addition is completed, the check_nginx.sh file is authorized so that the script can obtain the execution permission.
[root@localhost keepalived] # chmod-R 777 / etc/keepalived/check_nginx.sh
two。 Under the Master node / etc/keepalived directory, add the keepalived.conf file as follows:
Vrrp_script chk_nginx {script "/ etc/keepalived/check_nginx.sh" / / script to detect nginx processes interval 2 weight-20} global_defs {notification_email {/ / you can add email reminders}} vrrp_instance VI_1 {state MASTER # marks the status as MASTER backup sets the instance-bound Nic for BACKUP interface ens33 # (ip addr view Need to bind according to personal network card) virtual_router_id 51 # under the same instance, virtual_router_id must be the same mcast_src_ip 192.168.43.101 priority 250 # MASTER weight is more important than BACKUP, for example, the time interval of synchronization check between BACKUP 240advert_int 1 # MASTER and BACKUP load balancer Unit: nopreempt # non-preemptive mode authentication {# set authentication auth_type PASS # master-slave server authentication mode auth_pass 123456} track_script {check_nginx} virtual_ipaddress {# set vip 192.168.43.150 # multiple virtual IP, line wrap}}
3. Add a keepalived.conf configuration file under the Backup node etc/keepalived directory
The information is as follows:
Vrrp_script chk_nginx {script "/ etc/keepalived/check_nginx.sh" / / script to detect nginx processes interval 2 weight-20} global_defs {notification_email {/ / you can add email reminders}} vrrp_instance VI_1 {state BACKUP # Mark status as MASTER backup set instance bound Nic for BACKUP interface ens33 # (ip addr view) virtual_router_id 51 # the same instance The next virtual_router_id must be the same as mcast_src_ip 192.168.43.102 priority 240 # MASTER is more important than BACKUP, for example, the time interval of synchronization check between BACKUP 240 advert_int 1 # MASTER and BACKUP load balancer Unit: nopreempt # non-preemptive mode authentication {# set authentication auth_type PASS # master-slave server authentication mode auth_pass 123456} track_script {check_nginx} virtual_ipaddress {# set vip 192.168.43.150 # multiple virtual IP, line wrap}}
Tips: a few notes on configuration information
State-master server needs to be configured as MASTER, slave server needs to be configured as BACKUP
Interface-this is the name of the network card. I am using the version of VM12.0, so here the network card is called ens33.
Mcast_src_ip-configure their actual IP addresses
Priority-the priority of the master server must be higher than that of the slave server, where the master server is configured to 250 and the slave server is configured to 240
Virtual_ipaddress-configure virtual IP (192.168.43.150)
Authentication-auth_pass master and slave servers must be consistent, and keepalived relies on this to communicate
Virtual_router_id-Master and slave servers must be consistent
5. Cluster High availability (HA) verification
Step1 starts Keepalived and Nginx services for Master machines
[root@localhost keepalived] # keepalived-D-f / etc/keepalived/keepalived.conf [root@localhost keepalived] # service nginx start
View the service startup process
[root@localhost keepalived] # ps-aux | grep nginxroot 6390 0.0 20484 612? Ss 19:13 0:00 nginx: master process / usr/local/nginx/sbin/nginx-c / usr/local/nginx/conf/nginx.confnobody 6392 6392 23008 1628? S 19:13 0:00 nginx: worker processroot 6978 0.0 112672 968 pts/0 S + 20:08 0:00 grep-- color=auto nginx
View the Keepalived startup process
[root@localhost keepalived] # ps-aux | grep keepalivedroot 6402 0.0 45920 1016? Ss 19:13 0:00 keepalived-D-f / etc/keepalived/keepalived.confroot 6403 0.0 48044 1468? S 19:13 0:00 keepalived-D-f / etc/keepalived/keepalived.confroot 6404 0.0 50128 1780? S 19:13 0:00 keepalived-D-f / etc/keepalived/keepalived.confroot 7004 0.0 112672 976 pts/0 S + 20:10 grep-- color=auto keepalived
Use ip add to view virtual IP binding, and bind to Master node if 192.168.43.150 node information appears
[root@localhost keepalived] # ip add1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6:: 1/128 scope host valid_lft forever preferred_lft forever2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:91:bf:59 brd ff:ff:ff: Ff:ff:ff inet 192.168.43.101/24 brd 192.168.43.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.43.150/32 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::9abb:4544:f6db:8255/64 scope link valid_lft forever preferred_lft forever inet6 fe80::b0b3:d0ca:7382:2779/64 scope link tentative dadfailed valid_lft forever preferred_lft forever inet6 fe80::314f:5fe7:4e4b:64ed/64 scope link Tentative dadfailed valid_lft forever preferred_lft forever3: virbr0: mtu 1500 qdisc noqueue state DOWN qlen 1000 link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0 valid_lft forever preferred_lft forever4: virbr0-nic: mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000 link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff
Step 2 starts the Backup node Nginx service and the Keepalived service to check the service startup. If there is a virtual IP in the Backup node, there is a problem with the Keepalvied configuration file, which is called a brain fissure.
[root@localhost keepalived] # clear [root@localhost keepalived] # ip add1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6:: 1/128 scope host valid_lft forever preferred_lft forever2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:14:df: 79 brd ff:ff:ff:ff:ff:ff inet 192.168.43.102/24 brd 192.168.43.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::314f:5fe7:4e4b:64ed/64 scope link valid_lft forever preferred_lft forever3: virbr0: mtu 1500 qdisc noqueue state DOWN qlen 1000 link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff inet 192.168.122.1/24 brd 192.168.122 . 255 scope global virbr0 valid_lft forever preferred_lft forever4: virbr0-nic: mtu 1500 qdisc pfifo_fast master virbr0 state DOWN qlen 1000 link/ether 52:54:00:2b:74:aa brd ff:ff:ff:ff:ff:ff
Step 3 Authentication Service
Browse and force to refresh the address several times: http://192.168.43.150, you can see 103and 104display alternately, and display Nginx-1, which indicates that the Master node is forwarding web services.
Step 4 shuts down Master keepalived service and Nginx service, and accesses Web service to observe service transfer
[root@localhost keepalived] # killall keepalived [root@localhost keepalived] # service nginx stop
At this point, force refresh 192.168.43.150 to find that the page alternately displays 103and 104and shows Nginx-2, VIP has been transferred to 192.168.43.102, and it has been proved that the service automatically switches to the backup node.
Step 5 starts Master Keepalived service and Nginx service
At this time, it is found that the VIP has been recaptured by Master, and the page alternately displays 103and 104. at this time, Nginx-1 is displayed.
4. Keepalived preemptive mode and non-preemptive mode
The HA of keepalived is divided into preemptive mode and non-preemptive mode. When the MASTER recovers from the fault, it will preempt the VIP from the BACKUP node. Non-preemptive mode, that is, after MASTER is restored, it does not preempt the VIP after BACKUP is upgraded to MASTER.
Non-preemptive mode configuration:
1 > nopreempt instructions have been added to each of the two nodes under the vrrp_instance block to indicate that they do not compete for vip.
2 > the state of the node is BACKUP. When both keepalived nodes are started, the default is BACKUP. After sending the multicast message, both parties will elect a MASTER according to the priority. Because both are configured with nopreempt, MASTER does not preempt vip when it recovers from a failure. This avoids service delays that may be caused by VIP handover.
These are all the contents of the article "how Keepalived+Nginx+Tomcat achieves highly available Web clusters". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.