Ubuntu下的负载均衡Web集群配置

负载均衡的方案有很多种,这边采用HAProxy+Keepalived解决方案。

介绍

HAProxy介绍

HAproxy是一个高性能的TCP/HTTP负载均衡服务器软件,速度非常快,可用性极高。它适用于所有基于TCP或者HTTP的应用程序,尤其适用于繁忙的Web服务。在当今主流的服务器配置上,可以轻松地支持几万的并发链接。

Keepalived介绍

从Keepalived的名字可以看出,它的作用就是“keep alive”,让你的系统正常运转,永不宕机。我们使用Keepalived的目的,就是检测从多Web服务器的状态;如果有一台Web

服务器出现故障,Keepalived将很快检测到并把它从系统中剔除,待其恢复正常后,Keepalived会自动将其重新加入到服务器群中。整个过程不需要人工干涉,你所要做的,只是对发生故障的节点进行修复。

HAProxy+Keepalived的好处

这种集群方案的好处有很多:

配置十分方便。

万一有Web服务器宕机,系统可以及时发现,并把访问需求交给健康的服务器,这样就不会出现用户无法访问的情况。这就是“高可用性”的体现。

负载均衡服务器会把访问需求分给比较有空的Web服务器,而不会给那些本来就已经很忙的服务器。这是“负载均衡”的体现。

Failover(故障转移)时间极短。如果将状态检查频率设置为每1秒钟一次,那么一旦负载均衡服务器发生故障,从服务器会在1秒后立即变成主服务器。

HAProxy支持Session,会比较好地处理好Session的问题。很多网站都是基于Session的,比如论坛、购物车等;如果你在网上 购物时,在Apache节点一上创建了Session,那么万一节点二突然接管了你的访问请求,你的购物信息将丢失,因为Session也丢失了。但是我 们采用的方案,就不会产生这样的问题。

架构

在这个例子中,有2个Apache节点、2个负载均衡节点。这4台服务器需要用5个IP地址。其中4个IP地址当然是分配给4台服务器每台一个,剩下的那个IP,我们叫做“虚拟IP地址”,用来给2个负载均衡节点共享。

架构详情

配置信息如下:

负载均衡节点1:lb1.mytest.com(lb1);IP地址:192.168.1.10;

负载均衡节点2:lb2.mytest.com(lb2);IP地址:192.168.1.11;

Web节点1:www1.mytest.com(www1);IP地址:192.168.1.12;

Web节点2:www2.mytest.com(www2);IP地址:192.168.1.13;

虚拟IP:192.168.1.14,用于处理访问请求。

架构的实现

Web服务器的安装及配置

1.安装apache2

首先,我们在两台Web节点www1和www2上安装Apache2:

$sudo apt-get install apache2

安装程序已经创建了一个虚拟主机/etc/apache2/sites-available/default,其默认的www根目录为/var/www/。

2.修改apache2.conf

在本方案中,我们会将HAProxy配置成透明的反向代理,它会把访问者的IP地址用X-Forwarded-For变量传递给后面的Web服务器。我们当然希望Apache在日志中记录访问者的IP地址,而不是负载服务器的IP地址。

因此,我们需要修改apache2.conf将LogFormat中的%h改为%{X-Forwarded-For}i.

$sudo nano /etc/apache2/apache2.conf

[...]

#LogFormat ”%h  %l  %u  %t \ ”%r\” %>s %b \ ” %{Referer}i\” \”%{User-Agent}i\”" combined

LogFormat ”%{X-Forwarded-For}i %l %u %t \”%r\” %>s %b \ ” %{Referer}i\” \”%{User-Agent}i\”" combined

[...]

3.创建文件check.txt

我们将使用HAProxy来监控Web节点的状态。为此,我们需要在两个Web节点的/var/www/ 下面准备一个文件,如果HAProxy能够访问到这个文件,就说明Web节点正常;否则说明Web节点发生故障。

该文件的名称可以自己定义,我们把它命名为check.txt;

$sudo touch /var/www/check.txt

4.修改虚拟主机配置

我们不希望虚拟主机在访问日志中记录check.txt的访问情况,这样会干扰我们做日志分析。为此,我们需要修改虚拟主机的配置文件:

$sudo nano /etc/apache2/sites-available/default

[...]

SetEnvIf Request_URI ”^/check\.txt$” dontlog

CustimLog /var/log/apache2/access.log combined env=!dontlog

[...]

请确认该配置文件中没有其他的CustomLog。

最后,重新启动Apache:

$sudo /etc/init.d/apache2 restart

HAProxy的安装及配置

以下操作,均在lb1.mytest.com和lb2.mytest.com上执行。

1.安装HAProxy

好,现在我们来安装HAProxy:

$sudo apt-get install haproxy

2.配置haproxy.cfg

