centos 7+keepalived+nginx+tomcat搭建负载均衡

nginx

2018-09-04

76

1

centos 7+keepalived+nginx+tomcat搭建负载均衡

一、搭建环境
操作系统:linux centos7
nginx版本:nginx 1.15.2
jdk版本:jdk 1.8.0_181
tomcat: apache-tomcat-9.0.1
 
二、服务器IP地址:
VIP(Virtual IP) IP地址:192.168.12.129 访问web服务地址入口。
负载均衡地址主服务器:192.168.12.225
负载均衡地址从服务器:192.168.12.174
web服务器主服务器:192.168.12.155
web服务器从服务器:192.168.12.232
 
三、安装nginx
nginx安装就不介绍了,大家可以百度搜下安装方法。
 
四、配置nginx
两台nginx服务器都是如下配置:
配置文件路径:/usr/local/nginx/conf/nginx.conf
配置文件信息:
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;
    #设定请求缓冲
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
	
    sendfile     on;
    tcp_nopush   on;
    tcp_nodelay  on;
	
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip on;
    gzip_min_length    1k;
    gzip_buffers       4 16k;
    gzip_http_version  1.1;
    gzip_comp_level    2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
	
    upstream tomcat_pool
   {
	server 192.168.12.155:8080 weight=4 max_fails=2 fail_timeout=30s;
	server 192.168.12.232:8080 weight=4 max_fails=2 fail_timeout=30s;
   }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        
       #这里读取本地服务器的tomcat
        location / {
            root   /home/project/apache-tomcat-9.0.1/webapps;
            index  index.jsp index.html index.htm;
        }
		
       #这里读取的是远程服务器的tomcat
	location ~ \.(jsp|jspx|dp)?$
	{
		proxy_set_header Host $host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_pass http://tomcat_pool;
	}

        #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;
        #}
    }
 
 
五、配置192.168.12.155 web服务器
安装jdk。
安装tomcat。
发布webbase项目
 
六、配置192.168.12.232 web服务器
安装jdk。
安装tomcat。
发布webbase项目
 
七、运行效果
八、安装与配置keepalived
 
8.1、安装keepalived相关包
yum install -y keepalived
 
8.2、编辑keepalived.conf配置文件
①进入keepalived.conf所在目录:cd /etc/keepalived
②首先清除掉keepalived原有配置:> keepalived.conf
③重新编辑keepalived配置文件:vi keepalived.conf
内容如下:
global_defs {
    notification_email {
        997914490@qq.com
    }
    notification_email_from sns-lvs@gmail.com
    smtp_server 192.168.1.114
    smtp_connection_timeout 30
    router_id LVS_MASTER         # 设置lvs的id,在一个网络应该是唯一的
}
vrrp_instance VI_1 {
    state MASTER            # 指定keepalived的角色,MASTER为主,BACKUP为备
    interface eth0            # 当前进行vrrp通讯的网络接口卡(当前centos的网卡)
    virtual_router_id 66        # 虚拟路由编号,主从要一直
    priority 100            # 优先级,数值越大,获取处理请求的优先级越高
    advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.200        # 定义虚拟ip(VIP),可多设,每行一个
    }
}
# 定义对外提供的LVS的VIP以及port
virtual_server 192.168.1.200 8080 {
    delay_loop 6             # 设置健康检查时间,单位为秒
    lb_algo wrr            # 设置负载调度的算法为wrr
    lb_kind DR            # 设置lvs实现负载的机制,有NAT、TUN、DR三个模式
    nat_mask 255.255.255.0
    persistence_timeout 0        # 同一IP 0秒内的请求都发到同个real server
    protocol TCP
    real_server 192.168.1.111 8080 { # 指定real server1的ip地址
        weight 3        # 配置节点权值,数值越大权重越高
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.1.115 8080 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}    
 
 
配置从负载服务器
  和主负载服务器一样,先安装keepalived,然后编辑keepalived.conf,内容如下,与主负载服务器有些许差别
global_defs {
    notification_email {
        997914490@qq.com
    }
    notification_email_from sns-lvs@gmail.com
    smtp_server 192.168.1.112
    smtp_connection_timeout 30
    router_id LVS_BACKUP         # 设置lvs的id,在一个网络应该是唯一的
}
vrrp_instance VI_1 {
    state BACKUP            # 指定keepalived的角色,MASTER为主,BACKUP为备
    interface eth0            # 当前进行vrrp通讯的网络接口卡(当前进行vrrp通讯的网络接口卡)
    virtual_router_id 66        # 虚拟路由编号,主从要一致
    priority 99            # 优先级,数值越大,获取处理请求的优先级越高
    advert_int 1            # 检查间隔,默认为1s(vrrp组播周期秒数)
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.200        # 定义虚拟ip(VIP),可多设,每行一个
    }
}
# 定义对外提供的LVS的VIP以及port
virtual_server 192.168.1.200 8080 {
    delay_loop 6             # 设置健康检查时间,单位为秒
    lb_algo wrr            # 设置负载调度的算法为wrr
    lb_kind DR            # 设置lvs实现负载的机制,有NAT、TUN、DR三个模式
    nat_mask 255.255.255.0
    persistence_timeout 0        # 同一IP 0秒内的请求都发到同个real server
    protocol TCP
    real_server 192.168.1.111 8080 { # 指定real server1的ip地址
        weight 3        # 配置节点权值,数值越大权重越高
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.1.115 8080 {
        weight 3
        TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}    
 
 
keepalive 虚拟server ip 绑定网卡,查看网卡命令:可以使用 ip a 命令查看
 
启动keepalive服务,主从都要启动!
service keepalived start
 
linux centos 7下启动命令
systemctl start keepalived.service
 
 

发表评论

全部评论:0条

鸿福951

努力打造一个好用的webui

热评文章

推荐文章