nginx load balancing + keepalived availability

nginx load balancing + keepalived availability

Preparing the Environment

  • 192.168.88.111:nginx + keepalived   MASTER
  • 192.168.88.112:nginx + keepalived   BACKUP
  • 192.168.88.113:JDK+Tomcat (running Tomcat requires JDK), index.html content "88.113"
  • 192.168.88.114:JDK+Tomcat (running Tomcat requires JDK), index.html content "88.114"
  • 192.168.88.115:vip

Said machine should be first to install some commonly used software, such as vim, lrzsz (rz upload files locally, sz download the file to a local), gcc, and so on.

Nginx, JDK and Tomcat are used source package (* .tar.gz) to install the latest stable version to their official website to download. The software packages and installation script (script written in UTF8 format, upload it to remember to use linux vim editor: set ff = unix) into the machine under the specified folder easy to find, as I was: mkdir / root / install_packages.

 

88.113-114 mounting JDK + Tomcat

Install the JDK

# Install the package into the directory 
cd / root / install_packages # write jdk installation script vim install_java. SH # ! / Bin / bash # define variables PKN, record the name of the installation package PKN = jdk-8u131-Linux-x64. Tar .gz # extracting installation package to the specified path tar -xf $ PKN -C / usr / local /
# in / etc / append global variable JAVA_HOME lower profile = / usr / local / jdk1.8.0_131 , and let the changes take effect immediately echo " Export JAVA_HOME = /usr/local/jdk1.8.0_131 " >> / etc / Profile
Source
/ etc / Profile # wait for 4 seconds and then execute the script down
SLEEP 4
# additional environment in / etc / profile variables, with immediate effect
echo "export PATH = / sbin: / bin : / usr / sbin: / usr / bin: / usr / local / sbin: / usr / local / bin: $ JAVA_HOME / bin: $ JAVA_HOME / db / bin: $ JAVA_HOME / jre / bin " >> / etc / Profile Source / etc / Profile
# java test whether the installation is successful, if successful, it will display the version information java
- version SH . install_java SH # implementation of the script, and the script to install the package on the same path Source / etc / Profile # makes the / etc / profile changes take effect immediately, although the final script has been performed, but make sure the changes take effect, here once again execute the Java -version # jdk test whether the installation is successful

 Install Tomcat (relatively simple, you can unzip the start, do not deliberately written script)

CD / the root / install_packages 
# decompression installation package to the specified path 
the tar XF-tomcat- Apache 8.5 . 43 is . the tar .gz -C / usr / local / 
into the installation decompression path, a change of name 
#cd / usr / local /
 mv the Apache-tomcat- 8.5 . 43 tomcat8 
cd 
# start Tomcat 
SH / usr / local / tomcat8 / bin / the startup. SH 
# at 88.113 and 88.114, respectively, to write two index.html, into the site's root directory 
echo  " <h1> 88.113 </ h1 of> " > / usr / local / tomcat8 / the webapps / the ROOT / index.html
 echo  " <h1 of> 88.114 </ h1 of> " > /usr/local/tomcat8/webapps/ROOT/index.html

 

88.111-88.112 mounting Nginx + keepalived

Install Nginx (flawed script, restart the machine, nginx start a message can not be found "/ var / run / nginx / ..." is wrong, but the script has obviously created a corresponding path, to be fine).

cd / root / install_packages
 
# write scripts to install nginx vim install_nginx.
SH # define a variable, record tomcat archive name PKN = nginx- 1.16 . 0 . tar .gz # install dependencies, create users and groups yum -y install zlib zlib OpenSSL OpenSSL-devel PCRE -devel PCRE- devel the groupadd - R & lt Nginx the useradd -s / sbin / nologin -g Nginx - R & lt Nginx # extracting archive the tar XF $ PKN CD Nginx - 1.16 . 0 mkdir / var / RUN / Nginx /; chown nginx.nginx / var / RUN / Nginx / . /configure \ # produce the makefile --prefix = / usr / local \ # installation path specified --sbin-path = / usr / sbin / Nginx \ --conf-path = / etc / Nginx / nginx.conf \ # Specify the main profile path --error-log-path = / var / log / Nginx / the error.log \ --pid-path = / var / RUN / Nginx / nginx.pid \ --user = Nginx \ --group = nginx \ --with- http_ssl_module the make && the make install # compile, install # start nginx nginx

