Can't get sticky sessions working in AWS?
Asked Answered
W

2

9

I've got a web app running in tomcat instances on EC2 and I can't for the life of me get sticky sessions to work on the load balancer. I've followed all the steps in http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-sticky-sessions.html, tried using both the application-based (using the JSESSIONID cookie) and time-based (using 86400 seconds) methods, but either way, it doesn't work, at all. The same user keeps getting bounced around all the different nodes.. every time they click a new link on the webpage, it bounces them back to the home screen to login again.

I have no idea why it isn't working. I also don't know very much about this stuff (heck, until 2 hours ago I didn't even know what 'sticky session' meant--and no, I can't just get someone else to do it) so I'm not really sure where to start with debugging it, either.

EDIT: Looking at my cookies.. it appears that the AWSELB cookie keeps getting deleted and rewritten every single time I load the site, instead of persisting like it's supposed to.

EDIT 2: XML configuration of my load balancer:

{
    "LoadBalancerDescriptions": [
        {
            "Subnets": [
                "subnet-5c83aa39",
                "subnet-6a778830",
                "subnet-c41cdde8"
            ],
            "CanonicalHostedZoneNameID": "Z35SXDOTRQ7X7K",
            "CanonicalHostedZoneName": "<ELB_NAME>-<redacted>.us-east-1.elb.amazonaws.com",
            "ListenerDescriptions": [
                {
                    "Listener": {
                        "InstancePort": 5432,
                        "LoadBalancerPort": 5432,
                        "Protocol": "TCP",
                        "InstanceProtocol": "TCP"
                    },
                    "PolicyNames": []
                },
                {
                    "Listener": {
                        "InstancePort": 8888,
                        "LoadBalancerPort": 8888,
                        "Protocol": "HTTP",
                        "InstanceProtocol": "HTTP"
                    },
                    "PolicyNames": [
                        "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500995555135"
                    ]
                }
            ],
            "HealthCheck": {
                "HealthyThreshold": 2,
                "Interval": 10,
                "Target": "HTTP:44554/",
                "Timeout": 8,
                "UnhealthyThreshold": 4
            },
            "VPCId": "vpc-721bec0b",
            "BackendServerDescriptions": [],
            "Instances": [
                {
                    "InstanceId": "i-0ca9c244ed930d58f"
                },
                {
                    "InstanceId": "i-0cf47dc916f3f3443"
                },
                {
                    "InstanceId": "i-09ce2f24abc50259f"
                }
            ],
            "DNSName": "<ELB_NAME>-<redacted>.us-east-1.elb.amazonaws.com",
            "SecurityGroups": [
                "sg-f5ff638b"
            ],
            "Policies": {
                "LBCookieStickinessPolicies": [
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500564994754",
                        "CookieExpirationPeriod": 86400
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500565103581",
                        "CookieExpirationPeriod": 0
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500566463445",
                        "CookieExpirationPeriod": 28800
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500566440580",
                        "CookieExpirationPeriod": 0
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500476922828",
                        "CookieExpirationPeriod": 86400
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500481383343",
                        "CookieExpirationPeriod": 86400
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500479370743",
                        "CookieExpirationPeriod": 86400
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500475843862",
                        "CookieExpirationPeriod": 86400
                    },
                    {
                        "PolicyName": "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500995555135",
                        "CookieExpirationPeriod": 86400
                    }
                ],
                "AppCookieStickinessPolicies": [
                    {
                        "PolicyName": "AWSConsole-AppCookieStickinessPolicy-<ELB_NAME>-1500477157435",
                        "CookieName": "JSESSIONID"
                    },
                    {
                        "PolicyName": "AWSConsole-AppCookieStickinessPolicy-<ELB_NAME>-1500472642494",
                        "CookieName": "JSESSIONID"
                    },
                    {
                        "PolicyName": "AWSConsole-AppCookieStickinessPolicy-<ELB_NAME>-1500473344752",
                        "CookieName": "JSESSIONID"
                    },
                    {
                        "PolicyName": "AWSConsole-AppCookieStickinessPolicy-<ELB_NAME>-1500473227851",
                        "CookieName": "JSESSIONID"
                    }
                ],
                "OtherPolicies": []
            },
            "LoadBalancerName": "<ELB_NAME>",
            "CreatedTime": "2017-07-18T15:32:33.890Z",
            "AvailabilityZones": [
                "us-east-1a",
                "us-east-1b",
                "us-east-1c"
            ],
            "Scheme": "internet-facing",
            "SourceSecurityGroup": {
                "OwnerAlias": "558554867759",
                "GroupName": "<redacted>"
            }
        }
    ]
}

