본문 바로가기

Apache httpd server/Load Balancing

Apache Load Balancing

Apache, Tomcat Load Balancing


테스트환경

Windows7, JDK 1.8, Apache 2.4, Tomcat 8

로컬 시스템에 Apache 1개, Tomcat 바이너리 2개를 실행하고 Apache의 httpd.conf 파일에 로드발란싱을 설정하여 2개의 톰캣에 요청이 전달되도록 함

zip 파일로 배포되는 Tomcat 을 다운로드하여 압축을 해제하고 사용함

한 시스템에서 2개의 톰캣을 동시에 실행하기 위해서 톰캣의 server.xml 에서 각 포트번호를 서로 다르게 설정하여 실행함



References



Load Balancing 설정순서


이 내용은 아파치 웹서버의 VirtualHost 설정과는 무관한 내용이므로 앞선 내용에 이어서 참고하려면 가상호스트 설정을 해제해야한다

가상 호스트 설정을 해제하려면 아파치 설정파일 httpd.conf 의 내용 중에서 가상호스트 설정파일(httpd-vhosts.conf)을 포함하는 행을 주석처리하면 된다

아파치 가상 호스트설정은 한개의 IP를 가진 컴퓨터에 내용이 다른 2개의 웹사이트를 구성하는 방법이고, 

Load Balancing 설정은 한개의 웹사이트를 여러개이 톰캣이 로드를 분담하여 성능을 올리거나 유지보수성을 향상하거나 서비스 장애를 극복하여 서비스의 가용성을 향상시키는 방법이다

#Include conf/extra/httpd-vhosts.conf


1. Tomcat 2개, Apache 1개 설치


2. Tomcat Connector 다운로드 및 Apache에 복사

Tomcat Connector 파일을 다운로드하고 압축을 해제하여 mod_jk.so 파일을 Apache 의 modules 디렉토리 안에 복사한다

이 부분의 자세한 내용은 Apache & Tomcat Connection 페이지를 참조하세요


3. Apache의 httpd.conf 파일 설정

httpd.conf 파일의 하단에 아래의 내용을 추가한다 (아파치에 전달되는 모든 요청이 balanacer 라는 worker에게 전달되도록 설정함 )

LoadModule jk_module modules/mod_jk.so

JkWorkersFile conf/workers.properties


JkLogFile logs/mod_jk.log

JkLogLevel info

JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

JkRequestLogFormat "%w %V %T"


JkMount /  loadbalancer

JkMount /* loadbalancer


AddDefaultCharset UTF-8



4. workers.properties 파일 생성 및 설정

Apache/conf/workers.properties 파일 생성, 아래의 내용 입력

#list of top level workers

worker.list=loadbalancer, status


worker.tomcat1.port=8009

worker.tomcat1.host=localhost  <-- IP주소나 다른 도메인으로 변경하면 다른 컴퓨터와 연동할 수 있다

worker.tomcat1.type=ajp13

worker.tomcat1.lbfactor=100


worker.tomcat2.port=8010

worker.tomcat2.host=localhost

worker.tomcat2.type=ajp13

worker.tomcat2.lbfactor=100


worker.loadbalancer.type=lb

worker.loadbalancer.balanced_workers=tomcat1, tomcat2

worker.loadbalancer.method=B             # B(Busyness), R(Requests), T(Traffic)


worker.status.type=status


위의 설정에서 2개의 톰캣이 동일 시스템에서 실행되는 환경이라면 접속을 위한 포트번호가 서로 달라야 한다


worker.loadbalancer.method 의 설정 값은 다음 중 하나를 사용한다

B (Busyness): choose the worker with the lowest number of requests currently in progress

R (Requests): choose the worker that has processed the lowest number of requests overall

T (Traffic): choose the worker that transmitted the lowest number of bytes




Tomcat의 server.xml 파일을 설정할 때도 2개의 톰캣이 동일 시스템에서 실행될 때는 각 톰캣이 동일 포트를 사용하지 않도록 설정한다


5. Tomcat 1의 server.xml 파일 설정

<Server port="8005" shutdown="SHUTDOWN">

......

<Connector port="8080" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443" />

...........

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

..............

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">

  ......

   <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

참고: Setting jvmRoute


The jvmRoute attribute of the Engine element allows the load balancer to match requests to the JVM currently responsible for updating the relevant session. It does this by appending the name of the JVM to the JSESSIONID of the request, and matching this against the worker name provided in workers.properites.

In order to configure jvmRoute, make sure that the value of "jvmRoute" for all your Engines is paired with an identically named Worker name entry in mod_jk's worker.properties configuration file.

- See more at: https://www.mulesoft.com/tcat/tomcat-clustering#sthash.ZvYguKC4.dpuf


6. Tomcat 2의 server.xml 파일 설정 (위의 톰캣과 동일 시스템에서 실행되므로 위에서 사용된 포트번호는 중복해서 사용되지 않아야 한다)

<Server port="8006" shutdown="SHUTDOWN">

......

<Connector port="8081" protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8444" />

...........

<Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />

..............

<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">

  ......

   <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>



7. 테스트

 - 2개의 Tomcat 실행 (실행오류가 없는지 확인)

 - Apache 실행

 - 웹브라우저에서 http://localhost/index.jsp 으로 접속

 - Tomcat 1을 중지, Tomcat 2 실행하고 http://localhost/index.jsp 으로 접속

 - Tomcat 2를 중지, Tomcat 1 실행하고 http://localhost/index.jsp 으로 접속


위와 같이 테스트하여 여전히 톰캣의 첫 페이지가 화면에 제대로 출력되는지 확인한다


아래와 같은 페이지를 작성하여 각 톰캣의 ROOT 폴더에 저장하고 http://localhost/test.jsp 으로 접속하면서 위와 같은 테스트를 하면 더욱 확실하게 Fail Over 기능이 작동하는지 확인할 수 있다.

test.jsp

<!doctype html>

<html>

<body>

Tomcat 1 <!-- 톰캣 2에 저장할 때는 1 을 2 로 변경하여 저장하면 현재 어떤 톰캣이 요청을 처리하는지 화면에서 직접 확인할 수 있다 -->

<p>

<img src="tomcat.png">

<p>

</body>

</html>