Tomcat Expert Series Large Scale Deployments Filip Hanik SpringSource 2009 Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.
Agenda Basic Installation Prelude Flexible Directory Layout Configuration Templates Environment variables Application management Example installation and upgrade 2
Tomcat Folder Structure Folder structure overview apache-tomcat-6.0.x/ - bin - conf - lib - logs - temp - webapps - work 3
Tomcat Folder Structure Folder structure overview apache-tomcat-6.0.x/ - bin - used during startup - conf - CATALINA_BASE - lib - CATALINA_HOME - logs - logging.properties - temp - CATALINA_TMPDIR - webapps server.xml (<Host>) - work - server.xml (<Host>) 4
How Tomcat reads directories bin directory Only storage space for scripts and bootstrap libraries Never used during runtime All other directories are 'configurable' There are some slight exceptions We can take advantage of this Tomcat upgrades/downgrades JVM upgrades/downgrades 5
Starting up Tomcat bin/ startup.(sh bat) shutdown.(sh bat) <start> <stop> catalina.(sh bat) Startup Scripts 6
Starting up Tomcat catalina.(sh bat) <invokes> <invokes> setclasspath.(sh bat) setenv.(sh bat) JVM Launch Startup Scripts 7
Setting custom options setenv.sh - Any custom options here JAVA_HOME JAVA_OPTS CATALINA_OPTS CATALINA_HOME CATALINA_BASE CATALINA_TMPDIR CATALIA_PID All these are read by catalina.sh If no value exists, defaults are assigned 8
Setting Custom Options setenv.sh doesn't ship with Tomcat Upgrading Tomcat Simply copy setenv.sh No need to modify Tomcat scripts, or keep track of changes inside Tomcat scripts 9
Up/Down-grading JVM /development/tomcatx/example/ - apache-tomcat-6.0.16/ - apache-tomcat-6.0.18/ - jdk1.5.0_17/ - jdk1.6.0_11/ - myapplications setenv.sh JAVA_HOME points to 1.5 or 1.6 Easy to switch back and forth No need to modify any Tomcat scripts 10
Tomcat Folder Structure /development/tomcatx/example/ - apache-tomcat-6.0.16/ - apache-tomcat-6.0.18/ - jdk1.5.0_16/ - jdk1.6.0_10/ - tomcat-instance-01/ - bin - conf - logs - webapps - work - temp Instance data can be separated out 11
So far setenv.sh CATALINA_HOME=/usr/local/apache-tomcat-6.0.18 CATALINA_BASE=/usr/local/tomcat-instance-01 JAVA_HOME=/usr/local/jdk1.6.0_10 CATALINA_PID=$CATALINA_BASE/logs/tomcat.pid Modify JAVA_HOME to change JVM Modify CATALINA_HOME to change Tomcat 12
Tomcat upgrades Most important thing during a production upgrade: How to downgrade/roll back if something goes wrong With Tomcat, that's easy, keep multiple installations With JVM, that's easy, keep multiple installations 13
Going further So far Understanding setenv.sh Understanding file/directory structure Lets take it to the next step Lets create a setup that will make all this seem so much more intuitive 14
Creating the layout apache-tomcat-6.0.x/ * A regular Tomcat install run.sh * Control script shared/ * Shared instance data instance_1/ * An instance of a Tomcat server instance_2/ instance_n/ 15
Creating the layout shared/ - conf/ logging.properties server.xml tomcat-users.xml - logs/ * Instances can share configuration files! 16
Creating the layout instance_1/ - bin/ setenv.sh * Instance specific JVM options - conf/ * Instances configuration catalina.properties - logs/ * catalina.out goes here - webapps/ * Instance applications - work/ * Instance work directory - temp/ * Instance temp directory 17
Configuration shared/conf/server.xml <Server port="${shutdown.port}" shutdown="shutdown"> <Connector port="${http.port}" protocol="http/1.1" connectiontimeout="20000" /> run.sh * uses -config to denote configuration file 18
Configuration instance_1/conf/catalina.properties shutdown.port=8005 http.port=8080 * Anything in here gets read into System.getProperties(...) * Useful for application options -Dxxx=yyy 19
Configuration shared/conf/logging.properties 1catalina.org.apache.juli.FileHandler.level = FINE 1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/../shared/logs 1catalina.org.apache.juli.FileHandler.prefix = ${catalina.instance}.catalina. * Single configuration file Unique log files per instance 20
Configuration instane_1/bin/setenv.sh CATALINA_OPTS= -Xmx512m -Xss192k * Unique JVM options per instance 21
The Script run.sh The script that does it all Takes advantage of configuration options in Tomcat Creates a simple, flexible and yet powerful layout structure Provides ability to switch JVM and Tomcat versions without changing any configuration 22
Demo 10 minutes Walk through run.sh Create a new instance Configure the new instance Start the new instance Change the JVM for all instances Change the Tomcat version for all instances 23
Summary Easy to create a directory layout No changes were done to Tomcat's files!! No migration needed when upgrading Easy to create more instances Clean and simple configurations JVM options can be shared or instance specific If you were to change something catalina.out log rotation 24
Application Management Working with contexts Understanding deployment models Selecting a strategy 25
What is a Context? In Tomcat A context is a deployed web application A web application is deployed to match a URL path This is called the context path Often referred to as the first part of the URL /myapp/index.jsp However, contexts can be multi-level /multi/level/context/index.jsp 26
Defining contexts Deployed in this order server.xml Point to WAR or directory Inside or outside host's appbase context.xml style files Point to WAR or directory Inside or outside host's appbase WAR Inside host's appbase Directory Inside host's appbase 27
Defining contexts Remote deployments are also possible But one must expose a web application per host to deploy to resources and security Context paths must be unique within a Host Context paths can overlap /myapp /myapp/images Requests are mapped using longest matching context path 28
Deployment strategies No method is better than the other Select a method based on organizational needs Mixing deployment methods is not recommended Often leads to confusion due to how Tomcat manages the persistence of a deployment Select one method Stick to it 29
Deployment strategies Redeployment strategies Redeploy during runtime can cause OutOfMemoryErrors: PermGen Space Useful feature if tested properly and applications don't leak references Otherwise Separate applications under separate Tomcat instances Prevents 'bad apple problem' Tomcat startup time is neglible, 1 second 30
Deployment strategies webapps directory with auto deploy Is scanned in regular intervals Don't remote copy to this directory Might cause incomplete upload to be deployed incorrectly Work around Copy to a local directory Move to webapps 31
Application Management Summary Runtime redeploy is discourages Application management is an organization choice Many different alternatives Need to manually automate the system Do not mix deployment strategies 32
Summary Large scale deployments are possible Can be very large Our customers have thousands of managed instances Tomcat doesn't provide the infrastructure Provides flexibility no other container does Automate your own environment 33
Questions... and answers! 34