Skip to content

Commit 917e98f

Browse files
committed
Initial commit for playbook tomcat-mecached-failover
1 parent 4970851 commit 917e98f

File tree

22 files changed

+615
-0
lines changed

22 files changed

+615
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Copyright (c) 2015 Cuong Nguyen
2+
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
8+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## Tomcat failover with Memcached + Memcached Session Manager + Nginx (load blancer)
2+
3+
- Tested on Ansible 1.9.3 for Debian
4+
- Expects hosts: Centos 6.x
5+
6+
This playbook deploys a failover solution for clustered Tomcat using Nginx as load balancer and Memcached + MSM as session manager.
7+
8+
- Nginx: balances the requests by round robin.
9+
- Memcached: stores `sessionid` of tomcat.
10+
- MSM: manages tomcat session.
11+
12+
For more detail about session management, see https://github.com/magro/memcached-session-manager
13+
14+
This playbook also deploys a demo web app (https://github.com/magro/msm-sample-webapp) to test the session management.
15+
16+
17+
## Initial setup of inventory file
18+
19+
```
20+
[lb_servers]
21+
lbserver
22+
23+
[backend_servers]
24+
tomcat_server_1
25+
tomcat_server_2
26+
27+
[memcached_servers]
28+
cached_server1
29+
cached_server2
30+
```
31+
32+
Edit inventory file `hosts` to suit your requirements and run playbook:
33+
34+
```
35+
$ ansible-playbook -i host site.yml
36+
```
37+
38+
When finished, open web browser and access to http://nginx_ip/ to start testing.
39+
40+
## Ideas and improvements
41+
42+
- Setup SSL for load balancer.
43+
- HA load balancer.
44+
- Hardening iptables rules.
45+
46+
Pull requests are welcome.
47+
48+
## License
49+
50+
This work is licensed under MIT license.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Java variables
2+
3+
# Nginx variables
4+
nginx_http_port: 80
5+
nginx_https_port: 443
6+
7+
# Tomcat variables
8+
tomcat_http_port: 8080
9+
tomcat_https_port: 8443
10+
11+
# Memcached variables
12+
memcached_port: 11211

tomcat-memcached-failover/hosts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[lb_servers]
2+
lbserver
3+
4+
[backend_servers]
5+
tomcat_server_1
6+
tomcat_server_2
7+
8+
[memcached_servers]
9+
cached_server1
10+
cached_server2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
- name: restart iptables
3+
service: name=iptables state=restarted
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
- name: Install libselinux-python
3+
yum: name=libselinux-python state=present
4+
5+
- name: Install GPG key for EPEL
6+
get_url: url=https://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-6 dest=/etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
7+
8+
- name: Install EPEL repository
9+
yum: name=https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm state=present
10+
11+
- name: Setup Iptables rules
12+
template: src=iptables.j2 dest=/etc/sysconfig/iptables
13+
notify: restart iptables
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# {{ ansible_managed }}
2+
# Manual customization of this file is not recommended.
3+
*filter
4+
:INPUT ACCEPT [0:0]
5+
:FORWARD ACCEPT [0:0]
6+
:OUTPUT ACCEPT [0:0]
7+
8+
{% if (inventory_hostname in groups['lb_servers']) %}
9+
-A INPUT -p tcp --dport {{ nginx_http_port }} -j ACCEPT
10+
-A INPUT -p tcp --dport {{ nginx_https_port }} -j ACCEPT
11+
{% endif %}
12+
13+
{% if inventory_hostname in groups['backend_servers'] %}
14+
-A INPUT -p tcp --dport {{ tomcat_http_port }} -j ACCEPT
15+
-A INPUT -p tcp --dport {{ tomcat_https_port }} -j ACCEPT
16+
{% endif %}
17+
18+
{% if inventory_hostname in groups['memcached_servers'] %}
19+
-A INPUT -p tcp --dport {{ memcached_port }} -j ACCEPT
20+
{% endif %}
21+
22+
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
23+
-A INPUT -p icmp -j ACCEPT
24+
-A INPUT -i lo -j ACCEPT
25+
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
26+
COMMIT
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
- name: restart nginx
3+
service: name=nginx state=restarted
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
- name: Install nginx
3+
yum: name=nginx state=present
4+
5+
- name: Deliver main configuration file
6+
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
7+
notify: restart nginx
8+
9+
- name: Copy configuration file to nginx/sites-avaiable
10+
template: src=default.conf.j2 dest=/etc/nginx/conf.d/default.conf
11+
notify: restart nginx
12+
13+
- name: Make sure nginx start with boot
14+
service: name=nginx state=started enabled=yes
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
upstream tomcat {
2+
{% for host in groups['backend_servers'] %}
3+
server {{ host }}:{{ tomcat_http_port }};
4+
{% endfor %}
5+
}
6+
7+
server {
8+
listen 80 default_server;
9+
server_name {{ inventory_hostname }};
10+
include /etc/nginx/default.d/*.conf;
11+
12+
location / {
13+
proxy_pass http://tomcat;
14+
}
15+
16+
}

0 commit comments

Comments
 (0)