HAProxy的配置文件为/etc/haproxy.cfg, 我们先备份原文件,再创建一个新的:

$sudo mv /etc/haproxy.cfg /etc/haproxy.cfg-back

$sudo nano /etc/haproxy.cfg

global

log 127.0.0.1 local0

log 127.0.0.1 local1 notice

maxconn 4096

user haproxy

group haproxy

defaults

log global

mode http

option httplog

option dontlognull

retries 3

redispat ch

maxconn 2000

contimeout 5000

clitimeout 50000

srvtimeout 50000

listen webfarm 192.168.1.14:80

mode http

stats enable

sats auth admin:password

balance roundrobin

cookie JSESSIONID prefix

option httpclose

option forwardfor

option httpchk HEAD /check.txt HTTP/1.0

server webA 192.168.1.12:80 cookie A check

server webB 192.168.1.13:80 cookie B check

上述配置中的retries指定了重试次数,也就是在发现某个Web节点无法访问后,还会再重试3次。“cookie JSESSIONID prefix”一行,是用于处理Session的。

该配置让HAProxy监听在虚拟IP地址192.168.1.14的80端口上,后面的两台Web服务器分别为192.168.1.12和192.168.1.13,以文件/check.txt作为检查目标。

HAProxy的配置选项极为丰富,具体信息请参考:

http://haproxy.1wt.eu/download/1.3/doc/haproxy-en.txt

3.修改/etc/sysctl.conf

为了让HAProxy能够绑定到虚拟IP地址,我们需要修改/etc/sysctl.conf:

$sudo nano /etc/sysctl.conf

添加下面一行内容

net.ipv4.ip_nonlocal_bind = 1

然后,使之生效:

$sudo sysctl -p

4.让HAProxy自动启动

现在,我们来修改/etc/default/haproxy,以便让HAProxy在系统启动时也能够自动启动。

$sudo nano /etc/default/haproxy

#Set ENABLED to 1 if you want the init script to start haproxy.

ENABLED = 1

#Add extra flags here.

#EXTRAOPTS = ”-de -m 16″

Keepalived的安装及配置

我们刚才已经配置好lb1和lb2,让HAProxy监听虚拟IP地址192.168.1.14.不过,到底是lb1还是lb2应该监听该IP地址呢?这项工作是由Keepalived完成的。

Keepalived将lb1和lb2分配为“主”和“从”,平时工作由主服务器负责,从服务器处于待命状态。

1. 安装Keepalived

现在我们来安装Keepalived:

$sudo apt-get install keepalived

该软件也很小,很快就可以安装完毕。

2. 配置Keepalived

现在我们来配置Keepalived. 它的配置文件位于/etc/keepalived/keepalived.conf.

我们将lb1作为负载均衡的“主服务器”,将lb2作为“从服务器”。这是通过配置文件中的priority(优先级)来实现的。在“主服务喊叫”上要设置priority为101,“从服务器”则设置priority为100。

在lb1上,设置/etc/keepalived/keepalived.conf:

$sudo nano /etc/keepalived/keepalived.conf

vrrp_script chk_haproxy{

script ”kill -0 haproxy”

interval 2                #每2秒钟检查一次

weight 2

}

vrrp_instance VI_1{

interface eth0

state Master

virtual_router_id 51

priority 101 #101为“主”,100为“从”

virtual_ipaddress{

192.168.1.14

}

track_script{

chk_haproxy

}

}

然后,运行Keepalived服务:

$sudo /etc/init.d/keepalived start

在lb2上,设置/etc/keepalived/keepalived.conf:

$sudo nano /etc/keepalived/keepalived.conf

vrrp_script chk_haproxy{

script ”kill -0 haproxy”

interval 2                #每2秒钟检查一次

weight 2

}

vrrp_instance VI_1{

interface eth0

state Master

virtual_router_id 51

priority 100 #101为“主”,100为“从”

virtual_ipaddress{

192.168.1.14

}

track_script{

chk_haproxy

}

}

然后,运行Keepalived服务:

$sudo /etc/init.d/keepalived start

现在,在lb1和lb2上分别查看IP地址信息:

$ip addr sh eth0

在lb1上,应该可以看到虚拟IP地址192.168.1.14

在lb2上,刚看不到虚拟IP地址192.168.1.14

最后,在lb1和lb2上分别启动HAProxy:

$sudo /etc/init.d/haproxy start

