Saturday, June 15, 2013

Elastic Load Balancing (ELB) with a Java Web Application + Tomcat + Session Stickiness

Suppose you have a web application and you want to deploy it in Amazon cloud environment with load balance support. The whole process is pretty straight-forward and it generally doesn't take much time.

For this post, I'm using Apache Tomcat web server and I already have a war file from my HelloWorld application. 

Here is the Tomcat version I'm using:


I'm using two instances and I have extracted my tomcat zip file into /opt/ folder in each of those two instances. I have also placed HelloWorld.jar file into /opt/apache-tomcat-7.0.39/webapps folder.


Now, I will go to each of those two instances and will start tomcat server. After some minutes (or seconds) I should see my deployed web application is up and running. Which means, I can navigate to these URLs and able to see Log-In screen (initial page of my web app).

  • http://ip.address.instance-1:8080/HelloWorld/login.jsp
  • http://ip.address.instance-2:8080/HelloWorld/login.jsp

All of the above steps which I described so far, have nothing to do with Elastic Load Balancing (ELB). Just like everyone, I just deployed a web app in tomcat server. Before I start showing steps for ELB, I'm assuming your web application is also up and running and you can navigate through URLs separately.


Create Load Balancer

Step#1: On AWS EC2 console, click on the Load Balancer option under "Network & Security" section. If you do not have any ELB yet, you will see an empty list. Click on "Create Load Balancer" button.


Step#2: Write a name of your Load Balancer, this name will be used when it will create a default link. I'm also creating this Load Balancer inside my Virtual Private Cloud (VPC) that's why I'm selecting a specific VPC Id. By default, you might see only port 80 in the listerner configuration list, I have added port 8080 as my web app is running on port 8080. Add appropriate port based on your web application and click "Continue".


Step#3: This screen is dedicated to Health check configuration. Based on configuration, ELB will ping that path with that port to check the health condition and if it fails it will automatically remove your instances from the load balancer.


Since, Log-In is the default screen of my application (welcome page), so I'm using the path of Login screen as my ping path.

Step#4: Choose your Subnet id based on where you want to use your Load Balancer. For my case, subnet-2e961843 is my expected Subnet id.

Step#5: Next screen will ask you to select your security groups. I already have a security group for my VPC and I'm using it here too.

Step#6: In the "Add EC2 Instances" section, add the instances in where you already deployed Tomcat and your web application.

Step#7: This screen is for review purpose. Once you review it you can finally create your load balancer by clicking on the "Create" button.

Step#8: Once you create your balancer, it will redirect you to Load Balancer list and now you will see your newly created load balancer in the list. DNS Name column shows newly created DNS Name for your load balancer and you should be able to navigate it with proper port.

So for my case, I can navigate my load balancer by using:

http://helloworld-353060791.us-east-1.elb.amazonaws.com:8080/HelloWorld/login.jsp


Sticky Session:
Since you are using Tomcat with load balancer, it's pretty obvious that you might want to enable sticky session with session replication in Tomcat. My web application is a Spring MVC application and it uses Spring Security for all type of authorizations and authentications. If I directly go to the Log-In screen of my load balancer and try to authenticate, it might not work. It's expected as Tomcat gets confused when sending request and response in multiple instances. If I enable sticky session I will not face this issue.

You can do it with the help of AWS EC2 console. Open the Load Balancer screen and select your newly created load balancer.

If you look carefully at the port configuration part, you will see "Stickiness: Disabled" for all of your ports. By default, stickiness is disabled for all the ports you select for load balancer. Now click on the "edit" button of the port on where you want to enable stickiness. For my case, it will be port 8080. Once you click on the "edit" button, it will ask you how you want to enable session stickiness. You can either choose Load Balancer Generated Cookie Stickiness or Application Generated Cookie Stickiness. For my simple application, I have selected "Load Balancer Generated Cookie Stickiness" and I entered 86400 as my cookie expiration period which is a day in seconds.

After you enable it, you should be able to test your session stickiness. For my case, now I'm able to successfully authenticate to my application.

Some considerations: Sometimes you might see your load balancer is down or the link is not working or shows no page. In that case, best way to quickly test is to check each of the instance where tomcat is running and check whether you can access them individually (e.g. http://ip.address.instance-1:8080/HelloWorld/login.jsp). If you find that each of the instance is up and running, you can try removing them from your load balancer and add them again. Remember, "Status" section under "Description" tab of your load balancer does not get updated instantly. It takes some time and it waits for the result of the next health check. So wait few minutes until you see "Status: N of N instances in service".
.

That's pretty much it! This is the very basic AWS Load Balancer example with minimum configuration of Tomcat + Session Stickiness. Once its working for you, you can try other options (highly encouraged) and see how it works for you.


Note: For privacy purpose, I had to modify several lines on this post from my original post. So if you find something is not working or facing any issues, please do not hesitate to contact me :)

