# tomcat_cluster_playbook
**Repository Path**: alys114/tomcat_cluster_playbook
## Basic Information
- **Project Name**: tomcat_cluster_playbook
- **Description**: Ansible 一键部署 Tomcat 集群,并实现Session的共享。
- **Primary Language**: Shell
- **License**: Apache-2.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 1
- **Created**: 2019-09-29
- **Last Updated**: 2024-07-15
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# tomcat_cluster_playbook
Ansible 一键部署 Tomcat 集群,并实现Session的共享。
# 1. Tomcat 高可用集群
## 1.1 架构图
|角色|机器|说明|
|---|---|---|
|Tomcat|10.0.0.122|Tomcat集群机器|
|Tomcat|10.0.0.123|Tomcat集群机器|
|负载均衡器|10.0.0.124|使用Nginx实现|
|Session存储服务器|10.0.0.124|使用Memcache存储|
## 1.2 部署
根据架构图,我们的部署步骤如下:
- 安装Tomcat集群
- 部署Nginx反向代理
- 安装Memcached
- 配置Session共享
前面的几步都是很简单的内容,我们来看最后的配置Session共享部分。
## 1.3 配置Session共享
要想Tomcat使用Memcache存储Session,需要添加一些插件。
官方说明文档:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration#add-memcached-session-manager-jars-to-tomcat
### 1.3.1 添加对应的jar包
```bash
[root@Tomcat-122 lib]# ll /application/tomcat/lib/*mem*
-rw-r--r-- 1 root root 166409 Dec 22 13:28 /application/tomcat/lib/memcached-session-manager-2.0.0.jar
-rw-r--r-- 1 root root 10787 Dec 22 13:28 /application/tomcat/lib/memcached-session-manager-tc8-2.0.0.jar
-rw-r--r-- 1 root root 473774 Dec 22 13:28 /application/tomcat/lib/spymemcached-2.12.3.jar
```
### 1.3.2 配置context.xml
增加一下内容:
```bash
[root@Tomcat-122 tomcat]# tail -11 /application/tomcat/conf/context.xml
```
### 1.3.3 添加测试主页
```bash
[root@Tomcat-122 test]# cat /application/tomcat/webapps/test/index.jsp
Tomcat Session share
memcached save session
SessionID:<%=session.getId()%>
SessionIP:<%=request.getServerName()%>
SessionPort:<%=request.getServerPort()%>
10.0.0.122
```
同步到123的Tomcat,并修改文件中的IP地址;
### 1.3.4 重启服务并验证
```bash
[root@Ansible-host-80 ~]# ansible tomcat -m shell -a "source /etc/profile;/application/tomcat/bin/shutdown.sh"
172.16.1.122 | SUCCESS | rc=0 >>
172.16.1.123 | SUCCESS | rc=0 >>
[root@Ansible-host-80 ~]# ansible tomcat -m shell -a "source /etc/profile;/application/tomcat/bin/startup.sh"
172.16.1.122 | SUCCESS | rc=0 >>
Tomcat started.
172.16.1.123 | SUCCESS | rc=0 >>
Tomcat started.
[root@Ansible-host-80 ~]# ansible tomcat -m shell -a "/bin/netstat -lntup"
172.16.1.122 | SUCCESS | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1177/sshd
tcp 0 0 :::8009 :::* LISTEN 1919/java
tcp 0 0 :::8080 :::* LISTEN 1919/java
tcp 0 0 :::22 :::* LISTEN 1177/sshd
172.16.1.123 | SUCCESS | rc=0 >>
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1188/sshd
tcp 0 0 :::8009 :::* LISTEN 3103/java
tcp 0 0 :::8080 :::* LISTEN 3103/java
tcp 0 0 :::22 :::* LISTEN 1188/sshd
```
# 2. Ansible-roles 一键部署
## 2.1 相关文件
```bash
[root@Ansible-host-80 roles]# tree ~/.ansible/roles/
/root/.ansible/roles/
├── memcached
│ ├── files
│ ├── handlers
│ ├── tasks
│ │ ├── install_memcached.yml
│ │ └── main.yml
│ └── vars
├── nginx-make
│ ├── files
│ │ ├── nginx-1.10.3.tar.gz
│ │ └── nginx-1.14.2.tar.gz
│ ├── handlers
│ │ ├── main.yml
│ │ └── nginx_restart.yml
│ ├── tasks
│ │ ├── change_owner.yml
│ │ ├── config_nginx.yml
│ │ ├── create_group.yml
│ │ ├── create_softlink.yml
│ │ ├── create_user.yml
│ │ ├── install_nginx.bak
│ │ ├── install_nginx.yml
│ │ └── main.yml
│ ├── templates
│ │ ├── main.yml
│ │ ├── nginx.conf.j2
│ │ └── tomcat.conf.j2
│ └── vars
│ └── main.yml
└── tomcat
├── files
│ ├── apache-tomcat-8.0.27.tar.gz
│ ├── jdk-8u60-linux-x64.tar.gz
│ ├── jpress-web-newest.war
│ ├── memcached-session-manager-2.0.0.jar
│ ├── memcached-session-manager-tc8-2.0.0.jar
│ ├── myweb.war
│ └── spymemcached-2.12.3.jar
├── handlers
│ ├── main.yml
│ └── tomcat_restart.yml
├── tasks
│ ├── config_tomcat.yml
│ ├── deploy_cluster.yml
│ ├── install_jdk.yml
│ ├── install_tomcat.yml
│ ├── main.yml
│ ├── push_session_jar.yml
│ └── start_tomcat.yml
├── templates
│ ├── context.xml.j2
│ └── index.jsp.j2
└── vars
└── main.yml
17 directories, 37 files
```
## 2.2 主配置文件(执行入口)
对三个服务分别建立对应的role,通过主配置文件来调用。
```bash
[root@Ansible-host-80 .ansible]# cat tomcat_cluser.yml
---
- hosts: LB
remote_user: root
roles:
- role: nginx-make
- hosts: memcached
remote_user: root
roles:
- role: memcached
- hosts: tomcat
remote_user: root
roles:
- role: tomcat
```
## 2.3 Memcache
```bash
[root@Ansible-host-80 memcached]# tree
.
├── files
├── handlers
├── tasks
│ ├── install_memcached.yml
│ └── main.yml
└── vars
4 directories, 2 files
[root@Ansible-host-80 memcached]# cat tasks/main.yml
- include: install_memcached.yml
[root@Ansible-host-80 memcached]# cat tasks/install_memcached.yml
- name: yum install memcahced
yum: name=memcached
- name: enabled memcached service
service: name=memcached enabled=true state=started
```
## 2.3 负载均衡器
```bash
[root@Ansible-host-80 nginx-make]# tree
.
├── files
│ ├── nginx-1.10.3.tar.gz
│ └── nginx-1.14.2.tar.gz
├── handlers
│ ├── main.yml
│ └── nginx_restart.yml
├── tasks
│ ├── change_owner.yml
│ ├── config_nginx.yml
│ ├── create_group.yml
│ ├── create_softlink.yml
│ ├── create_user.yml
│ ├── install_nginx.bak
│ ├── install_nginx.yml
│ └── main.yml
├── templates
│ ├── main.yml
│ ├── nginx.conf.j2
│ └── tomcat.conf.j2
└── vars
└── main.yml
5 directories, 16 files
```
### 2.3.1 变量
```bash
[root@Ansible-host-80 nginx-make]# cat vars/main.yml
tomcat1: 172.16.1.122:8080
tomcat2: 172.16.1.123:8080
nginx_tomcat_port: 8080
tomcat_server_name: java.tomcat.ff
nginx_version: 1.10.3
nginx_user: www
nginx_group: www
nginx_uid: 801
nginx_gid: 801
```
### 2.3.2 任务列表
```bash
[root@Ansible-host-80 nginx-make]# cat tasks/main.yml
- include: create_group.yml
- include: create_user.yml
- include: install_nginx.yml
- include: create_softlink.yml
- include: config_nginx.yml
- include: change_owner.yml
```
### 2.3.3 权限
```bash
[root@Ansible-host-80 nginx-make]# cat tasks/create_group.yml
- name: create {{ nginx_group }} group
group: name={{ nginx_group }} system=yes gid={{ nginx_gid }}
[root@Ansible-host-80 nginx-make]# cat tasks/create_user.yml
- name: create {{ nginx_user }} user
user: name={{ nginx_user }} system=yes uid={{ nginx_uid }} group={{ nginx_group }} createhome=no
```
### 2.3.4 nginx安装
```bash
[root@Ansible-host-80 nginx-make]# cat tasks/install_nginx.yml
- name: install dependency
yum: name=pcre,pcre-devel,openssl,openssl-devel
- name: unarchive package
unarchive: copy=yes src=files/nginx-{{ nginx_version }}.tar.gz dest=/tools/
- name: compile nginx
shell: chdir=/tools/nginx-{{ nginx_version }} /bin/sh configure --user={{ nginx_user }} --group={{ nginx_group }} --prefix=/application/nginx-{{ nginx_version }}/ --conf-path=/application/nginx-{{ nginx_version }}/nginx.conf --with-http_stub_status_module --with-http_ssl_module
- name: make nginx
shell: chdir=/tools/nginx-{{ nginx_version }} /usr/bin/make && /usr/bin/make install
```
### 2.3.5 配置
```bash
# 创建软链接
[root@Ansible-host-80 nginx-make]# cat tasks/create_softlink.yml
- name: create softlink
file: src=/application/nginx-{{ nginx_version }} dest=/application/nginx state=link owner={{ nginx_user }} group={{ nginx_group }} force=yes
# 配置nginx
[root@Ansible-host-80 nginx-make]# cat tasks/config_nginx.yml
- name: nginx service
shell: /application/nginx/sbin/nginx;echo "/application/nginx/sbin/nginx" >> /etc/rc.local
- name: mkdir extra
file: name=/application/nginx/extra state=directory
- name: copy tomcat config
template: src=tomcat.conf.j2 dest=/application/nginx/extra/tomcat.conf
notify: restart nginx
- name: copy config
template: src=nginx.conf.j2 dest=/application/nginx/nginx.conf backup=true
notify: restart nginx
# 修改权限
[root@Ansible-host-80 nginx-make]# cat tasks/change_owner.yml
- name: change owner
file: name={{ item }} owner={{ nginx_user }} group={{ nginx_user }} recurse=yes
with_items:
- /application/nginx-{{ nginx_version }}
```
### 2.3.5 模板
```bash
[root@Ansible-host-80 nginx-make]# cat templates/main.yml
- include: nginx.conf.j2
- include: tomcat.conf.j2
[root@Ansible-host-80 nginx-make]# cat templates/nginx.conf.j2
worker_processes {{ ansible_processor_vcpus*4 }};
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include extra/*.conf;
}
[root@Ansible-host-80 nginx-make]# cat templates/tomcat.conf.j2
upstream tomcat_pools {
server {{ tomcat1 }} weight=1;
server {{ tomcat2 }} weight=1;
}
server {
listen {{ nginx_tomcat_port }};
server_name {{ tomcat_server_name }};
location / {
root html;
index index.jsp index.html index.htm;
proxy_pass http://tomcat_pools;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
```
### 2.3.6 触发器
```bash
[root@Ansible-host-80 nginx-make]# cat handlers/
main.yml nginx_restart.yml
[root@Ansible-host-80 nginx-make]# cat handlers/main.yml
- include: nginx_restart.yml
[root@Ansible-host-80 nginx-make]# cat handlers/nginx_restart.yml
- name: restart nginx
shell: /application/nginx/sbin/nginx -s reload
```
## 2.4 部署Tomcat集群
```bash
[root@Ansible-host-80 tomcat]# tree
.
├── files
│ ├── apache-tomcat-8.0.27.tar.gz
│ ├── jdk-8u60-linux-x64.tar.gz
│ ├── jpress-web-newest.war
│ ├── memcached-session-manager-2.0.0.jar
│ ├── memcached-session-manager-tc8-2.0.0.jar
│ ├── myweb.war
│ └── spymemcached-2.12.3.jar
├── handlers
│ ├── main.yml
│ └── tomcat_restart.yml
├── tasks
│ ├── config_tomcat.yml
│ ├── deploy_cluster.yml
│ ├── install_jdk.yml
│ ├── install_tomcat.yml
│ ├── main.yml
│ ├── push_session_jar.yml
│ └── start_tomcat.yml
├── templates
│ ├── context.xml.j2
│ └── index.jsp.j2
└── vars
└── main.yml
5 directories, 19 files
```
### 2.4.1 变量
```bash
[root@Ansible-host-80 tomcat]# cat vars/main.yml
session_uri: 10.0.0.124:11211
jdk_name: jdk-8u60-linux-x64.tar.gz
jdk_version: 1.8.0_60
tomcat_version: 8.0.27
```
### 2.4.2 任务列表
```bash
[root@Ansible-host-80 tomcat]# cat tasks/main.yml
- include: install_jdk.yml
- include: install_tomcat.yml
- include: push_session_jar.yml
- include: config_tomcat.yml
- include: deploy_cluster.yml
- include: start_tomcat.yml
```
### 2.4.3 安装jdk
```bash
[root@Ansible-host-80 tomcat]# cat tasks/install_jdk.yml
- name: unarchive jdk
unarchive: copy=yes src=files/{{ jdk_name }} dest=/application/
- name: soft link jdk
file: src=/application/jdk{{ jdk_version }} dest=/application/jdk state=link force=yes
- name: export variable
shell: sed -i.ori '$a export JAVA_HOME=/application/jdk\nexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH\nexport CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar' /etc/profile;source /etc/profile;
```
### 2.4.4 安装Tomcat
```bash
[root@Ansible-host-80 tomcat]# cat tasks/install_tomcat.yml
- name: unarchive tomcat
unarchive: copy=yes src=files/apache-tomcat-{{ tomcat_version }}.tar.gz dest=/application/
- name: soft link tomcat
file: src=/application/apache-tomcat-{{ tomcat_version }} dest=/application/tomcat state=link force=yes
- name: export variable
shell: echo 'export TOMCAT_HOME=/application/tomcat'>>/etc/profile ;source /etc/profile;
```
### 2.4.5 配置Session共享
```bash
# 添加jar包
[root@Ansible-host-80 tomcat]# cat tasks/push_session_jar.yml
- name: copy session jar
copy: src={{ item }} dest=/application/tomcat/lib/
with_items:
- files/memcached-session-manager-2.0.0.jar
- files/memcached-session-manager-tc8-2.0.0.jar
- files/spymemcached-2.12.3.jar
notify: restart tomcat
# 配置tomcat
[root@Ansible-host-80 tomcat]# cat tasks/config_tomcat.yml
- name: rewrite context.xml
template: src=context.xml.j2 dest=/application/tomcat/conf/context.xml backup=true
notify: restart tomcat
[root@Ansible-host-80 tomcat]# cat tasks/deploy_cluster.yml
- name: create test directory
file: dest=/application/tomcat/webapps/test state=directory
- name: copy index.jsp
template: src=index.jsp.j2 dest=/application/tomcat/webapps/test/index.jsp backup=true
# 启动服务
[root@Ansible-host-80 tomcat]# cat tasks/start_tomcat.yml
- name: start tomcat
shell: source /etc/profile;/application/tomcat/bin/startup.sh;echo "/application/tomcat/bin/startup.sh" >> /etc/rc.local
```
### 2.4.6 模板
```bash
[root@Ansible-host-80 tomcat]# cat templates/context.xml.j2
WEB-INF/web.xml
${catalina.base}/conf/web.xml
[root@Ansible-host-80 tomcat]# cat templates/index.jsp.j2
Tomcat Session share
memcached save session
SessionID:<%=session.getId()%>
SessionIP:<%=request.getServerName()%>
SessionPort:<%=request.getServerPort()%>
{{ ansible_all_ipv4_addresses.0 }}
```
### 2.4.7 触发器
```bash
[root@Ansible-host-80 tomcat]# cat handlers/main.yml
- include: tomcat_restart.yml
[root@Ansible-host-80 tomcat]# cat handlers/tomcat_restart.yml
- name: restart tomcat
shell: source /etc/profile ; /bin/sh /application/tomcat/bin/shutdown.sh
- name: restart tomcat
shell: source /etc/profile ; /bin/sh /application/tomcat/bin/startup.sh
```
# 3. 安装包说明
## 3.1 使用当前版本
上述列表中的安装包,文件太大,已上传到百度云:
链接:https://pan.baidu.com/s/1pSCq7wFC_7NwUzHkxq9F8Q
提取码:d1el
大家可以直接下载使用。
## 3.2 使用最新版本
请到各个工具的网站下载