Chef cookbook to install/configure Tomcat high-availability clusters with memcached
Installs/Configures Tomcat high-availability clusters with memcached using Martin Grotzke’s
https://code.google.com/p/memcached-session-manager.
$CATALINA_HOME
to be defined in the environment. If $CATALINA_HOME
is not defined, thennode['memcached_session_manager']['tomcat']['lib']
attribute.The default
recipe adds required memcached_session_manager jars to Tomcat. Use the attributes below to configure
memcached_session_manager. Note memcached_session_manager_context
resource will automatically include the default
recipe.
node['memcached_session_manager']['version']
- The version of memcached_session_manager to install. node['memcached_session_manager']['flavor']
- Flavors memcached
and couchbase
are supported.memcached
.node['memcached_session_manager']['tomcat']['base_version']
- Defaults to 7
.node['memcached_session_manager']['tomcat']['lib']
- Defaults to $CATALINA_HOME/lib/
.node['memcached_session_manager']['memcached']['spymemcached']
-spymemcached
to install for memcached
.node['memcached_session_manager']['couchbase']['couchbase-client']
-couchbase-client
to install for couchbase
.node['memcached_session_manager']['couchbase']['jettison']
-jettison
to install for couchbase
.node['memcached_session_manager']['couchbase']['commons-codec']
-commons-codec
to install for couchbase
.node['memcached_session_manager']['couchbase']['httpcore']
-httpcore
to install for couchbase
.node['memcached_session_manager']['couchbase']['httpcore-nio']
-httpcore-nio
to install for couchbase
.node['memcached_session_manager']['couchbase']['netty']
-netty
to install for couchbase
.Updates the context.xml
so that it contains the Manager configuration for the
memcached-session-manager, like in the examples below.
The following examples show configurations for sticky sessions and non-sticky sessions with memcached servers and
for non-sticky sessions with membase. The examples with memcached servers assume that there are two memcacheds
running, one on host1 and another one on host2. All sample configurations assume that you want to use kryo based
serialization.
The following example shows the configuration of the first tomcat, assuming that it runs on host1, together with
memcached “n1”. The attribute failoverNodes=”n1” tells msm to store sessions preferably in memcached “n2” and only
store sessions in “n1” (running on the same host/machine) if no other memcached node (here only n2) is available
(even if host1 goes down completely, the session is still available in memcached “n2” and could be served by the
tomcat on host2). For the second tomcat (on host2) you just need to change the failover node to “n2”, so that it
prefers the memcached “n1”. Everything else should be left unchanged.
memcached_session_manager_context '/path/to/context.xml' do
memcachedNodes 'n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211'
failoverNodes 'n1'
requestUriIgnorePattern '.*\.(ico|png|gif|jpg|css|js)$'
end
The following example shows a configuration for non-sticky sessions. In this case there’s no need for failoverNodes,
as sessions are served by all tomcats round-robin and they’re not bound to a single tomcat. For non-sticky sessions
the configuration (for both/all tomcats) would look like this:
memcached_session_manager_context '/path/to/context.xml' do
memcachedNodes 'n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211'
sticky false
sessionBackupAsync false
lockingMode 'uriPattern:/path1|/path2'
requestUriIgnorePattern '.*\.(ico|png|gif|jpg|css|js)$'
end
To connect to a membase bucket bucket1
the configuration would look like this:
memcached_session_manager_context '/path/to/context.xml' do
memcachedNodes 'http://host1.yourdomain.com:8091/pools'
username 'bucket1'
password 'topsecret'
memcachedProtocol 'binary'
sticky false
sessionBackupAsync false
requestUriIgnorePattern '.*\.(ico|png|gif|jpg|css|js)$'
end
If you are running multiple webapps/contexts sharing the same session id (e.g. by having set sessionCookiePath=”/“)
you must tell memcached session manager to add a prefix to the session id when storing a session in memcached.
For this you can use the storageKeyPrefix attribute as shown by this example (see also the more detailed attribute
description below):
memcached_session_manager_context '/path/to/context.xml' do
memcachedNodes 'n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211'
failoverNodes 'n1'
storageKeyPrefix 'context'
requestUriIgnorePattern '.*\.(ico|png|gif|jpg|css|js)$'
end
install
- Adds or updates the context.xml
so that it contains the Manager configuration forremove
- Removes the context.xml
so that it contains the Manager configuration forDocumentation below has been trimmed down from the original. Please read the complete documentation
here.
path
(required) - Defaults to name of the resource block. Specifies direct path to context.xml file.className
- Defaults to de.javakaffee.web.msm.MemcachedBackupSessionManager
. memcachedNodes
(required) - This attribute must contain all memcached nodes you have running, or the membasefailoverNodes
(optional, must not be used for non-sticky sessions) - Defaults to nil
. Specifies ids of theusername
- Defaults to nil
. Specifies the username used for a membase bucket or SASL. password
- Defaults to nil
. Specifies the password used for membase bucket or SASL authenticationmemcachedProtocol
- Defaults to text
- Specifies the memcached protocol to use, one of text
orbinary
.sticky
- Defaults to true
. Specifies if sticky or non-sticky sessions are used.lockingMode
(optional, for non-sticky sessions only) - Defaults to none
. Specifies the locking strategynone
, all
, auto
, and uriPattern:<regexp>
.requestUriIgnorePattern
- Defaults to nil
. Specifies a regular expression for request URIs, that shall notsessionBackupAsync
- Defaults to true
. Specifies if the session shall be stored asynchronously in memcached.backupThreadCount
- Defaults to nil
(number-of-cpu-cores). The number of threads that are used for asynchronoussessionBackupTimeout
- Defaults to 100
milliseconds. The timeout in milliseconds after that a session backup isoperationTimeout
- Defaults to 1000
. The memcached operation timeout used at various places, e.g. used for thestorageKeyPrefix
- Defaults to webappVersion
Allows to configure a prefix that’s added to the session id when asessionAttributeFilter
- Defaults to nil
. A regular expression to control which session attributes are serializedtranscoderFactoryClass
- Defaults to de.javakaffee.web.msm.JavaSerializationTranscoderFactory
.copyCollectionsForSerialization
- Defaults to false
. A boolean value that specifies, if iterating over collectioncustomConverter
- Defaults to nil
. Custom converter allow you to provide custom serialization of applicationenableStatistics
- Defaults to true
. A boolean value that specifies, if statistics shall be gathered. enabled
- Defaults to true
. Specifies if session storage in memcached is enabled or not, can also be changed atThis cookbook includes custom ChefSpec matchers you can use to test
your own cookbooks.
Example Matcher Usage
expect(chef_run).to install_memcached_session_manager_context('/path/to/context.xml').with(
memcachedNodes: 'n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211',
failoverNodes: 'n1',
storageKeyPrefix: 'context',
requestUriIgnorePattern: '.*\.(ico|png|gif|jpg|css|js)$'
)
Cookbook Matchers
Please refer to CONTRIBUTING.
MIT - see the accompanying LICENSE
file for details.