개발 ON
  • [Network | Rocky Linux] Session Clustering
    2024년 06월 20일 16시 34분 39초에 업로드 된 글입니다.
    작성자: 이주여이

    Apache: 클라이언트 요청이 왔을 때 정적 페이지로 응답한다.
    Tomcat: 동적 페이지를 만들기 위한 웹 컨테이너, 서블릿 컨테이너 역할을 한다.
    Session Clustering: apache tomcat이 2대 이상 운영되는 환경에서 세션을 동일하게 관리하는 것을 의미한다.

     

    ⭐ 아이콘은 중요하게 봐야하는 부분으로 개념도 같이 공부하자!

    1. 방화벽 추가(WEB, WAS) ⭐

    $ firewall-cmd --permanent --zone=public --add-port=8009/tcp
    $ firewall-cmd --permanent --zone=public --add-port=45564/tcp
    $ firewall-cmd --permanent --zone=public --add-port=45564/udp
    $ firewall-cmd --permanent --zone=public --add-port=4000-4100/tcp
    $ firewall-cmd --reload
    $ firewall-cmd --list-all

    7. 라우터 등록(WAS) ⭐

    💡 세션 클러스터링 간의 멀티 캐스트를 위한 라우팅 설정으로 WAS #1, WAS #2 서버에서 진행한다.

    ⭐ 참고로 이 작업 안해서 톰캣 서버 간에 세션 공유가 안됐던 이슈가 있었다.

    $ route # 라우터 확인
    $ route add -net 224.0.0.0 netmask 240.0.0.0 dev ${Iface }

    1. httpd.conf 수정(WEB)

    # > 64(vi 편집기에서 ':set number' 했을 때 표시된 넘버 라인)
    $ LoadModule jk_module /etc/httpd/modules/mod_jk.so
    $ JkWorkersFile /etc/httpd/conf/workers.properties
    $ JkLogFile /etc/httpd/logs/mod_jk.log
    $ JkShmFile /etc/httpd/logs/mod_jk.shm
    $ JkMount /* lb # 이 부분만 변경
    
    # > 107
    # 나는 호스트 네트워크로 포트 포워딩을 해놓은 상태이기 때문에 127.0.0.1(localhost)이 아닌 내 WEB 서버의 ip를 입력했다.
    $ ServerName 192.168.56.101 # 주석 해제 후 ip 주소 변경

    WAS 서버 2개를 lb라는 이름으로 worker.list에 명시했기 때문에 JkMount는 workers.properties 파일의 list명을 입력해줘야 한다.

    JkMount에 명시된 /* 모든 경로에 대해 lb가 받는다는 뜻이다.

    2. workers.properties 수정(WEB) ⭐

    $ worker.list=lb # httpd.conf의 jkMount에 명시된 list 이름
    $ worker.lb.balance_workers=tomcat1,tomcat2
    $ worker.lb.type=lb
    $ worker.lb.sticky_session=true
    $ 
    $ worker.tomcat1.port=8009
    $ worker.tomcat1.host=192.168.56.103
    $ worker.tomcat1.type=ajp13
    $ worker.tomcat1.lbfactor=1
    $ 
    $ worker.tomcat2.port=8009
    $ worker.tomcat2.host=192.168.56.104
    $ worker.tomcat2.type=ajp13
    $ worker.tomcat2.lbfactor=1

     

    로드 밸런스 참고 레퍼런스
    https://tomcat.apache.org/connectors-doc/common_howto/loadbalancers.html
    https://tomcat.apache.org/connectors-doc/reference/workers.html

    3. 테스트 페이지 생성(WAS)

    $ cd /usr/local/tomcat/webapps/ROOT/WEB-INF
    $ vi index.jsp
    
    $ <html>
    $ <head>
    $ <title>Session Clustering Test</title>
    $ </head>
    $ <body>
    $ <h1 style="color: blue">TOMCAT01</h1> # TOMCAT1은 WAS1, WAS2는 TOMCAT2로 작성한다.
    $ <%
    % Integer cnt = (Integer)session.getAttribute("cnt");
    $ 
    $ if(cnt == null) {
    $         cnt = new Integer(1);
    $ } else {
    $         cnt = new Integer(cnt.intValue() + 1);
    $ }
    $
    $ session.setAttribute("cnt", cnt);
    $ %>
    $ <h3>SESSION COUNT: <%= cnt %></h3>
    $ <h3>CURRENT SESSION ID: <%= request.getRequestedSessionId() %></h3>
    $ <%
    $ String id = session.getId();
    $ long lastTime = session.getLastAccessedTime();
    $ long createdTime = session.getCreationTime();
    $ long timeUsed = (lastTime - createdTime) / 60000;
    $ int inactive = session.getMaxInactiveInterval() / 60;
    $ boolean newSession = session.isNew();
    $ %>
    $ #1. session id is '<%= session.getId() %>'. <br>
    $ #2. the time you stayed on the website is '<%= timeUsed %>'. <br>
    $ #3. the session is valid for '<%= inactive %>'. <br>
    $ <%
    $ if(newSession) {
    $         out.println("#4. a new session has been created.");
    $ } else {
    $         out.println("#4. no session was created.");
    $ }
    $ %>
    $ </body>
    $ </html>

    4. server.xml 수정(WAS) ⭐

    $ :set number # vi 편집기 줄번호 표시

    💡 아래의 클러스터 기본 사항에 따라 사진과 같이 수정한다.

    # > 127
    $ port="8009" # WAS #1, WAS #2 모두 '8009'로 명시한다.
    
    # > 140
    # defaulthost - 변경 x
    # jvmRoute - workers.properties에 등록한 해당 WAS 서버의 프로퍼티명
    $ <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
    
    # > 154
    $ port="45564" ... # WAS #1, WAS #2 모두 '45564'로 명시한다.
    
    # > 155
    # address - jvmRoute가 정의한 workers의 ip 주소와 일치해야 한다.
    $ <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver" address="192.168.56.103" ... />
    
    # > 181
    # 실제 경로와 맞춰준다.

    5. web.xml 수정(WAS)

    $ G # 문서의 제일 마지막으로 이동(vi 편집기에서 ':' 입력 X)
    
    $ <distributable /> # 한 줄을 추가한다.
    $ </web-app>

     


    참고 레퍼런스

    https://jy-p.tistory.com/87

    https://joolib.tistory.com/14

    https://tomcat.apache.org/tomcat-7.0-doc/cluster-howto.html

    https://sysinfo.tistory.com/21 🔥

    https://itwarehouses.tistory.com/13 🔥

    댓글