keepalived+nginx+tomcat
更新:HHH   时间:2023-1-7


https://pan.baidu.com/s/1tXzFm2DVckZNS6HuwDwGoA //软件连接百度云

在80.100虚拟机下

systemctl stop firewalld      //关闭防火墙
setenforce 0         //关闭监控
yum install lrz* -y //可以直接把软件包拖到xshell当前目录里面的命令
tar xf nginx-1.13.9.tar.gz -C /opt/      //解压压缩包到/opt/下
cd /opt/
ls

cd /opt/nginx-1.13.9/    //进入文件

useradd -s /sbin/nologin -M nginx    //创建一个无法登陆系统的用户
yum install -y \        //安装编译工具及插件
 gcc \
 gcc-c++ \
 make \
 openssl-devel \
 zlib-devel \
 pcre-devel
./configure \               //个性化配置
--user=nginx \
--group=nginx \
--with-file-aio \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module
make && make install     //编译且安装
cd     //到root根目录下
vi /usr/local/nginx/conf/nginx.conf
    user nginx nginx;
    worker_processes  1;

    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    error_log  logs/error.log  info;

    pid        logs/nginx.pid;

    events {
            worker_connections  1024;
    }

    http {
            include       mime.types;
            default_type  application/octet-stream;

            log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                                                '$status $body_bytes_sent "$http_referer" '
                                                '"$http_user_agent" "$http_x_forwarded_for"';

            access_log  logs/access.log  main;
                    add_header X-Server $hostname;
                    server_names_hash_bucket_size 128;
                    server_name_in_redirect off;

            sendfile        on;
            tcp_nopush     on;
            tcp_nodelay on;

            #keepalive_timeout  0;
            keepalive_timeout  60;
                    client_header_buffer_size 32k;
                    large_client_header_buffers 4 128k;
                    client_max_body_size 512m;
                    open_file_cache max=65535 inactive=20s;
                    open_file_cache_valid 30s;
                    open_file_cache_min_uses 1;

            gzip  on;
                    gzip_static on;
                    gzip_http_version 1.1;
                    gzip_comp_level 2;
                    gzip_min_length 1024;
                    gzip_vary on;
                    gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;

    server_tokens off;
                    fastcgi_connect_timeout 300;
                    fastcgi_send_timeout 300;
                    fastcgi_read_timeout 300;
                    fastcgi_buffer_size 512k;
                    fastcgi_buffers 6 512k;
                    fastcgi_busy_buffers_size 512k;
                    fastcgi_temp_file_write_size 512k;
                    fastcgi_intercept_errors on;
                    client_body_buffer_size 128k;

                    proxy_connect_timeout 600;
                    proxy_read_timeout 600;
                    proxy_send_timeout 600;
                    proxy_buffer_size 32k;
                    proxy_buffers 4 32k;
                    proxy_busy_buffers_size 64k;
                    proxy_temp_file_write_size 2m;
                    proxy_ignore_client_abort on;
                    proxy_cache_path /usr/local/nginx/cache_temp levels=2:2

    keys_zone=cache_temp:128m inactive=30m max_size=2g;
                    proxy_cache_valid 200 302 10m;
                    include /usr/local/nginx/conf/conf.d/*.conf;

            server {
                    listen       80;
                    server_name  localhost;

                    charset UTF-8;

                    #access_log  logs/host.access.log  main;

                    location / {
                            root   html;
                            index  index.html index.htm;
                    }

                    #error_page  404              /404.html;

                        # redirect server error pages to the static page /50x.html
                        #
                    error_page   500 502 503 504  /50x.html;
                        location = /50x.html {
                            root   html;
                        }

                            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
                            #
                    #location ~ \.php$ {
                        #    proxy_pass   http://127.0.0.1;
        #}

                        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
                    #
        #location ~ \.php$ {
                    #    root           html;
                    #    fastcgi_pass   127.0.0.1:9000;
                    #    fastcgi_index  index.php;
                        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                            #    include        fastcgi_params;
        #}

                    # deny access to .htaccess files, if Apache's document root
                    # concurs with nginx's one
                    #
        #location ~ /\.ht {
                    #    deny  all;
        #}
    }

            # another virtual host using mix of IP-, name-, and port-based configuration
            #
    #server {
            #    listen       8000;
            #    listen       somename:8080;
                #    server_name  somename  alias  another.alias;

                    #    location / {
                    #        root   html;
                    #        index  index.html index.htm;
                    #    }
    #}

            # HTTPS server
            #
    #server {
            #    listen       443 ssl;
            #    server_name  localhost;

                #    ssl_certificate      cert.pem;
                #    ssl_certificate_key  cert.key;

                #    ssl_session_cache    shared:SSL:1m;
                #    ssl_session_timeout  5m;

                #    ssl_ciphers  HIGH:!aNULL:!MD5;
                #    ssl_prefer_server_ciphers  on;

                #    location / {
                #        root   html;
                #        index  index.html index.htm;
                #    }
    #}
    }
    :wq   //保存退出
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/       //软链接
nginx -t    //检查nginx有没有错误

cd /usr/local/nginx/conf/ 
mkdir conf.d
cd conf.d/

vi lvs01.conf          //新建子配置文件
    server {
                    listen 80;
                    server_name lvs01 192.168.80.100;       //服务器名称与IP地址
                    index index.html index.jsp;
                    root /usr/local/nginx/html;
                    access_log /usr/local/nginx/logs/tomcat.aa.com_access.log main;
                    location ~ .*\.jsp$ {
                    index index.jsp;
                    proxy_set_header HOST $host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header Client-IP $remote_addr;
                    proxy_set_header X-For $proxy_add_x_forwarded_for;
                    proxy_pass http://center_pool;
                    }
                    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
                    expires 30d;
            proxy_pass http://center_pool;
                    }
                    location ~ .*\.(js|css)?$ {
                    expires 1h;
            proxy_pass http://center_pool;

                    }
    }
    :wq     //保存退出
vi pool.conf       //创建服务器池
    #添加以下内容
    upstream center_pool {               //默认轮询
                    server 192.168.80.102:8080;
                    server 192.168.80.103:8080;
    }
    :wq    //保存退出
nginx -t

vi /etc/init.d/nginx
    #!/bin/bash
        # chkconfig: 35 99 20
            # description: Nginx Service Control Script
    PROG="/usr/local/nginx/sbin/nginx"
    PIDF="/usr/local/nginx/logs/nginx.pid"
    case "$1" in
        start)
            $PROG
            ;;
        stop)
            kill -s QUIT $(cat $PIDF)
            ;;
        restart)
            $0 stop
            $0 start
            ;;
        reload)
            kill -s HUP $(cat $PIDF)
            ;;
        *)
                    echo "Usage: $0 {start|stop|restart|reload}"
                    exit 1
    esac
    exit 0
    :wq   //保存退出
chmod +x /etc/init.d/nginx        //增加执行权限
chkconfig --add nginx             //加入系统管理服务
service nginx start               //启动nginx服务
 netstat -anpt | grep 80         //查看80端口有哪些服务在开启状态


在浏览器地址栏中输入192.168.80.100


在浏览器地址栏中输入192.168.80.100/index.jsp


在浏览器地址栏中输入192.168.80.100/index.jsp

scp /usr/local/nginx/conf/nginx.conf root@192.168.80.101:/usr/local/nginx/conf/nginx.conf      //把本机上的nginx.conf传送到80.101虚拟机上
yes
自己设置的root密码
scp * root@192.168.80.101:/usr/local/nginx/conf/conf.d/ //传送到80.101虚拟机上
自己设置的root密码

scp /etc/init.d/nginx root@192.168.80.101:/etc/init.d
自己设置的root密码
cd /etc/yum.repos.d/      //进入目录
cp back/* ./         //把back里面所有内容提取到上一个目录下
ifconfig ens34 down        //关闭网卡
ifconfig ens34 up        //开启网卡
yum install -y epel-release       //安装
yum install keepalived -y      //安装
cd      //到root根目录下
vi /etc/keepalived/keepalived.conf
    #把里面内容都删了添加以下内容

    ! Configuration File for keepalived
    global_defs {
                    route_id NGINX-01
         }
    vrrp_script nginx {
                    script "/opt/nginx.sh"
                    interval 2
                    weight -10
    }
    vrrp_instance VI_1 {
            state MASTER
            interface ens32
            virtual_router_id 51
            priority 150
            advert_int 1
            authentication {
                    auth_type PASS
                    auth_pass 1111
            }
            track_script {
                    nginx
                    }
                virtual_ipaddress {
                    192.168.80.188
            }
    }
    :wq    //保存退出
scp /etc/keepalived/keepalived.conf root@192.168.80.101:/etc/keepalived/
自己设置root的密码
cd /opt/
vi nginx.sh
    #!/bin/bash
    #Filename:nginx.sh
    A=$(ps -ef | grep keepalived | grep -v grep | wc -l)
    if [ $A -gt 0 ]; then
                    /etc/init.d/nginx start
    else
                    /etc/init.d/nginx stop
    fi
    :wq    //保存退出
chmod +x nginx.sh      //给这个文件权限
./nginx.sh        //执行这个脚本
systemctl start keepalived     //重启keepalived服务
systemctl stop firewalld         //关闭firewalld服务
chkconfig --add nginx           
service nginx start            //重启nginx服务
./nginx.sh        //在执行脚本

在浏览器地址栏中输入192.168.80.188


在浏览器地址栏中输入192.168.80.188/index.jsp


在80.101虚拟机下

systemctl stop firewalld      //关闭防火墙
setenforce 0           //关闭监控
yum install lrz* -y //可以直接把软件包拖到xshell当前目录里面的命令
yum install -y \        //安装编译工具及插件
 gcc \
 gcc-c++ \
 make \
 openssl-devel \
 zlib-devel \
 pcre-devel
 ./configure \               //个性化配置
--user=nginx \
--group=nginx \
--with-file-aio \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_realip_module
make && make install        //编译且安装
cd /usr/local/nginx/conf/       //进入文件夹
mkdir conf.d       //创建这个文件
vi conf.d/lvs01.conf
把80.100改成80.101

chkconfig --add nginx
useradd -s /sbin/nologin -M nginx       //创建一个无法登陆系统的用户
service nginx start         //重启nginx

在浏览器地址栏中输入192.168.80.101/index.jsp


cd /etc/yum.repos.d/       //进入这个目录
cp back/* ./                        //把back这个目录里面的内容转到上一个目录下
ifconfig ens34 down       //关闭网卡
ifconfig ens34 up        //开启网卡
yum install -y epel-release      //安装
yum install keepalived -y     //安装
vi /etc/keepalived/keepalived.conf      //进入文件

cd /opt/     //到/opt/下
vi nginx.sh
    #!/bin/bash
    #Filename:nginx.sh
    A=$(ip addr | grep 192.168.80.188/32 | grep -v grep | wc -l)
    if [ $A -gt 0 ]; then
                    /etc/init.d/nginx start
    else
                    /etc/init.d/nginx stop
    fi
    :wq     //保存退出
chmod +x nginx.sh     //给这个脚本执行权限
./nginx.sh     //启动这个脚本

systemctl start keepalived     //重启服务

在80.102虚拟机下

systemctl stop firewalld     //关闭防火墙
setenforce 0       //关闭监控
yum install lrz* -y //可以直接把软件包拖到xshell当前目录里面的命令
tar xf apache-tomcat-8.5.23-embed.tar.gz //压缩apache-tomcat-8.5.23-embed.tar.gz到当前目录上
tar xf jdk-8u144-linux-x64.tar.gz //压缩jdk-8u144-linux-x64.tar.gz到当前目录上

cp -rv jdk1.8.0_144/ /usr/local/java //把jdk1.8.0_144拷贝到 /usr/local/java 目录下
vi /etc/profile //在文件末尾新增
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib

source /etc/profile
java -version  //查看版本显示下面内容
java version "1.8.0_144"

……
……
--------------以上是搭建JAVA环境---------

cp -r apache-tomcat-8.5.23 /usr/local/tomcat8 //把apache-tomcat-8.5.23拷贝到/usr/local/tomcat8
ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup //把/usr/local/tomcat8/bin/startup.sh软链接到 /usr/bin/tomcatup 
ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown //把/usr/local/tomcat8/bin/shutdown.sh软链接到/usr/bin/tomcatdown
vi /usr/local/tomcat8/webapps/ROOT/index.jsp
在第一行加server aa
tomcatup
netstat -anpt | grep 8080 //查看当前8080端口有哪些程序在运行

在80.103虚拟机下

systemctl stop firewalld     //关闭防火墙
setenforce 0                //关闭监控
yum install lrz* -y //可以直接把软件包拖到xshell当前目录里面的命令
tar xf apache-tomcat-8.5.23-embed.tar.gz //压缩apache-tomcat-8.5.23-embed.tar.gz到当前目录上
tar xf jdk-8u144-linux-x64.tar.gz //压缩jdk-8u144-linux-x64.tar.gz到当前目录上

cp -rv jdk1.8.0_144/ /usr/local/java //把jdk1.8.0_144拷贝到 /usr/local/java 目录下
vi /etc/profile //在文件末尾新增
export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib

source /etc/profile
java -version  //查看版本显示下面内容
java version "1.8.0_144"

……
……
--------------以上是搭建JAVA环境---------

cp -r apache-tomcat-8.5.23 /usr/local/tomcat8 //把apache-tomcat-8.5.23拷贝到/usr/local/tomcat8
ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup //把/usr/local/tomcat8/bin/startup.sh软链接到 /usr/bin/tomcatup 
ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown //把/usr/local/tomcat8/bin/shutdown.sh软链接到/usr/bin/tomcatdown
vi /usr/local/tomcat8/webapps/ROOT/index.jsp
在第一行加server bb
tomcatup
netstat -anpt | grep 8080 //查看当前8080端口有哪些程序在运行
返回云计算教程...