Keepalived+Nginx+Tomcat+Redis实现高可用负载均衡(一)

Keepalived+Nginx+Tomcat+Redis实现高可用负载均衡(一)_第1张图片

 

01 前言   

    一个稳定Web服务器架构必须实现高可用与负载均衡。本期我们来配置Keepalived + Nginx + Tomcat + Redis的架构。keepalived用于管理Virtual IP,与nginx一起搭配实现高可用性的反向代理前端;后端使用Tomcat管理web服务,并利用Redis实现session共享。

    本文转自我个人的公众号:天目星    ,请大家多多关注。

请关注我的微信公众号


 

 

一、使用的软件版本与架构图。

    linux: CentOS 7.5.1804

    Keepalived: keepalived-1.3.5

    Nginx: nginx-1.12.2

    Tomcat: apache-tomcat-7.0.93

    Redis: redis-3.2.12

 

Keepalived+Nginx+Tomcat+Redis实现高可用负载均衡(一)_第2张图片

 


 

二、安装软件(前端)

    1、安装Nginx

    Nginx的安装方法在之前的文章中有提供,在此不再复述,需要的同学可以前往这里查看:点击传送门

 

    Nginx配置(两台前端一致)

user  nginx nginx;worker_processes  1;error_log  logs/error.log  info;
events {    use epoll;    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  65;
    # 配置后端两台Tomcat服务器    upstream test {        server 192.168.159.31:8080 weight=5;        server 192.168.159.32:8080 weight=5;    }
    server {        listen       80;        server_name  www.test.com;                location / {                proxy_set_header Host $host:80;                proxy_connect_timeout 240;                proxy_send_timeout    240;                proxy_read_timeout    240;                proxy_pass http://test;        }}

 

    2、安装Keepalived

    可以编译安装或使用yum源安装。因本文使用CentOS7,直接使用yum源安装。

yum install keepalived

    

    keepalived主要用来管理VIP,所以需要配置好切换VIP的条件。

! Configuration File for keepalived
global_defs {   notification_email {        root@localhost   }   notification_email_from root@localhost   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id NGINX_DEVEL}
#根据情况判断相关软件是否启动来切换VIPvrrp_script chk_nginx {    script "killall -0 nginx"    interval 2    weight  -5    fall     3    rise     5
vrrp_instance VI_1 {    state MASTER    interface ens33    virtual_router_id 51    # Master的权重需比Slave大    priority 100       advert_int 1    authentication {        # 两台keepalived的认证配置        auth_type PASS          auth_pass !w23@3sf/    }    virtual_ipaddress {        192.168.159.222 dev ens33    }    track_script {        chk_nginx    }}

 

    yum安装的keepalived日志默认写在系统日志中,建议自定义配置一下存放地点。

    配置keepalived日志级别:vim /etc/sysconfig/keepalived

# Options for keepalived. See `keepalived --help' output and keepalived(8) and# keepalived.conf(5) man pages for a list of all options. Here are the most# common ones :## --vrrp               -P    Only run with VRRP subsystem.# --check              -C    Only run with Health-checker subsystem.# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.# --dump-conf          -d    Dump the configuration data.# --log-detail         -D    Detailed log messages.# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)#
KEEPALIVED_OPTIONS="-D -d -S 0"

 

    更改rsyslog配置:vim /etc/rsyslog.conf

# 添加一行local0.*        /var/log/keepalived.log

 

    重启rsyslog服务生效

systemctl restart rsyslog.service

 


  因篇幅有限,这篇只介绍前端的配置,下一篇我将介绍Tomcat与redis的配置。

Keepalived+Nginx+Tomcat+Redis实现高可用负载均衡(一)_第3张图片

 

请关注我的个人公众号

你可能感兴趣的:(高可用)