nginx load balancing configuration (bold font is the load-balancing configuration)

worker_processes   . 1 ; #nginx number of processes, generally set equal to the number of cores cpu 
# operation mode and the maximum number of connections Events { use the epoll; #epoll operation mode select better than worker_connections
1024 ; the maximum number of connections a single process # } HTTP { the include the mime.types; file application default_type / OCTET - Uninterpreted Stream; the sendfile ON; # open efficient file transfer mode keepalive_timeout 65 ; # longer connection time, in seconds upstream tomcatserver {#weight is the weight, the greater the value, the greater the chance polling. If the server appears in the fail_timeout time max_fails times the connection fails, the server is nginx thought hang, no longer poll the server within the time fail_timeout Server 192.168 . 88.113 : 8080= weight . 4 max_fails = . 3 fail_timeout = 30s; Server 192.168 . 88.114 : 8080 weight = 2 max_fails = . 3 fail_timeout = 30s; } Server { the listen 80 ; server_name www.kamin.com; # Domain LOCATION / { proxy_pass HTTP: // tomcatserver ; name after #http and upstream to the same, and the need to pay attention, can not take the name "_" sign } } }

 Install and configure keepalived

yum install -y keepalived

vim /etc/keepalived/keepalived.conf
  1 ! Configuration File for keepalived
  2 
  3 global_defs {
  4    notification_email {    #基本不用,一般使用zabbix监控
  5        747631184@qq.com
  6    }
  7    notification_email_from 747631184@qq.com
  8    smtp_server 192.168.200.1
  9    smtp_connect_timeout 30
 10    router_id nginx_master    #keepalived machine id, different to different machines (114: nginx_backup)
  . 11 }
  12 is  
 13 is vrrp_instance VI_1 {
  14      State MASTER MASTER # a master, a BACKUP prepared (114: BACKUP), vip which will bind keepalived one, and always tied to the MASTER
  15      interface ens32     # name of the NIC
  16      virtual_router_id 51 is     # keepalived in the same group of the parameter must be identical
  . 17      priority 150     # priority, the larger the value, the greater the priority, access to resources (grab priority value vip), BACKUP should be smaller than the MASTER
  18 is      advert_int . 1     # heartbeat interval
  . 19      authentication password communication between two machines {#
  20 is          AUTH_TYPE the PASS
  21 is          AUTH_PASS 1111
 22 is      }
  23 is      virtual_ipaddress {VIP address
  24          192.168 . 88.115 / 24 
 25      }
  26 }

Installed server keepalived what's remember to turn off the firewall, to ensure that they can communicate with each other, or easily split brain condition occurs . Specific Baidu.

 

test

1. Access 192.168.88.115, under normal circumstances, be able to pass and the flow must go MASTER machine, because vip will always be tied to the keepalived service is not linked to the MASTER machine;

2.MASTER off, BACKUP open, under normal circumstances, can still access 192.168.88.115, because keepalived detected on the MASTER keepalived service does not start, so vip transferred to the BACKUP;

 

Issues to be considered

1.keepalived determine whether there is server downtime by detecting whether keepalived process, but if keepalived process nginx process gone so keepalived switchover will not call the shots, so we need to write a script to monitor nginx process exists, if not nginx keepalived presence will kill the process;

2. After the downtime to reboot your machine to restart all services and nginx keepalived service, keepalived service is installed via yum, you can set the boot from the start, nginx it?

 

Guess you like

Origin www.cnblogs.com/kaminwu/p/11351724.html