EDIT 3: Anyone? Please I'm getting desperate I need this to work...

Woolcott answered 19/7, 2017 at 15:0 Comment(4)
Please capture some request and response headers to illustrate your cookie "getting deleted."Escalate
@Michael-sqlbot not sure how to do that (again, I'm REALLY new at web stuff).. I'm just looking at the cookie list in chrome and noticing that the AWSELB cookie keeps changing...Woolcott
Can you send the XML description of your load balancer config, using the AWS CLI like that: aws elb describe-load-balancers --load-balancer-name my-loadbalancerEarshot
did you ever get sticky sessions to work on your Application Load Balancer ?Sumbawa
E
0

Firstly how this sticky stuff works ! ELB just adds a cookie AWSELB ( or might be some other name ; u can just look for all cookies and see which one is NOT yours ) the very first time. This cookie also has information which server this request should go.

Now, all requests coming from same browser will have that cookie and ELB knows where to forward. There is nothing much here which will not work.

Let's do following:

  1. Can you send me the website/webapp you are trying ? If it is accessible in public. Because even I can debug it.

  2. Make sure cookies are allowed; though looks like from your question that this is not an issue.

  3. What is the expiration period you have set on Edit Stickiness ! Make sure that when on ELB --> click on Edit Stickiness --> Select Radio button "Enable load balancer generated cookie stickiness" and MOST IMPORTANTLY leave Expiration period as blank.

  4. Repeat Step 3 for HTTPS also if u are using HTTPS.

Let me know if it works; i am here only for next hour.

Not sure why ur XML is showing SO MANY LBCookieStickinessPolicies !!

Especially answered 26/7, 2017 at 16:44 Comment(4)
Website is not accessible to the public. Cookies are allowed. Expiration period is blank. HTTPS is not being used.Woolcott
Was thinking about it; ONLY possible reason I can think of is that ur servers are going out of service from ELB.. Can you go to ELB --> Monitoring --> Healthy Hosts --> And see if any of ur instance is going un-healthy intermittently. Another thing I want u to try is another browser.Especially
Servers have been healthy for 4 weeks without interruption. Been trying with Chrome, Edge, and FirefoxWoolcott
Sorry man.. I was of no help to you.. if u could make site public; I could have given it a shot seeing cookies.Especially
R
0

You have a lot of different policies created with different configurations but only one is associated with the listener, and that one is a time based one.

"LoadBalancerDescriptions": [
    {
        "Subnets": [
            "subnet-5c83aa39",
            "subnet-6a778830",
            "subnet-c41cdde8"
        ],
        "CanonicalHostedZoneNameID": "Z35SXDOTRQ7X7K",
        "CanonicalHostedZoneName": "<ELB_NAME>-<redacted>.us-east-1.elb.amazonaws.com",
        "ListenerDescriptions": [
            {
                "Listener": {
                    "InstancePort": 5432,
                    "LoadBalancerPort": 5432,
                    "Protocol": "TCP",
                    "InstanceProtocol": "TCP"
                },
                "PolicyNames": []
            },
            {
                "Listener": {
                    "InstancePort": 8888,
                    "LoadBalancerPort": 8888,
                    "Protocol": "HTTP",
                    "InstanceProtocol": "HTTP"
                },
                "PolicyNames": [
 here ---->         "AWSConsole-LBCookieStickinessPolicy-<ELB_NAME>-1500995555135"
                ]
            }
        ],

The documentation points you to how associate a policy with the listener: http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-sticky-sessions.html#enable-sticky-sessions-duration

Try associating only one of the AppCookieStickinessPolicies that uses JSESSIONID to the listener

Rapprochement answered 26/7, 2017 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.