负载均衡与web服务集群搭建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了负载均衡与web服务集群搭建相关的知识,希望对你有一定的参考价值。
主机名 | ip地址 | 角色 | vip |
lvs01 | 192.168.1.90 | 4层负载 | 192.168.1.206 |
lvs02 | 192.168.1.91 | 4层负载 | 192.168.1.206 |
lb01 | 192.168.1.5 | 7层负载 | |
lb02 | 192.168.1.6 | 7层负载 | |
web-nginx01 | 172.16.1.33 | 静态服务器 | |
web-nginx02 | 172.16.1.34 | 静态服务器 | |
web-tomcat01 | 172.16.1.31 | 动态服务器 | |
web-tomcat02 | 172.16.1.32 | 动态服务器 |
lvs部署
1.编辑realserver脚本文件(在lb01 lb02上)
cd /etc/init.d/ vim realserver # chkconfig: 35 23 94 SNS_VIP=192.168.1.206 . /etc/init.d/functions case "$1" in start) ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP /sbin/route add -host $SNS_VIP dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $SNS_VIP >/dev/null 2>&1 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;; *) echo "Usage: $0 {start|stop}" exit 1 esac exit 0 chmod 755 realserver service realserver start chkconfig realserver on
2.配置主负载服务器
安装Keepalived ipvsadm相关包
yum install -y keepalived ipvsadm vim /etc/keepalived/keepalived.conf global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server 192.168.80.1 smtp_connection_timeout 30 router_id LVS01 #设置lvs的id,在一个网络内应该是唯一的 } vrrp_instance VI_1 { state MASTER #指定Keepalived的角色,MASTER为主,BACKUP为备 interface eth1 #指定Keepalived检测网卡 virtual_router_id 51 #虚拟路由编号,主备要一致 priority 100 #定义优先级,数字越大,优先级越高,主DR必须大于备用DR advert_int 1 #检查间隔,默认为1s authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.206 #定义虚拟IP(VIP)为192.168.1.206,可多设,每行一个 } } virtual_server 192.168.80.200 80 { #定义对外提供服务的LVS的VIP以及port delay_loop 6 #设置健康检查时间,单位是秒 lb_algo wrr #设置负载调度的算法为wrr lb_kind DR #设置LVS实现负载的机制,有NAT、TUN、DR三个模式 nat_mask 255.255.255.0 persistence_timeout 0 protocol TCP real_server 192.168.1.5 80 { #指定real server1的IP地址 weight 3 #配置节点权值,数字越大权重越高 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } real_server 192.168.1.6 80 { #指定real server2的IP地址 weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 80 } } }
3.配置备负载服务器
只需要修改
router_id LVS02 priority 99
4.启动keepalived (lvs01 lvs02)
/etc/init.d/keepalived start chkconfig keepalived on
nginx反向代理 7层负载
1.安装nginx(lb01 lb02)
yum install -y pcre-devel openssl-devel pcre openssl mkdir -p /application/tools/ cd /application/tools/ wget http://nginx.org/download/nginx-1.12.2.tar.gz useradd www -M -s /sbin/nologin -u 504 tar xf nginx-1.12.2.tar.gz cd nginx-1.12.2 ./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module make && make install ln -s /application/nginx-1.12.2/ /application/nginx echo "#nginx" >>/etc/rc.local echo "/application/nginx/sbin/nginx" >>/etc/rc.local cd /application/nginx/conf egrep -v "#|^$" /application/nginx/conf/nginx.conf.default > /application/nginx/conf/nginx.conf
添加后端服务器nginx健康检测模块
1.下载模块
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip unzip master
2.打补丁
cd /application/tools/nginx-1.12.2 patch -p1 < /application/tools/nginx_upstream_check_module-master/check_1.12.1+.patc
3.重新编译
./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --add-module=/application/tools/nginx_upstream_check_module-master make rm -rf /usr/local/bin/nginx mv /application/nginx/sbin/nginx /application/nginx/sbin/nginx.bak cp ./objs/nginx /application/nginx/sbin/ ln -s /application/nginx/sbin/nginx /usr/local/bin/nginx
4.修改配置文件 动静分离 静态内容由nginx服务器处理(web-nginx01 web-nginx02) 动态内容由tomcat处理(web-tomcat01 web-tomcat02)
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream tomcat { server 172.16.1.31:8080 weight=1; server 172.16.1.32:8080 weight=1; check interval=3000 rise=2 fall=5 timeout=1000 type=http; } upstream nginx { server 172.16.1.33 weight=1; server 172.16.1.34 weight=1; check interval=3000 rise=2 fall=5 timeout=1000 type=http; } server { listen 80; server_name www.e3mall.com; location / { proxy_pass http://tomcat; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ { proxy_pass http://nginx; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /status { check_status; access_log off; allow 172.16.1.0/24; deny all; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
nginx服务器安装(web-nginx01 web-nginx02)
1.部署nginx
yum install -y pcre-devel openssl-devel pcre openssl rpm -qa pcre-devel openssl-devel
2.下载nginx-1.12.2
cd /application/tools/ wget http://nginx.org/download/nginx-1.12.2.tar.gz useradd www -M -s /sbin/nologin -u 504
3.编译安装
tar xf nginx-1.12.2.tar.gz cd nginx-1.12.2 ./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module make && make install ln -s /application/nginx-1.12.2/ /application/nginx chown -R www:www /application/nginx-1.12.2 echo "#nginx" >>/etc/rc.local echo "/application/nginx/sbin/nginx" >>/etc/rc.local cd /application/nginx/conf egrep -v "#|^$" nginx.conf.default > nginx.conf
tomcat服务器安装(web-tomcat01 web-tomcat02)
1.安装jdk1.7
http://download.oracle.com/otn/java/jdk/7u80-b15/jdk-7u80-linux-i586.tar.gz mkdir /java cd /java wget http://download.oracle.com/otn/java/jdk/7u80-b15/jdk-7u80-linux-i586.tar.gz tar xf jdk-7u80-linux-x64.tar.gz chown -R root:root /java/jdk1.7.0_80 cat << EOF >>/etc/profile export PATH=/java/jdk1.7.0_80/bin:$PATH export CLASSPATH=.:/java/jdk1.7.0_80/lib/dt.jar:$JAVA_HOME/lib/tools.jar EOF source /etc/profile
2.安装tomcat
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.88/bin/apache-tomcat-7.0.88.tar.gz tar xf apache-tomcat-7.0.88.tar.gz ln -s /application/tools/apache-tomcat-7.0.88 /application/apache-tomcat cd /application/apache-tomcat/bin/ tar xf commons-daemon-native.tar.gz cd commons-daemon-1.1.0-native-src/unix/ ./configure make cp jsvc /application/apache-tomcat/bin/ 建立一个用来运行Tomcat的用户 useradd tomcat -M -s /usr/sbin/nologin -u 504 vim /application/apache-tomcat/bin/daemon.sh 靠前的地方加入注释 #!/bin/sh # chkconfig: - 80 20 若创建用户不是tomcat 这里修改为创建用户 test ".$TOMCAT_USER" = . && TOMCAT_USER=tomcat cd /application/apache-tomcat/bin/ chown -R tomcat:tomcat /application/tools/apache-tomcat-7.0.88 chmod a+x /application/apache-tomcat/bin/daemon.sh ln -s /application/apache-tomcat/bin/daemon.sh /etc/init.d/tomcat7 chkconfig tomcat7 on /etc/init.d/tomcat7 start
部署动静分离测试网站
1.tomcat1
cd /application/apache-tomcat/webapps/ mkdir nginx/image -p chown -R tomcat.tomcat /application/apache-tomcat/webapps/nginx cd nginx vim index.jsp <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <body> <h2>Nginx动静分离</h2> <% out.println("["+request.getLocalAddr()+":" +request.getLocalPort()+"]" + "<br/>session id:" + session.getId()); %> %<br/> %<img alt="图片" src="/nginx/img/1.jpg" style="width:300px;height:300px"> %</body> %</html> vim /application/apache-tomcat/conf/server.xml <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="/application/apache-tomcat/webapps/nginx" reloadable="true" /> tomcat2上同上修改网站路径
2.复制测试网站到其他三台服务器
scp -r nginx/ [email protected]:/application/apache-tomcat/webapps/ scp -r nginx/ [email protected]:/application/nginx/html scp -r nginx/ [email protected]:/application/nginx/html
3.nginx01 nginx02 配置
chown -R www.www /application/nginx/html/nginx vim /application/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.e3mall.com; location / { root html/nginx; index index.jsp; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
动静分离测试
1.删除tomcat服务器下的image目录下的图片
[[email protected] image]# rm -rf 1.png [[email protected] image]# rm -rf 1.png
2.网页访问测试
以上是关于负载均衡与web服务集群搭建的主要内容,如果未能解决你的问题,请参考以下文章