转载于:https://www.cnblogs.com/myphoebe/archive/2011/08/18/2144939.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1,tomcat8的配置: 1.1修改tomcat8.x/conf/context.xml的配置如下: <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- The contents of this file will be loaded for each web application --> <Context> <!-- Default set of monitored resources. If one of these changes, the --> <!-- web application will be reloaded. --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> <Resources cachingAllowed="true" cacheMaxSize="100000" /> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:127.0.0.1:11211" username="root" password="" sticky="false" sessionBackupAsync="false" lockingMode="uriPattern:/path1|/path2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory" /> --> <Resources cachingAllowed="true" cacheMaxSize="100000" /> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:127.0.0.1:11211" username="root" password="" sticky="false" sessionBackupAsync="false" lockingMode="uriPattern:/path1|/path2" requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" sessionBackupTimeout="18000" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false" /> </Context> 1.2添加memcached如下依赖的jar包到tomcat8.x/lib/: asm-5.1.jar couchbase-client-1.4.12.jar javolution-5.5.1.jar kryo-4.0.0.jar kryo-serializers-0.38.jar memcached-session-manager-2.0.0.jar memcached-session-manager-tc8-2.0.0.jar minlog-1.3.jar msm-javolution-serializer-2.0.0.jar msm-kryo-serializer-2.0.0.jar msm-xstream-serializer-2.0.0.jar objenesis-2.1.jar reflectasm-1.09.jar spymemcached-2.12.1.jar 2,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; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; sendfile on; tcp_nopush on; tcp_nodelay on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; #设定负载均衡的服务器列表 upstream 127.0.0.1 { #设定负载均衡的服务器列表 #ip_hash; #同一机器在多网情况下,路由切换,ip可能不同 #weigth参数表示权值,权值越高被分配到的几率越大 server 127.0.0.1:8085 weight=1 max_fails=20 fail_timeout=600s; server 127.0.0.1:8086 weight=1 max_fails=20 fail_timeout=600s; } map $http_upgrade $connection_upgrade { default upgrade; '' close; } server { listen 80; server_name localhost; charset UTF-8; #设定本虚拟主机的访问日志 access_log logs/host.access.log main; #对 "/" 所有应用启用负载均衡 location / { proxy_pass http://127.0.0.1; #保留用户真实信息 proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; index index.html index.htm index.aspx; } #对 "/Dossm3RabbitMQConsumer/" 启用负载均衡 location /Dossm3RabbitMQConsumer/ { proxy_pass http://localhost:8086; #保留用户真实信息 proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; index index.html index.htm index.aspx; } } # 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; # } #} } 3,cas的配置(): 3.1 修改/CAS/WEB-INF/spring-configuration/ticketRegistry.xml <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to Jasig under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. Jasig licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at the following location: http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <description> Configuration for the default TicketRegistry which stores the tickets in-memory and cleans them out as specified intervals. </description> <!-- memcached 配置开始 --> <!-- Ticket Registry --> <bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.MemCacheTicketRegistry"> <constructor-arg index="0"> <bean class="net.spy.memcached.spring.MemcachedClientFactoryBean" p:servers="127.0.0.1:11211" p:protocol="BINARY" p:locatorType="ARRAY_MOD" p:failureMode="Redistribute" p:transcoder-ref="serialTranscoder"> <property name="hashAlg"> <util:constant static-field="net.spy.memcached.DefaultHashAlgorithm.FNV1A_64_HASH" /> </property> </bean> </constructor-arg> <!-- TGT timeout in seconds --> <constructor-arg index="1" value="36000" /> <!-- ST timeout in seconds --> <constructor-arg index="2" value="2" /> </bean> <bean id="serialTranscoder" class="net.spy.memcached.transcoders.SerializingTranscoder" p:compressionThreshold="2048" /> <!-- memcached 配置结束 --> <!--Quartz --> <!-- 默认配置开始 --> <!-- Ticket Registry --> <!-- <bean id="ticketRegistry" class="org.jasig.cas.ticket.registry.DefaultTicketRegistry" />--> <!-- TICKET REGISTRY CLEANER --> <!-- <bean id="ticketRegistryCleaner" class="org.jasig.cas.ticket.registry.support.DefaultTicketRegistryCleaner" p:ticketRegistry-ref="ticketRegistry" p:logoutManager-ref="logoutManager" /> <bean id="jobDetailTicketRegistryCleaner" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" p:targetObject-ref="ticketRegistryCleaner" p:targetMethod="clean" /> <bean id="triggerJobDetailTicketRegistryCleaner" class="org.springframework.scheduling.quartz.SimpleTriggerBean" p:jobDetail-ref="jobDetailTicketRegistryCleaner" p:startDelay="200000" p:repeatInterval="50000000" /> --> <!-- 默认配置结束 --> </beans> 3.2 添加cas和memcached整合的如下依赖jar包到/CAS/WEB-INF/lib: cas-server-integration-memcached-4.0.0.jar mockito-core-2.1.0-RC.1.jar spymemcached-2.11.2.jar 参考CAS官方配置:https://apereo.github.io/cas/4.2.x/installation/Memcached-Ticket-Registry.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值