nginx+tomcat+keepalived implement a highly available Web cluster:
Environment: Cenos 6.5
nginx-master:10.10.10.128
nginx-backup:10.10.10.129
tomcat1:10.10.10.130
tomcat2:10.10.10.131
vip:10.10.10.100
First, the environmental basic configuration
1, replace the domestic yum source
2. Turn off the firewall, SELinux
3. Time synchronization
Second, web-side installation Web Services
1. See if jdk[[email protected] ~]# Java-versionjava Version "1.8.0_171" Java (TM) SE Runtime Environment (build 1.8.0 _171-B11) Java HotSpot (TM) 64-bit Server VM (build 25.171-b11, Mixed mode) 2, install JDK download binary package extract to/usr/local/java tar zxvf JDK -8u171-linux-x64.tar.gz-c/usr/local/java/Modify Environment variables/etc/profile Java_home=/usr/local/java PATH= $JAVA _home/bin: $PATH Classpath= $JAVA _home/jre/lib/ext: $JAVA _home/lib/tools.jar export PATH java_home CLASSPATH make the environment variable effective: Source/etc/profi Le3, download Tomcat source bundle: Wget-o/opt/apache-tomcat-9.0.7.tar.gz http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.7/ BIN/APACHE-TOMCAT-9.0.7.TAR.GZ4, unzip to/usr/local/tomcat tar zxvf/opt/apache-tomcat-9.0.7.tar.gz-c/USR/LOCAL/5, Modify Tomcat's home page rm-rf/usr/local/apache-tomcat-9.0.7/webapps/root/* echo "TOMCAT1" >/usr/local/apache-tomcat-9.0.7/w Ebapps/root/index.html #Tomcat1 echo "Tomcat2" >/usr/local/apache-tomcat-9.0.7/webapps/root/index.html #Tomcat26 , test whether Tomcat can start normally [[email protected] Bin]#./usr/local/apache-tomcat-9.0.7/bin/startup.sh Using catalina_base:/usr/local/apache-tomcat-9.0.7using CATALINA _home:/usr/local/apache-tomcat-9.0.7using catalina_tmpdir:/usr/local/apache-tomcat-9.0.7/tempusing JRE_HOME:/u Sr/local/javausing CLASSPATH:/usr/local/apache-tomcat-9.0.7/bin/bootstrap.jar:/usr/local/apache-tomcat-9.0.7/ Bin/tomcat-juli.jartomcat started.curl 10.10.10.130:8080 #返回Tomcat1curl 10.10.10.131:8080 #返回Tomcat2至此web端配置完成.
Second, Nginx reverse proxy installation
1, install dependent software yum install-y gcc gcc-c++ yum install-y pcre pcre-devel OpenSSL openssl-devel zlib zlib-devel2, official website Download source Package 3, Solution Pressure source Package tar zxvf nginx-1.14.0.tar.gz-c/TMP/4, compile and install Useradd-s/bin/false-m nginx./configure--user=nginx--group= Nginx--prefix=/usr/local/nginx-1.14.0/--with-http_v2_module--with-http_ssl_module--with-http_sub_module-- With-http_stub_status_module--with-http_gzip_static_module--with-pcre make && make install5, configure reverse proxy/usr/ Local/nginx/conf/nginx.confworker_processes 1;pid/usr/local/nginx/logs/nginx.pid;worker_rlimit_nofile 51200;even TS {use epoll; Worker_connections 51200;} HTTP {include mime.types; Default_type Application/octet-stream; Server_names_hash_bucket_size 128; Client_header_buffer_size 32k; Large_client_header_buffers 4 32k; Client_max_body_size 8m; Sendfile on; Tcp_nopush on; Keepalive_timeout 65; Tcp_nodelay on; gzip on; Gzip_min_length 1k; Gzip_buffers 4 16k; Gzip_htTp_version 1.0; Gzip_comp_level 2; Gzip_types test/plain application/x-javascript test/css application/xml; Gzip_vary on; Upstream backend {server 10.10.10.130:8080; Server 10.10.10.131:8080; } server {Listen 80; server_name 10.10.10.128; #Nginx2改为: 10.10.10.129 location/{root/var/www/html; Index index.php index.html index.htm; Proxy_pass Http://backend; }}} First Test and restart: [[email protected] ~]#/usr/local/nginx-1.14.0/sbin/nginx-tnginx:the configuration File/usr/loc al/nginx-1.14.0//conf/nginx.conf syntax is oknginx:configuration file/usr/local/nginx-1.14.0//conf/nginx.conf test is successful[[email protected] ~]#/usr/local/nginx-1.14.0/sbin/nginx[[email protected] ~]# lsof-i : 80COMMAND PID USER FD TYPE DEVICE size/off NODE namenginx 4896 root 6u IPv4 18439 0t0 TCP *:http (LIS TEN) nginx 4897 nginx 6u IPv4 18439 0t0 TCP *:http (LISTENCurl 10.10.10.128 #轮询返回Tomcat1 Tomcat2nginx2 the same build. At this point the Nginx reverse proxy setup is complete.
Third, use keepalived for high availability
1, Installation: Yum install Keepalived-y2, modify the configuration file/etc/keepalived/keepalived.confmaster side:! Configuration File for Keepalivedvrrp_script check_nginx {script "/etc/keepalived/check_nginx.sh" interval 2 weight 2}global_defs {notification_email {[email protected] [email protected] [EMAIL&NB Sp;protected]} notification_email_from [email protected] smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id lvs_devel1}vrrp_instance vi_1 {State MASTER interface eth0 virtual_router_id Vert_int 1 Authentication {auth_type PASS auth_pass 1111} virtual_ipaddress {10.10.10.100 /24 Dev Eth0} track_script {Check_nginx}}backup end: Modified: router_id lvs_devel2state backuppriority 90 detect nginx script: #!/ Bin/bashnginxpid= ' ps-c nginx--no-header | Wc-l ' If [$nginxpid-eq 0];then/etc/init.d/keepalived stopfichmod +x/etc/keepalived/check_nginx.sh restart keepalived Service Service KeepAliveD Restart
Iv. High availability of inspection services
Nginx1执行:killall nginx发现 web访问依然正常Tomcat1执行:/usr/local/apache-tomcat-9.0.7/bin/shutdown.sh发现 web访问依然正常高可用的环境搭建完毕。
Nginx+tomcat+keepalived implementing a highly available Web cluster