Keepalived+Nginx+tomcat实现主备+负载

 

 

 

部署系统:

Red Hat Enterprise Linux Server release 7.0

软件版本:apache-tomcat-7.0.92.tar.gz
keepalived-2.0.11.tar.gz
 nginx-1.15.7.tar.gz  (openssl-1.1.1.tar.gz   pcre-8.40.tar.gz  zlib-1.2.11.tar.gz )

架构图

vip:192.168.56.80-----> A:192.168.56.129      nginx(8001)   tomcat (8080)

                                      B:192.168.56.130     nginx(8001)    tomcat (8080)

 

开始部署

1、准备两台虚拟机,网络我选择的是nat,如下图.  这种网络有点麻烦后面需要做一下映射。

在vmware 编辑下虚拟网络编辑器,选择NAT设置

这样通过本机IP访问这此端口了.

服务器上网络准备如下:

[root@mrice_02 sbin]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=eno16777736
ONBOOT=yes
IPADDR0=192.168.56.130
PREFIX0=32
GATEWAY0=192.168.56.2
HWADDR=00:0c:29:b9:46:b5
PEERDNS=yes

 

2、服务器准备完毕后,开始安装软件.

tomcat 不用说了,解压后,编写一个默认页面.

 

nginx 安装   请参考https://www.cnblogs.com/mrice/p/9882781.html

nginx.conf 配置文件如下  ,nginx配置可简单也可复杂,可根据生产中进行配置.

#user  nobody;
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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream mrice{
    server 192.168.56.129:8080 weight=3;
    server 192.168.56.130:8080 weight=3;
    }

    server {
        listen       8001;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
    
    location /mrice {
    proxy_pass http://mrice/;
    }

        #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;
    #    }
    #}

}

验证负载是否正常

 

 

keepalived 安装

本次使用的是源码安装

tar -xzvf  keepalived-2.0.11.tar.gz
cd keepalived-2.0.11
./configure --prefix=/usr/local/keepalived    安装目录根据需要修改

make && make install

cp keepalived-2.0.11/keepalived/etc/init.d/keepalived /etc/init.d/

cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

 cp keepalived-2.0.11/keepalived/etc/sysconfig/keepalived

cp /usr/local/keepalived/sbin/keepalived /usr/sbin/

配置keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
    mrice_02@mrice.com
   }
   notification_email_from mrice@mrice.com
   smtp_server 192.168.56.8
   smtp_connect_timeout 30
   router_id mrice_backup
}
vrrp_script chk_http_port {
    script "/opt/nginx/check_nginx.sh"
    interval 2
    weight 2
}

vrrp_instance VI_1 {
    state BACKUP
    interface eno16777736
    virtual_router_id 51
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.56.80
    }
}

其中配置文件中/opt/nginx/check_nginx.sh检查nginx进程

 

#!/bin/bash
A=`ps -C nginx --no-header |wc -l`        
if [ $A -eq 0 ];then                            
    /usr/nginx/sbin/nginx                #重启nginx
    if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    #nginx重启失败
        exit 1
    else
        exit 0
    fi
else
    exit 0
fi

 

 

 

通过VIP访问是否正常。

 

 

 可查看VIP地址在哪台服务器上

[root@mrice_02 sbin]# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    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 forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:b9:46:b5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.56.130/32 brd 192.168.56.130 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet 192.168.56.80/32 scope global eno16777736
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feb9:46b5/64 scope link
       valid_lft forever preferred_lft forever

 截止目前主备+负载配置完成,过程很简单,很多细节需要动手操作才会发现.

 

转载于:https://www.cnblogs.com/mrice/p/10945647.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值