80 comments:

  1. Thank you for sharing the information here. Its much informative and really i got some valid information. You had posted the amazing article.

    Hadoop Training in Chennai

    ReplyDelete
    Replies
    1. Great Article android based projects

      Java Training in Chennai Project Center in Chennai Java Training in Chennai projects for cse The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training Project Centers in Chennai

      Delete
  2. Thank you for sharing the information here. Its much informative and really i got some valid information. You had posted the amazing article.
    AWS training in chennai

    ReplyDelete
  3. Pretty article! I found some useful information in your blog, it was awesome to read,thanks for sharing this great content to my vision, keep sharing..
    BE Project Center in Chennai | MBA Project Center in Chennai | ME Project Center in Chennai | Projects Center in Velachery

    ReplyDelete
  4. Hi There,.

    Thanks for the info and the steps.

    My Concern is the SSL on the Load balancer and redirection rules how I should do,

    Where all the http request should be redirected to https only.

    How can I achieve it, Would be grateful if you can help,.

    Thanks
    Sam

    ReplyDelete



  5. Best Mobility Services
    Very good article. this bolg article are good and keep sharing more article.

    ReplyDelete

  6. Very nice information and explanation is good. Thanks for sharing. keep on updating such a nice information.
    Austere Technologies |Internet Of Things

    ReplyDelete
  7. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.

    IOT services | Inernet of Things Services | Austere Technologies

    ReplyDelete
  8. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.

    Best Digital Transformation Services | Austere Technology Solutions

    ReplyDelete
  9. Great article, really very helpful content you made. Thank you, keep sharing.

    Best Cloud Solutions | Austere Technologies

    ReplyDelete
  10. Needed to compose one little word yet thanks for the suggestions that you are contributed here, would like to read this blog regularly to get more important updates...
    Best Online Software Training Institute | Java Training

    ReplyDelete
  11. wow...nice blog, very helpful information. Thanks for sharing.

    Best Mobility Services | Austere Technology

    ReplyDelete
  12. It is very interesting to learn from to easy understood. Thank you for giving information. Please let us know and more information get post to link. salesforce admin training in hyderabad

    ReplyDelete
  13. Hello. This post couldn’t be written any better! Reading this post reminds me of my previous roommate. He always kept chatting about this. I will forward this page to him. Fairly certain he will have a good read. Thank you for sharing.
    bigdata training institute in chennai

    ReplyDelete
  14. Thank you for sharing this valuable information. But get out of this busy life and find some peace with a beautiful trip book Best Andaman Tour Package

    ReplyDelete
  15. Really great blog, it's very helpful and has great knowledgeable information.

    Best Blockchain services | Massil Technologies

    ReplyDelete
  16. Hi Thanks for the nice information its very useful to read your blog. We provide best Block Chain Services

    ReplyDelete
  17. Hi Thanks for the nice information its very useful to read your blog. We provide best Finance Training in Hyderabad

    ReplyDelete
  18. Hi Thanks for the nice information its very useful to read your blog. We provide best Association Of Chartered Certified Accountants

    ReplyDelete
  19. Hi Thanks for the nice information its very useful to read your blog. We provide best Certified Financial Analyst

    ReplyDelete
  20. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book ANDAMAN TOUR PACKAGE@14599/

    ReplyDelete
  21. Hi Thanks for the nice information its very useful to read your blog. We provide best Massil Technologies

    ReplyDelete
  22. Hi Thanks for the nice information its very useful to read your blog. We provide best Find All Isfs Courses

    ReplyDelete
  23. You have provided a nice article, Thank you very much for this one. And I hope this will be useful for many people. And I am waiting for your next post keep on updating these kinds of knowledgeable things.
    Cloud Computing Courses in Chennai
    Hadoop Training in Chennai
    Digital Marketing Course in Chennai
    Selenium Training in Chennai
    JAVA Training in Chennai
    German Classes in chennai
    PHP Training in Chennai
    PHP Training in Velachery

    ReplyDelete
  24. Great casino, ever slot machine games online you can safely put the top ten, with the new year already playing. At first I didn’t enter the topic as it is not hung with all sorts of bells and whistles I used to, but over time you realize that only good gaming games, bonuses and timely payments are needed in the casino, everything else interferes and distracts

    ReplyDelete
  25. It is very good and very informative. There is a useful information in it.Thanks for posting...
    https://www.apponix.com/Java-Institute/Java-Training-Institute-in-Bangalore.html

    ReplyDelete
  26. This is the exact information I am been searching for, Thanks for sharing the required infos with the clear update and required points. To appreciate this I like to share some useful information regarding Microsoft Azure which is latest and newest,

    Regards,
    Ramya

    Azure Training in Chennai
    Azure Training Center in Chennai
    Best Azure Training in Chennai
    Azure Devops Training in Chenna
    Azure Training Institute in Chennai
    Azure Training in Chennai OMR
    Azure Training in Chennai Velachery
    Azure Online Training
    Azure Training in Credo Systemz
    DevOps Training in Credo Systemz

    ReplyDelete
  27. One of the best content i have found on internet for Data Science training in Chennai .Every point for Data Science training in Chennai is explained in so detail,So its very easy to catch the content for Data Science training in Chennai .keep sharing more contents for Trending Technologies and also updating this content for Data Science and keep helping others.
    Cheers !
    Thanks and regards ,
    Data Science course in Velachery
    Data Scientists course in chennai
    Best Data Science course in chennai
    Top data science institute in chennai

    ReplyDelete
  28. The article was up to the point and described the information very effectively. Thanks to blog author for wonderful and informative post.
    website development

    ReplyDelete
  29. Cognex is the AWS Training in Chennai. Cognex offer so many courses depends upon the students requriments.

    ReplyDelete
  30. Thanks for the required information & the things I never imagined, and I would request, write more blog and blog post like that for us. Thank you once again
    by cognex is the aws training in chennai(visit here)

    ReplyDelete
  31. Great work done by author of this blog. Its an Amazing blogger.
    by cognex is the AWS Training in Chennai.

    ReplyDelete

  32. To hire our Call Girls in Mahipalpur you can speak with our call girl representative over the call. It is very easy to get escort service in Aerocity now. Call us now!! They love to make relationships for the night with Indian men and that is why they join our escort agency to provide foreign Call Girls in Delhi. These ladies are referred to as Foreign escorts in Aerocity.
    Hi Profile Escorts in Delhi
    Russian Call Girls in Gurgaon
    Call Girls in Manesar
    Russian Escorts in Agra

    ReplyDelete
  33. thanks for sharing this nice information , here is more info about trending cloud computing courses for readers to know AWS Training in Chennai

    ReplyDelete
  34. Very informative and helpful. Thank You for sharing the blog.  aws training in chennai aws course in chennai 

    ReplyDelete
  35. Very informative and helpful. Thank You for sharing the blog.  aws training in chennai aws course in chennai 

    ReplyDelete
  36. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    aws training chennai |
    aws training institute in chennai |
    aws training center in chennai |
    best aws training in chennai |
    aws course in chennai

    ReplyDelete
  37. Thank you for this interesting article<a href="https: https://www.credosystemz.com/courses/azure-training/ Azure Training in Chennai</a>

    ReplyDelete
  38. I believe there are many more pleasurable opportunities ahead for
    individuals that looked at your site.
    dot net training center in Chennai
    best core java training in Chennai
    manual testing training in Chennai

    ReplyDelete
  39. How to get in with Slot Games at the UK Casino
    Slot games can also be found 먹튀탐정 on the gambling market. This is 텐벳 먹튀 because 포커 online casinos are offering real money prizes 가입시 꽁 머니 환전 to their customers. You  007 카지노 로얄 토렌트 Rating: 4 · ‎7 votes

    ReplyDelete
  40. Wild West Slot Review & Casinos 2021
    Wild West is 메리트 카지노 주소 a brand new slot machine from Pragmatic Play 제왕카지노 회원가입 that 온카지노 커뮤니티 looks to 토토 분석 사이트 be a fantastic sequel to the popular classic 우리 카지노 계열사 game from Pragmatic.

    ReplyDelete
  41. Needed to compose a very little word to thank you yet again
    for the nice suggestions you’ve contributed here..

    ReplyDelete

  42. Thanks for this blog. provided great information. All the details are explained clearly with the great explanation.
    AWS Training in Chennai

    ReplyDelete

  43. Thanks for this blog. provided great information. All the details are explained clearly with the great explanation.
    AWS Training in Chennai

    ReplyDelete
  44. Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
    angularjs training in chennai

    ReplyDelete
  45. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
    Cloud Computing Training in Chennai

    ReplyDelete
  46. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.
    Cloud Computing Training in Chennai

    ReplyDelete